index.c 6.6 KB
Newer Older
H
refact  
Hongze Cheng 已提交
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/>.
 */

dengyihao's avatar
dengyihao 已提交
16 17
#include "index.h"
#include "indexInt.h"
dengyihao's avatar
dengyihao 已提交
18
#include "index_cache.h"
H
refact  
Hongze Cheng 已提交
19

dengyihao's avatar
dengyihao 已提交
20 21
#ifdef USE_LUCENE
#include "lucene++/Lucene_c.h"
H
refact  
Hongze Cheng 已提交
22 23
#endif

dengyihao's avatar
dengyihao 已提交
24

dengyihao's avatar
dengyihao 已提交
25
typedef struct SIdxFieldInfo {
dengyihao's avatar
dengyihao 已提交
26 27
  int fieldId;     // generated by index internal  
  int cVersion;
dengyihao's avatar
dengyihao 已提交
28 29 30 31
  int type; // field type 
} SIdxFieldInfo; 

static pthread_once_t isInit = PTHREAD_ONCE_INIT;
dengyihao's avatar
dengyihao 已提交
32 33
static void indexInit();

dengyihao's avatar
dengyihao 已提交
34 35 36 37 38 39 40
static int indexMergeCacheIntoTindex(struct SIndex *sIdx) {
  if (sIdx == NULL) {
    return -1; 
  }
  indexWarn("suid %" PRIu64 " merge cache into tindex", sIdx->suid); 
  return 0;
}
dengyihao's avatar
dengyihao 已提交
41 42
SIndex *indexOpen(SIndexOpts *opts, const char *path) {
  pthread_once(&isInit, indexInit);
dengyihao's avatar
dengyihao 已提交
43
  SIndex *sIdx = calloc(1, sizeof(SIndex));
dengyihao's avatar
dengyihao 已提交
44

dengyihao's avatar
dengyihao 已提交
45 46
#ifdef USE_LUCENE  
  index_t *index = index_open(path);      
dengyihao's avatar
dengyihao 已提交
47
  sIdx->index = index;
dengyihao's avatar
dengyihao 已提交
48
#endif
dengyihao's avatar
dengyihao 已提交
49 50 51 52
  
  sIdx->cache    = (void*)indexCacheCreate();     
  sIdx->tindex   = NULL;
  sIdx->fieldObj = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
dengyihao's avatar
dengyihao 已提交
53 54
  sIdx->fieldId  = 1; 
  sIdx->cVersion = 1; 
dengyihao's avatar
dengyihao 已提交
55 56
  pthread_mutex_init(&sIdx->mtx, NULL);
  return sIdx;  
H
refact  
Hongze Cheng 已提交
57
}
dengyihao's avatar
dengyihao 已提交
58

dengyihao's avatar
dengyihao 已提交
59
void indexClose(SIndex *sIdx) {
dengyihao's avatar
dengyihao 已提交
60
#ifdef USE_LUCENE  
dengyihao's avatar
dengyihao 已提交
61 62
  index_close(sIdex->index); 
  sIdx->index = NULL;
H
refact  
Hongze Cheng 已提交
63
#endif
dengyihao's avatar
dengyihao 已提交
64 65 66 67
  indexCacheDestroy(sIdx->cache);
  taosHashCleanup(sIdx->fieldObj); 
  pthread_mutex_destroy(&sIdx->mtx);
  free(sIdx);  
dengyihao's avatar
dengyihao 已提交
68 69
  return;
}
dengyihao's avatar
dengyihao 已提交
70

dengyihao's avatar
dengyihao 已提交
71
int indexPut(SIndex *index, SArray* fVals, int uid) {
dengyihao's avatar
dengyihao 已提交
72
  
dengyihao's avatar
dengyihao 已提交
73 74 75 76 77 78
#ifdef USE_LUCENE 
    index_document_t *doc = index_document_create(); 

    char buf[16] = {0};
    sprintf(buf, "%d", uid);
    
dengyihao's avatar
dengyihao 已提交
79 80
    for (int i = 0; i < taosArrayGetSize(fVals); i++) {
      SIndexTerm *p = taosArrayGetP(fVals, i); 
dengyihao's avatar
dengyihao 已提交
81 82 83 84 85 86 87
      index_document_add(doc, (const char *)(p->key), p->nKey, (const char *)(p->val), p->nVal, 1); 
    }
    index_document_add(doc, NULL, 0, buf, strlen(buf), 0);

    index_put(index->index, doc);
    index_document_destroy(doc);
#endif
dengyihao's avatar
dengyihao 已提交
88 89

   //TODO(yihao): reduce the lock range 
dengyihao's avatar
dengyihao 已提交
90
  pthread_mutex_lock(&index->mtx);   
dengyihao's avatar
dengyihao 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  for (int i = 0; i < taosArrayGetSize(fVals); i++) {
    SIndexTerm     *p = taosArrayGetP(fVals, i);
    SIdxFieldInfo *fi = taosHashGet(index->fieldObj, p->key, p->nKey);
    if (fi == NULL) {
      SIdxFieldInfo tfi = {.fieldId = index->fieldId, .type = p->type};
      index->cVersion++; 
      index->fieldId++;
      taosHashPut(index->fieldObj, p->key, p->nKey, &tfi, sizeof(tfi)); 
    } else {
      //TODO, del  
    }
  }   

  for (int i = 0; i < taosArrayGetSize(fVals); i++) {
    SIndexTerm     *p = taosArrayGetP(fVals, i);
    SIdxFieldInfo *fi = taosHashGet(index->fieldObj, p->key, p->nKey);
    assert(fi !=  NULL); 
    int32_t fieldId = fi->fieldId; 
    int32_t colType = fi->type;  
    int32_t version = index->cVersion;
     
  }
dengyihao's avatar
dengyihao 已提交
113
  pthread_mutex_unlock(&index->mtx);
dengyihao's avatar
dengyihao 已提交
114 115
  return 1;
}
dengyihao's avatar
dengyihao 已提交
116 117
int indexSearch(SIndex *index, SIndexMultiTermQuery *multiQuerys, SArray *result) {
#ifdef USE_LUCENE 
dengyihao's avatar
dengyihao 已提交
118 119 120 121 122 123 124 125
  EIndexOperatorType opera = multiQuerys->opera; 

  int nQuery = taosArrayGetSize(multiQuerys->query); 
  char **fields = malloc(sizeof(char *) * nQuery);
  char **keys   = malloc(sizeof(char *) * nQuery); 
  int  *types = malloc(sizeof(int)    * nQuery);

  for (int i = 0; i < nQuery; i++) {
dengyihao's avatar
dengyihao 已提交
126 127
     SIndexTermQuery *p    = taosArrayGet(multiQuerys->query, i); 
     SIndexTerm      *term = p->field_value; 
dengyihao's avatar
dengyihao 已提交
128 129 130 131 132 133 134

     fields[i]  = calloc(1, term->nKey + 1); 
     keys[i]    = calloc(1, term->nVal + 1);

     memcpy(fields[i], term->key, term->nKey);
     memcpy(keys[i],   term->val, term->nVal);
     types[i] = (int)(p->type);
dengyihao's avatar
dengyihao 已提交
135
  }    
dengyihao's avatar
dengyihao 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  int *tResult = NULL; 
  int tsz= 0;
  index_multi_search(index->index, (const char **)fields, (const char **)keys, types, nQuery, opera, &tResult, &tsz);
  
  for (int i = 0; i < tsz; i++) {
    taosArrayPush(result, &tResult[i]);
  }

  for (int i = 0; i < nQuery; i++) {
    free(fields[i]);
    free(keys[i]);
  }
  free(fields);
  free(keys);
  free(types);
dengyihao's avatar
dengyihao 已提交
151
#endif
dengyihao's avatar
dengyihao 已提交
152 153 154
  return 1;
}

dengyihao's avatar
dengyihao 已提交
155

dengyihao's avatar
dengyihao 已提交
156
int indexDelete(SIndex *index, SIndexMultiTermQuery *query) {
dengyihao's avatar
dengyihao 已提交
157
    
dengyihao's avatar
dengyihao 已提交
158 159 160 161
  return 1;
}
int indexRebuild(SIndex *index, SIndexOpts *opts);

H
refact  
Hongze Cheng 已提交
162

dengyihao's avatar
dengyihao 已提交
163
SIndexOpts *indexOptsCreate() {
dengyihao's avatar
dengyihao 已提交
164 165 166
#ifdef USE_LUCENE 
#endif
return NULL;
dengyihao's avatar
dengyihao 已提交
167 168
}
void indexOptsDestroy(SIndexOpts *opts) {
dengyihao's avatar
dengyihao 已提交
169 170
#ifdef USE_LUCENE 
#endif
dengyihao's avatar
dengyihao 已提交
171 172 173 174 175 176
}
/*
 * @param: oper 
 *
*/

dengyihao's avatar
dengyihao 已提交
177 178 179 180
SIndexMultiTermQuery *indexMultiTermQueryCreate(EIndexOperatorType opera) {
  SIndexMultiTermQuery *p = (SIndexMultiTermQuery *)malloc(sizeof(SIndexMultiTermQuery));
  if (p == NULL) { return NULL; }
  p->opera  = opera; 
dengyihao's avatar
dengyihao 已提交
181
  p->query = taosArrayInit(4, sizeof(SIndexTermQuery)); 
dengyihao's avatar
dengyihao 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
  return p;
}
void indexMultiTermQueryDestroy(SIndexMultiTermQuery *pQuery) {
  for (int i = 0; i < taosArrayGetSize(pQuery->query); i++) {
    SIndexTermQuery *p = (SIndexTermQuery *)taosArrayGet(pQuery->query, i);
    indexTermDestroy(p->field_value);
  }
  taosArrayDestroy(pQuery->query);     
  free(pQuery);
};
int indexMultiTermQueryAdd(SIndexMultiTermQuery *pQuery, const char *field, int32_t nFields, const char *value, int32_t nValue, EIndexQueryType type){
  SIndexTerm *t = indexTermCreate(field, nFields, value, nValue);  
  if (t == NULL) {return -1;}
  SIndexTermQuery q = {.type = type, .field_value = t};   
  taosArrayPush(pQuery->query, &q);
  return 0;
dengyihao's avatar
dengyihao 已提交
198 199
}

dengyihao's avatar
dengyihao 已提交
200 201 202 203 204 205 206 207 208 209 210

SIndexTerm *indexTermCreate(const char *key, int32_t nKey, const char *val, int32_t nVal) {
  SIndexTerm *t = (SIndexTerm *)malloc(sizeof(SIndexTerm)); 
  t->key  = (char *)calloc(nKey + 1, 1);
  memcpy(t->key, key, nKey);
  t->nKey = nKey;

  t->val = (char *)calloc(nVal + 1, 1);
  memcpy(t->val, val, nVal);
  t->nVal = nVal;
  return t;
dengyihao's avatar
dengyihao 已提交
211
}
dengyihao's avatar
dengyihao 已提交
212 213 214 215 216 217 218 219
void indexTermDestroy(SIndexTerm *p) {
  free(p->key);
  free(p->val);
  free(p);
}  

SArray *indexMultiTermCreate() {
  return taosArrayInit(4, sizeof(SIndexTerm *)); 
dengyihao's avatar
dengyihao 已提交
220 221
}

dengyihao's avatar
dengyihao 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234
int indexMultiTermAdd(SArray *array, const char *field, int32_t nField, const char *val, int32_t nVal) {
   SIndexTerm *term = indexTermCreate(field,  nField, val, nVal);  
   if (term == NULL) { return -1; }
   taosArrayPush(array, &term);
   return 0;
}
void indexMultiTermDestroy(SArray *array) {
  for (int32_t i = 0; i < taosArrayGetSize(array); i++) {
    SIndexTerm *p = taosArrayGetP(array, i);
    indexTermDestroy(p);
  }
  taosArrayDestroy(array);
}
dengyihao's avatar
dengyihao 已提交
235

dengyihao's avatar
dengyihao 已提交
236 237 238
void indexInit() {
  //do nothing
}