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

📄 pbm.mx

📁 一个内存数据库的源代码这是服务器端还有客户端
💻 MX
📖 第 1 页 / 共 2 页
字号:
@' The contents of this file are subject to the MonetDB Public License@' Version 1.1 (the "License"); you may not use this file except in@' compliance with the License. You may obtain a copy of the License at@' http://monetdb.cwi.nl/Legal/MonetDBLicense-1.1.html@'@' Software distributed under the License is distributed on an "AS IS"@' basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the@' License for the specific language governing rights and limitations@' under the License.@'@' The Original Code is the MonetDB Database System.@'@' The Initial Developer of the Original Code is CWI.@' Portions created by CWI are Copyright (C) 1997-2007 CWI.@' All Rights Reserved.@f pbm@v 0.1@a M.L.Kersten@+ Partitioned BAT ManagerIn real-life database applications the BATs tend to grow beyondthe memory size. Although in most situation this does notprovide severe limitations, it helps to be able to work withPartitioned BATs (PBAT).For example, the updates could be collected inseparate BATs, while all querying may still focus on the table at large.The partition optimizers will step in to either assemble the pieces beforea function is called, or to replace the instruction with a program fragmentto produce the result incrementally.The code snippet below illustrates a sequence over timeto build the partitioned BAT. BATs are incrementallyadded to a named PBAT @sc{Sales}.@verbatim    b1:= bat.new(:oid,:int);    bat.setPersistent(b1);    pbm.deposit("Sales2005","06",b1);    b2:= bat.new(:oid,:int);    bat.setPersistent(b2);    pbm.deposit("Sales2005","07",b2);    b3:= bat.new(:oid,:int);    bat.setPersistent(b3);    pbm.deposit("Sales2005","08",b3);    n:= pbm.getNames(); # get content of partitition catalogue.    io.print(n);@end verbatimThe components of a PBAT can be selectively retrieved by name or using an iterator.@verbatim    a:= pbm.take("Sales2005","06");barrier c:= pbm.newIterator("Sales2005");    io.print(c);    redo c:= pbm.getNextComponent("Sales2005");exit c;@end verbatimAlternatively, a static PBAT definition can be retrieved for manipulationby the MAT optimizer.@verbatim#	d:= mat.take("Sales2005");    replaced by mat.optimizer into    _21:= pbm.take("Sales2005","06");    _22:= pbm.take("Sales2005","07");    _23:= pbm.take("Sales2005","08");    d:= mat.new(_21,_22,_23);@end verbatimA related, but separate issues, are the BAT distribution and replicationmanagers.BEWARE, it is not yet protected against concurrent access yet.Also support partitions by enumerations@malmodule pbm;command open():voidaddress PBMopencomment "Locate and open the partition box";command close():voidaddress PBMclosecomment "Save and close the partition box ";command destroy():voidaddress PBMdestroycomment "Destroy the partition box";command take(grp:str,elm:str):bat[:any_1,:any_2]address PBMtakecomment "Retrieve a single component of a PBAT";command take(grp:str,idx:int):bat[:any_1,:any_2]address PBMtakeIndexedcomment "Retrieve a single component of a PBAT by index";command getNames():bat[:str,:str]address PBMgetNamescomment "Retrieve the component names of all PBATs";command getNextName()(:int,:str,:str)address PBMgetNextNamecomment "Locate next element in the partition box";command getComponents(grp:str):bat[:str,:str]address PBMgetComponentscomment "Retrieve the component names of a PBAT";command deposit(nme:str,elm:str,idx:int) :voidaddress PBMdepositcomment "Add an existing BAT to a PBAT in the partition box";command deposit(nme:str,elm:str,b:str) :voidaddress PBMdepositByNamecomment "Add a BAT to a PBAT in the partition box";command deposit(nme:str,elm:str,b:bat[:any_1,:any_2]) :voidaddress PBMdepositcomment "Add a BAT to a PBAT in the partition box";command deposit(nme:str,b:bat[:oid,:any_2]) :voidaddress PBMdepositIndexedcomment "Add a BAT to a PBAT in the partition box";command releaseAll(nme:str):voidaddress PBMreleaseAllcomment "Release a PBAT definitions";command discard() :voidaddress PBMdiscardAllcomment "Release all PBAT variable from the box";command discard(name:str) :voidaddress PBMdiscardcomment "Release a PBAT variable from the box";command discard(nme:str, b:bat[:oid,:any_1]) :voidaddress PBMdiscardComponentcomment "Release a single BAT from a PBAT";command newIterator()(:int,:str,:str)address PBMnewIteratorBasecomment "Create an iterator over the partition box";command generator(s:str):bat[:oid,:any_2]address PBMdummycomment "Place holder for generator optimizer";command newIterator(grp:str):bat[:oid,:any_2]address PBMnewIteratorcomment "Create an iterator over the BAT partitions.";command newIterator(grp:str,first:oid,last:oid)		:bat[:oid,:any_2]address PBMnewIteratorRngcomment "Create an iterator over the BAT partitions.";command getNextElement(grp:str) :bat[:oid,:any_1]address PBMgetNextElementcomment "Localize the next partition for processing.";command getNextElement(grp:str,first:oid,last:oid) :bat[:oid,:any_2]address PBMgetNextElementRngcomment "Localize the next partition for processing.";command getLast(grp:str):bat[:oid,:any_2]address PBMgetLastcomment "Obtain the last partition for update";command getRange(b:bat[:oid,:any_1])(first:oid,last:oid)address PBMgetRangecomment "Obtain the oid range for a partition";command compress(grp:str):voidaddress PBMcompresscomment "Compress the group to remove oid holes";command setWriteMode(nme:str):bat[:oid,:any_2]address PBMdummycomment "Perform mode change for all components of the partitioned BAT";command setReadMode(b:bat[:oid,:any_2]):bat[:oid,:any_2]address PBMdummycomment "Perform mode change for all components of the partitioned BAT";command dump()address PBMdump;@{command prelude() :voidaddress PBMprelude;command epilogue() :voidaddress PBMepilogue;pbm.prelude();@-@+ BAT Partition Manager ImplementationThe implementation is organized around a shared box, which shouldbe saved between session. It is up to other layers to ensure that BATsbeing deleted are also removed from the partition box to avoidmis-represented information.The internal data structure is used as a cache for improved access.@h#ifndef _MAL_PBM#define _MAL_PBM#include <mal.h>#include <mal_client.h>#include <mal_interpreter.h>#ifdef WIN32#ifndef LIBPBM#define pbm_export extern __declspec(dllimport)#else#define pbm_export extern __declspec(dllexport)#endif#else#define pbm_export extern#endiftypedef struct {	str grp;		/* group name */	str elm;		/* element name */	bat bid;		/* element bat identifier */	int type;		/* to ease filling the types */@-For the common case of :bat[:void,:any_1] we keep the boundariesaround for fast matching.@h	ValRecord hmin;		/* lowest head value stored */	ValRecord hmax;		/* largest head value */	int next;		/* next one in the PBAT list */	int prev;		/* previous one in the PBAT list */} PBATrecord, *PBAT;pbm_export PBAT partitions ;pbm_export int ptop, plimit;pbm_export void PBMresize(int size);pbm_export str PBMdump(void);pbm_export str PBMprelude(int *ret);pbm_export str PBMepilogue(int *ret);pbm_export str PBMopen(int *ret);pbm_export str PBMclose(int *ret);pbm_export str PBMdestroy(int *ret);pbm_export str PBMtakePBAT(int *ret, str *grp);pbm_export str PBMtake(int *ret, str *grp, str *elm);pbm_export str PBMtakeIndexed(int *ret, str *grp, int *bid);pbm_export str PBMgetComponents(int *ret, str *grp);pbm_export str PBMgetAllComponents(int *ret);pbm_export int PBMfindGrp(int bid);pbm_export int PBMfindPBAT(str grp);pbm_export int PBMfindPBATcomponent(str grp, str elm);pbm_export str PBMdeposit(int *ret, str *grp, str *elm, int *bid);pbm_export str PBMdepositIndexed(int *ret, str *grp, int *idx);pbm_export str PBMdepositByName(int *ret, str *grp, str *elm, str *nme);pbm_export str PBMgetRange(oid *first, oid *last, int *bid);pbm_export str PBMgetLast(int *ret, str *grp);pbm_export str PBMdiscard(int *ret, str *grp);pbm_export str PBMdiscardAll(int *ret);pbm_export str PBMreleaseAll(int *ret, int *bid);pbm_export str PBMdiscardComponent(int *ret, str *grp, int *bid);pbm_export str PBMnewIteratorBase(int *ret, str *grp, str *elm);pbm_export str PBMnewIterator(int *res, str *grp);pbm_export str PBMnewIteratorRng(int *res, str *grp, oid *first, oid *last);pbm_export str PBMgetNextElement(int *res, str *grp );pbm_export str PBMgetNextElementRng(int *res, str *grp, oid *first, oid *last);pbm_export str PBMcompress(int *ret, str *grp);pbm_export str PBMgetNextName(int *ret, str *nme, str *elm);pbm_export str PBMgetNames(int *ret);pbm_export str PBMdummy(int *ret, str *grp);#endif@c#include "mal_config.h"#include "pbm.h"PBAT partitions = NULL;int ptop=0, plimit=0;#define INCREMENT 1024voidPBMresize(int size){	PBAT p;	if( size <= plimit) return;	p = GDKmalloc(size * sizeof(PBATrecord));	memset((char*)p, 0, size * sizeof(PBATrecord));	if (partitions) {		if (ptop > size)			GDKfatal("assertion error in PBMresize");		memcpy(p, partitions, sizeof(PBATrecord) * ptop);		GDKfree(partitions);	}	partitions = p;	partitions[0].grp = NULL;	partitions[0].elm = NULL;	partitions[0].bid = -1;	partitions[0].prev = -1;	partitions[0].next = -1;	plimit = size;}static voidPBMfreePartition(int i){	BAT *b;	if( partitions[i].grp){		b= (BAT*) BBPgetdesc(partitions[i].bid);		if( b== 0)			GDKerror("inconsistent pbm, lost a BAT");		else{			b->batPersistence = TRANSIENT;			BBPdecref(b->batCacheid,TRUE);			BBPunfix(b->batCacheid);		}	}	if( partitions[i].grp) GDKfree(partitions[i].grp);	if( partitions[i].elm) GDKfree(partitions[i].elm);	partitions[i].grp = NULL;	partitions[i].elm = NULL;	partitions[i].bid = -1;	partitions[i].prev = -1;	partitions[i].next = -1;}static voidPBMfree(){	int i;	if (partitions) {		for(i=0; i<plimit; i++){			if( partitions[i].grp) GDKfree(partitions[i].grp);			if( partitions[i].elm) GDKfree(partitions[i].elm);		}		GDKfree(partitions);		partitions = NULL;		}}strPBMdump(){	stream *fd = GDKout;	int i;	stream_printf(fd, "ptop=%d plimit=%d\n", ptop, plimit);	for (i = 0; i < ptop; i++){		stream_printf(fd, "[%d] grp=%s elm=%s bid=%d type=%d prv=%d nxt=%d\n", i, 			partitions[i].grp, partitions[i].elm, partitions[i].bid, 			partitions[i].type, partitions[i].prev, partitions[i].next);			/* bounds for later */	}	return MAL_SUCCEED;}@- Box administrationThe box currently does not carry any client protection.This should later be added or being provided by the BAT access.@cstrPBMprelude(int *ret){	Box box;	(void) ret;	box = openBox("partitions");	(void) box;/*	if (box == 0)		throw(MAL, "pbm.prelude", "failed to open box");*/	PBMresize(INCREMENT);	return MAL_SUCCEED;}strPBMepilogue(int *ret){	*ret = closeBox("partitions", 0);	PBMfree();	if (*ret != 0)		throw(MAL, "pbm.prelude", "failed to open box");	return MAL_SUCCEED;}strPBMopen(int *ret){	(void)* ret;	if (openBox("pbm") != 0)		return MAL_SUCCEED;	throw(MAL, "pbm.open", "failed to open pbm box");}strPBMclose(int *ret){	stream *f;	str boxfile, boxfilebak;	Box box;	BAT *b;	int i;	(void)* ret;	box = openBox("pbm");	f = prepareSaveBox(box, &boxfile, &boxfilebak);	if (f != NULL) {		/* save the info */		for (i = 0; i < ptop; i++)			if (partitions[i].bid != -1) {				b = (BAT *) BBPgetdesc(partitions[i].bid);				if (b && b->batPersistence & PERSISTENT)					stream_printf(f, "pbm.deposit(\"%s\",\"%s\",\"%s\");\n", 						partitions[i].grp,						partitions[i].elm,						BBPname(partitions[i].bid));			}		stream_close(f);		GDKfree(boxfile);		GDKfree(boxfilebak);	}	return MAL_SUCCEED;}strPBMdestroy(int *ret){	(void)* ret;

⌨️ 快捷键说明

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