index_fst_registry.c 5.5 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
Shengliang Guan 已提交
16
#include "os.h"
dengyihao's avatar
dengyihao 已提交
17 18
#include "index_fst_registry.h"

dengyihao's avatar
dengyihao 已提交
19
uint64_t fstRegistryHash(FstRegistry* registry, FstBuilderNode* bNode) {
20
  // TODO(yihaoDeng): refactor later
dengyihao's avatar
dengyihao 已提交
21
  const uint64_t FNV_PRIME = 1099511628211;
22
  uint64_t       h = 14695981039346656037u;
dengyihao's avatar
dengyihao 已提交
23

24
  h = (h ^ (uint64_t)bNode->isFinal) * FNV_PRIME;
dengyihao's avatar
dengyihao 已提交
25 26
  h = (h ^ (bNode)->finalOutput) * FNV_PRIME;

27
  uint32_t sz = (uint32_t)taosArrayGetSize(bNode->trans);
dengyihao's avatar
dengyihao 已提交
28
  for (uint32_t i = 0; i < sz; i++) {
dengyihao's avatar
dengyihao 已提交
29
    FstTransition* trn = taosArrayGet(bNode->trans, i);
30 31 32 33 34
    h = (h ^ (uint64_t)(trn->inp)) * FNV_PRIME;
    h = (h ^ (uint64_t)(trn->out)) * FNV_PRIME;
    h = (h ^ (uint64_t)(trn->addr)) * FNV_PRIME;
  }
  return h % (registry->tableSize);
dengyihao's avatar
dengyihao 已提交
35
}
dengyihao's avatar
dengyihao 已提交
36
static void fstRegistryCellSwap(SArray* arr, uint32_t a, uint32_t b) {
dengyihao's avatar
dengyihao 已提交
37
  size_t sz = taosArrayGetSize(arr);
dengyihao's avatar
dengyihao 已提交
38
  if (a >= sz || b >= sz) { return; }
dengyihao's avatar
dengyihao 已提交
39

dengyihao's avatar
dengyihao 已提交
40 41
  FstRegistryCell* cell1 = (FstRegistryCell*)taosArrayGet(arr, a);
  FstRegistryCell* cell2 = (FstRegistryCell*)taosArrayGet(arr, b);
dengyihao's avatar
dengyihao 已提交
42

43
  FstRegistryCell t = {.addr = cell1->addr, .node = cell1->node};
dengyihao's avatar
dengyihao 已提交
44 45 46 47 48 49 50 51 52

  cell1->addr = cell2->addr;
  cell1->node = cell2->node;

  cell2->addr = t.addr;
  cell2->node = t.node;
  return;
}

dengyihao's avatar
dengyihao 已提交
53
static void fstRegistryCellPromote(SArray* arr, uint32_t start, uint32_t end) {
54
  size_t sz = taosArrayGetSize(arr);
dengyihao's avatar
dengyihao 已提交
55
  if (start >= sz && end >= sz) { return; }
56

dengyihao's avatar
dengyihao 已提交
57 58 59 60
  assert(start >= end);

  int32_t s = (int32_t)start;
  int32_t e = (int32_t)end;
61
  while (s > e) {
dengyihao's avatar
dengyihao 已提交
62 63 64 65
    fstRegistryCellSwap(arr, s - 1, s);
    s -= 1;
  }
}
dengyihao's avatar
dengyihao 已提交
66

dengyihao's avatar
dengyihao 已提交
67 68
FstRegistry* fstRegistryCreate(uint64_t tableSize, uint64_t mruSize) {
  FstRegistry* registry = malloc(sizeof(FstRegistry));
dengyihao's avatar
dengyihao 已提交
69
  if (registry == NULL) { return NULL; }
dengyihao's avatar
dengyihao 已提交
70

71
  uint64_t nCells = tableSize * mruSize;
dengyihao's avatar
dengyihao 已提交
72
  SArray*  tb = (SArray*)taosArrayInit(nCells, sizeof(FstRegistryCell));
73 74 75
  if (NULL == tb) {
    free(registry);
    return NULL;
dengyihao's avatar
dengyihao 已提交
76 77
  }

dengyihao's avatar
dengyihao 已提交
78
  for (uint64_t i = 0; i < nCells; i++) {
79 80
    FstRegistryCell cell = {.addr = NONE_ADDRESS, .node = fstBuilderNodeDefault()};
    taosArrayPush(tb, &cell);
dengyihao's avatar
dengyihao 已提交
81
  }
82 83 84 85 86

  registry->table = tb;
  registry->tableSize = tableSize;
  registry->mruSize = mruSize;
  return registry;
dengyihao's avatar
dengyihao 已提交
87 88
}

dengyihao's avatar
dengyihao 已提交
89
void fstRegistryDestroy(FstRegistry* registry) {
dengyihao's avatar
dengyihao 已提交
90
  if (registry == NULL) { return; }
dengyihao's avatar
dengyihao 已提交
91

dengyihao's avatar
dengyihao 已提交
92
  SArray* tb = registry->table;
93
  size_t  sz = taosArrayGetSize(tb);
dengyihao's avatar
dengyihao 已提交
94
  for (size_t i = 0; i < sz; i++) {
dengyihao's avatar
dengyihao 已提交
95
    FstRegistryCell* cell = taosArrayGet(tb, i);
96
    fstBuilderNodeDestroy(cell->node);
dengyihao's avatar
dengyihao 已提交
97 98 99 100 101
  }
  taosArrayDestroy(tb);
  free(registry);
}

dengyihao's avatar
dengyihao 已提交
102
FstRegistryEntry* fstRegistryGetEntry(FstRegistry* registry, FstBuilderNode* bNode) {
dengyihao's avatar
dengyihao 已提交
103
  if (taosArrayGetSize(registry->table) <= 0) { return NULL; }
dengyihao's avatar
dengyihao 已提交
104
  uint64_t bucket = fstRegistryHash(registry, bNode);
105 106 107
  uint64_t start = registry->mruSize * bucket;
  uint64_t end = start + registry->mruSize;

dengyihao's avatar
dengyihao 已提交
108
  FstRegistryEntry* entry = malloc(sizeof(FstRegistryEntry));
dengyihao's avatar
dengyihao 已提交
109
  if (end - start == 1) {
dengyihao's avatar
dengyihao 已提交
110
    FstRegistryCell* cell = taosArrayGet(registry->table, start);
111
    // cell->isNode &&
dengyihao's avatar
dengyihao 已提交
112
    if (cell->addr != NONE_ADDRESS && fstBuilderNodeEqual(cell->node, bNode)) {
113 114 115
      entry->state = FOUND;
      entry->addr = cell->addr;
      return entry;
dengyihao's avatar
dengyihao 已提交
116
    } else {
117 118 119
      fstBuilderNodeCloneFrom(cell->node, bNode);
      entry->state = NOTFOUND;
      entry->cell = cell;  // copy or not
dengyihao's avatar
dengyihao 已提交
120 121
    }
  } else if (end - start == 2) {
dengyihao's avatar
dengyihao 已提交
122
    FstRegistryCell* cell1 = taosArrayGet(registry->table, start);
dengyihao's avatar
dengyihao 已提交
123
    if (cell1->addr != NONE_ADDRESS && fstBuilderNodeEqual(cell1->node, bNode)) {
124 125
      entry->state = FOUND;
      entry->addr = cell1->addr;
dengyihao's avatar
dengyihao 已提交
126
      return entry;
127
    }
dengyihao's avatar
dengyihao 已提交
128
    FstRegistryCell* cell2 = taosArrayGet(registry->table, start + 1);
dengyihao's avatar
dengyihao 已提交
129
    if (cell2->addr != NONE_ADDRESS && fstBuilderNodeEqual(cell2->node, bNode)) {
130 131
      entry->state = FOUND;
      entry->addr = cell2->addr;
dengyihao's avatar
dengyihao 已提交
132
      // must swap here
133 134
      fstRegistryCellSwap(registry->table, start, start + 1);
      return entry;
dengyihao's avatar
dengyihao 已提交
135
    }
136
    // clone from bNode, refactor later
dengyihao's avatar
dengyihao 已提交
137
    fstBuilderNodeCloneFrom(cell2->node, bNode);
dengyihao's avatar
dengyihao 已提交
138 139

    fstRegistryCellSwap(registry->table, start, start + 1);
dengyihao's avatar
dengyihao 已提交
140
    FstRegistryCell* cCell = taosArrayGet(registry->table, start);
141 142
    entry->state = NOTFOUND;
    entry->cell = cCell;
dengyihao's avatar
dengyihao 已提交
143
  } else {
144
    uint32_t i = start;
dengyihao's avatar
dengyihao 已提交
145
    for (; i < end; i++) {
dengyihao's avatar
dengyihao 已提交
146
      FstRegistryCell* cell = (FstRegistryCell*)taosArrayGet(registry->table, i);
dengyihao's avatar
dengyihao 已提交
147
      if (cell->addr != NONE_ADDRESS && fstBuilderNodeEqual(cell->node, bNode)) {
148 149
        entry->state = FOUND;
        entry->addr = cell->addr;
dengyihao's avatar
dengyihao 已提交
150 151 152
        fstRegistryCellPromote(registry->table, i, start);
        break;
      }
153
    }
dengyihao's avatar
dengyihao 已提交
154
    if (i >= end) {
155
      uint64_t         last = end - 1;
dengyihao's avatar
dengyihao 已提交
156
      FstRegistryCell* cell = (FstRegistryCell*)taosArrayGet(registry->table, last);
157 158
      // clone from bNode, refactor later
      fstBuilderNodeCloneFrom(cell->node, bNode);
dengyihao's avatar
dengyihao 已提交
159 160

      fstRegistryCellPromote(registry->table, last, start);
dengyihao's avatar
dengyihao 已提交
161
      FstRegistryCell* cCell = taosArrayGet(registry->table, start);
162 163
      entry->state = NOTFOUND;
      entry->cell = cCell;
dengyihao's avatar
dengyihao 已提交
164
    }
165
  }
dengyihao's avatar
dengyihao 已提交
166 167
  return entry;
}
dengyihao's avatar
dengyihao 已提交
168 169 170
void fstRegistryEntryDestroy(FstRegistryEntry* entry) {
  free(entry);
}