2

Is there a way to use MonetDBLite as a storage engine in a C++ application without installing the R package? Or are there any other ways of using MonetDB as an embedded database like SQLite?

olafpadme
  • 76
  • 7

1 Answers1

5

You can use this from C/C++ etc., like so:

Download/clone the R package source from GitHub https://github.com/hannesmuehleisen/MonetDBLite, then go to the src folder and run configure:

./configure --enable-embedded --disable-fits --disable-geom --disable-rintegration --disable-gsl --disable-netcdf --disable-jdbc --disable-merocontrol --disable-odbc --disable-console --disable-microhttpd --without-openssl --without-uuid --without-curl --without-bz2 --without-lzma --without-libxml2 --without-perl --without-python2 --without-python3 --without-unixodbc --disable-mapi --without-samtools --without-sphinxclient --without-geos --without-samtools --without-readline --enable-optimize --enable-silent-rules --disable-int128

Build it with make -j (this will take a while)

You can then construct a MonetDBLite shared library as follows:

gcc -shared -o libmonetdb5.so `find common gdk mal/mal mal/modules mal/optimizer sql embedded -name "*.o" | tr "\n" " "` -lpthread -lpcre -lz -liconv

You should end up with a big (5 MB) file libmonetdb5.so which contains all of the MonetDBLite code. Now to use it from your own program:

Here is a sample C program to use embedded MonetDB:

#include "monetdb_config.h"
#include "sql_scenario.h"
#include "mal.h"
#include "embedded.h"

int main() {
    char* err = NULL;
    void* conn = NULL;
    res_table* result = NULL;

    err = monetdb_startup("/tmp/embedded-dbfarm", 1, 0);
    if (err != NULL) {
        fprintf(stderr, "Init fail: %s\n", err);
        return -1;
    }
    conn = monetdb_connect();
    err = monetdb_query(conn, "SELECT * FROM tables;", 1, (void**) &result);
    if (err != NULL) {
        fprintf(stderr, "Query fail: %s\n", err);
        return -2;
    }
    fprintf(stderr, "Query result with %i cols and %lu rows\n", result->nr_cols, BATcount(BATdescriptor(result->cols[0].b)));
    monetdb_disconnect(conn);
    monetdb_shutdown();
}

Save this into a file in the src folder (here, test.c) and build it:

gcc test.c -Icommon/options -Icommon/stream -Igdk -Imal/mal -Imal/modules/atoms -Imal/modules/mal -Isql/include -Isql/backends/monet5 -Isql/server -Isql/common -Isql/storage -Iembedded -lmonetdb5 -L.

That should be all, when you run it (./a.out), it should say Query result with 9 cols and 44 rows. Note that you only need to ship the libmonetdb5.so file along with your compiled program and not the rest of the sourcetree.

As a sidenote, we hope to streamline this process in the future.

Hannes Mühleisen
  • 2,542
  • 11
  • 13