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

📄 database.cpp

📁 实现内存数据库的源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
//-< DATABASE.CPP >--------------------------------------------------*--------*
// FastDB                    Version 1.0         (c) 1999  GARRET    *     ?  *
// (Main Memory Database Management System)                          *   /\|  *
//                                                                   *  /  \  *
//                          Created:     20-Nov-98    K.A. Knizhnik  * / [] \ *
//                          Last update: 14-Jan-99    K.A. Knizhnik  * GARRET *
//-------------------------------------------------------------------*--------*
// Database memory management, query execution, scheme evaluation
//-------------------------------------------------------------------*--------*

#define INSIDE_FASTDB

#include <ctype.h>
#include <math.h>
#include <sys/stat.h>
#include "fastdb.h"
#include "compiler.h"
#include "hashtab.h"
#include "ttree.h"
#include "symtab.h"

dbNullReference null;

char const* const dbMetaTableName = "Metatable";
 
unsigned dbDatabase::dbParallelScanThreshold = 1000;

size_t dbDatabase::internalObjectSize[] = {
    0,
    dbPageSize,
    sizeof(dbTtree),
    sizeof(dbTtreeNode),
    sizeof(dbHashTable),
    sizeof(dbHashTableItem)
};

int dbDatabase::getVersion()
{
    return FASTDB_VERSION;
}

inline void convertIntToString(dbInheritedAttribute&   iattr,
			       dbSynthesizedAttribute& sattr)
{
    char buf[32];
    sattr.array.size = sprintf(buf, INT8_FORMAT, sattr.ivalue) + 1;
    sattr.array.base = dbStringValue::create(buf, iattr);
}

inline void convertRealToString(dbInheritedAttribute&   iattr,
				dbSynthesizedAttribute& sattr)
{
    char buf[32];
    sattr.array.size = sprintf(buf, "%f", sattr.fvalue) + 1;
    sattr.array.base = dbStringValue::create(buf, iattr);
}

inline void concatenateStrings(dbInheritedAttribute&   iattr,
			       dbSynthesizedAttribute& sattr,
			       dbSynthesizedAttribute& sattr2)
{
    char* str = 
	dbStringValue::create(sattr.array.size + sattr.array.size - 1, iattr);
    memcpy(str, sattr.array.base, sattr.array.size-1);
    memcpy(str + sattr.array.size - 1, sattr2.array.base, sattr2.array.size);
    sattr.array.base = str;
    sattr.array.size += sattr2.array.size-1;
}

inline int compareStringsForEquality(dbSynthesizedAttribute& sattr1,
				     dbSynthesizedAttribute& sattr2)
{
#ifdef IGNORE_CASE
    return stricmp(sattr1.array.base, sattr2.array.base);
#else
    return strcmp(sattr1.array.base, sattr2.array.base);
#endif
}

inline int compareStrings(dbSynthesizedAttribute& sattr1,
			  dbSynthesizedAttribute& sattr2)
{
#ifdef USE_LOCALE_SETTINGS
#ifdef IGNORE_CASE
    return stricoll(sattr1.array.base, sattr2.array.base);
#else
    return strcoll(sattr1.array.base, sattr2.array.base);
#endif
#else
#ifdef IGNORE_CASE
    return stricmp(sattr1.array.base, sattr2.array.base);
#else
    return strcmp(sattr1.array.base, sattr2.array.base);
#endif
#endif
}

#ifdef IGNORE_CASE
#define GET_CHAR(c) toupper((byte)(c))
#else
#define GET_CHAR(c) (c)
#endif

inline bool matchStrings(dbSynthesizedAttribute& sattr1,
			 dbSynthesizedAttribute& sattr2,
			 char escapeChar)
{
    char *str = sattr1.array.base;
    char *pattern = sattr2.array.base;
    char *wildcard = NULL; 
    char *strpos = NULL;
    while (true) { 
	int ch = GET_CHAR(*str);
	if (*pattern == dbMatchAnySubstring) { 
	    wildcard = ++pattern;
	    strpos = str;
	} else if (ch == '\0') { 
	    return (*pattern == '\0');
	} else if (*pattern == escapeChar && GET_CHAR(pattern[1]) == ch) { 
	    str += 1;
	    pattern += 2;
	} else if (*pattern != escapeChar
		   && (ch == GET_CHAR(*pattern)
		       || *pattern == dbMatchAnyOneChar))
        { 
	    str += 1;
	    pattern += 1;
	} else if (wildcard) { 
	    str = ++strpos;
	    pattern = wildcard;
	} else { 
	    return false;
	}
    }
}

inline bool matchStrings(dbSynthesizedAttribute& sattr1,
			 dbSynthesizedAttribute& sattr2)
{
    char *str = sattr1.array.base;
    char *pattern = sattr2.array.base;
    char *wildcard = NULL; 
    char *strpos = NULL;
    while (true) { 
	int ch = GET_CHAR(*str);
	if (*pattern == dbMatchAnySubstring) { 
	    wildcard = ++pattern;
	    strpos = str;
	} else if (ch == '\0') { 
	    return (*pattern == '\0');
	} else if (ch == GET_CHAR(*pattern) || *pattern == dbMatchAnyOneChar) {
	    str += 1;
	    pattern += 1;
	} else if (wildcard) { 
	    str = ++strpos;
	    pattern = wildcard;
	} else { 
	    return false;
	}
    }
}


inline void lowercaseString(dbInheritedAttribute&   iattr,
			    dbSynthesizedAttribute& sattr) 
{ 
    char *dst = dbStringValue::create(sattr.array.size, iattr);
    char *src = sattr.array.base;
    sattr.array.base = dst;
    while ((*dst++ = tolower(byte(*src++))) != '\0');
}    

inline void uppercaseString(dbInheritedAttribute&   iattr,
			    dbSynthesizedAttribute& sattr) 
{ 
    char *dst = dbStringValue::create(sattr.array.size, iattr);
    char *src = sattr.array.base;
    sattr.array.base = dst;
    while ((*dst++ = toupper(byte(*src++))) != '\0');
}    

inline void copyString(dbInheritedAttribute&   iattr,
		       dbSynthesizedAttribute& sattr, char* str) 
{ 
    sattr.array.base = dbStringValue::create(str, iattr);
    sattr.array.size = strlen(str) + 1;
    delete[] str;
}    

inline void searchArrayOfBool(dbSynthesizedAttribute& sattr, 
			      dbSynthesizedAttribute& sattr2)
{
    bool *p = (bool*)sattr2.array.base;
    int   n = sattr2.array.size;
    bool  v = (bool)sattr.bvalue;
    while (--n >= 0) { 
	if (v == *p++) { 
	    sattr.bvalue = true;
	    return;
	}
    }
    sattr.bvalue = false;
}

inline void searchArrayOfInt1(dbSynthesizedAttribute& sattr, 
			      dbSynthesizedAttribute& sattr2)
{
    int1 *p = (int1*)sattr2.array.base;
    int   n = sattr2.array.size;
    int1  v = (int1)sattr.ivalue;
    while (--n >= 0) { 
	if (v == *p++) { 
	    sattr.bvalue = true;
	    return;
	}
    }
    sattr.bvalue = false;
}

inline void searchArrayOfInt2(dbSynthesizedAttribute& sattr, 
			      dbSynthesizedAttribute& sattr2)
{
    int2 *p = (int2*)sattr2.array.base;
    int   n = sattr2.array.size;
    int2  v = (int2)sattr.ivalue;
    while (--n >= 0) { 
	if (v == *p++) { 
	    sattr.bvalue = true;
	    return;
	}
    }
    sattr.bvalue = false;
}

inline void searchArrayOfInt4(dbSynthesizedAttribute& sattr, 
			      dbSynthesizedAttribute& sattr2)
{
    int4 *p = (int4*)sattr2.array.base;
    int   n = sattr2.array.size;
    int4  v = (int4)sattr.ivalue;
    while (--n >= 0) { 
	if (v == *p++) { 
	    sattr.bvalue = true;
	    return;
	}
    }
    sattr.bvalue = false;
}

inline void searchArrayOfInt8(dbSynthesizedAttribute& sattr, 
			      dbSynthesizedAttribute& sattr2)
{
    int8 *p = (int8*)sattr2.array.base;
    int   n = sattr2.array.size;
    int8  v = sattr.ivalue;
    while (--n >= 0) { 
	if (v == *p) { 
	    sattr.bvalue = true;
	    return;
	}
	p += 1;
    }
    sattr.bvalue = false;
}

inline void searchArrayOfReal4(dbSynthesizedAttribute& sattr, 
			      dbSynthesizedAttribute& sattr2)
{
    real4* p = (real4*)sattr2.array.base;
    int    n = sattr2.array.size;
    real4  v = (real4)sattr.fvalue;
    while (--n >= 0) { 
	if (v == *p++) { 
	    sattr.bvalue = true;
	    return;
	}
    }
    sattr.bvalue = false;
}

inline void searchArrayOfReal8(dbSynthesizedAttribute& sattr, 
			       dbSynthesizedAttribute& sattr2)
{
    real8 *p = (real8*)sattr2.array.base;
    int    n = sattr2.array.size;
    real8  v = sattr.fvalue;
    while (--n >= 0) { 
	if (v == *p) { 
	    sattr.bvalue = true;
	    return;
	}
	p += 1;
    }
    sattr.bvalue = false;
}

inline void searchArrayOfReference(dbSynthesizedAttribute& sattr, 
				   dbSynthesizedAttribute& sattr2)
{
    oid_t *p = (oid_t*)sattr2.array.base;
    int    n = sattr2.array.size;
    oid_t  v = sattr.oid;
    while (--n >= 0) { 
	if (v == *p) { 
	    sattr.bvalue = true;
	    return;
	}
	p += 1;
    }
    sattr.bvalue = false;
}

inline void searchArrayOfString(dbSynthesizedAttribute& sattr, 
				dbSynthesizedAttribute& sattr2)
{
    dbVarying *p = (dbVarying*)sattr2.array.base;
    int        n = sattr2.array.size;
    char*      str = sattr.array.base;
    char*      base = (char*)sattr2.base; 
    while (--n >= 0) { 
	if (strcmp(base + p->offs, str) == 0) { 
	    sattr.bvalue = true;
	    return;
	}
	p += 1;
    }
    sattr.bvalue = false;
}

inline void searchInString(dbSynthesizedAttribute& sattr, 
			   dbSynthesizedAttribute& sattr2)
{
    if (sattr.array.size > sattr2.array.size) { 
	sattr.bvalue = false;
    } else if (sattr2.array.size > dbBMsearchThreshold) { 
	int len = sattr.array.size - 2;
	int n = sattr2.array.size - 1;
	int i, j, k;
	int shift[256];
	byte* pattern = (byte*)sattr.array.base;
	byte* str = (byte*)sattr2.array.base;
	for (i = 0; i < (int)items(shift); i++) { 
	    shift[i] = len+1;
	}
 	for (i = 0; i < len; i++) { 
	    shift[pattern[i]] = len-i;
	}
	for (i = len; i < n; i += shift[str[i]]) { 
	    j = len;
	    k = i;
	    while (pattern[j] == str[k]) { 
		k -= 1;
		if (--j < 0) { 
		    sattr.bvalue = true;
		    return;
		}
	    }
	}
	sattr.bvalue = false;
    } else { 
	sattr.bvalue = strstr(sattr2.array.base, sattr.array.base) != NULL;
    }
}

inline int8 powerIntInt(int8 x, int8 y) 
{
    int8 res = 1;

    if (y < 0) {
	x = 1/x;
	y = -y;
    }
    while (y != 0) {
	if (y & 1) { 
	    res *= x;
	}
	x *= x;
	y >>= 1;
    }
    return res;    
}

inline real8 powerRealInt(real8 x, int8 y) 
{
    real8 res = 1.0;

    if (y < 0) {
	x = 1/x;
	y = -y;
    }
    while (y != 0) {
	if (y & 1) { 
	    res *= x;
	}
	x *= x;
	y >>= 1;
    }
    return res;    
}

bool dbDatabase::evaluate(dbExprNode* expr, oid_t oid, dbTable* table, dbAnyCursor* cursor)
{
    dbInheritedAttribute iattr;
    dbSynthesizedAttribute sattr;
    iattr.db = this;
    iattr.oid = oid;
    iattr.table = table;
    iattr.record = (byte*)getRow(oid);    
    iattr.paramBase = (size_t)cursor->paramBase;
    execute(expr, iattr, sattr);
    return sattr.bvalue != 0;
}


void _fastcall dbDatabase::execute(dbExprNode*             expr, 
				   dbInheritedAttribute&   iattr, 
				   dbSynthesizedAttribute& sattr)
{
    dbSynthesizedAttribute sattr2, sattr3;

    switch (expr->cop) {
      case dbvmVoid:
	sattr.bvalue = true; // empty condition
	return;
      case dbvmCurrent:

⌨️ 快捷键说明

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