tdbTest.cpp 4.0 KB
Newer Older
1
#include <gtest/gtest.h>
H
Hongze Cheng 已提交
2

H
Hongze Cheng 已提交
3
#include "tdbInt.h"
H
more  
Hongze Cheng 已提交
4

H
Hongze Cheng 已提交
5 6
#include <string>

H
Hongze Cheng 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
typedef struct SPoolMem {
  int64_t          size;
  struct SPoolMem *prev;
  struct SPoolMem *next;
} SPoolMem;

static SPoolMem *openPool() {
  SPoolMem *pPool = (SPoolMem *)malloc(sizeof(*pPool));

  pPool->prev = pPool->next = pPool;
  pPool->size = 0;

  return pPool;
}

static void closePool(SPoolMem *pPool) {
  SPoolMem *pMem;

  do {
    pMem = pPool->next;

    if (pMem == pPool) break;

    pMem->next->prev = pMem->prev;
    pMem->prev->next = pMem->next;
    pPool->size -= pMem->size;

    free(pMem);
  } while (1);

  assert(pPool->size == 0);

  free(pPool);
}

static void *poolMalloc(void *arg, int size) {
  void     *ptr = NULL;
  SPoolMem *pPool = (SPoolMem *)arg;
  SPoolMem *pMem;

  pMem = (SPoolMem *)malloc(sizeof(*pMem) + size);
  if (pMem == NULL) {
    assert(0);
  }

  pMem->size = sizeof(*pMem) + size;
  pMem->next = pPool->next;
  pMem->prev = pPool;

  pPool->next->prev = pMem;
  pPool->next = pMem;
  pPool->size += pMem->size;

  ptr = (void *)(&pMem[1]);
  return ptr;
}

static void poolFree(void *arg, void *ptr) {
  SPoolMem *pPool = (SPoolMem *)arg;
  SPoolMem *pMem;

  pMem = &(((SPoolMem *)ptr)[-1]);

  pMem->next->prev = pMem->prev;
  pMem->prev->next = pMem->next;
  pPool->size -= pMem->size;

  free(pMem);
}

static int tKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2) {
  int k1, k2;

  std::string s1((char *)pKey1 + 3, kLen1 - 3);
  std::string s2((char *)pKey2 + 3, kLen2 - 3);
  k1 = stoi(s1);
  k2 = stoi(s2);

  if (k1 < k2) {
    return -1;
  } else if (k1 > k2) {
    return 1;
  } else {
    return 0;
  }
}

static int tDefaultKeyCmpr(const void *pKey1, int keyLen1, const void *pKey2, int keyLen2) {
  int mlen;
  int cret;

  ASSERT(keyLen1 > 0 && keyLen2 > 0 && pKey1 != NULL && pKey2 != NULL);

  mlen = keyLen1 < keyLen2 ? keyLen1 : keyLen2;
  cret = memcmp(pKey1, pKey2, mlen);
  if (cret == 0) {
    if (keyLen1 < keyLen2) {
      cret = -1;
    } else if (keyLen1 > keyLen2) {
      cret = 1;
    } else {
      cret = 0;
    }
  }
  return cret;
}
H
more  
Hongze Cheng 已提交
113

H
Hongze Cheng 已提交
114
TEST(tdb_test, simple_test) {
H
more  
Hongze Cheng 已提交
115 116 117 118 119
  int            ret;
  STEnv         *pEnv;
  STDB          *pDb;
  FKeyComparator compFunc;
  int            nData = 10000000;
H
more  
Hongze Cheng 已提交
120

H
Hongze Cheng 已提交
121
  // Open Env
H
Hongze Cheng 已提交
122
  ret = tdbEnvOpen("tdb", 4096, 256000, &pEnv);
H
Hongze Cheng 已提交
123
  GTEST_ASSERT_EQ(ret, 0);
H
Hongze Cheng 已提交
124

H
Hongze Cheng 已提交
125
  // Create a database
H
more  
Hongze Cheng 已提交
126 127
  compFunc = tKeyCmpr;
  ret = tdbDbOpen("db.db", TDB_VARIANT_LEN, TDB_VARIANT_LEN, compFunc, pEnv, &pDb);
H
Hongze Cheng 已提交
128
  GTEST_ASSERT_EQ(ret, 0);
H
Hongze Cheng 已提交
129

H
Hongze Cheng 已提交
130
  {
H
Hongze Cheng 已提交
131 132 133
    char key[64];
    char val[64];

H
Hongze Cheng 已提交
134 135 136 137 138 139 140 141 142 143 144
    {  // Insert some data

      for (int i = 1; i <= nData; i++) {
        sprintf(key, "key%d", i);
        sprintf(val, "value%d", i);
        ret = tdbDbInsert(pDb, key, strlen(key), val, strlen(val));
        GTEST_ASSERT_EQ(ret, 0);
      }
    }

    {  // Query the data
H
Hongze Cheng 已提交
145 146 147
      void *pVal = NULL;
      int   vLen;

H
Hongze Cheng 已提交
148 149 150 151
      for (int i = 1; i <= nData; i++) {
        sprintf(key, "key%d", i);
        sprintf(val, "value%d", i);

H
Hongze Cheng 已提交
152
        ret = tdbDbGet(pDb, key, strlen(key), &pVal, &vLen);
H
Hongze Cheng 已提交
153 154 155 156 157
        GTEST_ASSERT_EQ(ret, 0);

        GTEST_ASSERT_EQ(vLen, strlen(val));
        GTEST_ASSERT_EQ(memcmp(val, pVal, vLen), 0);
      }
H
Hongze Cheng 已提交
158 159

      TDB_FREE(pVal);
H
Hongze Cheng 已提交
160
    }
H
Hongze Cheng 已提交
161

H
Hongze Cheng 已提交
162 163 164 165 166 167 168 169
    {  // Iterate to query the DB data
      STDBC *pDBC;
      void  *pKey = NULL;
      void  *pVal = NULL;
      int    vLen, kLen;
      int    count = 0;

      ret = tdbDbcOpen(pDb, &pDBC);
H
Hongze Cheng 已提交
170
      GTEST_ASSERT_EQ(ret, 0);
H
Hongze Cheng 已提交
171 172 173 174

      for (;;) {
        ret = tdbDbNext(pDBC, &pKey, &kLen, &pVal, &vLen);
        if (ret < 0) break;
H
more  
Hongze Cheng 已提交
175 176 177 178 179

        // std::cout.write((char *)pKey, kLen) /* << " " << kLen */ << " ";
        // std::cout.write((char *)pVal, vLen) /* << " " << vLen */;
        // std::cout << std::endl;

H
Hongze Cheng 已提交
180 181 182 183 184 185 186 187 188
        count++;
      }

      GTEST_ASSERT_EQ(count, nData);

      tdbDbcClose(pDBC);

      TDB_FREE(pKey);
      TDB_FREE(pVal);
H
Hongze Cheng 已提交
189
    }
H
Hongze Cheng 已提交
190
  }
H
Hongze Cheng 已提交
191 192

  ret = tdbDbDrop(pDb);
H
Hongze Cheng 已提交
193
  GTEST_ASSERT_EQ(ret, 0);
H
Hongze Cheng 已提交
194

H
Hongze Cheng 已提交
195 196
  // Close a database
  tdbDbClose(pDb);
H
Hongze Cheng 已提交
197

H
Hongze Cheng 已提交
198 199 200
  // Close Env
  ret = tdbEnvClose(pEnv);
  GTEST_ASSERT_EQ(ret, 0);
H
Hongze Cheng 已提交
201
}