📄 database.cpp
字号:
//-< 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 "fastdb.h"
#include "compiler.h"
#include "symtab.h"
#include "hashtab.h"
#include "ttree.h"
#include "rtree.h"
#include <ctype.h>
#include <math.h>
#ifndef _WINCE
#include <sys/stat.h>
#endif
BEGIN_FASTDB_NAMESPACE
dbNullReference null;
char const* const dbMetaTableName = "Metatable";
size_t dbDatabase::internalObjectSize[] = {
0,
dbPageSize,
sizeof(dbTtree),
sizeof(dbTtreeNode),
sizeof(dbHashTable),
sizeof(dbHashTableItem),
sizeof(dbRtree),
sizeof(dbRtreePage)
};
int dbDatabase::getVersion()
{
return header->getVersion();
}
FixedSizeAllocator::FixedSizeAllocator()
{
minSize = 0;
maxSize = 0;
bufSize = 0;
quantum = 0;
nChains = 0;
chains = NULL;
holes = NULL;
vacant = NULL;
}
FixedSizeAllocator::~FixedSizeAllocator()
{
TRACE_MSG(("hits=%ld, faults=%ld, retries=%ld\n", (long)hits, (long)faults, (long)retries));
delete[] chains;
delete[] holes;
}
void FixedSizeAllocator::reset()
{
memset(chains, 0, sizeof(Hole*)*nChains);
if (bufSize > 0) {
for (size_t i = 1; i < bufSize; i++) {
holes[i-1].next = &holes[i];
}
holes[bufSize-1].next = NULL;
}
vacant = holes;
hits = 0;
faults = 0;
retries = 0;
}
void FixedSizeAllocator::init(size_t minSize, size_t maxSize, size_t quantum, size_t bufSize)
{
delete[] chains;
delete[] holes;
this->minSize = minSize;
this->maxSize = maxSize;
this->quantum = quantum;
this->bufSize = bufSize;
nChains = (maxSize - minSize + quantum - 1) / quantum + 1;
chains = new Hole*[nChains];
holes = new Hole[bufSize];
reset();
}
coord_t FASTDB_DLL_ENTRY distance(rectangle const& r, rectangle const& q)
{
if (r & q) {
return 0;
}
coord_t d = 0;;
for (int i = 0; i < rectangle::dim; i++) {
if (r.boundary[i] > q.boundary[rectangle::dim+i]) {
coord_t di = r.boundary[i] - q.boundary[rectangle::dim+i];
d += di*di;
} else if (q.boundary[i] > r.boundary[rectangle::dim+i]) {
coord_t di = q.boundary[i] - r.boundary[rectangle::dim+i];
d += di*di;
}
}
return (coord_t)sqrt((double)d);
}
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
dbException::dbException(int p_err_code, char const* p_msg, int p_arg)
: err_code (p_err_code),
msg (NULL),
arg (p_arg)
{
if (p_msg != NULL) {
msg = new char[strlen(p_msg)+1];
strcpy(msg, p_msg);
}
}
dbException::dbException(dbException const& ex)
{
err_code = ex.err_code;
arg = ex.arg;
if (ex.msg != NULL) {
msg = new char[strlen(ex.msg)+1];
strcpy(msg, ex.msg);
} else {
msg = NULL;
}
}
dbException::~dbException() throw()
{
delete[] msg;
}
const char* dbException::what() const throw()
{
return getMsg();
}
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)
{
db_int8 *p = (db_int8*)sattr2.array.base;
int n = sattr2.array.size;
db_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)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -