index_fst_registry.c 5.4 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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/>.
 */

#include "index_fst_registry.h"

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

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

26
  uint32_t sz = (uint32_t)taosArrayGetSize(bNode->trans);
dengyihao's avatar
dengyihao 已提交
27
  for (uint32_t i = 0; i < sz; i++) {
dengyihao's avatar
dengyihao 已提交
28
    FstTransition* trn = taosArrayGet(bNode->trans, i);
29 30 31 32 33
    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 已提交
34
}
dengyihao's avatar
dengyihao 已提交
35
static void fstRegistryCellSwap(SArray* arr, uint32_t a, uint32_t b) {
dengyihao's avatar
dengyihao 已提交
36
  size_t sz = taosArrayGetSize(arr);
dengyihao's avatar
dengyihao 已提交
37
  if (a >= sz || b >= sz) { return; }
dengyihao's avatar
dengyihao 已提交
38

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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