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 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 28
  for (uint32_t i = 0; i < sz; i++) {
    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 36
static void fstRegistryCellSwap(SArray *arr, uint32_t a, uint32_t b) {
  size_t sz = taosArrayGetSize(arr);
37 38 39
  if (a >= sz || b >= sz) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
40

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

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

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

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

static void fstRegistryCellPromote(SArray *arr, uint32_t start, uint32_t end) {
55 56 57 58 59
  size_t sz = taosArrayGetSize(arr);
  if (start >= sz && end >= sz) {
    return;
  }

dengyihao's avatar
dengyihao 已提交
60 61 62 63
  assert(start >= end);

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

70 71 72 73 74
FstRegistry *fstRegistryCreate(uint64_t tableSize, uint64_t mruSize) {
  FstRegistry *registry = malloc(sizeof(FstRegistry));
  if (registry == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
75

76 77 78 79 80
  uint64_t nCells = tableSize * mruSize;
  SArray * tb = (SArray *)taosArrayInit(nCells, sizeof(FstRegistryCell));
  if (NULL == tb) {
    free(registry);
    return NULL;
dengyihao's avatar
dengyihao 已提交
81 82
  }

dengyihao's avatar
dengyihao 已提交
83
  for (uint64_t i = 0; i < nCells; i++) {
84 85
    FstRegistryCell cell = {.addr = NONE_ADDRESS, .node = fstBuilderNodeDefault()};
    taosArrayPush(tb, &cell);
dengyihao's avatar
dengyihao 已提交
86
  }
87 88 89 90 91

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

dengyihao's avatar
dengyihao 已提交
94
void fstRegistryDestroy(FstRegistry *registry) {
95 96 97
  if (registry == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
98 99

  SArray *tb = registry->table;
100
  size_t  sz = taosArrayGetSize(tb);
dengyihao's avatar
dengyihao 已提交
101
  for (size_t i = 0; i < sz; i++) {
102 103
    FstRegistryCell *cell = taosArrayGet(tb, i);
    fstBuilderNodeDestroy(cell->node);
dengyihao's avatar
dengyihao 已提交
104 105 106 107 108
  }
  taosArrayDestroy(tb);
  free(registry);
}

dengyihao's avatar
dengyihao 已提交
109 110
FstRegistryEntry *fstRegistryGetEntry(FstRegistry *registry, FstBuilderNode *bNode) {
  if (taosArrayGetSize(registry->table) <= 0) {
111 112
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
113
  uint64_t bucket = fstRegistryHash(registry, bNode);
114 115 116
  uint64_t start = registry->mruSize * bucket;
  uint64_t end = start + registry->mruSize;

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

    fstRegistryCellSwap(registry->table, start, start + 1);
    FstRegistryCell *cCell = taosArrayGet(registry->table, start);
150 151
    entry->state = NOTFOUND;
    entry->cell = cCell;
dengyihao's avatar
dengyihao 已提交
152
  } else {
153
    uint32_t i = start;
dengyihao's avatar
dengyihao 已提交
154 155
    for (; i < end; i++) {
      FstRegistryCell *cell = (FstRegistryCell *)taosArrayGet(registry->table, i);
dengyihao's avatar
dengyihao 已提交
156
      if (cell->addr != NONE_ADDRESS && fstBuilderNodeEqual(cell->node, bNode)) {
157 158
        entry->state = FOUND;
        entry->addr = cell->addr;
dengyihao's avatar
dengyihao 已提交
159 160 161
        fstRegistryCellPromote(registry->table, i, start);
        break;
      }
162
    }
dengyihao's avatar
dengyihao 已提交
163
    if (i >= end) {
164 165 166 167
      uint64_t         last = end - 1;
      FstRegistryCell *cell = (FstRegistryCell *)taosArrayGet(registry->table, last);
      // clone from bNode, refactor later
      fstBuilderNodeCloneFrom(cell->node, bNode);
dengyihao's avatar
dengyihao 已提交
168 169 170

      fstRegistryCellPromote(registry->table, last, start);
      FstRegistryCell *cCell = taosArrayGet(registry->table, start);
171 172
      entry->state = NOTFOUND;
      entry->cell = cCell;
dengyihao's avatar
dengyihao 已提交
173
    }
174
  }
dengyihao's avatar
dengyihao 已提交
175 176
  return entry;
}
177
void fstRegistryEntryDestroy(FstRegistryEntry *entry) { free(entry); }