Thursday, August 7, 2008

Mnapi: Mnesia API for Java and PHP

Mnesia is a scalable database system designed to work as a storage system for large, distributed applications. In my previous post I showed you how to set up a Mnesia cluster using Erlang shell. Unfortunately, Mnesia cannot be easily accessed from languages other than Erlang. If you are a web developer, there is even an MVC framework for you - Erlyweb. But what if you are a mainstream programmer who would like to use the power of Mnesia, but is not willing to learn another language and change the whole way of thinking in order to understand the functional programming philosophy?

This is why I created Mnapi - API for Java and PHP, which provides basic methods to create, fetch, update and delete records in Mnesia. It is licensed under LGPL, which means that you can get it and use it in any commercial or non-commercial applications, and you are only obliged to publish only the source code of Mnapi if you extend it or modify it (the license does not affect an application which makes a use of it). You can download Mnapi here.

Please bear in mind that Mnesia does not support creating multiple databases or schemas within one instance, joining tables, etc. - and the API reflects those restrictions. You can learn more about it reading documentation attached to the source code and running example applications, so I will not be describing it here in detail. What you need to start using Mnapi is to compile Erlang driver for Mnesia:
erlc mnapi.erl

then start an Erlang shell:
erl -sname erl -setcookie mysecretcookie

and run Mnapi server from the shell:
mnapi:start().

Now you can use Java or PHP to talk to Mnesia through Mnapi libraries.

Note that to use Mnapi from PHP you first need to install and configure Erlang extension for PHP. Read instructions provided with the extension to learn how to configure you PHP installation.

To compile Java api you need Jinterface. If you cannot find Jinterface in lib folder of your standard Erlang installation, you can either download Erlang source code distribution or install it as a package from CEAN.

Have fun!

11 comments:

Slight Slightly said...

Hi there. This looks very interesting as an alternative to memcached.

Do you expect to maintain the API ongoing?

kklis said...

Yes, I do. Currently I work on implementing tcerl (http://code.google.com/p/tcerl/) functionality into the API - will post a bit more info about it soon.

If you are looking for a good memcached alternative in Erlang, there is already a solution called Cacherl (http://code.google.com/p/cacherl/). I haven't tested it by myself, but authors claim that it has a fully memcached compatible API, which makes it a slick drop-in replacement for memcached.

Slight Slightly said...

Thanks for the tip :)

The lack of even a readme on cacherl is a little bit worrying, but I'll definitely keep an eye on it to see how develops, cheers.

Tolan

Gleber said...

Hi, Slight.
I'm a minor contributor to the Cacherl project. It is used by me (and as far as i know, by its creator Zhou) in productions systems and we are quite happy with it. At this moment I feel no need to expand its features.
Though a idea to add README is quite good ;) Feel free to contact in case of questions me through gleber.p@gmail.com.

Claria said...

hi,

thx for this awesome api.
I succesfulled installed but I cannot find a way to connect to an existing db :/

I run an ejabberd server where the mnesia db is in /lib/var/spool/

how do I tell your api to load tables from this dir ?

Best Regards
claria

kklis said...

Hi Claria,

I desgined Mnapi not only to allow PHP / Java code to use Mnesia, but also to provide mapping of associative arrays to Mnesia tables. Because Mnesia does not provide some mechanisms necessary to do this (for example you cannot get column by name, only by number), Mnapi uses its own table design and additional tables to help store Java / PHP arrays in Mnesia. Therefore you cannot use Mnapi to simply access tables not created with Mnapi.

If you need to get to data stored by ejabberd, it might be a lot easier for you to make ejabberd use MySQL instead of Mnesia as its backend. This way you will be able to manipulate easily on ejabberd data using any software that can use MySQL. You can get detailed information about using MySQL with ejabberd at https://support.process-one.net/doc/display/MESSENGER/Using+ejabberd+with+MySQL+native+driver.

Hope this helps.

Claria said...

hi Krzysztof,

thx for your fast answer.
This is badly the anwser i was waiting for :)

I cannot use mysql with ejjaberd because i user their shared roster module that is not yet developped for mysql, only mnesia. This is why I need something to access this db.

Anyway thx for your help, I started coding a small erl file with few functions to access the ejabberd db but I cannot connect to its node ( extra_db_node)... still continuing my searches...

have a good day,

claria

kklis said...

Try reading my tips & tricks about connecting Erlang nodes and working with Mnesia, they may be helpful in your research.

rgz said...

Thank you very much you don't know how much I needed this one :)

kklepper said...

How do you download from the given link???

https://docs.google.com/folder/d/0BzdUPD5OILTeWWlIS0FNQ1Bxd1U/edit

kklis said...

I moved my files to Google Drive, so access is provided by the GDrive interface. To download a file first you need to click it. Because it's an archive, a new window will appear showing the contents of the archive. Move the mouse up the browser window and pay attention to an arrow icon which appears in the middle of the upper part of the window. Click the arrow to download the file.