database.cpp

来自「FastDb是高效的内存数据库系统」· C++ 代码 · 共 2,469 行 · 第 1/5 页

CPP
2,469
字号
//-< 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),
      dbPageSize
    };

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)
{
  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)
{
  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)itemsof(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 db_int8 powerIntInt(db_int8 x, db_int8 y)
{
  db_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, db_int8 y)
{
  real8 res = 1.0;

  if (y < 0)
  {
    x = 1/x;
    y = -y;
  }

  while (y != 0)
  {
    if (y & 1)
    {

⌨️ 快捷键说明

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