You are viewing a plain text version of this content. The canonical link for it is here.
Posted to triplesoup-commits@incubator.apache.org by le...@apache.org on 2007/04/13 08:56:16 UTC
svn commit: r528394 [7/35] - in
/incubator/triplesoup/donations/TRIPLES-3-RDFStore: ./ dbms/ dbms/client/
dbms/client/t/ dbms/dbmsproxy/ dbms/deamon/ dbms/doc/ dbms/include/
dbms/libdbms/ dbms/utils/ doc/ include/ lib/ lib/DBD/ lib/RDFStore/
lib/RDFSto...
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/main.c
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/main.c?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/main.c (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/main.c Fri Apr 13 01:56:01 2007
@@ -0,0 +1,567 @@
+/*
+ * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+ * Dirk-Willem van Gulik <di...@webweaving.org>
+ *
+ * NOTICE
+ *
+ * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+ * file you should have received together with this source code. If you did not get a
+ * a copy of such a license agreement you can pick up one at:
+ *
+ * http://rdfstore.sourceforge.net/LICENSE
+ *
+ *
+ * $Id: main.c,v 1.22 2006/06/19 10:10:22 areggiori Exp $
+ */
+#include "dbms.h"
+#include "dbms_compat.h"
+#include "dbms_comms.h"
+#include "dbmsd.h"
+#include "version.h"
+
+#include "deamon.h"
+#include "handler.h"
+#include "mymalloc.h"
+
+#ifndef USER
+#define USER "nobody"
+#endif
+
+#ifndef PID_FILE
+#define PID_FILE "/var/run/dbms.pid"
+#endif
+
+#ifndef DUMP_FILE
+#define DUMP_FILE "/var/tmp/dbms.dump"
+#endif
+
+#ifndef PORT
+#define PORT 1234
+#endif
+
+#ifndef DIR_PREFIX
+#define DIR_PREFIX "/pen/dbms"
+#endif
+
+#ifndef CONF_FILE
+#define CONF_FILE DIR_PREFIX "/dbms.conf"
+#endif
+
+/* Listen queue, see listen() */
+#ifndef MAX_QUEUE
+#define MAX_QUEUE 128
+#endif
+
+/* If defined, we check for timeouts (in seconds). When left
+ * undefined, no time/bookkeeping is done.
+ */
+/* #define TIMEOUT 250 */
+
+#ifdef RDFSTORE_DBMS_DEBUG_TIME
+float total_time;
+#endif
+#ifdef FORKING
+struct child_rec * children=NULL;
+#endif
+struct connection * client_list=NULL;
+struct connection * mum;
+fd_set rset,wset,eset,alleset,allrset,allwset;
+int sockfd,maxfd,mum_pid, mum_pgid;
+char * my_dir = DIR_PREFIX;
+char * pid_file = PID_FILE;
+char * conf_file = CONF_FILE;
+int max_processes=MAX_CHILD;
+int max_dbms=MAX_DBMS_CHILD;
+int max_clients=MAX_CLIENT;
+
+int sysloglog = 1;
+int stderrlog = 0;
+FILE * errorout;
+
+#define SERVER_NAME "DBMS-Dirkx/3.00"
+
+int debug = 0;
+int verbose = 0;
+int trace_on = 0;
+
+#define barf(x) { dbms_log(L_FATAL,x ":%s",strerror(errno)); exit(1); }
+
+static char *_exp[]={
+ "**FATAL", "**ERROR", "Warning", " Inform", "verbose", " bloat", " debug"
+ };
+
+static int lexp[]={
+ LOG_ALERT, LOG_ERR, LOG_WARNING, LOG_INFO,
+ LOG_DEBUG,LOG_DEBUG,LOG_DEBUG
+ };
+
+void
+reply_log(connection * r, int level, char * fmt, ...)
+{
+ char tmp[ 1024 ];
+ char tmp2[ 2*1024 ];
+ va_list ap;
+ pid_t p = getpid();
+ DBT v;
+ errorout = stderr;
+
+ va_start(ap,fmt);
+ vsnprintf(tmp,sizeof(tmp),fmt,ap);
+ va_end(ap);
+
+ if (level<=verbose) {
+ snprintf(tmp2,sizeof(tmp2),"%d:%s %s %s",p,
+ (!mum_pid) ? "Mum" : "Cld",
+ _exp[ level - L_FATAL ],
+ tmp);
+
+ if (sysloglog)
+ syslog(lexp[ level - L_FATAL ],"%s",tmp2);
+
+ if (stderrlog)
+ fprintf(errorout,"%s\n",tmp2);
+ }
+
+ v.data = tmp;
+ v.size = strlen(tmp);
+ dispatch(r,TOKEN_ERROR,&v,NULL);
+}
+
+void
+dbms_log(int level, char * fmt, ...)
+{
+ char tmp[ 1024 ];
+ va_list ap;
+ pid_t p = getpid();
+ errorout = stderr;
+
+ if (level>verbose)
+ return;
+
+ snprintf(tmp,1024,"%d:%s %s %s",p,
+ (!mum_pid) ? "Mum" : "Cld",
+ _exp[ level - L_FATAL ],
+ fmt);
+
+if (sysloglog) {
+ va_start(ap,fmt);
+ vsyslog(lexp[ level - L_FATAL ],tmp,ap);
+ va_end(ap);
+}
+if (stderrlog) {
+ va_start(ap,fmt);
+ vfprintf(errorout,tmp,ap);
+ va_end(ap);
+ fprintf(errorout,"\n");
+}
+}
+
+void
+trace(char * fmt, ...)
+{
+ char tmp[ 1024 ];
+ va_list ap;
+ time_t tt;
+ pid_t p = getpid();
+
+ if (!trace_on) return;
+
+ time(&tt);
+
+ snprintf(tmp,1024,"%d:%s %20s\t%s\n",
+ p,
+ (!mum_pid) ? "Mum" : "Cld",
+ asctime(gmtime(&tt)),
+ fmt
+ );
+
+ va_start(ap,fmt);
+ vprintf(tmp,ap);
+ va_end(ap);
+
+ fflush(stdout);
+}
+
+int check_children=0;
+
+void loglevel( int i) {
+ if (i == SIGUSR1 ) verbose++;
+ if (i == SIGUSR2 ) {
+ verbose=0;
+ for(i=0; i < sizeof(cmd_table) / sizeof(struct command_req); i++)
+ cmd_table[i].cnt = 0;
+ };
+
+ dbms_log(L_ERROR,"Log level changed by signal to %d",debug);
+ }
+
+
+void dumpie ( int i ) {
+#ifdef FORKING
+ child_rec * c;
+#endif
+ connection * r;
+ dbase * d;
+ time_t t=time(NULL);
+ FILE * f;
+ if ((f=fopen(DUMP_FILE,"w"))==NULL) {
+ dbms_log(L_ERROR,"Cannot open dbmsd.dump: %s",strerror(errno));
+ return;
+ };
+
+ fprintf(f,"# Dump DBMS - pid=%d - %s\n",(int)getpid(),ctime(&t));
+#ifdef FORKING
+ fprintf(f,"# Children\n");
+ for( c=children; c; c=c->nxt) {
+ fprintf(f," %7p Pid %5d conn=%p fd=%d\n",
+ c,c->pid,c->r,c->r ? c->r->clientfd : -1);
+ for( d=first_dbp; d; d=d->nxt ) if (d->handled_by == c)
+ fprintf(f,"\t%7p %s\n",d,d->name);
+ };
+#endif
+ fprintf(f,"# Databases\n");
+ for( d=first_dbp; d; d=d->nxt )
+#ifdef FORKING
+ fprintf(f," %7p %s %p\n", d,d->name, d->handled_by);
+#else
+ fprintf(f," %7p %s\n", d,d->name);
+#endif
+
+ fprintf(f,"# Clients\n");
+ for( r=client_list; r; r=r->next)
+ fprintf(f," %7p fd=%d type=%d Dbase %7p %s\n",
+ r,r->clientfd,r->type,r->dbp,
+ ( r->dbp ? r->dbp->name : 0));
+
+ fprintf(f,"# Stats\n");
+ for(i=0; i < sizeof(cmd_table) / sizeof(struct command_req); i++)
+ fprintf(f," %8s: %d\n",cmd_table[i].info, cmd_table[i].cnt);
+
+#ifdef RDFSTORE_DBMS_DEBUG_MALLOC
+ fprintf(f,"# Memory in use\n");
+ debug_malloc_dump(f);
+#endif
+ fprintf(f,"\n---------\n");
+ fclose(f);
+ };
+
+void childied( int i ) {
+ int oerrno=errno;
+ int status;
+ int pid;
+
+ /* reap children, and mark them closed.
+ */
+ while((pid=waitpid(-1,&status,WNOHANG))>0) {
+#ifdef FORKING
+ child_rec * c;
+ dbms_log(L_INFORM,"Skeduled to zap one of my children pid=%d",pid);
+ for(c=children;c;c=c->nxt)
+ if (c->pid == pid)
+ c->close = 1; MX;
+#endif
+ }
+
+ if ((pid == -1) && ( errno=ECHILD)) {
+#if 0
+ dbms_log(L_ERROR,"Gotten CHILD died signal, but no child...");
+#endif
+ } else
+ if (pid == -1) {
+ dbms_log(L_ERROR,"Failed to get deceased PID: %s",strerror(errno));
+ }
+
+ errno=oerrno;
+ return;
+}
+
+int
+main( int argc, char * argv[])
+{
+ struct sockaddr_in server;
+ int port;
+ int dtch=1;
+ int one=1,i;
+ struct rlimit l;
+ int needed=0;
+ char * as_user=USER;
+ struct sigaction act,oact;
+ in_addr_t bound = INADDR_ANY;
+ const char * erm;
+
+ errorout = stderr;
+
+ port = PORT;
+
+ for( i=1; i<argc; i++) {
+ if ((!strcmp(argv[i],"-p")) && (i+1<argc)) {
+ port=atoi(argv[++i]);
+ if (port<=1) {
+ fprintf(stderr,"Aborted: You really want a port number >1.\n");
+ exit(1);
+ };
+ } else
+ if ((!strcmp(argv[i],"-b")) && (i+1<argc)) {
+ char * iface = argv[++i];
+ bound = inet_addr(iface); /* First treat it as an UP address */
+ if (bound == INADDR_NONE) {
+ /* Not a valid IP address - try to look it up */
+ struct hostent * hp;
+ if((hp = gethostbyname(iface))==NULL) {
+ perror("Address to listen on not found");
+ exit(1);
+ };
+ bound = *(u_long *) hp->h_addr;
+ }
+ } else
+ if ((!strcmp(argv[i],"-d")) && (i+1<argc)) {
+ my_dir = argv[++i];
+ } else
+ if ((!strcmp(argv[i],"-u")) && (i+1<argc)) {
+ as_user= argv[++i];
+ } else
+ if (!strcmp(argv[i],"-U")) {
+ as_user= NULL;
+ } else
+ if ((!strcmp(argv[i],"-C")) && (i+1<argc)) {
+ conf_file = argv[++i];
+ if ((erm=parse_config(conf_file))) {
+ fprintf(stderr,"Aborted: %s\n",erm);
+ exit(1);
+ };
+ printf("Config file parsed OK\n");
+ exit(0);
+ } else
+ if ((!strcmp(argv[i],"-c")) && (i+1<argc)) {
+ conf_file = argv[++i];
+ } else
+ if ((!strcmp(argv[i],"-P")) && (i+1<argc)) {
+ pid_file= argv[++i];
+ } else
+ if ((!strcmp(argv[i],"-n")) && (i+1<argc)) {
+ max_processes = atoi( argv[ ++i ] );
+ if ((max_processes < 1) || (max_processes > MAX_CHILD)) {
+ fprintf(stderr,"Aborted: Max Number of child processes must be between 1 and %d\n",MAX_CHILD);
+ exit(1);
+ };
+ } else
+ if ((!strcmp(argv[i],"-m")) && (i+1<argc)) {
+ max_dbms = atoi( argv[ ++i ] );
+ if ((max_dbms < 1) || (max_dbms > MAX_DBMS)) {
+ fprintf(stderr,"Aborted: Max Number of DB's must be between 1 and %d\n",MAX_DBMS);
+ exit(1);
+ };
+ } else
+ if ((!strcmp(argv[i],"-C")) && (i+1<argc)) {
+ max_clients = atoi( argv[ ++i ] );
+ if ((max_clients < 1) || (max_clients> MAX_CLIENT)) {
+ fprintf(stderr,"Aborted: Max Number of children must be between 1 and %d\n",MAX_CLIENT);
+ exit(1);
+ };
+ } else
+ if (!strcmp(argv[i],"-x")) {
+ verbose++; debug++;
+ if (debug>2) dtch = 0;
+ } else
+ if (!strcmp(argv[i],"-D")) {
+ dtch = 0;
+ } else
+ if (!strcmp(argv[i],"-t")) {
+ trace_on= 1;
+ } else
+ if (!strcmp(argv[i],"-v")) {
+ printf("%s\n",get_full());
+ printf("Max clients: %d\n",MAX_CLIENT);
+ printf("Max DBs: %d\n",MAX_DBMS);
+ printf("Max Children: %d\n",MAX_CHILD);
+ printf("Max Payload: %d bytes\n",MAX_PAYLOAD);
+ printf("Default dir: %s\n",DIR_PREFIX);
+ printf("Default config: %s\n",CONF_FILE);
+ exit(0);
+ } else
+ if (!strcmp(argv[i],"-X")) {
+ verbose=debug=100; dtch = 0; sysloglog = 0; stderrlog = 1;
+ } else
+ if ((!strcmp(argv[i],"-e")) && (i+1<argc)) {
+ stderrlog = 1;
+ if ((errorout = fopen(argv[++i],"a")) == NULL) {
+ fprintf(stderr,"Aborted. Cannot open logfile %s for writing: %s\n",
+ argv[i],strerror(errno));
+ exit(1);
+ };
+ } else
+ if (!strcmp(argv[i],"-E")) {
+ stderrlog = 1;
+ } else {
+ fprintf(stderr,"Syntax: %s [-U | -u <userid>] [-E] [-P <pid-file>] [-d <directory_prefix>] [-b <ip to bind to>] [-p <port>] [-x] [-n <max children>] [-m <max databases>] [-C <max clients>] <-c conffile>\n",argv[0]);
+ exit(1);
+ };
+ };
+ if ((erm=parse_config(conf_file))) {
+ fprintf(stderr,"Aborted: %s\n",erm);
+ exit(1);
+ }
+
+ if (HARD_MAX_CLIENTS < max_clients +3) {
+ fprintf(stderr,"Aborted: Max number of clients larger than compiled hard max(%d)\n",HARD_MAX_CLIENTS);
+ exit(1);
+ };
+
+ needed=MAX(max_processes, max_clients/max_processes+max_dbms/max_processes) + 5;
+
+ if (FD_SETSIZE < needed ) {
+ fprintf(stderr,"Aborted: Number of select()-able file descriptors too low (FD_SETSIZE)\n");
+ exit(1);
+ };
+
+ if (getrlimit(RLIMIT_NOFILE,&l)==-1)
+ barf("Could not obtain limit of files open\n");
+
+ if (l.rlim_cur < needed ) {
+ fprintf(stderr,"Aborted: Resource limit imposes on number of open files too limiting\n");
+ exit(1);
+ };
+
+#ifndef RDFSTORE_PLATFORM_SOLARIS
+ if (getrlimit(RLIMIT_NPROC,&l)==-1)
+ barf("Could not obtain limit on children\n");
+
+ if (l.rlim_cur < 2+max_processes) {
+ fprintf(stderr,"Aborted: Resource limit imposes on number of children too limiting\n");
+ exit(1);
+ };
+#endif
+
+ if (sysloglog)
+ openlog("dbms",LOG_LOCAL4, LOG_PID | LOG_CONS);
+
+ if ( (sockfd = socket( AF_INET, SOCK_STREAM, 0))<0 )
+ barf("Cannot open socket");
+
+ if( (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(const char *)&one,sizeof(one))) <0)
+ barf("Could not set REUSEADDR option");
+
+ if( (setsockopt(sockfd,IPPROTO_TCP,TCP_NODELAY,(const void *)&one,sizeof(one))) <0)
+ barf("Could not distable Nagle algoritm");
+
+{
+ int sendbuf = 32 * 1024;
+ int recvbuf = 32 * 1024;
+
+ if( (setsockopt(sockfd,SOL_SOCKET,SO_SNDBUF,(const void *)&sendbuf,sizeof(sendbuf))) <0)
+ barf("Could not set sendbuffer size");
+
+ if( (setsockopt(sockfd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf))) <0)
+ barf("Could not set sendbuffer size");
+}
+ if ( (i=fcntl(sockfd, F_GETFL, 0)<0) || (fcntl(sockfd, F_SETFL,i | O_NONBLOCK)<0) )
+ barf("Could not make socket non blocking");
+
+ bzero( (char *) &server,sizeof(server) );
+ server.sin_family = AF_INET;
+ server.sin_addr.s_addr = bound; /* Already in network order. */
+ server.sin_port = htons( port );
+
+ if ( (bind( sockfd, ( struct sockaddr *) &server, sizeof (server)))<0 )
+ barf("Cannot bind server to (lcoal) address.");
+
+ /* Allow for a que.. */
+ if ( listen(sockfd,MAX_QUEUE)<0 )
+ barf("Could not start to listen to my port");
+
+ /* fork and detach if ness.
+ */
+ if (dtch) {
+#ifdef FORKING
+ pid_t pid;
+/*
+ fclose(stdin);
+ if (!trace_on) fclose(stdout);
+*/
+ if ( (pid = fork()) < 0) {
+ perror("Could not fork");
+ exit(1);
+ }
+ else if (pid != 0) {
+ FILE * fd;
+ if (!(fd=fopen(pid_file,"w"))) {
+ fprintf(stderr,"Warning: Could not write pid file %s:%s",pid_file,strerror(errno));
+ exit(1);
+ };
+ fprintf(fd,"%d\n", (int)pid);
+ fclose(fd);
+ exit(0);
+ };
+
+#else
+ fprintf(stderr,"No forking compiled in, no detach\n");
+#endif
+
+ /* become session leader
+ */
+ if ((mum_pgid = setsid())<0)
+ barf("Could not become session leader");
+ };
+
+ /* XXX security hole.. yes I know...
+ */
+ if (as_user != NULL) {
+ struct passwd * p = getpwnam( as_user );
+ uid_t uid;
+
+ uid = (p == NULL) ? atoi( as_user ) : p->pw_uid;
+
+ if ( !uid || setuid(uid) ) {
+ perror("Cannot setuid");
+ exit(0);
+ };
+ };
+
+#if 0
+ chdir(my_dir); /* change working directory */
+// chroot(my_dir); /* for sanities sake */
+ umask(0); /* clear our file mode creation mask */
+#endif
+
+ mum_pid = 0;
+
+ FD_ZERO(&allrset);
+ FD_ZERO(&allwset);
+ FD_ZERO(&alleset);
+
+ FD_SET(sockfd,&allrset);
+ FD_SET(sockfd,&alleset);
+
+ maxfd=sockfd;
+ client_list=NULL;
+
+ dbms_log(L_INFORM,"Waiting for connections");
+
+ signal(SIGHUP,dumpie);
+ signal(SIGUSR1,loglevel);
+ signal(SIGUSR2,loglevel);
+ signal(SIGINT,cleandown);
+ signal(SIGQUIT,cleandown);
+ signal(SIGKILL,cleandown);
+ signal(SIGTERM,cleandown);
+#ifdef FORKING
+ signal(SIGCHLD,childied);
+#endif
+ mum = NULL;
+
+ trace("Tracing started\n");
+
+ /* for now, SA_RESTART any interupted PIPE calls
+ */
+ act.sa_handler = SIG_IGN;
+ sigemptyset(&act.sa_mask);
+ act.sa_flags = SA_RESTART;
+ sigaction(SIGPIPE,&act,&oact);
+
+ init_cmd_table();
+
+ select_loop();
+ /* get down to handling.. (as the mother) */
+
+ return 0; /* keep the compiler happy.. */
+ }
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/mymalloc.c
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/mymalloc.c?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/mymalloc.c (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/mymalloc.c Fri Apr 13 01:56:01 2007
@@ -0,0 +1,110 @@
+/*
+ * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+ * Dirk-Willem van Gulik <di...@webweaving.org>
+ *
+ * NOTICE
+ *
+ * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+ * file you should have received together with this source code. If you did not get a
+ * a copy of such a license agreement you can pick up one at:
+ *
+ * http://rdfstore.sourceforge.net/LICENSE
+ *
+ * Simple debugging malloc, when/if needed
+ *
+ * $Id: mymalloc.c,v 1.11 2006/06/19 10:10:22 areggiori Exp $
+ */
+#include "dbms.h"
+#include "dbms_compat.h"
+#include "dbms_comms.h"
+
+#include "dbmsd.h"
+
+#include "deamon.h"
+#include "mymalloc.h" /* to keep us honest */
+
+char * memdup( void * data, size_t size ) {
+ void * out = mymalloc( size );
+ if (out == NULL)
+ return NULL;
+ memcpy(out, data, size );
+ return out;
+ }
+
+
+#ifdef RDFSTORE_DBMS_DEBUG_MALLOC
+struct mp {
+ void * data;
+ int line;
+ int len;
+ char * file;
+ TIMESPEC tal;
+ struct mp * nxt;
+ } mp;
+
+struct mp * mfirst=NULL;
+int mpfree=0,mpalloc=0;
+
+void * debug_malloc( size_t len, char * file, int line ) {
+ struct mp * p = malloc( sizeof( mp ) );
+ if (p==NULL)
+ return NULL;
+ p->data = malloc( len );
+ if (p->data==NULL)
+ return NULL;
+#ifdef RDFSTORE_DBMS_DEBUG
+ bzero(p->data,len);
+#endif
+ p->tal = time(NULL);
+ p->file = strdup( file );
+ p->line = line;
+ p->len = len;
+ p->nxt = mfirst;
+ mfirst = p;
+ mpalloc++;
+ return p->data;
+ }
+
+void debug_free( void * addr, char * file, int line ) {
+ struct mp *q, * * p;
+
+ for( p=&mfirst; *p; p=&((*p)->nxt))
+ if ((*p)->data == addr)
+ break;
+
+ if (!*p) {
+ dbms_log(L_ERROR,"Unanticipated Free from %s:%d",file,line);
+#ifdef RDFSTORE_DBMS_DEBUG
+ abort();
+#endif
+ };
+ q = *p;
+ *p=(*p)->nxt;
+#ifdef RDFSTORE_DBMS_DEBUG
+ bzero(q->data,q->len);
+#endif
+ free(q->data);
+ free(q->file);
+ free(q);
+
+ mpfree++;
+ }
+
+void debug_malloc_dump(FILE * f) {
+ struct mp * p;
+ TIMESPEC now=time(NULL);
+ unsigned int t = 0;
+
+ fprintf(f,"Memory==malloc %d == %d free\n",mpalloc,mpfree);
+ for( p = mfirst; p; p=p->nxt) {
+ fprintf(f,"%05d(%s) %12s %5d size %9d age %.0f at %p\n",
+ getpid(),mum_pid ? "Chd" : "Mum",
+ p->file,p->line,p->len,
+ difftime(now,p->tal),
+ p->data
+ );
+ t+=p->len;
+ }
+ fprintf(f,"Total %u bytes\n",t);
+}
+#endif
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/mymalloc.h
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/mymalloc.h?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/mymalloc.h (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/mymalloc.h Fri Apr 13 01:56:01 2007
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+ * Dirk-Willem van Gulik <di...@webweaving.org>
+ *
+ * NOTICE
+ *
+ * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+ * file you should have received together with this source code. If you did not get a
+ * a copy of such a license agreement you can pick up one at:
+ *
+ * http://rdfstore.sourceforge.net/LICENSE
+ *
+ *
+ * $Id: mymalloc.h,v 1.6 2006/06/19 10:10:22 areggiori Exp $
+ */
+char * memdup( void * data, size_t size );
+
+#ifdef RDFSTORE_DBMS_DEBUG_MALLOC
+void * debug_malloc( size_t len, char * file, int line);
+void debug_free( void * addr, char * file, int line );
+void debug_malloc_dump(FILE * file);
+
+#define mymalloc(x) debug_malloc(x,__FILE__,__LINE__)
+#define myfree(x) debug_free(x,__FILE__,__LINE__)
+#else
+#define mymalloc(x) malloc(x)
+#define myfree(x) free(x)
+#endif
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/pathmake.c
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/pathmake.c?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/pathmake.c (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/pathmake.c Fri Apr 13 01:56:01 2007
@@ -0,0 +1,147 @@
+/*
+ * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+ * Dirk-Willem van Gulik <di...@webweaving.org>
+ *
+ * NOTICE
+ *
+ * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+ * file you should have received together with this source code. If you did not get a
+ * a copy of such a license agreement you can pick up one at:
+ *
+ * http://rdfstore.sourceforge.net/LICENSE
+ *
+ *
+ * $Id: pathmake.c,v 1.9 2006/06/19 10:10:22 areggiori Exp $
+ */
+
+#include "dbms.h"
+#include "dbms_compat.h"
+#include "dbms_comms.h"
+#include "deamon.h"
+#include "pathmake.h"
+
+/* returns null and/or full path
+ * to a hashed directory tree.
+ * the final filename is hashed out
+ * within that three. Way to complex
+ * by now. Lifted from another project
+ * which needed more.
+ */
+char *
+mkpath(char * base, char * infile)
+{
+ char * file; int i,j;
+ char * slash,* dirname;
+ static char tmp[ MAXPATHLEN ];
+ char * inpath;
+ char tmp2[ MAXPATHLEN ];
+#define MAXHASH 2
+ static char hash[ MAXHASH+1 ];
+
+ strcpy(inpath=tmp2,infile);
+
+ memset(hash,'_',MAXHASH);
+ hash[ MAXHASH ]='\0';
+
+ if (base==NULL)
+ base="/";
+
+ if (inpath==NULL || inpath[0] == '\0') {
+ dbms_log(L_ERROR,"No filename or path for the database specified");
+ return NULL;
+ };
+
+ /* remove our standard docroot if present
+ * so we can work with something relative.
+ * really a legacy thing from older perl DBMS.pm
+ * versions. Can go now.
+ */
+ if (!(strncmp(base,inpath,strlen(base))))
+ inpath += strlen(base);
+
+ /* fetch the last leaf name
+ */
+ if((file = strrchr(inpath, '/')) != NULL) {
+ *file = '\0';
+ file++;
+ }
+ else {
+ file = inpath;
+ inpath = "/";
+ };
+
+ if (!strlen(file)) {
+ dbms_log(L_ERROR,"No filename for the database specified");
+ return NULL;
+ };
+
+ strncpy(hash,file,MIN(strlen(file),MAXHASH));
+
+// strcat(tmp,base,"/",inpath,"/",hash,"/",file,NULL);
+ strcpy(tmp,"/");
+ strcat(tmp,base);
+ strcat(tmp,"/");
+ strcat(tmp,inpath);
+ strcat(tmp,"/");
+ strcat(tmp,hash);
+ strcat(tmp,"/");
+ strcat(tmp,file);
+
+// sanity for leaf names...
+// actually this is really bad..
+//
+ if ((slash=strrchr(tmp,'.')) !=NULL) {
+ if ( (!strcasecmp(slash+1,"db")) ||
+ (!strcasecmp(slash+1,"dbm")) ||
+ (!strcasecmp(slash+1,"gdb"))
+ ) *slash = '\0';
+ };
+
+ strcat(tmp,".db");
+
+ for(i=0,j=0; tmp[i]; i++) {
+ if (i && tmp[i]=='/' && tmp[i-1] =='/')
+ continue;
+ if (i != j) tmp[j] = tmp[i];
+ j++;
+ };
+ tmp[j] = '\0';
+
+
+ /* run through the full path name, and verify that
+ * each directory along the path actually exists
+ */
+ slash = tmp;
+ dirname = tmp;
+
+ while((slash=strchr(slash+1,'/')) != NULL) {
+ struct stat s;
+ *slash='\0';
+ *dirname='/';
+ /* check if tmp exists and is a directory (or a link
+ * to one.. if not, create it, else give an error
+ */
+ if (stat(tmp,&s) == 0) {
+ /* something exists.. it must be a directory
+ */
+ if ((s.st_mode & S_IFDIR) == 0) {
+ dbms_log(L_ERROR,"Creation of %s failed; path element not directory",tmp);
+ return NULL;
+ };
+ }
+ else if ( errno == ENOENT ) {
+ if ((mkdir(tmp,(S_IRWXU | S_IRWXG | S_IRWXO))) != 0) {
+ dbms_log(L_ERROR,"Creation of %s failed; %s",tmp, strerror(errno));
+ return NULL;
+ };
+ }
+ else {
+ dbms_log(L_ERROR,"Path creation to failed at %s:%s",tmp,strerror(errno));
+ return NULL;
+ };
+ dirname=slash;
+ }
+ *dirname='/';
+
+ return tmp;
+ };
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/pathmake.h
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/pathmake.h?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/pathmake.h (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/pathmake.h Fri Apr 13 01:56:01 2007
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+ * Dirk-Willem van Gulik <di...@webweaving.org>
+ *
+ * NOTICE
+ *
+ * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+ * file you should have received together with this source code. If you did not get a
+ * a copy of such a license agreement you can pick up one at:
+ *
+ * http://rdfstore.sourceforge.net/LICENSE
+ *
+ *
+ * $Id: pathmake.h,v 1.4 2006/06/19 10:10:23 areggiori Exp $
+ */
+#ifndef _PATH_MAKE_H
+#define _PATH_MAKE_H
+
+char *
+mkpath(char * base, char * inpath);
+
+#endif
+
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/test-xs.conf
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/test-xs.conf?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/test-xs.conf (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/test-xs.conf Fri Apr 13 01:56:01 2007
@@ -0,0 +1,13 @@
+foo 1.2.3.4
+foo 10.0.0.4
+bar 1.2.3.4
+bar 7.6.5.4
+carp 5.4.3.2
+foo 10.0.1.0
+caffee 1.2.3.4
+caffee 10.0.0.2
+caffee 9.8.7.6
+caffee 9.8.7.5
+caffee 206.16.0.139
+caffee www.news.com
+caffee 127.0.0.1
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/version.h
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/version.h?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/version.h (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/version.h Fri Apr 13 01:56:01 2007
@@ -0,0 +1,17 @@
+/*
+ * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+ * Dirk-Willem van Gulik <di...@webweaving.org>
+ *
+ * NOTICE
+ *
+ * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+ * file you should have received together with this source code. If you did not get a
+ * a copy of such a license agreement you can pick up one at:
+ *
+ * http://rdfstore.sourceforge.net/LICENSE
+ *
+ *
+ * $Id: version.h,v 1.4 2006/06/19 10:10:23 areggiori Exp $
+ */
+int get_version(void);
+char * get_full(void);
Propchange: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/version.h
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/version.pl
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/version.pl?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/version.pl (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/version.pl Fri Apr 13 01:56:01 2007
@@ -0,0 +1,79 @@
+#!/usr/bin/perl
+# *
+# * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+# * Dirk-Willem van Gulik <di...@webweaving.org>
+# *
+# * NOTICE
+# *
+# * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+# * file you should have received together with this source code. If you did not get a
+# * a copy of such a license agreement you can pick up one at:
+# *
+# * http://rdfstore.sourceforge.net/LICENSE
+# *
+$rcsid='$Id: version.pl,v 1.8 2006/06/19 10:10:23 areggiori Exp $';
+#
+$h=`hostname`;
+chop $h;
+$id='??';
+if ($^O =~ m/solaris/i) {
+ $id=`whoami`;
+ chop $id;
+} else {
+ $a=`id -u -n`;
+ $id=$1 if $a=~ m/(\w+)/;
+}
+
+# $a=`pwd`;
+# $a =~ m/dbms-(\d+)\.(\d+)/
+# or die "no version in $a";
+# $version =$1 * 100 +$2;
+
+$rcsid =~ m/Id:\s+\S+\s+([\d\.]+)\s+/
+ or die "No version in rcs version";
+
+$version = $1;
+$date=gmtime(time);
+
+print qq|\
+/*>version.c - $0 generated - $version
+ * $rcsid
+ */
+#include <sys/types.h>
+
+#include "dbms_compat.h"
+
+#if 0
+static char rcsid[]="$rcsid";
+#endif
+
+static char version[]= "DBMS/$version - $date - $id\@$h - "
+#ifdef DB_VERSION_MAJOR
+ DB_VERSION_STRING
+#else
+ "Berkeley DB 1.x (perhaps BSD-ish built in library)"
+#endif
+
+#ifdef FORKING
+ " - forking"
+#else
+ " - NON forking"
+#endif
+#ifdef STATIC_BUFF
+ " - recycle structs "
+#endif
+#ifdef STATIC_CS_BUFF
+ " - static Client buffs "
+#endif
+#ifdef STATIC_SC_BUFF
+ " - static Server buffs "
+#endif
+
+ ;
+
+char * get_full( void ) {
+ return version;
+ }
+|;
+
+
Propchange: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/deamon/version.pl
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/doc/dbms.html
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/doc/dbms.html?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/doc/dbms.html (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/doc/dbms.html Fri Apr 13 01:56:01 2007
@@ -0,0 +1,134 @@
+<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
+<!--
+ * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+ * Dirk-Willem van Gulik <di...@webweaving.org>
+ *
+ * NOTICE
+ *
+ * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+ * file you should have received together with this source code. If you did not get a
+ * a copy of such a license agreement you can pick up one at:
+ *
+ * http://rdfstore.sourceforge.net/LICENSE
+ *
+-->
+
+<html>
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
+ <meta name="GENERATOR" content="Mozilla/4.76 [en] (X11; U; Linux 2.2.12 i386) [Netscape]">
+ <title>DBMS - A TCP/IP remote storage service</title>
+</head>
+
+<body link="#0000FF" vlink="#800080" bgcolor="#FFFFFF">
+
+<br><b><font face="Garamond"><font size=+2>The DB engine</font></font></b>
+<p><font face="Garamond"><font >For the <a href="http://parleunet.jrc.it">ParlEuNet</a> project - a federated user editable hierarchical datastore with a web front end - a fast networked transactional object store was required as a back end. Multiple single key hash based BerkeleyDB where used along with Object Serialization in Perl and an optimized network routing daemon with a single thread/process per database.</font></font> <p><font face="Garamond"><font >For an educational project a web application is build which allows for federated management of hierarchical multimedia content by its users. Due to the nature of the project, most users will use the system at the same time, during short windows on Wednesday afternoon. The develop, test and deploy timeline of the project is less than a year. Most users will be, at best, be utilizing a shared ISDN connection to the internet. As the project is to use standard web browsers and the general purpose HTTP protocol latency is a
serious issue; in order to reply quick enough only a fraction of the user perceived handling time can be spend by the application logic; and even less is to be spend on database lookups. Each user has access to almost all content, and almost all content can be edited at any time. To the project sharing and collaboration between users is of importance. For this reason it was decided early in the project that all operations will be carried out on a single data holding. Given the time line, a waterfall or layered design and construct approach was not considered feasible. This would leave to much to chance near the end of the project. Where, in our experience, most of the time actually should have been spend on operations and UI tuning issues. So instead a number of technologies are chosen allowing for rapid development of a environment in which ideas can be tested and which can be used as a template for the final production system. Given the expected complexity of the applicati
on logic the language perl is selected over php.These four requirements also translate to a rather stiff set of requirements for the backend. It needs to be <b>very fast</b>, <b>support atomic and/or transactional operations</b>, has to deal with <b>arbitrary sized data</b>, and can <b>handle multiple connections concurrently</b>. A fifth requirement however is considered even more important, that of <b>avoiding a 'stagger' situation</b>; a pseudo 'overload' situation where the (web) server appears to halt for short periods of times, and processes a lot of requests in parallel, and has a load far surpassing its hardware resources, followed by a long relatively quiet period. To understand this consider the following. On a high volume web server which serves mostly dynamic content serving an individual request can take relatively long to handle, process and send out. In order to deal which such loads most web servers will parellelize requests. As the load increases at some mom
ent is inevitable to have request overlapping. As soon as several requests start to overlap, and compete for resources, the server will take a noticeable performance hit; causing the requests to be handled significantly slower; and thus occupying a longer time slot. Thus increasing the number of requests which have to be handled in parallel quite dramatically. At this point server performance trails off, though overall average throughput stays nearly the same. As users have a certain 'click' through behaviour which has the temporal properties of pink noise at some moments normal <a href="http://www.humanfactor.com/cgi-bin/cgi-delegate/apache-ML/nh/1998/Apr/0260.html">Markof queue</a> assumptions no longer hold, and the application starts to act as both a alpha over beta filter which loops back and a phase loop back amplifier which will, at first slowly but progressively quicker, lock in to a specific Eigen frequency<!--which is a function of ??? << elaborate more on th
e above - and show the kap graphs from the german site and the finland measure trials>>-->. It is important to note that this is more a 'perception' problem leading to loss of user satisfaction; the average throughput has not gone down by much (the only real cost is some context switching by the kernel, and when the datasets are large and the access patterns is sparse, loss of cache effectiveness). Hence normal 'peak' benchmarks do not detect such.</font></font>
+
+<p><a NAME="_Toc463634473"></a><b><font face="Garamond"><font >Approach</font></font></b>
+<p><font face="Garamond"><font >All operations are considered atomic,
+and hence need to be serialized for a given table. As both lock (flock())
+and mutexes can be expensive, and cause stammers, each table accessor is
+given its own thread of execution. Within this space incoming requests
+are served sequentially on a first come, first serve basis. Using non blocking
+IO and extensive buffering of both incoming and outgoing packets processing
+stalls are reduced to a minimum. A client connects to a well known port
+and presents an INIT request for a specific table with specific read, create
+or write permissions. A protocol version is also exchanged. The mother
+process receives the request and check's if any of the existing children
+already has this table open. If so the mother issues an file descriptor
+handoff message to the child, along with a copy of the INIT request. If
+the table is not yet open, the mother either created a new child or assigns
+the table to the least loaded child using a simple lowest number of tables
+round robin algorithm. Each child thus receives an INIT from the connection
+to the mother; and then uses the file descriptor handoff message to connect
+directly with the client; and sends an ACK (with fault) to both the mother
+and client. The mother process closes the file descriptor to the client
+as soon as the ACK is received. From this moment on any request by the
+client, such as for example a 'get', is communicated directly with the
+one child which handles the associated table. Each handler process, both
+child and mother, consist of a single execution thread, blocking only in
+a select() loop on all open file descriptors with periodic time out. When
+select() returns the bitarray is scanned for read, write or exceptions
+on the corresponding file descriptor. Both read and write's are done with
+an iovector where possible; and are non blocking. Specific buffers are
+kept with each connection. This implies that as the server gets busy and
+catches several operation during the outstanding select handle, the actual
+handling becomes proportional longer. Hence scaling is better than n.log
+n. Another important optimization is that both during receiving a request
+and sending back the results the initial non blocking io call uses an iovector
+on the buffers to be submitted or the buffers to returned from the database
+accessors and the protocol engine. Only if this initial non blocking call
+fails is the perl connection buffering used; the remaining bytes from the
+iovec are concatenated into it and the corresponding bit in the file descriptor
+mask is set. This is made possible by because for each table we are guaranteed
+a single execution environment; and thus the mmaped segments are available
+during the io operation. The above optimization alone makes almost any
+multi-threading or mutex approach slower in terms of operations required.
+Especially when most of the hash tables are fully mapped in, or when a
+raid array is used with good caching. The ParlEuNet system uses both. But
+even tests on a more modest system, with system intensive IDE disks showed
+that the above simplification is effective with a BSD kernel under almost
+all circumstances but a small single table.</font></font>
+<p><a NAME="_Toc463634474"></a><b><font face="Garamond"><font >Problems</font></font></b>
+<p><font face="Garamond"><font >As discussed a the large number
+of front end processes, each requiring several long term connections to
+the DBMS, are essential in when dealing with the stagger problem. This
+implies that on the backend server several thousands of DBMS connections
+to just a few unix processes are open at any given time. These connections
+are long lived. This use of resources is clearly outside the range to which
+a general purpose unix machine is tuned. As the DBMS machine has been designed
+for a specific purpose general purpose assumptions no longer hold: the
+machine has got a lot of memory, no normal 'users' and is geared towards
+a single tasks. For this reason it is advantageous to increase both the
+MAXUSER and FD_SETSIZE values. The first ensures that more buffer space
+is allocated, the latter allows form more file descriptors. It would have
+been better to tune the TCP/IP related buffers directly, as MAXUSER also
+increases some buffers not heavily used but so far that has not been needed.
+Secondly the requests are of a specific nature; the client sends a request
+packet, to which the server answers quickly. This is followed by a relatively
+long period no communication. Setting the MBUF size as the advantage that
+replies are immediately sent out with less fragmentation. Finally in the
+'select()'[/sys/kern/sys_generic.c] function of BSD there is a static file
+descriptor mask buffer of 2k which is used whenever possible to avoid a
+more expensive kernel malloc(). This value can be increased. With the approach
+taken, and assuming that context switches are cheap, there is a fundamental
+flaw in that during the database operation, and specifically during disk
+io, no further concurrent actions take place within the processing thread
+for a given table. This does not cause any serious penalties because during
+such a wait the kernel will correctly buffer incoming request, deal and
+purge outgoing data. But still for some of the smaller, most frequently
+accessed, tables whose hash indexes fit in main memory, sub optimal performance
+is observed; for example the ATPS for four tables of identical size is
+19000, for just one table 110000 and for four tables of relative sizes
+1, 2 4 and 8 (but still with the same hash table size and density as the
+previous two) 16000. This suggest that close to an order of two performance
+increase might be realized by either changing the kernel semantics to control
+the context switch or by giving each table space two threads with a single
+mutex. The latter was tried; but did not show any serious performance;
+in part because both on FreeBSD 3.2 and on Linux 2.2.3 threads are not
+yet that efficient. Cursory tests on Solaris 2.6 suggest that this is better.</font></font>
+<p><a NAME="_Toc463634475"></a><b><font face="Garamond"><font >Results</font></font></b>
+<p><font face="Garamond"><font >The final prototype has been in
+production use for over 6 months without significant problems; and routinely
+deals with over 120 requests/second with no noticeable load. This is one
+order of magnitude below cruse performance and two orders of magnitude
+between peek performance. On a general Internet scale of growth this implies
+that the server, hardware an software technology, will suffice for two
+to three years. This is on par or better than currently provided by commercial
+off the shelf products.</font></font>
+
+<P>
+<BR>
+<CITE>Written by <a href="mailto:dirkx@webweaving.org">Dirk-Willem van Gulik</a>  Aug 1999</CITE>
+
+<!--Conclusion:
+
+
+ improving round robin assign
+ allowing dynamic reallocate from children
+ report on experiemnts wth fuully threaded solution
+
+-->
+</body>
+</html>
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/include/dbms.h
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/include/dbms.h?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/include/dbms.h (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/include/dbms.h Fri Apr 13 01:56:01 2007
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+ * Dirk-Willem van Gulik <di...@webweaving.org>
+ *
+ * NOTICE
+ *
+ * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+ * file you should have received together with this source code. If you did not get a
+ * a copy of such a license agreement you can pick up one at:
+ *
+ * http://rdfstore.sourceforge.net/LICENSE
+ *
+ */
+
+/*
+ *
+ * WARNING! if you change this file you have also to manually update ../../lib/DBMS.pm to be in-sync with these definitions
+ *
+ */
+
+#ifndef _H_DBMS_
+#define _H_DBMS_
+
+#include "dbms_compat.h"
+
+typedef uint32_t dbms_counter;
+
+typedef enum {
+ DBMS_EVENT_RECONNECT,
+ DBMS_EVENT_WAITING
+} dbms_cause_t;
+
+typedef enum {
+ DBMS_XSMODE_DEFAULT = 0,
+ DBMS_XSMODE_RDONLY,
+ DBMS_XSMODE_RDWR,
+ DBMS_XSMODE_CREAT,
+ DBMS_XSMODE_DROP
+} dbms_xsmode_t;
+
+typedef int dbms_error_t;
+
+typedef struct {
+ char * name;
+ char * host;
+ unsigned long port;
+ int mode;
+ int sockfd;
+ unsigned long addr;
+ int bt_compare_fcn_type;
+
+ void * (* malloc)(size_t s);
+ void (* free)(void * adr);
+ void (* callback)(dbms_cause_t cause, int cnt);
+ void (* error)(char * err, int erx);
+
+ char err[ 256 ];
+ } dbms;
+
+extern char *
+dbms_get_error(
+ dbms * me
+);
+
+extern dbms *
+dbms_connect(
+ char *name,
+ char * host, int port,
+ dbms_xsmode_t mode,
+ void *(*_my_malloc)( size_t size),
+ void(*_my_free)(void *),
+ void(*_my_report)(dbms_cause_t cause, int count),
+ void(*_my_error)(char * err, int erx),
+ int bt_compare_fcn_type
+);
+
+extern dbms_error_t
+dbms_disconnect(
+ dbms * me
+);
+
+extern dbms_error_t
+dbms_comms (
+ dbms * me,
+ int token,
+ int * retval,
+ DBT * v1,
+ DBT * v2,
+ DBT * r1,
+ DBT * r2
+ );
+
+/* dbms_error_t values; beside normal
+ * errno values from libc et. al.
+ */
+#define E_UNDEF 1000
+#define E_NONNUL 1001
+#define E_FULLREAD 1002
+#define E_FULLWRITE 1003
+#define E_CLOSE 1004
+#define E_HOSTNAME 1005
+#define E_VERSION 1006
+#define E_PROTO 1007
+#define E_ERROR 1008
+#define E_NOMEM 1009
+#define E_RETRY 1010
+#define E_NOPE 1011
+#define E_XXX 1012
+#define E_TOOBIG 1013
+#define E_BUG 1014
+
+#endif
Propchange: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/include/dbms.h
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/include/dbms_comms.h
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/include/dbms_comms.h?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/include/dbms_comms.h (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/include/dbms_comms.h Fri Apr 13 01:56:01 2007
@@ -0,0 +1,110 @@
+/*
+ * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+ * Dirk-Willem van Gulik <di...@webweaving.org>
+ *
+ * NOTICE
+ *
+ * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+ * file you should have received together with this source code. If you did not get a
+ * a copy of such a license agreement you can pick up one at:
+ *
+ * http://rdfstore.sourceforge.net/LICENSE
+ *
+ */
+
+#ifndef _H_DBMS_COMMS
+#define _H_DBMS_COMMS
+
+#include "dbms_compat.h"
+
+/* Define standard min/max macro's if they
+ * are not defined.
+ */
+#ifndef MIN
+#define MIN(x,y) (((x) < (y)) ? (x) : (y))
+#endif
+
+#ifndef MAX
+#define MAX(x,y) (((x) > (y)) ? (x) : (y))
+#endif
+
+#ifdef RDFSTORE_DBMS_DEBUG_TIME
+#define P0 0
+#else
+#define P0 1
+#endif
+
+#define DBMS_HOST "127.0.0.1"
+#define DBMS_PORT 1234
+#define DBMS_MODE (DBMS_XSMODE_RDWR)
+
+#define MASK_SOURCE (128+64)
+#define F_CLIENT_SIDE 128
+#define F_SERVER_SIDE 64
+#define F_INTERNAL (128+64)
+
+#define MASK_STATUS 32
+#define F_FOUND 32
+#define F_NOTFOUND 0
+
+#define MASK_TOKEN 31
+#define TOKEN_ERROR 0
+#define TOKEN_FETCH 1
+#define TOKEN_STORE 2
+#define TOKEN_DELETE 3
+#define TOKEN_NEXTKEY 4
+#define TOKEN_FIRSTKEY 5
+#define TOKEN_EXISTS 6
+#define TOKEN_SYNC 7
+#define TOKEN_INIT 8
+#define TOKEN_CLOSE 9
+#define TOKEN_CLEAR 10
+#define TOKEN_FDPASS 11 /* only used for internal passing FD*/
+#define TOKEN_PING 12 /* only used between servers ?? */
+#define TOKEN_INC 13 /* atomic increment */
+#define TOKEN_LIST 14 /* list all keys */
+#define TOKEN_DEC 15 /* atomic decrement */
+#define TOKEN_PACKINC 16 /* atomic packed increment */
+#define TOKEN_PACKDEC 17 /* atomic packed decrement */
+#define TOKEN_DROP 18 /* Drop database */
+#define TOKEN_FROM 19 /* Get first 'from' this point for a btree */
+
+#define TOKEN_MAX 20 /* last token.. */
+
+struct header {
+ unsigned char token;
+ unsigned long len1;
+ unsigned long len2;
+#ifdef RDFSTORE_DBMS_DEBUG_TIME
+ struct timeval stamp;
+#endif
+ };
+
+#define MAX_STATIC_NAME 256
+#define MAX_STATIC_PFILE MAXPATHLEN
+
+#ifndef MAX_PAYLOAD
+
+/* increased (original value was 32) for RDFStore by AR and DW 2002/09/11 after too many Reconnect and dbms: 676:Cld **ERROR RQ string(s) to big
+ anyway this should be somehow related to MAXRECORDS in compress.h */
+#define MAX_PAYLOAD (128*1024)
+
+#endif
+
+#ifdef STATIC_CS_BUFF
+#define MAX_CS_PAYLOAD MAX_PAYLOAD
+#define P2 1
+#else
+#define P2 0
+#endif
+
+#ifdef STATIC_SC_BUFF
+#define MAX_SC_PAYLOAD MAX_PAYLOAD
+#define P1 1
+#else
+#define P1 0
+#endif
+
+#define DBMS_PROTO (110+P0*1+P1*2+P2*4)
+
+#endif
Propchange: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/include/dbms_comms.h
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/include/dbms_compat.h
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/include/dbms_compat.h?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/include/dbms_compat.h (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/include/dbms_compat.h Fri Apr 13 01:56:01 2007
@@ -0,0 +1,155 @@
+/*
+ * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+ * Dirk-Willem van Gulik <di...@webweaving.org>
+ *
+ * NOTICE
+ *
+ * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+ * file you should have received together with this source code. If you did not get a
+ * a copy of such a license agreement you can pick up one at:
+ *
+ * http://rdfstore.sourceforge.net/LICENSE
+ *
+ */
+
+#ifndef _DBMS_COMPAT_H
+
+#if !defined(WIN32)
+#include <sys/param.h>
+#endif
+
+/* to make it compiling on Solaris basically */
+#ifndef INADDR_NONE
+#define INADDR_NONE 0xffffffff
+#endif /* INADDR_NONE */
+
+#include <limits.h>
+#include <sys/types.h>
+#include <sys/uio.h>
+#define _XPG4_2
+#include <sys/socket.h>
+#undef _XPG4_2
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#ifdef RDFSTORE_PLATFORM_DARWIN
+#include <stdint.h>
+#endif
+#include <stdarg.h>
+#include <errno.h>
+#include <ctype.h>
+#include <time.h>
+
+#include <assert.h>
+#include <signal.h>
+#include <string.h>
+/* SOLARIS */
+#include <strings.h>
+
+#include <pwd.h>
+
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+#include <sys/resource.h>
+
+#include <netdb.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <arpa/inet.h>
+
+#include <syslog.h>
+
+
+#if 0
+/* the following is needed to get rid of 'dereferencing pointer to incomplete type' error on line 144
+ of this file on SuSe Linux - probably to be fixed in the dependences */
+struct hostent {
+ char *h_name; /* official name of host */
+ char **h_aliases; /* alias list */
+ int h_addrtype; /* host address type */
+ int h_length; /* length of address */
+ char **h_addr_list; /* list of addresses from name server */
+ };
+#define h_addr h_addr_list[0] /* address, for backward compatibility */
+struct hostent * gethostbyname(const char *name);
+#endif
+
+/* not using the following but euristics into main Makefile.PL to guess out paths for db.h */
+#if 0
+#ifdef DB1_INCLUDE
+# include <db1/db.h>
+#else
+#ifdef DB2_INCLUDE
+# include <db2/db.h>
+#else
+#ifdef DB3_INCLUDE
+# include <db3/db.h>
+#else
+#ifdef DB4_INCLUDE
+# include <db4/db.h>
+#else
+#ifdef COMPAT185
+# include <db_185.h>
+#else
+# include "db.h"
+#endif
+#endif
+#endif
+#endif
+#endif
+#endif
+/* end not used */
+
+#ifdef COMPAT185
+# include "db_185.h"
+#else
+# include "db.h"
+#endif
+
+#ifdef DB_VERSION_MAJOR
+# if DB_VERSION_MAJOR == 2
+# define BERKELEY_DB_1_OR_2
+# endif
+# if DB_VERSION_MAJOR > 3 || (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR >= 2)
+# define AT_LEAST_DB_3_2
+# endif
+# define R_FIRST DB_FIRST
+# define R_NEXT DB_NEXT
+# define R_CURSOR DB_SET_RANGE
+#else /* db version 1.x */
+# define BERKELEY_DB_1
+# define BERKELEY_DB_1_OR_2
+#endif
+
+#if defined(BSD)
+#define _HAS_TIME_T
+#define _HAS_SENSIBLE_SPRINTF
+#endif
+
+#if defined(_HAS_TIMESPEC)
+#define TIMESPEC struct timespec
+#endif
+
+#if defined(_HAS_TIMESTRUC_T)
+#define TIMESPEC timestruc_t
+#endif
+
+#if defined(_HAS_TIME_T)
+#define TIMESPEC time_t
+#endif
+
+#if defined(_HAS_SENSIBLE_SPRINTF)
+#define STRLEN(x) (x)
+#endif
+
+#if defined(_HAS_SILLY_SPRINTF)
+#define STRLEN(x) strlen(x)
+#endif
+
+
+#define _DBMS_COMPAT_H 1
+#endif
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/.cvsignore
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/.cvsignore?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/.cvsignore (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/.cvsignore Fri Apr 13 01:56:01 2007
@@ -0,0 +1,4 @@
+libdbms.dylib
+gmon.out
+libdbms.3.gz
+dump
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/Makefile
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/Makefile?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/Makefile (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/Makefile Fri Apr 13 01:56:01 2007
@@ -0,0 +1,71 @@
+# *
+# * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+# * Dirk-Willem van Gulik <di...@webweaving.org>
+# *
+# * NOTICE
+# *
+# * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+# * file you should have received together with this source code. If you did not get a
+# * a copy of such a license agreement you can pick up one at:
+# *
+# * http://rdfstore.sourceforge.net/LICENSE
+# *
+
+# external dbmsd to use for the make test functionality.
+#
+TMPDIR=/tmp
+DBMSD=../deamon/dbmsd
+PIDFILE=${TMPDIR}/dbmsd.test.pid
+
+include ../arch.conf
+SRCS = libdbms.c
+OBJS = ${SRCS:c=o}
+INCL =
+
+INSTALL = install -c
+
+all: dump libdbms.a $(INCL) ../include/dbms.h Makefile
+
+dump: dump.o libdbms.a $(INCL) ../include/dbms.h Makefile
+ $(CC) $(CFLAGS) $(INCLUDES) $(DEFINES) $(LIBS_DIR) $(LIBS) -o dump dump.o libdbms.a
+
+
+test: test.o all ../deamon/dbmsd dbms-test.conf
+ $(CC) $(CFLAGS) $(INCLUDES) $(DEFINES) $(LIBS_DIR) $(LIBS) -o test test.o libdbms.a
+ ${DBMSD} -U -d ${TMPDIR} -c ./dbms-test.conf -P ${PIDFILE}
+ - rm ${TMPDIR}/te/test.db
+ -./test
+ kill `cat ${PIDFILE}`
+ rm ${PIDFILE}
+
+${OBJS}: Makefile
+
+.c.o: Makefile
+ $(CC) $(CFLAGS) $(INCLUDES) $(DEFINES) $(LIBS_DIR) -c -o $@ $<
+
+libdbms.a: ${OBJS}
+ $(AR) cr $@ ${OBJS}
+ $(RANLIB) $@
+
+clean:
+ rm -f *.o *.gmon bb.* core.* *.core *.db *.a *.dylib
+
+man: libdbms.3
+ cat libdbms.3 | gzip > libdbms.3.gz
+
+install: man libdbms.a
+ -mkdir -p $(INSTDIR)/lib $(INSTDIR)/man/man3 $(INSTDIR)/include
+ $(INSTALL) -m 0555 -o $(UID) -g $(GID) libdbms.a $(INSTDIR)/lib
+ $(INSTALL) -m 0644 -o $(UID) -g $(GID) libdbms.3.gz $(INSTDIR)/man/man3
+ $(INSTALL) -m 0444 -o $(UID) -g $(GID) ../include/dbms.h $(INSTDIR)/include
+ $(INSTALL) -m 0444 -o $(UID) -g $(GID) ../include/dbms_comms.h $(INSTDIR)/include
+ $(INSTALL) -m 0444 -o $(UID) -g $(GID) ../include/dbms_compat.h $(INSTDIR)/include
+
+depend:
+ makedepend $(INCLUDES) $(DEFINES) $(SRCS)
+
+distclean: clean
+ rm -f *%
+
+lint:
+ $(LINT) $(LINTFLAGS) $(INCLUDES) $(DEFINES) $(LIBS_DIR) ${SRCS}
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/dump
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/dump?view=auto&rev=528394
==============================================================================
Binary file - no diff available.
Propchange: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/dump
------------------------------------------------------------------------------
svn:executable = *
Propchange: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/dump
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/dump.c
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/dump.c?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/dump.c (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/dump.c Fri Apr 13 01:56:01 2007
@@ -0,0 +1,141 @@
+/*
+ * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+ * Dirk-Willem van Gulik <di...@webweaving.org>
+ *
+ * NOTICE
+ *
+ * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+ * file you should have received together with this source code. If you did not get a
+ * a copy of such a license agreement you can pick up one at:
+ *
+ * http://rdfstore.sourceforge.net/LICENSE
+ *
+ */
+
+#include "dbms.h"
+#include "dbms_comms.h"
+
+static char hex[] = "0123456789abcdef";
+
+static const char * cls(DBT * p) {
+ char * q = (char *) p->data;
+ static char buff[ 1024 ];
+ char * r = buff;
+ int i;
+ for(i=0; i<sizeof(buff)-4 && i < p->size;i++, q++) {
+ if ((*q == '\\'))
+ *r++ = *q;
+
+ if (isprint((int)*q) && (*q != '\n') && (*q != '\r'))
+ *r++ = *q;
+ else {
+ *r++ = '\\';
+ *r++ = hex[ (*q >> 4) & 0x0F ];
+ *r++ = hex[ *q & 0x0F ];
+ }
+ }
+
+ * r = '\0';
+ return buff;
+}
+
+static const char * xls(DBT * p) {
+ char * q = (char *) p->data;
+ static char buff[ 1024 ];
+ char * r = buff;
+ int i;
+ for(i=0; i<sizeof(buff)-6 && i < p->size;i++, q++) {
+
+ if (isprint((int)*q) && (*q != '<') && (*q != '>')) {
+ *r++ = *q;
+ }
+ else if ((*q == '<') || (*q == '>')) {
+ *r++ = '&';
+ *r++ = (*q == '<') ? 'l' : 'g';
+ *r++ = 't';
+ *r++ = ';';
+ } else {
+ *r++ = '&';
+ *r++ = '#';
+ *r++ = hex[ *q / 100 ];
+ *r++ = hex[ (*q / 10 ) % 10 ];
+ *r++ = hex[ *q % 10 ];
+ *r++ = ';';
+ }
+ }
+
+ * r = '\0';
+ return buff;
+}
+
+int main(int argc, char * * argv)
+{
+ dbms_error_t e;
+ DBT key,val;
+ int t,i, r;
+ dbms * d;
+ int xml = ((argc == 3) && (strcmp(argv[1],"-x") == 0)) ? 1 : 0;
+
+ if ((argc < 2 || argc > 3) || ((argc == 3) && (!xml))) {
+ fprintf(stderr,"Syntax: %s [-x] <dbname>\n",
+ argv[0]);
+ exit(1);
+ };
+
+ if (!(d = dbms_connect(argv[argc-1], NULL, 0,
+ DBMS_XSMODE_RDONLY, NULL, NULL, NULL, NULL, 0)))
+ {
+ fprintf(stderr,"Failed to connect\n");
+ exit(1);
+ };
+
+ val.data = NULL;
+ val.size = 0;
+ key.data = NULL;
+ key.size = 0;
+
+ if (xml) printf(
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<!DOCTYPE plist PUBLIC \"-//@Semantics S.R.L.//DTD PLIST 1.0//EN\" "
+ "\"http://www.asemanitcs.com/dtds/simple.dtd\">\n"
+ "<dbase id=\"%s\">\n",argv[argc-1]);
+
+
+ for(t = TOKEN_FIRSTKEY,i = 0, r = 0; r==0; t = TOKEN_NEXTKEY) {
+ DBT nkey = { NULL, 0 };
+ DBT nval = { NULL, 0 };
+
+ e = dbms_comms(d, t, &r, &key, &val, &nkey, &nval);
+ if (e) {
+ fprintf(stderr,"Op NEXT failed %s\n",
+ dbms_get_error(d));
+ exit(1);
+ };
+
+ if (xml) {
+ printf(" <item id=\"%06d\">\n",++i);
+ printf(" <key>%s</key>\n",xls(&key));
+ printf(" <val>%s</val>\n",xls(&val));
+ printf(" </item>\n");
+ } else {
+ printf("Key/Value pair # %06d\n",++i);
+ printf("%s\n", cls(&key));
+ printf("%s\n\n",cls(&val));
+ }
+
+ if (key.size && key.data) free(key.data);
+ if (val.size && val.data) free(val.data);
+ val = nval;
+ key = nkey;
+ };
+ if (xml)
+ printf("</dbase>\n");
+
+ e = dbms_comms(d, TOKEN_CLOSE, NULL, NULL, NULL, NULL, NULL);
+ if (e) {
+ fprintf(stderr,"close failed %s\n", dbms_get_error(d));
+ exit(1);
+ };
+
+ exit(0);
+}
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/libdbms.3
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/libdbms.3?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/libdbms.3 (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/libdbms.3 Fri Apr 13 01:56:01 2007
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+ * Dirk-Willem van Gulik <di...@webweaving.org>
+ *
+ * NOTICE
+ *
+ * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+ * file you should have received together with this source code. If you did not get a
+ * a copy of such a license agreement you can pick up one at:
+ *
+ * http://rdfstore.sourceforge.net/LICENSE
+ *
+ */
+
+Call back hanlder
+ (void)hanlder( dbms_cause_t event, int counter)
+
+ dbms_cause_t:
+ DBMS_EVENT_RECONNECT Lost connection, is reconnecting
+ DBMS_EVENT_WAITING Is connecting but long wait
+
+
+ Return values possible
+
+ dbms * General struct. Do not touch.
+ dbms_error_t Error value; either errno or one of the E_ values see below.
+
+extern char *
+get_dbms_error(
+ dbms * me
+);
+
+extern dbms *
+dbms_connect(
+ char *name, char * host, int port, int mode,
+ void *(*_my_malloc)( size_t size),
+ void(*_my_free)(void *),
+ void(*_my_report)(dbms_cause_t cause, int count),
+ int bt_compare_fcn_type
+);
+
+extern dbms_error_t
+dbms_comms (
+ dbms * me,
+ int token,
+ int * retval,
+ DBT * v1,
+ DBT * v2,
+ DBT * r1,
+ DBT * r2
+ );
+
+dbms_error_t values; beside normal
+errno values from libc et. al.
+
+E_UNDEF
+E_NONNUL
+E_FULLREAD
+E_FULLWRITE
+E_CLOSE
+E_HOSTNAME
+E_VERSION
+E_PROTO
+E_ERROR
+E_NOMEM
+E_RETRY
+E_NOPE
+E_XXX
+E_TOOBIG
+E_BUG
+
+Trick
+
+ if 'DBMS_LOG' is set
+ transactions are logged to that file.
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/libdbms.3.gz
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/libdbms.3.gz?view=auto&rev=528394
==============================================================================
Binary file - no diff available.
Propchange: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/libdbms.3.gz
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/libdbms.a
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/libdbms.a?view=auto&rev=528394
==============================================================================
Binary file - no diff available.
Propchange: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/libdbms.a
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/libdbms.c
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/libdbms.c?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/libdbms.c (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/libdbms.c Fri Apr 13 01:56:01 2007
@@ -0,0 +1,893 @@
+/*
+ * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+ * Dirk-Willem van Gulik <di...@webweaving.org>
+ *
+ * NOTICE
+ *
+ * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+ * file you should have received together with this source code. If you did not get a
+ * a copy of such a license agreement you can pick up one at:
+ *
+ * http://rdfstore.sourceforge.net/LICENSE
+ *
+ * Perl 'tie' interface to a socket connection. Possibly to
+ * the a server which runs a thin feneer to the Berkely DB.
+ *
+ */
+
+#include "dbms.h"
+#include "dbms_comms.h"
+
+#include <stdio.h>
+
+typedef dbms *DBMS;
+
+static char _erm[256] = "\0";
+
+static char *dbms_error[] = {
+ /* E_UNDEF 1000 */
+ "Not defined",
+ /* E_NONNUL 1001 */
+ "Undefined Error",
+ /* E_FULLREAD 1002 */
+ "Could not receive all bytes from DBMS server",
+ /* E_FULLWRITE 1003 */
+ "Could not send all bytes to DBMS server",
+ /* E_CLOSE 1004 */
+ "DBMS server closed the connection",
+ /* E_HOSTNAME 1005 */
+ "Could not find/resolve DBMS servers hostname",
+ /* E_VERSION 1006 */
+ "DBMS Version not supported",
+ /* E_PROTO 1007 */
+ "DBMS Reply not understood",
+ /* E_ERROR 1008 */
+ "DBMS Server side error"
+ /* E_NOMEM 1009 */
+ "Out of memory",
+ /* E_RETRY 1010 */
+ "Failed after several retries",
+ /* E_NOPE 1011 */
+ "No such database",
+ /* E_XXX 1012 */
+ "No such database",
+ /* E_TOOBIG 1013 */
+ "Packed bigger than static",
+ /* E_BUG 1014 */
+ "Conceptual error"
+};
+
+static dbms_error_t reconnect(dbms * me);
+static dbms_error_t reselect(dbms * me);
+static dbms_error_t getpack(dbms * me, unsigned long len, DBT * r);
+static dbms_error_t i_comms(dbms * me, int token, int *retval, DBT * v1, DBT * v2, DBT * r1, DBT * r2);
+
+static void
+mark_dbms_error(dbms * me, char *msg, dbms_error_t erx)
+{
+ bzero(me->err, sizeof(me->err));
+ if (erx == E_ERROR) {
+ snprintf(me->err, sizeof(me->err),
+ "DBMS Error %s: %s", msg,
+ errno == 0 ? "" : (strlen(strerror(errno)) <= sizeof(me->err)) ? strerror(errno) : "");
+ } else if ((erx > E_UNDEF) && (erx <= E_BUG)) {
+ strncat(me->err, msg, sizeof(me->err) - 1);
+ strncat(me->err, ": ", sizeof(me->err) - 1);
+ strncat(me->err, dbms_error[erx - E_UNDEF], sizeof(me->err) - 1);
+ } else {
+ strncat(me->err, msg, sizeof(me->err) - 1);
+ strncat(me->err, ": ", sizeof(me->err) - 1);
+ if (strlen(strerror(erx)) <= sizeof(me->err) - strlen(me->err) - 1)
+ strncat(me->err, strerror(erx), sizeof(me->err) - 1);
+ };
+
+ if (strlen(me->err) <= sizeof(_erm))
+ strcpy(_erm, me->err);
+};
+
+static void
+set_dbms_error(dbms * me, char *msg, dbms_error_t erx)
+{
+ mark_dbms_error(me, msg, erx);
+ if (me->error)
+ (*(me->error)) (me->err, erx);
+};
+
+extern char *
+dbms_get_error(dbms * me)
+{
+ if (me == NULL)
+ return _erm;
+ else
+ return me->err;
+}
+
+static void
+_warning(dbms_cause_t event, int count)
+{
+ /*
+ * Note: _erm use NOT thread safe. Should change function signature
+ * to pass dbms * pointer.
+ */
+ if (event == DBMS_EVENT_RECONNECT)
+ fprintf(stderr, "DBMS Reconnecting %i (%s)...\n", count, _erm);
+ else if (event == DBMS_EVENT_WAITING)
+ fprintf(stderr, "DBMS Waiting %i...\n", count);
+ else
+ fprintf(stderr, "DBMS Unknown event (%s)\n", _erm);
+}
+
+static int cnt = 0;
+static FILE *logfile = NULL;
+
+static void
+_tlog(char *fmt,...)
+{
+ if (!logfile)
+ return;
+ {
+ char tmp[1024];
+ char buf[ 128 * 1024 ];
+ va_list ap;
+ time_t tt;
+ time(&tt);
+ snprintf(tmp, sizeof(tmp), "%04d:%20s %s", cnt, asctime(gmtime(&tt)), fmt);
+
+ va_start(ap, fmt);
+ vsnprintf(buf,sizeof(buf)-1, tmp, ap);
+ va_end(ap);
+
+ fprintf(logfile,"%s\n",buf);
+ fflush(logfile);
+ }
+}
+
+static char * _token2name(int x) {
+ x = x & MASK_TOKEN;
+#define CC(x) case x: return #x; break;
+ switch (x) {
+CC(TOKEN_ERROR);
+CC(TOKEN_FETCH );
+CC(TOKEN_STORE );
+CC(TOKEN_DELETE );
+CC(TOKEN_NEXTKEY );
+CC(TOKEN_FIRSTKEY);
+CC(TOKEN_FROM );
+CC(TOKEN_EXISTS );
+CC(TOKEN_SYNC );
+CC(TOKEN_INIT );
+CC(TOKEN_CLOSE );
+CC(TOKEN_CLEAR );
+CC(TOKEN_FDPASS );
+CC(TOKEN_PING );
+CC(TOKEN_INC );
+CC(TOKEN_LIST );
+CC(TOKEN_DEC );
+CC(TOKEN_PACKINC );
+CC(TOKEN_PACKDEC );
+CC(TOKEN_DROP );
+ default: return "TOKEN_UNKNOWN";
+ };
+ return "XXX";
+}
+
+static char *
+_hex(dbms * me, int len, void *str)
+{
+ size_t i;
+ char *r = NULL;
+
+ if (len == 0) {
+ r = (char *) (*me->malloc)( strlen("[0]\"\"")+1 );
+ strcpy( r, "[0]\"\"" );
+ return r;
+ };
+
+ if (str == NULL) {
+ r = (char *) (*me->malloc)( strlen("<null>")+1 );
+ strcpy( r, "<null>" );
+ return r;
+ };
+
+ if (len > 50000) {
+ r = (char *) (*me->malloc)( strlen("<toolong>")+1 );
+ strcpy( r, "<toolong>" );
+ return r;
+ };
+
+ r = (char *) (*me->malloc)(3*len + 100);
+ if (r == NULL) {
+ r = (char *) (*me->malloc)( strlen("<outofmem>")+1 );
+ strcpy( r, "<outofmem>" );
+ return r;
+ };
+
+ sprintf(r, "[%06d]\"", len);
+
+ for (i = 0; i < len; i++) {
+ char p[3];
+ unsigned int c = ((unsigned char *) str)[i];
+
+ if (c && isprint(c) && (c != '%')) {
+ p[0] = c; p[1] = '\0';
+ } else {
+ sprintf(p,"%%%02x", c);
+ };
+ strcat(r,p);
+ };
+ strcat(r,"\"");
+ return r;
+}
+
+extern dbms *
+dbms_connect(
+ char *name, char *host, int port,
+ dbms_xsmode_t mode,
+ void *(*_my_malloc) (size_t s),
+ void (*_my_free) (void *adr),
+ void (*_my_report) (dbms_cause_t event, int count),
+ void (*_my_error) (char *err, int erx),
+ int bt_compare_fcn_type
+)
+{
+ dbms *me;
+ int err = 0;
+
+ if ((name == NULL) || (*name == '\0'))
+ return NULL;
+
+ if ((host == NULL) || (*host == '\0'))
+ host = DBMS_HOST;
+
+ if (port == 0)
+ port = DBMS_PORT;
+
+
+ if (_my_malloc == NULL)
+ _my_malloc = &malloc;
+
+ if (_my_free == NULL)
+ _my_free = &free;
+
+ if (_my_report == NULL)
+ _my_report = &_warning;
+
+ me = (dbms *) (*_my_malloc) (sizeof(dbms));
+ if (me == NULL)
+ return NULL; /* rely on errno */
+
+ me->bt_compare_fcn_type = bt_compare_fcn_type;
+
+ me->malloc = _my_malloc;
+ me->free = _my_free;
+ me->callback = _my_report;
+ me->error = _my_error;
+ bzero(me->err, sizeof(me->err));
+
+ switch (mode) {
+ case DBMS_XSMODE_DEFAULT:
+ mode = DBMS_MODE; /* default */
+ break;
+ ;;
+ case DBMS_XSMODE_RDONLY:
+ break;
+ ;;
+ case DBMS_XSMODE_RDWR:
+ break;
+ ;;
+ case DBMS_XSMODE_CREAT:
+ break;
+ ;;
+ case DBMS_XSMODE_DROP:
+ break;
+ ;;
+ default:
+ {
+ char _buff[1024];
+ snprintf(_buff, sizeof(_buff), "Unknown DBMS Access type (%d)", (int) mode);
+ set_dbms_error(me, _buff, 0);
+ }
+ (*(me->free)) (me);
+ return NULL;
+ break;
+ }
+
+ me->sockfd = -1;
+ me->mode = (u_long) mode;
+ me->port = port;
+ me->name = (char *) (*me->malloc)( strlen(name)+1 );
+ if( me->name == NULL ) {
+ (*(me->free)) (me);
+ return NULL;
+ };
+ strcpy( me->name, name );
+ me->host = (char *) (*me->malloc)( strlen(host)+1 );
+ if( me->host == NULL ) {
+ (*(me->free)) (me->name);
+ (*(me->free)) (me);
+ return NULL;
+ };
+ strcpy( me->host, host );
+
+ /*
+ * quick and dirty hack to check for IP vs FQHN and fall through when
+ * in doubt to resolving.
+ */
+ me->addr = INADDR_NONE;
+ {
+ int i = 0;
+ for (; me->host[i] != '\0'; i++)
+ if (!isdigit((int) (me->host[i])) && me->host[i] != '.')
+ break;
+
+ if (me->host[i] == '\0')
+ me->addr = inet_addr(host);
+ }
+
+ if (me->addr == INADDR_NONE) {
+ struct hostent *hp;
+ if ((hp = gethostbyname(me->host)) == NULL) {
+ set_dbms_error(me, "Hostname lookup failed", errno);
+ (*(me->free)) (me->name);
+ (*(me->free)) (me->host);
+ (*(me->free)) (me);
+ return NULL;
+ };
+ /*
+ * copy the address, rather than the pointer as we need it
+ * later. It is an unsigned long.
+ */
+ me->addr = *(u_long *) hp->h_addr;
+ };
+
+ if ((err = reconnect(me))) {
+ set_dbms_error(me, "Connection failed", err);
+ (*(me->free)) (me->name);
+ (*(me->free)) (me->host);
+ (*(me->free)) (me);
+ return NULL;
+ };
+
+ if ((err = reselect(me))) {
+ set_dbms_error(me, "Selection failed", err);
+ (*(me->free)) (me->name);
+ (*(me->free)) (me->host);
+ (*(me->free)) (me);
+ return NULL;
+ };
+
+ {
+ char * file = getenv("DBMS_LOG");
+ cnt++;
+ if (file && (logfile == NULL)) {
+ if ((logfile = fopen(file, "a"))) {
+ fprintf(stderr, "Logging to %s\n", file);
+ } else {
+ fprintf(stderr,"Failure to log to %s: %s\n",file,strerror(errno));
+ };
+ }
+ if (logfile)
+ _tlog("start %d %s",cnt,name);
+ }
+ return me;
+}
+
+
+static dbms_error_t
+reconnect(
+ dbms * me
+)
+{
+ struct sockaddr_in server;
+ int one = 1;
+ int csnd_len, csnd, sndbuf = 16 * 1024;
+ int e = 0;
+
+ /*
+ * we could moan if me->sockfd is still set.. or do a silent close,
+ * just in case ?
+ */
+ if (me->sockfd >= 0) {
+ shutdown(me->sockfd, 2);
+ close(me->sockfd);
+ }
+ if ((me->sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
+ set_dbms_error(me, "socket", errno);
+ return E_ERROR;
+ }
+ if (0) {
+ /*
+ * allow for re-use; to avoid that we have to wait for a fair
+ * amounth of time after disasterous crashes,
+ */
+ if ((setsockopt(me->sockfd, SOL_SOCKET, SO_REUSEADDR,
+ (const char *) &one, sizeof(one))) < 0) {
+ set_dbms_error(me, "setsockopt(reuse)", errno);
+ me->sockfd = -1;
+ close(me->sockfd);
+ return E_ERROR;
+ };
+ }
+ csnd_len = sizeof(csnd);
+ if (getsockopt(me->sockfd, SOL_SOCKET, SO_SNDBUF,
+ (void *) &csnd, (void *) &csnd_len) < 0) {
+ set_dbms_error(me, "getsockopt(sndbuff)", errno);
+ me->sockfd = -1;
+ close(me->sockfd);
+ return E_ERROR;
+ };
+ assert(csnd_len == sizeof(csnd));
+
+ /*
+ * only set when smaller
+ */
+ if ((csnd < sndbuf) &&
+ (setsockopt(me->sockfd, SOL_SOCKET, SO_SNDBUF,
+ (const void *) &sndbuf, sizeof(sndbuf)) < 0)) {
+ set_dbms_error(me, "setsockopt(sndbuff)", errno);
+ me->sockfd = -1;
+ close(me->sockfd);
+ return E_ERROR;
+ };
+
+ /*
+ * Discard any data still in our send buffer whe closing and do not
+ * linger around for any thing from the server.
+ */
+ {
+ struct linger l = {1, 0}; /* Linger On, Lingertime 0 */
+ if ((setsockopt(me->sockfd, SOL_SOCKET, SO_LINGER,
+ (const char *) &l, sizeof(l))) < 0) {
+ set_dbms_error(me, "setsockopt(disble-nagle)", errno);
+ me->sockfd = -1;
+ close(me->sockfd);
+ return E_ERROR;
+ };
+ }
+ /*
+ * disable Nagle, for speed
+ */
+ if ((setsockopt(me->sockfd, IPPROTO_TCP, TCP_NODELAY,
+ (const char *) &one, sizeof(one))) < 0) {
+ set_dbms_error(me, "setsockopt(disble-nagle)", errno);
+ me->sockfd = -1;
+ close(me->sockfd);
+ return E_ERROR;
+ };
+
+ /*
+ * larger buffer; as we know that we can initially slide open the
+ * window bigger.
+ */
+
+ while (e++ < 4) {
+ bzero((char *) &server, sizeof(server));
+
+ server.sin_family = AF_INET;
+ server.sin_addr.s_addr = me->addr;
+ server.sin_port = htons(me->port);
+
+ if (connect(me->sockfd, (struct sockaddr *) & server, sizeof(server)) == 0)
+ return 0;
+ if (errno != EADDRINUSE)
+ break;
+
+ usleep(e * e * 100 * 1000); /* wait 0.1, 0.4, 0.9, 2.5
+ * second */
+ }
+ mark_dbms_error(me, "connect()", errno);
+ me->sockfd = -1;
+ return E_ERROR;
+}
+
+dbms_error_t
+dbms_disconnect(
+ dbms * me
+)
+{
+ int retval;
+
+ assert(me);
+ assert(me->sockfd >= 0);
+
+ dbms_comms(me, TOKEN_CLOSE, &retval, NULL, NULL, NULL, NULL);
+#ifdef TPS
+ if (getenv("GATEWAY_INTERFACE") == NULL)
+ fprintf(stderr, "Performance: %ld # %.2f mSec/trans = %.1f\n",
+ ttps, ttime / ttps / 1000.0,
+ 1000000.0 * ttps / ttime
+ );
+#endif
+ shutdown(me->sockfd, 2);
+ close(me->sockfd);
+ (*(me->free)) (me->name);
+ (*(me->free)) (me->host);
+ (*(me->free)) (me);
+ if (logfile) fclose(logfile);
+
+ return 0;
+}
+
+static dbms_error_t
+getpack(
+ dbms * me,
+ unsigned long len,
+ DBT * r
+)
+{
+ unsigned int gotten;
+ char *at;
+
+ r->size = 0;
+ r->data = NULL;
+
+ if (len == 0)
+ return 0;
+
+ if (r == NULL)
+ return E_BUG;
+
+#ifdef STATIC_SC_BUFF
+ if (len > MAX_SC_PAYLOAD)
+ return E_TOOBIG;
+#endif
+ r->size = 0;
+ r->data = (char *) (*me->malloc) (len);
+
+ if (r->data == 0)
+ return E_NOMEM;
+
+ /* should block ? */
+
+ for (at = r->data, gotten = 0; gotten < len;) {
+ ssize_t l;
+ l = recv(me->sockfd, at, len - gotten, 0);
+ if (l < 0) {
+ set_dbms_error(me, "packet-recv()", errno);
+ (*me->free) (r->data);
+ r->data = NULL;
+ return E_ERROR;
+ } else if (l == 0) {
+ (*me->free) (r->data);
+ r->data = NULL;
+ return E_CLOSE;
+ };
+ gotten += l, at += l;
+ };
+
+ r->size = len;
+ return 0;
+};
+
+static
+dbms_error_t
+i_comms(
+ dbms * me,
+ int token,
+ int *retval,
+ DBT * v1,
+ DBT * v2,
+ DBT * r1,
+ DBT * r2
+)
+{
+ int err = 0;
+ DBT rr1, rr2;
+ struct header cmd;
+ struct iovec iov[3];
+ struct msghdr msg;
+ size_t s;
+
+ if (retval)
+ *retval = -1;
+
+ rr1.data = rr2.data = NULL;
+
+ cmd.token = token | F_CLIENT_SIDE;
+
+ cmd.len1 = htonl((v1 == NULL) ? 0 : v1->size);
+ cmd.len2 = htonl((v2 == NULL) ? 0 : v2->size);
+
+ iov[0].iov_base = (char *) &cmd;
+ iov[0].iov_len = sizeof(cmd);
+
+ iov[1].iov_base = (v1 == NULL) ? NULL : v1->data;
+ iov[1].iov_len = (v1 == NULL) ? 0 : v1->size;
+
+ iov[2].iov_base = (v2 == NULL) ? NULL : v2->data;
+ iov[2].iov_len = (v2 == NULL) ? 0 : v2->size;
+
+#ifdef STATIC_CS_BUFF
+ if (iov[0].iov_len + iov[1].iov_len + iov[2].iov_len > MAX_CS_PAYLOAD)
+ return E_TOOBIG;
+#endif
+ msg.msg_name = NULL;
+ msg.msg_namelen = 0;
+ msg.msg_iov = iov;
+ msg.msg_iovlen = 3;
+/* temporal fix to make CYGWN compile the basic thing - need better solution */
+#ifndef RDFSTORE_PLATFORM_CYGWIN
+ msg.msg_control = NULL;
+ msg.msg_controllen = 0;
+ msg.msg_flags = 0;
+#endif
+ s = sendmsg(me->sockfd, &msg, 0);
+
+ if (s == 0) {
+ err = E_CLOSE;
+ goto retry_com;
+ } else if (s < 0) {
+ mark_dbms_error(me, "sendmsg()", errno);
+ err = E_ERROR;
+ goto retry_com;
+ } else if (s != iov[0].iov_len + iov[1].iov_len + iov[2].iov_len) {
+ err = E_FULLWRITE;
+ goto exit_com;
+ };
+
+ s = recv(me->sockfd, &cmd, sizeof(cmd), 0);
+
+ if (s == 0) {
+ err = E_CLOSE;
+ goto retry_com;
+ } else if (s < 0) {
+ mark_dbms_error(me, "header-recv()", errno);
+ err = E_ERROR;
+ goto retry_com;
+ } else if (s != sizeof(cmd)) {
+ err = E_FULLREAD;
+ goto exit_com;
+ };
+
+ cmd.len1 = ntohl(cmd.len1);
+ cmd.len2 = ntohl(cmd.len2);
+
+ rr2.data = rr1.data = NULL;
+ if ((err = getpack(me, cmd.len1, r1 ? r1 : &rr1)) != 0)
+ goto retry_com;
+
+ if ((err = getpack(me, cmd.len2, r2 ? r2 : &rr2)) != 0)
+ goto retry_com;
+
+ if ((cmd.token & MASK_TOKEN) == TOKEN_ERROR) {
+ char *d = NULL;
+ int l = 0;
+ if (r1) {
+ l = r1->size;
+ d = r1->data;
+ } else {
+ l = rr1.size;
+ d = rr1.data;
+ };
+ errno = 0;
+ if ((d) && (l > 0)) {
+ d[l] = '\0';
+ } else {
+ d = "DBMS side errror, no cause reported";
+ };
+ err = E_ERROR;
+ errno = 0;
+ set_dbms_error(me, d, err);
+ goto exit_com;
+ } else if (((cmd.token & MASK_TOKEN) != token) ||
+ ((cmd.token | F_SERVER_SIDE) == 0)) {
+ err = E_PROTO;
+ goto exit_com;
+ };
+
+ if ((rr1.data != NULL) && (rr1.size)) {
+ (*me->free) (rr1.data);
+ rr1.size = 0;
+ };
+
+ if ((rr2.data != NULL) && (rr2.size)) {
+ (*me->free) (rr2.data);
+ rr1.size = 0;
+ };
+
+ if ((cmd.token & MASK_STATUS) == F_FOUND) {
+ if (retval)
+ *retval = 0;
+ } else {
+ if (retval)
+ *retval = 1;
+ if (r1 != NULL) {
+ if ((r1->size) && (r1->size))
+ (*me->free) (r1->data);
+ r1->data = NULL;
+ r1->size = 0;
+ };
+ if (r2 != NULL) {
+ if ((r2->size) && (r2->size))
+ (*me->free) (r2->data);
+ r2->data = NULL;
+ r2->size = 0;
+ };
+ };
+
+ err = 0;
+ goto done_com;
+
+retry_com:
+exit_com:
+ if ((r1 != NULL) && (r1->data != NULL) && (r1->size != 0)) {
+ (*me->free) (r1->data);
+ r1->size = 0;
+ };
+
+ if ((r2 != NULL) && (r2->data != NULL) && (r2->size != 0)) {
+ (*me->free) (r2->data);
+ r2->size = 0;
+ };
+
+ if ((rr1.data != NULL) && (rr1.size)) {
+ (*me->free) (rr1.data);
+ rr1.size = 0;
+ };
+
+ if ((rr2.data != NULL) && (rr1.size)) {
+ (*me->free) (rr2.data);
+ rr2.size = 0;
+ };
+
+done_com:
+
+ return err;
+}
+
+
+static dbms_error_t
+reselect(dbms * me)
+{
+ DBT r1, r2, v1;
+ int retval;
+ u_long buff[3];
+ int err = 0;
+ u_long proto = DBMS_PROTO;
+ u_long mode = me->mode;
+ char *name = me->name;
+ u_long bt_compare_fcn_type = me->bt_compare_fcn_type;
+
+ assert(sizeof(buff) == 12); /* really 4 bytes on the network ? */
+
+ buff[0] = htonl(proto);
+ buff[1] = htonl(mode);
+ buff[2] = htonl(bt_compare_fcn_type);
+
+ r1.size = sizeof(buff);
+ r1.data = &buff;
+
+ r2.size = strlen(name);
+ r2.data = name;
+
+ v1.data = NULL; /* set up buffer for return protocol and
+ * confirmation */
+ v1.size = 0;
+
+ if ((err = i_comms(me, TOKEN_INIT, &retval, &r1, &r2, &v1, NULL))) {
+ /*
+ * keep the exit code fprintf(stderr,"Fail2\n");
+ */
+ } else if (retval == 1) {
+ err = E_NOPE;
+ } else if (retval < 0) {
+ err = E_PROTO;
+ } else if (ntohl(*((u_long *) v1.data)) > DBMS_PROTO) {
+ err = E_VERSION;
+ };
+
+ if (v1.size)
+ (*me->free) (v1.data);
+ return err;
+}
+
+extern dbms_error_t
+dbms_comms(
+ dbms * me,
+ int token,
+ int *retval,
+ DBT * v1,
+ DBT * v2,
+ DBT * r1,
+ DBT * r2
+)
+{
+ int errs = 5;
+ int err = 0;
+
+ struct sigaction act, oact;
+#ifdef TPS
+ gettimeofday(&tstart, NULL);
+#endif
+
+ if (logfile) {
+ char *p1 = NULL;
+ char *p2 = NULL;
+ if (v1)
+ p1 = _hex(me, v1->size, v1->data);
+ if (v2)
+ p2 = _hex(me, v2->size, v2->data);
+
+ _tlog("%s@%s:%d %s(%02d) >>> %s %s",
+ me->name, me->host,me->port, _token2name(token), token,
+ p1 ? p1 : "<null>",
+ p2 ? p2 : "<null>");
+ if (p1) (*me->free)(p1);
+ if (p2) (*me->free)(p2);
+ }
+ /*
+ * for now, SA_RESTART any interupted function calls
+ */
+ act.sa_handler = SIG_IGN;
+ sigemptyset(&act.sa_mask);
+ act.sa_flags = SA_RESTART;
+
+ sigaction(SIGPIPE, &act, &oact);
+ if (retval)
+ *retval = -1;
+
+ /*
+ * now obviously this is wrong; we do _not_ want to continue during
+ * certain errors.. ah well..
+ */
+ for (errs = 0; errs < 10; errs++) {
+ if ((me->sockfd >= 0) &&
+ ((err = i_comms(me, token, retval, v1, v2, r1, r2)) == 0))
+ break;
+
+ /*
+ * we could of course exit on certain errors, but which ? we
+ * call recv, et.al.
+ */
+ if (err == EAGAIN || err == EINTR)
+ continue;
+
+ /*
+ * If the DB on the other end reported an error - then it
+ * obviously is not a comms problem - so retrying makes no
+ * sense
+ */
+ if (err == E_ERROR)
+ break;
+
+ sleep(errs * 2);
+ shutdown(me->sockfd, 2);
+ close(me->sockfd);
+
+ me->sockfd = -1;/* mark that we have an issue */
+ if (((err = reconnect(me)) == 0) &&
+ ((err = reselect(me)) == 0)) {
+ if (errs)
+ (*(me->callback)) (DBMS_EVENT_RECONNECT, errs);
+ } else if (errs)
+ (*(me->callback)) (DBMS_EVENT_WAITING, errs);
+ };
+
+#ifdef TPS
+ gettimeofday(&tnow, NULL);
+ ttps++;
+ ttime +=
+ (tnow.tv_sec - tstart.tv_sec) * 1000000 +
+ (tnow.tv_usec - tstart.tv_usec) * 1;
+#endif
+ /*
+ * restore whatever it was before
+ */
+ sigaction(SIGPIPE, &oact, &act);
+ if (logfile) {
+ char *q1 = NULL;
+ char *q2 = NULL;
+ if (r1)
+ q1 = _hex(me, r1->size, r1->data);
+ if (r2)
+ q2 = _hex(me, r2->size, r2->data);
+
+ _tlog("%s@%s:%d %s(%02d) <<< %s %s",
+ me->name, me->host,me->port, _token2name(token), token,
+ q1 ? q1 : "<null>",
+ q2 ? q2 : "<null>");
+ if (q1) (*me->free)(q1);
+ if (q2) (*me->free)(q2);
+ }
+ return err;
+};
Propchange: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/libdbms.c
------------------------------------------------------------------------------
svn:executable = *
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/test.c
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/test.c?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/test.c (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/dbms/libdbms/test.c Fri Apr 13 01:56:01 2007
@@ -0,0 +1,206 @@
+/*
+ * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+ * Dirk-Willem van Gulik <di...@webweaving.org>
+ *
+ * NOTICE
+ *
+ * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+ * file you should have received together with this source code. If you did not get a
+ * a copy of such a license agreement you can pick up one at:
+ *
+ * http://rdfstore.sourceforge.net/LICENSE
+ *
+ */
+
+#include "dbms.h"
+#include "dbms_comms.h"
+
+/* Number of fetch/store ops to try. */
+#define N (50000)
+#define M (100)
+
+static void _note(dbms_cause_t event, int counter)
+{
+ fprintf(stderr,"Note %d callback\n",event);
+ switch(event) {
+ case DBMS_EVENT_RECONNECT:
+ fprintf(stderr,"\tLost connection, is reconnecting.\n");
+ break;
+
+ case DBMS_EVENT_WAITING:
+ fprintf(stderr,"\tIs connecting but long wait\n");
+ break;
+
+ default:
+ fprintf(stderr,"\tUnk. callback event\n");
+ };
+}
+
+static void _err(char * msg, int erx)
+{
+ fprintf(stderr,"Error %d - callback\n\t%s\n--\n",erx,msg);
+}
+
+int main(int argc, char * * argv)
+{
+ dbms * d = dbms_connect( "test", NULL, 0, DBMS_XSMODE_DROP, NULL, NULL, &_note, &_err, 0);
+ dbms_error_t e;
+
+ if (!d) {
+ perror("Duh");
+ exit(1);
+ } else
+ printf("Open (and create) ok\n");
+
+ e = dbms_comms(d, TOKEN_PING, NULL, NULL, NULL, NULL, NULL);
+ if (e) {
+ fprintf(stderr,"Op failed %s\n", dbms_get_error(d));
+ exit(1);
+ } else
+ printf("Ping ok\n");
+
+{
+ DBT key,val;
+ int r = 0;
+ key.data = (char *) "Foo";
+ key.size=strlen((char *)key.data);
+ val.data = NULL;
+ val.size = 0;
+ e = dbms_comms(d, TOKEN_FETCH, &r, &key, NULL, NULL, &val);
+ if (e) {
+ fprintf(stderr,"Op FETCH failed %s\n", dbms_get_error(d));
+ exit(1);
+ } else
+ if (r == 0) {
+ fprintf(stderr,"Fetch returned a 'found' - EEK!\n");
+ exit(1);
+ }
+ if (r != 1) {
+ fprintf(stderr,"Fetch returned a strange value - EEK!\n");
+ exit(1);
+ } else
+ printf("Fetch ok (it came up empty)\n");
+}
+{
+ DBT key,val;
+ int r = 0;
+
+ /* include traling \0 to make printing on retrival easier */
+ key.data = (char *) "Foo";
+ key.size=strlen((char *)key.data)+1;
+
+ val.data = (char *) "Bar";
+ val.size = strlen((char *)val.data) + 1;
+
+ e = dbms_comms(d, TOKEN_STORE, &r, &key, &val, NULL, NULL);
+ if (e) {
+ fprintf(stderr,"Op STORE failed %s\n", dbms_get_error(d));
+ exit(1);
+ } else
+ if (r != 0) {
+ fprintf(stderr,"Fetch returned a strange value - EEK!\n");
+ exit(1);
+ } else
+ printf("Store ok\n");
+}
+{
+ DBT key,val;
+ int r = 0;
+ key.data = (char *) "Foo";
+ key.size=strlen((char *)key.data)+1;
+ e = dbms_comms(d, TOKEN_FETCH, &r, &key, NULL, NULL, &val);
+ if (e) {
+ fprintf(stderr,"Op FETCH failed %s\n", dbms_get_error(d));
+ exit(1);
+ } else
+ if (r != 0) {
+ fprintf(stderr,"Fetch returned a strange value - EEK!\n");
+ exit(1);
+ };
+
+ if (strcmp((char *)(val.data),"Bar")) {
+ fprintf(stderr,"Fetch failed (it came up with \"%s\")\n",
+ (char *)(val.data));
+ exit(1);
+ };
+
+ printf("Fetch ok (it came up with \"%s\")\n",
+ (char *)(val.data));
+ free(val.data);
+}
+{
+int j;
+char * pad = NULL;
+for(j=0;j<M;j+=M/10) {
+ pad = malloc(j)+1;
+ memset(pad,'X',j);
+ pad[j]='\0';
+printf("Size %d\n",j);
+{
+ DBT key,val;
+ char foo[ 10240 ];
+ char bar[ 10240 ];
+ int r,i;
+ time_t start = time(NULL);
+ for(i=0;i<N;i++) {
+ snprintf(foo,sizeof(foo),"Foo %06d",i);
+ snprintf(bar,sizeof(bar),"Bar %06d %s",i,pad);
+ key.data = (char *) foo;
+ key.size = strlen(foo)+1;
+ val.data = (char *) bar;
+ val.size = strlen(bar)+1;
+ r = 0;
+ e = dbms_comms(d, TOKEN_STORE, &r, &key, &val, NULL, NULL);
+ if (e || r != 0) {
+ fprintf(stderr,"%d store failed\n",i);
+ exit(1);
+ };
+ };
+ printf("%d ingests done: %f seconds or %.2f transactions/second\n",N,difftime(time(NULL),start),N/difftime(time(NULL),start));
+}
+{
+ DBT key,val;
+ char foo[ 10240 ], bar[ 10240 ];
+ int r,i;
+ time_t start = time(NULL);
+ for(i=0;i<N;i++) {
+ snprintf(foo,sizeof(foo),"Foo %06d",i);
+ snprintf(bar,sizeof(bar),"Bar %06d %s",i,pad);
+ key.data = (char *) foo;
+ key.size = strlen(foo)+1;
+ r = 0;
+ e = dbms_comms(d, TOKEN_FETCH, &r, &key, NULL, NULL, &val);
+ if (e || r != 0) {
+ fprintf(stderr,"%d fetch failed\n",i);
+ exit(1);
+ } else
+ if (strcmp((char *)(val.data),bar)) {
+ fprintf(stderr,"Dud reply %s != %s\n",foo,(char *)val.data);
+ exit(1);
+ }
+ free(val.data);
+ };
+ printf("%d fetches done: %f seconds %.2f transactions/second\n",N,difftime(time(NULL),start),N/difftime(time(NULL),start));
+}
+//free(pad);
+}
+}
+
+ e = dbms_comms(d, TOKEN_DROP, NULL, NULL, NULL, NULL, NULL);
+ if (e) {
+ fprintf(stderr,"drop failed\n\t%s\n", dbms_get_error(d));
+ exit(1);
+ } else
+ printf("Drop ok\n");
+
+
+ e = dbms_comms(d, TOKEN_CLOSE, NULL, NULL, NULL, NULL, NULL);
+ if (e) {
+ fprintf(stderr,"close failed %s\n", dbms_get_error(d));
+ exit(1);
+ } else
+ printf("Close ok\n");
+
+ printf("Clean exit\n");
+ exit(0);
+}