Changeset: 90dea3bff809 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=90dea3bff809
Added Files:
monetdb5/modules/mal/rapi.c
monetdb5/modules/mal/rapi.h
monetdb5/modules/mal/rapi.mal
Modified Files:
monetdb5/modules/mal/Makefile.ag
monetdb5/modules/mal/mal_init.mal
Branch: RIntegration
Log Message:
Basic files for Rapi
Compilation wont work until we have LIBR added to the configuration files.
diffs (truncated from 313 to 300 lines):
diff --git a/monetdb5/modules/mal/Makefile.ag b/monetdb5/modules/mal/Makefile.ag
--- a/monetdb5/modules/mal/Makefile.ag
+++ b/monetdb5/modules/mal/Makefile.ag
@@ -47,6 +47,7 @@ lib_mal = {
mat.c mat.h \
mdb.c mdb.h \
mkey.c mkey.h \
+ rapi.c rapi.h \
pcre.c \
pqueue.c pqueue.h \
profiler.c profiler.h \
@@ -74,7 +75,7 @@ headers_mal = {
profiler.mal const.mal batExtensions.mal \
inspect.mal manual.mal mal_io.mal pqueue.mal mkey.mal \
iterator.mal clients.mal \
- factories.mal groupby.mal mdb.mal pcre.mal mat.mal \
+ factories.mal groupby.mal mdb.mal pcre.mal rapi.mal mat.mal \
urlbox.mal transaction.mal \
mal_mapi.mal sabaoth.mal remote.mal \
txtsim.mal recycle.mal \
@@ -84,7 +85,7 @@ headers_mal = {
}
EXTRA_DIST = batExtensions.mal iterator.mal constraints.mal \
- groupby.mal mal_init.mal manual.mal mkey.mal pcre.mal \
+ groupby.mal mal_init.mal manual.mal mkey.mal pcre.mal rapi.mal \
profiler.mal recycle.mal remote.mal sabaoth.mal trader.mal \
transaction.mal txtsim.mal tablet.mal tablet.h sample.mal json_util.mal
\
mal_mapi.mal mat.mal tokenizer.mal pqueue.mal calc.mal \
diff --git a/monetdb5/modules/mal/mal_init.mal
b/monetdb5/modules/mal/mal_init.mal
--- a/monetdb5/modules/mal/mal_init.mal
+++ b/monetdb5/modules/mal/mal_init.mal
@@ -110,6 +110,7 @@ include cluster;
include txtsim;
include tokenizer;
include zorder;
+include rapi;
# library logger;
# include logger;
diff --git a/monetdb5/modules/mal/rapi.c b/monetdb5/modules/mal/rapi.c
new file mode 100644
--- /dev/null
+++ b/monetdb5/modules/mal/rapi.c
@@ -0,0 +1,168 @@
+/*
+ * The contents of this file are subject to the MonetDB Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.monetdb.org/Legal/MonetDBLicense
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ *
+ * The Original Code is the MonetDB Database System.
+ *
+ * The Initial Developer of the Original Code is CWI.
+ * Portions created by CWI are Copyright (C) 1997-July 2008 CWI.
+ * Copyright August 2008-2013 MonetDB B.V.
+ * All Rights Reserved.
+ */
+
+/*
+ * H. Muhleissen, M. Kersten
+ * The R interface
+ */
+#include "monetdb_config.h"
+#include "rapi.h"
+
+char * R_HomeDir() {
+ // this won't work in general
+ return "/usr/lib64/R";
+}
+
+// The R-environment should be single threaded, calling for some protective
measures.
+static MT_Lock rapiLock;
+
+str
+RAPIprelude(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci){
+ (void) cntxt;
+ (void) mb;
+ (void) stk;
+ (void) pci;
+ char *rargv[] = { "whatever", "--quiet", "--no-save", "--vanilla" };
+
+ MT_lock_init( &rapiLock,"rapi_lock");
+ Rf_initEmbeddedR(4, rargv);
+ // these globals are indicative for non-thread safe R settings.
+ ptr_R_WriteConsole = GDKout;
+ ptr_R_WriteConsoleEx = GDKerr;
+ R_Outputfile = NULL;
+ R_Consolefile = NULL;
+ return MAL_SUCCEED;
+}
+
+str
+RAPIpostlude(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci){
+ (void) cntxt;
+ (void) mb;
+ (void) stk;
+ (void) pci;
+ Rf_endEmbeddedR(0);
+ return MAL_SUCCEED;
+}
+
+str
+RAPIeval(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci){
+ str expr = *(str*) getArgReference(stk,pci,pci->retc+1);
+ SEXP x;
+ ParseStatus status;
+ int i;
+ char buf[256]={"rapi"};
+ char *args, *msg = createException(MAL,"rapi.eval","NYI");
+
+
+ (void) mb;
+
+ args = (str*) GDKmalloc(sizeof(str) * pci->argc);
+ if ( args == NULL)
+ throw(MAL,"rapi.eval", MAL_MALLOC_FAIL);
+
+ MT_lock_set(&rapiLock,"rapi.evaluate");
+ // check R expression for errors
+
+#ifdef _RAPI_DEBUG_
+ mnstr_printf(cntxt->fdout,"#Running R expression %s\n",exprStr);
+#else
+ (void) cntxt;
+#endif
+ // install the MAL variables into the R environment
+ for( i = pci->retc+1; i< pci->argc; i++){
+ SEXP varname;
+ SEXP varvalue;
+
+ snprintf(buf,"arg%d", i);
+ args[i] = GDKstrdup(buf);
+ varname = Rf_install( GDKstrdup(buf));
+ // check for BAT or scalar first !!
+ if ( isaBatType(getArgType(pci,i))){
+ // hand over a BAT into a vector
+ } else
+ switch(ATOMstorage(getTailType(getArgType(pci,i)))){
+ case TYPE_int:
+ varvalue = NEW_INTEGER(*(int*)
getArgReference(stk,pci,i));
+ break;
+ case TYPE_flt:
+ varvalue = NEW_FLOAT(*(flt*)
getArgReference(stk,pci,i));
+ break;
+ case TYPE_dbl:
+ varvalue = NEW_DOUBLE(*(dbl*)
getArgReference(stk,pci,i));
+ break;
+ case TYPE_bte:
+ case TYPE_sht:
+ case TYPE_lng:
+ // no clue what type to consider
+ default:
+ msg = createException(MAL,"mapi.eval","unknown argument
type");
+ goto wrapup;
+ }
+ Rf_defineVar(varname,varvalue, R_GlobalEnv);
+ }
+
+ // install vector into R environment
+ x = R_ParseVector(mkString(exprStr), INT_MAX, &status, R_NilValue);
+ if (status != PARSE_OK){
+ msg= createException(MAL,"RAPI %s\n", exprStr);
+ goto wrapup;
+ }
+
+ for (i = 0; i < LENGTH(x); i++) {
+ eval(VECTOR_ELT(x, i), R_GlobalEnv);
+ }
+
+ // collect the return values
+ for( i=0; i< pci->retc; i++){
+ SEXP othervar;
+ snprintf(bug, "ret%d",i);
+ args[i] = GDKstrdup(buf);
+ othervar = Rf_findVar(Rf_install(buf), R_GlobalEnv);
+ //do something with it
+ if ( isaBatType(getArgType(pci,i))){
+ // hand over the vector into a BAT
+ } else {
+ switch(ATOMstorage(getTailType(getArgType(pci,i)))){
+ case TYPE_int:
+ *(int*) getArgReference(stk,pci,i) = *(int*) othervar;;
+ break;
+ case TYPE_flt:
+ *(flt*) getArgReference(stk,pci,i) = *(flt*) othervar;
+ break;
+ case TYPE_dbl:
+ case TYPE_bte:
+ case TYPE_sht:
+ case TYPE_lng:
+ // no clue what type to consider
+ default:
+ msg = createException(MAL,"mapi.eval","unknown argument
type");
+ goto wrapup;
+ }
+ }
+ msg = MAL_SUCCEED;
+wrapup:
+ MT_lock_unset(&rapiLock,"rapi.evaluate");
+ // free all names variables introduced so far.
+ // Beware, they still live in the R global context
+ for(i=0; i<pci->argc; i++)
+ if( args[i])
+ GDKree(args[i]);
+ GDKfree(args);
+ return msg;
+}
diff --git a/monetdb5/modules/mal/rapi.h b/monetdb5/modules/mal/rapi.h
new file mode 100644
--- /dev/null
+++ b/monetdb5/modules/mal/rapi.h
@@ -0,0 +1,56 @@
+/*
+ * The contents of this file are subject to the MonetDB Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.monetdb.org/Legal/MonetDBLicense
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ *
+ * The Original Code is the MonetDB Database System.
+ *
+ * The Initial Developer of the Original Code is CWI.
+ * Portions created by CWI are Copyright (C) 1997-July 2008 CWI.
+ * Copyright August 2008-2013 MonetDB B.V.
+ * All Rights Reserved.
+ */
+
+/*
+ * H. Muhleissen, M. Kersten
+ * The R interface
+ */
+#ifndef _RAPI_LIB_
+#define _RAPI_LIB_
+#include "monetdb_config.h"
+#include <string.h>
+
+#include "mal.h"
+#include "mal_exception.h"
+#include "mal_interpreter.h"
+
+// R headers
+#include <Rembedded.h>
+#include <Rdefines.h>
+#define R_INTERFACE_PTRS
+#include <Rinterface.h>
+#include <Rinternals.h>
+#include <R_ext/Parse.h>
+
+#ifdef WIN32
+#if !defined(LIBMAL) && !defined(LIBATOMS) && !defined(LIBKERNEL) &&
!defined(LIBMAL) && !defined(LIBOPTIMIZER) && !defined(LIBSCHEDULER) &&
!defined(LIBMONETDB5)
+#define rapi_export extern __declspec(dllimport)
+#else
+#define rapi_export extern __declspec(dllexport)
+#endif
+#else
+#define rapi_export extern
+#endif
+
+rapi_export str RAPIeval(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr
pci);
+rapi_export str RAPIprelude(Client cntxt, MalBlkPtr mb, MalStkPtr stk,
InstrPtr pci);
+rapi_export str RAPIpostlude(Client cntxt, MalBlkPtr mb, MalStkPtr stk,
InstrPtr pci);
+rapi_export char * R_HomeDir();
+
+#endif /* _RAPI_LIB_ */
diff --git a/monetdb5/modules/mal/rapi.mal b/monetdb5/modules/mal/rapi.mal
new file mode 100644
--- /dev/null
+++ b/monetdb5/modules/mal/rapi.mal
@@ -0,0 +1,34 @@
+# The contents of this file are subject to the MonetDB Public License
+# Version 1.1 (the "License"); you may not use this file except in
+# compliance with the License. You may obtain a copy of the License at
+# http://www.monetdb.org/Legal/MonetDBLicense
+#
+# Software distributed under the License is distributed on an "AS IS"
+# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+# License for the specific language governing rights and limitations
+# under the License.
+#
+# The Original Code is the MonetDB Database System.
+#
+# The Initial Developer of the Original Code is CWI.
+# Portions created by CWI are Copyright (C) 1997-July 2008 CWI.
+# Copyright August 2008-2013 MonetDB B.V.
+# All Rights Reserved.
+
+module rapi;
+
+pattern eval(expr:str):any
+address RAPIevaluate
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list