⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mal_box.c

📁 一个内存数据库的源代码这是服务器端还有客户端
💻 C
字号:
#line 256 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_box.mx"#include "mal_config.h"#include "mal_box.h"#include "mal_interpreter.h"	/* for garbageCollector() & garbageElement() */#include "mal_client.h"		/* for MCgetClient() */#include "mal_debugger.h"	/* for printStack() */#include "mal_import.h"		/* for malInclude() */#define MAXSPACES 64		/* >MAXCLIENTS+ max modules !! */Box box[MAXSPACES];BoxnewBox(str name){	Box obj = 0;	int i;	mal_set_lock(mal_contextLock, "newBox");#ifdef DEBUG_MAL_BOX	stream_printf(GDKout, "create new box '%s'\n", name);#endif	for (i = 0; i < MAXSPACES; i++)		if (box[i] != NULL && idcmp(name, box[i]->name) == 0) {			mal_unset_lock(mal_contextLock, "newBox");			GDKwarning("newBox:duplicate box definition\n");			return box[i];		}	for (i = 0; i < MAXSPACES; i++)		if (box[i] == NULL) {			#line 249 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_box.mx"	obj= (Box) GDKzalloc(sizeof(BoxRecord));	obj->name= GDKstrdup(name);	obj->sym=  newMalBlk(MAXVARS,STMT_INCREMENT);	obj->val = newGlobalStack(MAXVARS);	MT_lock_init(&obj->lock);#line 284 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_box.mx"			box[i] = obj;			break;		}	mal_unset_lock(mal_contextLock, "newBox");	if (i == MAXSPACES)		showException(MAL,"box.new", "too many boxes");#ifdef DEBUG_MAL_BOX	stream_printf(GDKout, "succeeded at %d\n", i);#endif	return obj;}voidfreeBox(int i){		if (box[i]) {			garbageCollector(box[i]->sym, box[i]->val,TRUE);			GDKfree(box[i]->name);			freeMalBlk(box[i]->sym);			GDKfree(box[i]->val);			GDKfree(box[i]);			box[i] = 0;		}}voidfreeBoxes(){	int i;	for (i = 0; i < MAXSPACES; i++)		freeBox(i);}BoxfindBox(str name){	int i;	mal_set_lock(mal_contextLock, "findBox");	for (i = 0; i < MAXSPACES; i++)		if (box[i] != NULL && idcmp(name, box[i]->name) == 0) {#ifdef DEBUG_MAL_BOX			stream_printf(GDKout, "found the box '%s' %d\n", name, i);#endif			mal_unset_lock(mal_contextLock, "findBox");			return box[i];		}	mal_unset_lock(mal_contextLock, "findBox");#ifdef DEBUG_MAL_BOX	stream_printf(GDKout, "could not find the box '%s' \n", name);#endif	return 0;}BoxopenBox(str name){	Box box = findBox(name);	if (box)		return box;	box = newBox(name);	loadBox(name);	box->dirty= FALSE;	return box;}intcloseBox(str name, int flag){	Box box;	if ((box = findBox(name))) {		saveBox(box, flag);		MT_destroy_lock(box->lock);		return 0;	}	return -1;}voiddestroyBox(str name){	int i, j;	str boxfile;	mal_set_lock(mal_contextLock, "destroyBox");	for (i = j = 0; i < MAXSPACES; i++) {		if (idcmp(box[j]->name, name) == 0) {			free(box[i]->name);			freeMalBlk(box[i]->sym);			freeStack(box[i]->val);			boxfile = boxFileName(box[i], 0);			unlink(boxfile);			GDKfree(boxfile);			MT_destroy_lock(box[i]->lock);		} else			box[i] = box[j++];	}	mal_unset_lock(mal_contextLock, "destroyBox");}#line 394 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_box.mx"intdepositBox(Box box, str name, ValPtr val){	int i;	ValPtr v;	i = findVariable(box->sym, name);	if (i < 0) {		i = newVariable(box->sym, GDKstrdup(name), val->vtype);		chkStack(box->val, i);	}	v = &box->val->stk[i];	box->val->stktop++;	VALcopy(v, val);	box->dirty= TRUE;#ifdef DEBUG_MAL_BOX	stream_printf(GDKout, "depositBox: entered '%s' at %d type %d\n", name, i, v->vtype);#endif	return 0;}voidinsertToBox(Box box, str nme, str val){	ValRecord vr;	vr.vtype = TYPE_str;	vr.val.sval = val? val: (str)str_nil;	vr.len = strlen(vr.val.sval);	depositBox(box, nme, &vr);}#line 430 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_box.mx"inttakeBox(Box box, str name, ValPtr val, int tpe){	int i;	ValPtr v;	i = findVariable(box->sym, name);#ifdef DEBUG_MAL_BOX	stream_printf(GDKout, "takeBox: found '%s' at %d\n", name, i);#endif	if (i < 0)		return i;	v = &box->val->stk[i];	if (val->vtype != v->vtype && v->vtype != TYPE_any && tpe != TYPE_any) {#ifdef DEBUG_MAL_BOX		stream_printf(GDKout, "takeBox:type error %d,%d\n", val->vtype, box->val->stk[i].vtype);#endif		return -1;	}	VALcopy(val, &box->val->stk[i]);	if (val->vtype == TYPE_bat)		BBPincref(val->val.br.id, TRUE);#ifdef DEBUG_MAL_BOX	stream_printf(GDKout, "takeBox:'%s' from '%s'\n", name, box->name);#endif	return 0;}#line 466 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_box.mx"intbindBAT(Box box, str name, str location){	int i;	ValPtr v;	i = findVariable(box->sym, name);	if (i < 0)		i = newVariable(box->sym, GDKstrdup(name), TYPE_any);	v = &box->val->stk[i];	v->val.br.id = BBPindex(location);	if (v->val.br.id == 0)		return -1;	v->vtype = TYPE_bat;#ifdef DEBUG_MAL_BOX	stream_printf(GDKout, "bindBox:'%s' to '%s' [%d] in '%s'\n", name, location, v->val.br.id, box->name);#endif	return 0;}intreleaseBox(Box box, str name){	int i;#ifdef DEBUG_MAL_BOX	stream_printf(GDKout, "releaseBox:%s from %s\n", name, box->name);#endif	i = findVariable(box->sym, name);	if (i < 0)		return i;	return 0;}intreleaseAllBox(Box box){#ifdef DEBUG_MAL_BOX	stream_printf(GDKout, "releaseAllBox:%s \n", box->name);#else	(void) box;#endif	return 0;}intdiscardBox(Box box, str name){	int i, j;#ifdef DEBUG_MAL_BOX	stream_printf(GDKout, "discardBox:%s from %s\n", name, box->name);#endif	i = findVariable(box->sym, name);	if (i < 0)		return i;	garbageElement(&box->val->stk[i]);	for (j = i; j < box->sym->vtop - 2; j++) {		box->sym->var[j] = box->sym->var[j + 1];		VALcopy(&box->val->stk[j], &box->val->stk[j + 1]);	}	box->sym->vtop--;	box->val->stktop--;	box->dirty= TRUE;	return 0;}#line 539 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_box.mx"intnextBoxElement(Box box, lng *cursor, ValPtr v){	if (*cursor >= box->sym->vtop) {		*cursor = -1;		return 0;	}	v->vtype = TYPE_str;	v->val.pval = getBoxName(box, *cursor);	*cursor = *cursor + 1;	return 0;}strgetBoxName(Box box, lng i){	str s;	s = getVarName(box->sym, (int) i);	return GDKstrdup(s);}strtoString(Box box, lng i){	(void) box;	(void) i;		/* fool the compiler */	return GDKstrdup("");}strgetBoxNames(int *bid){	BAT *b;	int i;	b = BATnew(TYPE_int, TYPE_str, (size_t) MAXSPACES);	for (i = 0; i < MAXSPACES; i++)		if (box[i] != NULL) {			BUNins(b, &i, box[i]->name, FALSE);		}	BBPincref(*bid = b->batCacheid, TRUE);	BBPunfix(*bid);	return MAL_SUCCEED;}#line 592 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_box.mx"strboxFileName(Box box, str backup){	char boxfile[PATHLENGTH];	size_t i = 0;	snprintf(boxfile, PATHLENGTH, "%s%c%s%cbox", GDKgetenv("gdk_dbfarm"), DIR_SEP, GDKgetenv("gdk_dbname"), DIR_SEP);	if (mkdir(boxfile, 0755) < 0 && errno != EEXIST) {		showException(MAL,"box.fileName", "can not create box directory");		return NULL;	}	i = strlen(boxfile);	if (backup) {		snprintf(boxfile + i, PATHLENGTH - i, "%c%s", DIR_SEP, backup);		if (mkdir(boxfile, 0755) < 0 && errno != EEXIST) {			showException(MAL,"box.fileName", "can not create box directory");			return NULL;		}		i += strlen(backup) + 1;	}	snprintf(boxfile + i, PATHLENGTH - i, "%c%s.box", DIR_SEP, box->name);	return GDKstrdup(boxfile);}#ifndef R_OK#define R_OK 4#endifstream *prepareSaveBox(Box box, str *boxfile, str *boxfilebak){	stream *f;	*boxfile = boxFileName(box, 0);	*boxfilebak = boxFileName(box, "backup");	if (*boxfile == 0) {		showException(MAL, "box.saveBox", "can not determine box file");		return 0;	}	if (access(*boxfile,R_OK)==0 && rename(*boxfile, *boxfilebak)) {#ifdef DEBUG_MAL_BOX		stream_printf(GDKout, "saveBox:can not rename %s\n", boxfile);#endif		showException(MAL,"box.saveBox", "can not rename backup");		GDKfree(*boxfile); *boxfile = NULL;		GDKfree(*boxfilebak); *boxfilebak = NULL;		return 0;		}	f = open_wastream(*boxfile);#ifndef S_IRUSR#define S_IRUSR 0400#define S_IWUSR 0200#endif	if (f != NULL)		chmod(*boxfile, (S_IRUSR | S_IWUSR));	else		showException(MAL,"box.saveBox", "can not create box file");	if (f == NULL) {		GDKfree(*boxfile); *boxfile= NULL;		GDKfree(*boxfilebak); *boxfilebak= NULL;	}	return f;}intsaveBox(Box box, int flag){	int i;	stream *f;	ValPtr v;	str boxfile, boxfilebak;	(void) flag;		/* fool the compiler */	if( box->dirty== FALSE)		return 0;	f = prepareSaveBox(box, &boxfile, &boxfilebak);	if (f == NULL)		return 1;#ifdef DEBUG_MAL_BOX	stream_printf(GDKout, "saveBox:created %s\n", boxfile);#endif	for (i = 0; i < box->sym->vtop; i++) {		v = &box->val->stk[i];		if (v->vtype == TYPE_bat) {			BAT *b = (BAT *) BBPgetdesc(v->val.br.id);			if (b && b->batPersistence & PERSISTENT){				stream_printf(f, "%s.bind(\"%s\",\"%s\");\n", 					box->name, getVarName(box->sym, i), 					BBPname(v->val.br.id));				BATcommit(b);			}		} else {			stream_printf(f, "%s := ", getVarName(box->sym, i));			ATOMprint(v->vtype, VALptr(v), f);			stream_printf(f, ":%s;\n", getTypeName(v->vtype));			stream_printf(f, "%s.deposit(\"%s\",%s);\n", 				box->name, getVarName(box->sym, i), getVarName(box->sym, i));		}	}	close_stream(f);	GDKfree(boxfile);	GDKfree(boxfilebak);	return 0;}#line 704 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_box.mx"voidloadBox(str name){	char boxfile[PATHLENGTH];	size_t i = 0;	snprintf(boxfile, PATHLENGTH, "%s%c%s%cbox", GDKgetenv("gdk_dbfarm"), DIR_SEP, GDKgetenv("gdk_dbname"), DIR_SEP);	mkdir(boxfile,0755); /* ignore errors */	i = strlen(boxfile);	snprintf(boxfile + i, PATHLENGTH - i, "%c%s.box", DIR_SEP, name);#ifdef DEBUG_MAL_BOX	stream_printf(GDKout, "loadBox:start loading the file %s\n", boxfile);#endif	if (access(boxfile, R_OK) == 0) {		Client child= MCforkClient(MCgetClient());		defaultScenario(child);		evalFile(child, boxfile, 0);		MCkillClient(child,2);#ifdef DEBUG_MAL_BOX		stream_printf(GDKout, "loadBox:loaded the file %s\n", boxfile);#endif	}}#line 732 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_box.mx"voidprintBox(stream *fd, Box obj){	printStack(fd, obj->sym, obj->val,0);}voidprintSomeBox(stream *fd, int k){	if (box[k] == 0) {		showException(MAL,"box.printSomeBox", "illegal space id: %d", k);	} else		printBox(fd, box[k]);}#line 748 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB5/src/mal/mal_box.mx"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -