tdbTest.cpp 4.3 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
typedef struct SPoolMem {
  int64_t          size;
  struct SPoolMem *prev;
  struct SPoolMem *next;
} SPoolMem;

static SPoolMem *openPool() {
H
Hongze Cheng 已提交
14
  SPoolMem *pPool = (SPoolMem *)tdbOsMalloc(sizeof(*pPool));
H
Hongze Cheng 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

  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;

H
Hongze Cheng 已提交
34
    tdbOsFree(pMem);
H
Hongze Cheng 已提交
35 36 37 38
  } while (1);

  assert(pPool->size == 0);

H
Hongze Cheng 已提交
39
  tdbOsFree(pPool);
H
Hongze Cheng 已提交
40 41
}

H
Hongze Cheng 已提交
42 43
#define clearPool closePool

H
Hongze Cheng 已提交
44 45 46 47 48
static void *poolMalloc(void *arg, int size) {
  void     *ptr = NULL;
  SPoolMem *pPool = (SPoolMem *)arg;
  SPoolMem *pMem;

H
Hongze Cheng 已提交
49
  pMem = (SPoolMem *)tdbOsMalloc(sizeof(*pMem) + size);
H
Hongze Cheng 已提交
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
  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;

H
Hongze Cheng 已提交
76
  tdbOsFree(pMem);
H
Hongze Cheng 已提交
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 113 114
}

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 已提交
115

H
Hongze Cheng 已提交
116
TEST(tdb_test, simple_test) {
H
more  
Hongze Cheng 已提交
117
  int            ret;
H
Hongze Cheng 已提交
118
  TEnv          *pEnv;
H
Hongze Cheng 已提交
119
  TDB           *pDb;
H
more  
Hongze Cheng 已提交
120
  FKeyComparator compFunc;
H
Hongze Cheng 已提交
121
  int            nData = 1000000;
H
more  
Hongze Cheng 已提交
122

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

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

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

H
Hongze Cheng 已提交
136
    {  // Insert some data
H
Hongze Cheng 已提交
137 138 139 140 141 142 143 144 145 146
      int       i = 1;
      SPoolMem *pPool;
      int       memPoolCapacity = 16 * 1024;

      pPool = openPool();

      tdbTxnBegin(pEnv);

      for (;;) {
        if (i > nData) break;
H
Hongze Cheng 已提交
147 148 149 150 151

        sprintf(key, "key%d", i);
        sprintf(val, "value%d", i);
        ret = tdbDbInsert(pDb, key, strlen(key), val, strlen(val));
        GTEST_ASSERT_EQ(ret, 0);
H
Hongze Cheng 已提交
152 153 154 155 156 157 158 159 160 161

        if (pPool->size >= memPoolCapacity) {
          tdbTxnCommit(pEnv);

          clearPool(pPool);

          tdbTxnBegin(pEnv);
        }

        i++;
H
Hongze Cheng 已提交
162
      }
H
Hongze Cheng 已提交
163 164

      closePool(pPool);
H
Hongze Cheng 已提交
165 166 167
    }

    {  // Query the data
H
Hongze Cheng 已提交
168 169 170
      void *pVal = NULL;
      int   vLen;

H
Hongze Cheng 已提交
171 172 173 174
      for (int i = 1; i <= nData; i++) {
        sprintf(key, "key%d", i);
        sprintf(val, "value%d", i);

H
Hongze Cheng 已提交
175
        ret = tdbDbGet(pDb, key, strlen(key), &pVal, &vLen);
H
Hongze Cheng 已提交
176 177 178 179 180
        GTEST_ASSERT_EQ(ret, 0);

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

      TDB_FREE(pVal);
H
Hongze Cheng 已提交
183
    }
H
Hongze Cheng 已提交
184

H
Hongze Cheng 已提交
185
    {  // Iterate to query the DB data
H
Hongze Cheng 已提交
186 187 188 189 190
      TDBC *pDBC;
      void *pKey = NULL;
      void *pVal = NULL;
      int   vLen, kLen;
      int   count = 0;
H
Hongze Cheng 已提交
191 192 193 194 195 196 197

      ret = tdbDbcOpen(pDb, &pDBC);
      GTEST_ASSERT_EQ(ret, 0);

      for (;;) {
        ret = tdbDbNext(pDBC, &pKey, &kLen, &pVal, &vLen);
        if (ret < 0) break;
H
more  
Hongze Cheng 已提交
198 199 200 201 202

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

H
Hongze Cheng 已提交
203 204 205 206 207 208 209 210 211
        count++;
      }

      GTEST_ASSERT_EQ(count, nData);

      tdbDbcClose(pDBC);

      TDB_FREE(pKey);
      TDB_FREE(pVal);
H
Hongze Cheng 已提交
212
    }
H
Hongze Cheng 已提交
213
  }
H
Hongze Cheng 已提交
214 215

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

H
Hongze Cheng 已提交
218 219
  // Close a database
  tdbDbClose(pDb);
H
Hongze Cheng 已提交
220

H
Hongze Cheng 已提交
221 222 223
  // Close Env
  ret = tdbEnvClose(pEnv);
  GTEST_ASSERT_EQ(ret, 0);
H
Hongze Cheng 已提交
224
}