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

dengyihao's avatar
dengyihao 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
uint64_t fstRegistryHash(FstRegistry *registry, FstBuilderNode *bNode) {
  //TODO(yihaoDeng): refactor later
  const uint64_t FNV_PRIME = 1099511628211;
  uint64_t h = 14695981039346656037u;

  h = (h ^ (uint64_t)bNode->isFinal) * FNV_PRIME; 
  h = (h ^ (bNode)->finalOutput) * FNV_PRIME;

  uint32_t sz = (uint32_t)taosArrayGetSize(bNode->trans); 
  for (uint32_t i = 0; i < sz; i++) {
    FstTransition *trn = taosArrayGet(bNode->trans, i);
    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
}
dengyihao's avatar
dengyihao 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
static void fstRegistryCellSwap(SArray *arr, uint32_t a, uint32_t b) {
  size_t sz = taosArrayGetSize(arr);
  if (a >= sz || b >= sz) { return; }

  FstRegistryCell *cell1 = (FstRegistryCell *)taosArrayGet(arr, a);  
  FstRegistryCell *cell2 = (FstRegistryCell *)taosArrayGet(arr, b);

  FstRegistryCell t = {.addr = cell1->addr, .node = cell1->node}; 

  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) {
  size_t sz = taosArrayGetSize(arr); 
  if (start >= sz && end >= sz) {return; }
  
  assert(start >= end);

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

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

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

dengyihao's avatar
dengyihao 已提交
79
  for (uint64_t i = 0; i < nCells; i++) {
dengyihao's avatar
dengyihao 已提交
80 81
    FstRegistryCell cell = {.addr = NONE_ADDRESS, .node = fstBuilderNodeDefault()}; 
    taosArrayPush(tb, &cell);  
dengyihao's avatar
dengyihao 已提交
82 83 84 85 86 87 88 89
  }
  
  registry->table = tb;   
  registry->tableSize = tableSize; 
  registry->mruSize   = mruSize;
  return registry;   
}

dengyihao's avatar
dengyihao 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102
void fstRegistryDestroy(FstRegistry *registry) {
  if (registry == NULL) { return; }

  SArray *tb = registry->table;
  size_t sz = taosArrayGetSize(tb);
  for (size_t i = 0; i < sz; i++) {
    FstRegistryCell *cell = taosArrayGet(tb, i); 
    fstBuilderNodeDestroy(cell->node); 
  }
  taosArrayDestroy(tb);
  free(registry);
}

dengyihao's avatar
dengyihao 已提交
103 104 105 106 107 108 109 110 111 112 113 114
FstRegistryEntry *fstRegistryGetEntry(FstRegistry *registry, FstBuilderNode *bNode) {
  if (taosArrayGetSize(registry->table) <= 0) {
    return NULL;    
  }  
  uint64_t bucket = fstRegistryHash(registry, bNode);
  uint64_t start  = registry->mruSize * bucket;
  uint64_t end    = start + registry->mruSize;
  
  FstRegistryEntry *entry = malloc(sizeof(FstRegistryEntry));
  if (end - start == 1) {
    FstRegistryCell *cell = taosArrayGet(registry->table, start); 
    //cell->isNode && 
dengyihao's avatar
dengyihao 已提交
115
    if (cell->addr != NONE_ADDRESS && fstBuilderNodeEqual(cell->node, bNode)) {
dengyihao's avatar
dengyihao 已提交
116
       entry->state = FOUND;
dengyihao's avatar
dengyihao 已提交
117
       entry->addr  = cell->addr ;
dengyihao's avatar
dengyihao 已提交
118 119
       return entry; 
    } else {
dengyihao's avatar
dengyihao 已提交
120
       fstBuilderNodeCloneFrom(cell->node, bNode);
dengyihao's avatar
dengyihao 已提交
121 122 123 124 125
       entry->state = NOTFOUND;
       entry->cell  = cell; // copy or not
    }
  } else if (end - start == 2) {
    FstRegistryCell *cell1 = taosArrayGet(registry->table, start); 
dengyihao's avatar
dengyihao 已提交
126
    if (cell1->addr != NONE_ADDRESS && fstBuilderNodeEqual(cell1->node, bNode)) {
dengyihao's avatar
dengyihao 已提交
127 128 129 130 131
      entry->state = FOUND;   
      entry->addr  = cell1->addr;
      return entry;
    }      
    FstRegistryCell *cell2 = taosArrayGet(registry->table, start + 1); 
dengyihao's avatar
dengyihao 已提交
132
    if (cell2->addr != NONE_ADDRESS && fstBuilderNodeEqual(cell2->node, bNode)) {
dengyihao's avatar
dengyihao 已提交
133 134 135 136 137 138 139
      entry->state = FOUND; 
      entry->addr  = cell2->addr;
      // must swap here
      fstRegistryCellSwap(registry->table, start, start + 1); 
      return entry; 
    }
    //clone from bNode, refactor later 
dengyihao's avatar
dengyihao 已提交
140
    fstBuilderNodeCloneFrom(cell2->node, bNode);
dengyihao's avatar
dengyihao 已提交
141 142 143 144 145 146 147 148 149

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

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