gdk.mx
来自「这个是内存数据库中的一个管理工具」· MX 代码 · 共 1,875 行 · 第 1/5 页
MX
1,875 行
# endif# ifdef HAVE_SYS_DIR_H# include <sys/dir.h># endif# ifdef HAVE_NDIR_H# include <ndir.h># endif#endif#ifdef HAVE_SYS_WAIT_H# include <sys/wait.h>#endif#ifndef WEXITSTATUS# define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)#endif#ifndef WIFEXITED# define WIFEXITED(stat_val) (((stat_val) & 255) == 0)#endif#ifdef __cplusplusextern "C" {#endif#include "gdk_system.h"#include "gdk_posix.h"#include <stream.h>#undef MIN#undef MAX#define MAX(A,B) ((A)<(B)?(B):(A))#define MIN(A,B) ((A)>(B)?(B):(A))/* defines from ctype with casts that allow passing char values */#define GDKisprint(c) isprint((int) ((unsigned char) (c)))#define GDKisspace(c) isspace((int) ((unsigned char) (c)))#define GDKisalnum(c) isalnum((int) ((unsigned char) (c)))#define GDKisgraph(c) isgraph((int) ((unsigned char) (c)))#define GDKisdigit(c) (((unsigned char) (c)) >= '0' && ((unsigned char) (c)) <= '9')#define GDKisxcntrl(c) (((unsigned char) (c)) >= 128 && ((unsigned char) (c)) <= 160)#define GDKisspecial(c) (((unsigned char) (c)) >= 161 && ((unsigned char) (c)) <= 191)#define GDKisupperl(c) (((unsigned char) (c)) >= 192 && ((unsigned char) (c)) <= 223)#define GDKislowerl(c) (((unsigned char) (c)) >= 224 && ((unsigned char) (c)) <= 255)#define GDKPROP 6 /* use one spare! */#define MONETHOME "MONETHOME"#ifndef NATIVE_WIN32#define BATDIR "bat"#define DELDIR "bat/DELETE_ME"#define BAKDIR "bat/BACKUP"#define SUBDIR "bat/BACKUP/SUBCOMMIT"#define LEFTDIR "bat/LEFTOVERS"#else#define BATDIR "bat"#define DELDIR "bat\\DELETE_ME"#define BAKDIR "bat\\BACKUP"#define SUBDIR "bat\\BACKUP\\SUBCOMMIT"#define LEFTDIR "bat\\LEFTOVERS"#endif#define DBLOGFILE "MonetLog"#ifdef MAXPATHLEN#define PATHLENGTH MAXPATHLEN#else#define PATHLENGTH 1024 /* maximum file pathname length */#endif@@}@- GDK session handling@multitable @columnfractions 0.08 0.7@item int@tab GDKinit (char *db, char *dbfarm, int allocmap)@item int@tab GDKexit (int status)@end multitable@The session is bracketed by @%GDKinit@ and @%GDKexit@. Initialization involvessetting up the administration for database access, such as memory allocationfor the database buffer pool.During the exit phase any pending transaction is aborted and the database isfreed for access by other users.A zero is returned upon encountering an erroneous situation.@{@- DefinitionsThe interface definitions for the application programs are shownbelow. The global variables should not be modified directly.@h#define NEG(A) (((int)(A))>0?-((int)(A)):((int)(A)))#define ABS(A) (((int)(A))>0?((int)(A)):-((int)(A)))#ifndef TRUE#define TRUE 1#define FALSE 0#endif#define BOUND2BTRUE 2 /* TRUE, and bound to be so */#define IDLENGTH 64 /* maximum BAT id length */#define OLDLENGTH 32 /* old max BAT id length */#define BATMARGIN 1.2 /* extra free margin for new heaps */#define BATTINY_BITS 8#define BATTINY (1<<BATTINY_BITS) /* minimum allocation buncnt for a BAT */#define TYPE_void 0#define TYPE_bit 1#define TYPE_chr 2#define TYPE_bte 3#define TYPE_sht 4#define TYPE_bat 5 /* BAT id: index in BBPcache */#define TYPE_int 6#define TYPE_oid 7#define TYPE_wrd 8#define TYPE_ptr 9 /* C pointer! */#define TYPE_flt 10#define TYPE_dbl 11#define TYPE_lng 12#define TYPE_str 13#define TYPE_any 255 /* limit types to <255! */typedef signed char bit;typedef signed char chr;typedef signed char bte;typedef short sht;#ifdef MONET_OID32#define SIZEOF_OID SIZEOF_INTtypedef unsigned int oid;#else#define SIZEOF_OID SIZEOF_SIZE_Ttypedef size_t oid;#endif#define SIZEOF_WRD SIZEOF_SSIZE_Ttypedef ssize_t wrd;typedef int bat; /* Index into BBP */typedef void *ptr; /* Internal coding of types */#define SIZEOF_PTR SIZEOF_VOID_Ptypedef float flt;typedef double dbl;typedef char *str;#if SIZEOF_INT==8# define LL_CONSTANT(val) (val)#elif SIZEOF_LONG==8# define LL_CONSTANT(val) (val##L)#elif defined(HAVE_LONG_LONG)# define LL_CONSTANT(val) (val##LL)#elif defined(HAVE___INT64)# define LL_CONSTANT(val) (val##i64)#endif/* Base types for all architectures. These types work on IRIX (64 & 32) and for gcc on Linux. */typedef unsigned char gdk_uint8;typedef char gdk_int8;typedef unsigned short gdk_uint16;typedef short gdk_int16;typedef unsigned int gdk_uint32;typedef int gdk_int32;#ifdef HAVE_LONG_LONGtypedef unsigned long long gdk_uint64;typedef long long gdk_int64;#endif/* define printf formats for printing ptrdiff_t variables */#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901 && !defined(__svr4__) && !defined(WIN32) && !defined(__sgi)#define PDFMT "%td"#elif SIZEOF_PTRDIFF_T == SIZEOF_INT#define PDFMT "%d"#elif SIZEOF_PTRDIFF_T == SIZEOF_LONG#define PDFMT "%ld"#elif SIZEOF_PTRDIFF_T == SIZEOF_LONG_LONG || SIZEOF_PTRDIFF_T == SIZEOF___INT64#define PDFMT LLFMT#else#error no definition for PDFMT#endif/* define printf format for printing pointer values */#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901#define PTRFMT "%p"#define PTRFMTCAST /* no cast needed */#elif SIZEOF_VOID_P == SIZEOF_INT#define PTRFMT "%x"#define PTRFMTCAST (unsigned int)#elif SIZEOF_VOID_P == SIZEOF_LONG#define PTRFMT "%lx"#define PTRFMTCAST (unsigned long)#elif SIZEOF_VOID_P == SIZEOF_LONG_LONG#define PTRFMT "%llx"#define PTRFMTCAST (unsigned long long)#elif SIZEOF_VOID_P == SIZEOF___INT64#define PTRFMT "%I64x"#define PTRFMTCAST (unsigned __int64)#else#error no definition for PTRFMT#endiftypedef char *BUN; /* pointer in some heap. */typedef char long_str[IDLENGTH]; /* standard GDK static string */typedef char short_str[OLDLENGTH]; /* old GDK small static string */typedef oid hash_t; /* type used for hash tables */#if SIZEOF_OID == SIZEOF_INT#define HASH_MAX INT_MAX#else#define HASH_MAX LLONG_MAX#endiftypedef oid var_t; /* type used for heap index of var-sized BAT */#if SIZEOF_OID == SIZEOF_INT /* a type compatible with var_t */#define TYPE_var TYPE_int#if SIZEOF_SIZE_T == SIZEOF_INT #define OIDFMT SZFMT#else#define OIDFMT "%u"#endif#else#define TYPE_var TYPE_lng#define OIDFMT SZFMT#endif@- Checking and Error definitions:@htypedef enum { GDK_FAIL, GDK_SUCCEED } gdk_return;gdk_export int GDKsilent; /* should GDK shut up? */#define FATALcheck(tst, msg) if (tst) GDKfatal(msg);#define ERRORcheck(tst, msg) if (tst) { GDKerror(msg); return 0; }#define WARNcheck(tst, msg) if (tst) GDKwarning(msg)#define BATcheck(tst, msg) if (tst == NULL) { \ GDKerror("%s: BAT required.\n",msg); \ return 0; \ }/* needed for mel */#define ATOMextern(t) (ATOMstorage(t) >= TYPE_str)#define TYPEcastable(t1,t2) (ATOMtype(t1)==ATOMtype(t2))#define TYPEequal(t1,t2) (ATOMtype(t1)==ATOMtype(t2))#define TYPEcomp(t1,t2) (ATOMstorage(ATOMtype(t1))==ATOMstorage(ATOMtype(t2)))#define TYPEerror(t1,t2) (!TYPEcomp(t1,t2))#define TYPEcheck(t1,t2) \ if (TYPEerror(t1,t2)) { \ GDKerror("TYPEcheck: Incompatible types %s and %s.\n", \ ATOMname(t2), ATOMname(t1)); \ return 0; \ } else if (!TYPEcomp(t1,t2)) { \ GDKwarning("Interpreting %s as %s.\n", \ ATOMname(t2), ATOMname(t1)); \ }#define BATcompatible(P1,P2) { \ ERRORcheck(P1 == NULL, "BATcompatible: BAT required\n"); \ ERRORcheck(P2 == NULL, "BATcompatible: BAT required\n"); \ if (TYPEerror(BAThtype(P1),BAThtype(P2)) || \ TYPEerror(BATttype(P1),BATttype(P2))) \ { \ GDKerror("Incompatible operands.\n"); \ return 0; \ } \ if (BAThtype(P1) != BAThtype(P2) && \ ATOMtype((P1)->htype) != ATOMtype((P2)->htype)) { \ GDKwarning("Interpreting %s as %s.\n", \ ATOMname(BAThtype(P2)), ATOMname(BAThtype(P1))); \ } \ if (BATttype(P1) != BATttype(P2) && \ ATOMtype((P1)->ttype) != ATOMtype((P2)->ttype)) { \ GDKwarning("Interpreting %s as %s.\n", \ ATOMname(BATttype(P2)), ATOMname(BATttype(P1))); \ } \}@htypedef struct { size_t maxsize; /* maximum realloc size (bytes) */ size_t free; /* index where free area starts. */ size_t size; /* size of the heap (bytes) */ BUN base; /* base pointer in memory. */ char storage; /* storage mode (mmap/malloc). */ bit copied; /* a copy of an existing map. */ chr newstorage; /* new desired storage mode at re-allocation. */ str filename; /* file containing image of the heap */} Heap;typedef struct { int type; /* type of index entity */ size_t lim; /* collision list size */ hash_t mask; /* number of hash buckets-1 (power of 2) */ hash_t *hash; /* hash table */ hash_t *link; /* collision list */ Heap *heap; /* heap where the hash is stored */} Hash;@@}@+ Binary Association TablesHaving gone to the previous preliminary definitions, we willnow introduce the structure of Binary Association Tables (BATs)in detail. They are the basic storage unit on which GDK is modelled.The BAT holds an unlimited number of binary associations, calledBUNs (@strong{Binary UNits}). The two attributes of a BUN are called@strong{head} (left) and @strong{tail} (right) in the remainder of thisdocument. @c image{http://monetdb.cwi.nl/projects/monetdb-mk/imgs/bat1,,,,feps}The above figure shows what a BAT looks like. It consists of twocolumns, called head and tail, such that we have always binary tuples(BUNs). The overlooking structure is the @strong{BAT record}. It points to a heap structure called the @strong{BUN heap}.This heap contains the atomic values inside the two columns. If theyare fixed-sized atoms, these atoms reside directly in the BUN heap. Ifthey are variable-sized atoms (such as string or polygon), however,the columns has an extra heap for storing those (such@strong{variable-sized atom heaps} are thenreferred to as @strong{Head Heap}s and @strong{Tail Heap}s). The BUN heap thencontains integer byte-offsets (fixed-sized, of course) into a head-or tail-heap.@The BUN heap contains a contiguous range of BUNs. It starts afterthe @strong{first} pointer, and finishes at the end in the @strong{free}area of the BUN. All BUNs after the @strong{inserted} pointer have beenadded in the last transaction (and will be deleted on a transactionabort). All BUNs between the @strong{deleted} pointer and the @strong{first}have been deleted in this transaction (and will be reinserted at atransaction abort).@-The location of a certain BUN in a BATmay change between successive library routine invocations. Therefore, oneshould avoid keeping references into the BAT storage area for longperiods.@Passing values between the library routines and the enclosing Cprogram is primarily through value pointers of type @%ptr@. Pointersinto the BAT storage area should only be used for retrieval. Directupdates of data stored in a BAT is forbidden. The user should adhereto the interface conventions to guarantee the integrity rules and tomaintain the (hidden) auxiliary search structures.@- GDK variant record typeWhen manipulating values, MonetDB puts them into value records.The built-in types have a direct entry in the union. Others shouldbe represented as a pointer of memory in pval or as a string, whichis basically the same. In such cases the @%len@ field indicatesthe size of this piece of memory.@{@htypedef struct { bat id; int stamp;} batrec;typedef struct { union { /* storage is first in the record */ int ival; oid oval; sht shval; bte btval; wrd wval; chr cval[4]; flt fval; ptr pval; struct BAT *Bval; /* this field is only used by mel */ bat bval; batrec br; /* bval + stamp */ str sval; dbl dval; lng lval; } val;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?