tstreamUpdateTest.cpp 8.0 KB
Newer Older
R
root 已提交
1 2 3 4 5 6
#include <gtest/gtest.h>

#include "tstreamUpdate.h"
#include "ttime.h"

using namespace std;
5
54liuyao 已提交
7
#define MAX_NUM_SCALABLE_BF    100000
R
root 已提交
8

5
54liuyao 已提交
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
bool equalSBF(SScalableBf* left, SScalableBf* right) {
  if (left->growth != right->growth) return false;
  if (left->numBits != right->numBits) return false;
  int lsize = taosArrayGetSize(left->bfArray);
  int rsize = taosArrayGetSize(right->bfArray);
  if (lsize != rsize) return false;
  for (int32_t i = 0; i < lsize; i++) {
    SBloomFilter* pLeftBF = (SBloomFilter*)taosArrayGetP(left->bfArray, i);
    SBloomFilter* pRightBF = (SBloomFilter*)taosArrayGetP(right->bfArray, i);
    if (pLeftBF->errorRate != pRightBF->errorRate) return false;
    if (pLeftBF->expectedEntries != pRightBF->expectedEntries) return false;
    if (pLeftBF->hashFn1 != pRightBF->hashFn1) return false;
    if (pLeftBF->hashFn2 != pRightBF->hashFn2) return false;
    if (pLeftBF->hashFunctions != pRightBF->hashFunctions) return false;
    if (pLeftBF->numBits != pRightBF->numBits) return false;
    if (pLeftBF->numUnits != pRightBF->numUnits) return false;
    if (pLeftBF->size != pRightBF->size) return false;
    uint64_t* leftUint = (uint64_t*) pLeftBF->buffer;
    uint64_t* rightUint = (uint64_t*) pRightBF->buffer;
    for (int32_t j = 0; j < pLeftBF->numUnits; j++) {
      if (leftUint[j] != rightUint[j]) return false;
    }
  }
  return true;
}

R
root 已提交
35
TEST(TD_STREAM_UPDATE_TEST, update) {
5
54liuyao 已提交
36 37
  const int64_t interval = 20 * 1000;
  const int64_t watermark = 10 * 60 * 1000;
R
root 已提交
38
  SUpdateInfo *pSU = updateInfoInit(interval, TSDB_TIME_PRECISION_MILLI, watermark);
5
54liuyao 已提交
39
  GTEST_ASSERT_EQ(updateInfoIsUpdated(pSU,1, 0), false);
5
54liuyao 已提交
40
  GTEST_ASSERT_EQ(updateInfoIsUpdated(pSU,1, -1), true);
R
root 已提交
41 42

  for(int i=0; i < 1024; i++) {
5
54liuyao 已提交
43
    GTEST_ASSERT_EQ(updateInfoIsUpdated(pSU,i, 1), false);
R
root 已提交
44 45
  }
  for(int i=0; i < 1024; i++) {
5
54liuyao 已提交
46
    GTEST_ASSERT_EQ(updateInfoIsUpdated(pSU,i, 1), true);
R
root 已提交
47 48 49
  }

  for(int i=0; i < 1024; i++) {
5
54liuyao 已提交
50
    GTEST_ASSERT_EQ(updateInfoIsUpdated(pSU,i, 2), false);
R
root 已提交
51 52
  }
  for(int i=0; i < 1024; i++) {
5
54liuyao 已提交
53
    GTEST_ASSERT_EQ(updateInfoIsUpdated(pSU,i, 2), true);
R
root 已提交
54 55 56
  }

  for(int i=0; i < 1024; i++) {
5
54liuyao 已提交
57
    GTEST_ASSERT_EQ(updateInfoIsUpdated(pSU,i, 1), true);
R
root 已提交
58 59
  }

5
54liuyao 已提交
60
  TSKEY uid = 0;
R
root 已提交
61
  for(int i=3; i < 1024; i++) {
5
54liuyao 已提交
62
    GTEST_ASSERT_EQ(updateInfoIsUpdated(pSU, uid, i), false);
R
root 已提交
63
  }
5
54liuyao 已提交
64
  GTEST_ASSERT_EQ(*(TSKEY*)taosHashGet(pSU->pMap, &uid, sizeof(uint64_t)), 1023);
R
root 已提交
65 66

  for(int i=3; i < 1024; i++) {
5
54liuyao 已提交
67
    GTEST_ASSERT_EQ(updateInfoIsUpdated(pSU, uid, i), true);
R
root 已提交
68
  }
5
54liuyao 已提交
69
  GTEST_ASSERT_EQ(*(TSKEY*)taosHashGet(pSU->pMap, &uid, sizeof(uint64_t)), 1023);
R
root 已提交
70 71 72

  SUpdateInfo *pSU1 = updateInfoInit(interval, TSDB_TIME_PRECISION_MILLI, watermark);
  for(int i=1; i <= watermark / interval; i++) {
5
54liuyao 已提交
73
    GTEST_ASSERT_EQ(updateInfoIsUpdated(pSU1, 1, i * interval + 5), false);
R
root 已提交
74 75 76 77 78 79 80 81 82 83
    GTEST_ASSERT_EQ(pSU1->minTS, interval);
    GTEST_ASSERT_EQ(pSU1->numSBFs, watermark / interval);
  }
  for(int i=0; i < pSU1->numSBFs; i++) {
    SScalableBf *pSBF = (SScalableBf *)taosArrayGetP(pSU1->pTsSBFs, i);
    SBloomFilter *pBF = (SBloomFilter *)taosArrayGetP(pSBF->bfArray, 0);
    GTEST_ASSERT_EQ(pBF->size, 1);
  }

  for(int i= watermark / interval + 1, j = 2 ; i <= watermark / interval + 10; i++,j++) {
5
54liuyao 已提交
84
    GTEST_ASSERT_EQ(updateInfoIsUpdated(pSU1, 1, i * interval + 5), false);
R
root 已提交
85 86 87 88 89 90 91 92
    GTEST_ASSERT_EQ(pSU1->minTS, interval*j);
    GTEST_ASSERT_EQ(pSU1->numSBFs, watermark / interval);
    SScalableBf *pSBF = (SScalableBf *)taosArrayGetP(pSU1->pTsSBFs, pSU1->numSBFs - 1);
    SBloomFilter *pBF = (SBloomFilter *)taosArrayGetP(pSBF->bfArray, 0);
    GTEST_ASSERT_EQ(pBF->size, 1);
  }
  
  for(int i= watermark / interval * 100, j = 0; j < 10; i+= (watermark / interval * 2), j++) {
5
54liuyao 已提交
93
    GTEST_ASSERT_EQ(updateInfoIsUpdated(pSU1, 1, i * interval + 5), false);
R
root 已提交
94 95 96 97 98
    GTEST_ASSERT_EQ(pSU1->minTS, (i-(pSU1->numSBFs-1))*interval);
    GTEST_ASSERT_EQ(pSU1->numSBFs, watermark / interval);
  }

  SUpdateInfo *pSU2 = updateInfoInit(interval, TSDB_TIME_PRECISION_MILLI, watermark);
5
54liuyao 已提交
99
  GTEST_ASSERT_EQ(updateInfoIsUpdated(pSU2, 1, 1 * interval + 5), false);
R
root 已提交
100 101
  GTEST_ASSERT_EQ(pSU2->minTS, interval);
  for(int i= watermark / interval * 100, j = 0; j < 10; i+= (watermark / interval * 10), j++) {
5
54liuyao 已提交
102
    GTEST_ASSERT_EQ(updateInfoIsUpdated(pSU2, 1, i * interval + 5), false);
R
root 已提交
103 104
    GTEST_ASSERT_EQ(pSU2->minTS, (i-(pSU2->numSBFs-1))*interval);
    GTEST_ASSERT_EQ(pSU2->numSBFs, watermark / interval);
5
54liuyao 已提交
105 106
    TSKEY uid2 = 1;
    GTEST_ASSERT_EQ(*(TSKEY*)taosHashGet(pSU2->pMap, &uid2, sizeof(uint64_t)), i * interval + 5);
R
root 已提交
107 108 109 110 111
  }
  
  SUpdateInfo *pSU3 = updateInfoInit(interval, TSDB_TIME_PRECISION_MILLI, watermark);
  for(int j = 1; j < 100; j++) {
    for(int i = 0; i < pSU3->numSBFs; i++) {
5
54liuyao 已提交
112
      GTEST_ASSERT_EQ(updateInfoIsUpdated(pSU3, i, i * interval + 5 * j), false);
R
root 已提交
113 114
      GTEST_ASSERT_EQ(pSU3->minTS, 0);
      GTEST_ASSERT_EQ(pSU3->numSBFs, watermark / interval);
5
54liuyao 已提交
115 116
      uint64_t uid3 = i;
      GTEST_ASSERT_EQ(*(TSKEY*)taosHashGet(pSU3->pMap, &uid3, sizeof(uint64_t)), i * interval + 5 * j);
R
root 已提交
117 118 119 120 121 122
      SScalableBf *pSBF = (SScalableBf *)taosArrayGetP(pSU3->pTsSBFs, i);
      SBloomFilter *pBF = (SBloomFilter *)taosArrayGetP(pSBF->bfArray, 0);
      GTEST_ASSERT_EQ(pBF->size, j);
    }
  }

5
54liuyao 已提交
123
  SUpdateInfo *pSU4 = updateInfoInit(-1, TSDB_TIME_PRECISION_MILLI, -1);
5
54liuyao 已提交
124
  GTEST_ASSERT_EQ(pSU4->watermark, pSU4->interval);
5
54liuyao 已提交
125 126 127
  GTEST_ASSERT_EQ(pSU4->interval, MILLISECOND_PER_MINUTE);

  SUpdateInfo *pSU5 = updateInfoInit(0, TSDB_TIME_PRECISION_MILLI, 0);
5
54liuyao 已提交
128
  GTEST_ASSERT_EQ(pSU5->watermark, pSU4->interval);
5
54liuyao 已提交
129 130
  GTEST_ASSERT_EQ(pSU5->interval, MILLISECOND_PER_MINUTE);

5
54liuyao 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
  SUpdateInfo *pSU7 = updateInfoInit(interval, TSDB_TIME_PRECISION_MILLI, watermark);
  updateInfoAddCloseWindowSBF(pSU7);
  for(int64_t i = 1; i < 2048000; i++) {
    GTEST_ASSERT_EQ(updateInfoIsUpdated(pSU7,i, i), false);
  }
  GTEST_ASSERT_EQ(updateInfoIsUpdated(pSU7,100, 1), true);
  GTEST_ASSERT_EQ(updateInfoIsUpdated(pSU7,110, 10), true);
  GTEST_ASSERT_EQ(updateInfoIsUpdated(pSU7,200, 20), true);

  int32_t bufLen = updateInfoSerialize(NULL, 0, pSU7);
  void* buf = taosMemoryCalloc(1, bufLen);
  int32_t resSize = updateInfoSerialize(buf, bufLen, pSU7);

  SUpdateInfo *pSU6 = updateInfoInit(0, TSDB_TIME_PRECISION_MILLI, 0);
  int32_t desSize = updateInfoDeserialize(buf, bufLen, pSU6);
  GTEST_ASSERT_EQ(desSize, 0);

  GTEST_ASSERT_EQ(pSU7->interval, pSU6->interval);
  GTEST_ASSERT_EQ(pSU7->maxVersion, pSU6->maxVersion);
  GTEST_ASSERT_EQ(pSU7->minTS, pSU6->minTS);
  GTEST_ASSERT_EQ(pSU7->numBuckets, pSU6->numBuckets);
  GTEST_ASSERT_EQ(pSU7->numSBFs, pSU6->numSBFs);
  GTEST_ASSERT_EQ(pSU7->scanGroupId,  pSU6->scanGroupId);
  GTEST_ASSERT_EQ(pSU7->scanWindow.ekey, pSU6->scanWindow.ekey);
  GTEST_ASSERT_EQ(pSU7->scanWindow.skey, pSU6->scanWindow.skey);
  GTEST_ASSERT_EQ(pSU7->watermark, pSU6->watermark);
  GTEST_ASSERT_EQ(equalSBF(pSU7->pCloseWinSBF, pSU6->pCloseWinSBF), true);

  int32_t mapSize = taosHashGetSize(pSU7->pMap);
  GTEST_ASSERT_EQ(mapSize, taosHashGetSize(pSU6->pMap));
  void*  pIte = NULL;
  size_t keyLen = 0;
  while ((pIte = taosHashIterate(pSU7->pMap, pIte)) != NULL) {
    void* key = taosHashGetKey(pIte, &keyLen);
    void* value6 = taosHashGet(pSU6->pMap, key, keyLen);
    GTEST_ASSERT_EQ(*(TSKEY*)pIte, *(TSKEY*)value6);
  }

  int32_t buSize = taosArrayGetSize(pSU7->pTsBuckets);
  GTEST_ASSERT_EQ(buSize, taosArrayGetSize(pSU6->pTsBuckets));
  for (int32_t i = 0; i < buSize; i++) {
    TSKEY ts1 = *(TSKEY*)taosArrayGet(pSU7->pTsBuckets, i);
    TSKEY ts2 = *(TSKEY*)taosArrayGet(pSU6->pTsBuckets, i);
    GTEST_ASSERT_EQ(ts1, ts2);
  }
  int32_t lSize = taosArrayGetSize(pSU7->pTsSBFs);
  int32_t rSize = taosArrayGetSize(pSU6->pTsSBFs);
  GTEST_ASSERT_EQ(lSize, rSize);
  for (int32_t i = 0; i < lSize; i++) {
    SScalableBf* pLeftSBF = (SScalableBf*)taosArrayGetP(pSU7->pTsSBFs, i);
    SScalableBf* pRightSBF = (SScalableBf*)taosArrayGetP(pSU6->pTsSBFs, i);
    GTEST_ASSERT_EQ(equalSBF(pLeftSBF, pRightSBF), true);
  }
R
root 已提交
184 185 186 187 188

  updateInfoDestroy(pSU);
  updateInfoDestroy(pSU1);
  updateInfoDestroy(pSU2);
  updateInfoDestroy(pSU3);
5
54liuyao 已提交
189 190
  updateInfoDestroy(pSU4);
  updateInfoDestroy(pSU5);
5
54liuyao 已提交
191 192 193
  updateInfoDestroy(pSU6);
  updateInfoDestroy(pSU7);

R
root 已提交
194 195 196 197 198 199
}

int main(int argc, char* argv[]) {
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}