tsdbCacheRead.c 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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 "taoserror.h"
#include "tarray.h"
#include "tcommon.h"
#include "tsdb.h"

21
#define HASTYPE(_type, _t) (((_type) & (_t)) == (_t))
22

H
Haojun Liao 已提交
23 24
static int32_t saveOneRow(SArray* pRow, SSDataBlock* pBlock, SCacheRowsReader* pReader, const int32_t* slotIds,
                          void** pRes, const char* idStr) {
25 26
  int32_t numOfRows = pBlock->info.rows;

27
  if (HASTYPE(pReader->type, CACHESCAN_RETRIEVE_LAST)) {
28 29
    bool allNullRow = true;

30 31 32
    for (int32_t i = 0; i < pReader->numOfCols; ++i) {
      SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
      SFirstLastRes*   p = (SFirstLastRes*)varDataVal(pRes[i]);
33

34 35 36 37 38
      if (slotIds[i] == -1) {  // the primary timestamp
        SLastCol* pColVal = (SLastCol*)taosArrayGet(pRow, 0);
        p->ts = pColVal->ts;
        p->bytes = TSDB_KEYSIZE;
        *(int64_t*)p->buf = pColVal->ts;
39
        allNullRow = false;
40 41 42 43 44 45
      } else {
        int32_t   slotId = slotIds[i];
        SLastCol* pColVal = (SLastCol*)taosArrayGet(pRow, slotId);

        p->ts = pColVal->ts;
        p->isNull = !COL_VAL_IS_VALUE(&pColVal->colVal);
46 47
        allNullRow = p->isNull & allNullRow;

48 49 50 51
        if (!p->isNull) {
          if (IS_VAR_DATA_TYPE(pColVal->colVal.type)) {
            varDataSetLen(p->buf, pColVal->colVal.value.nData);
            memcpy(varDataVal(p->buf), pColVal->colVal.value.pData, pColVal->colVal.value.nData);
52
            p->bytes = pColVal->colVal.value.nData + VARSTR_HEADER_SIZE;  // binary needs to plus the header size
53 54 55 56 57 58
          } else {
            memcpy(p->buf, &pColVal->colVal.value, pReader->pSchema->columns[slotId].bytes);
            p->bytes = pReader->pSchema->columns[slotId].bytes;
          }
        }
      }
59

60
      // pColInfoData->info.bytes includes the VARSTR_HEADER_SIZE, need to substruct it
61
      p->hasResult = true;
62
      varDataSetLen(pRes[i], pColInfoData->info.bytes - VARSTR_HEADER_SIZE);
63 64
      colDataAppend(pColInfoData, numOfRows, (const char*)pRes[i], false);
    }
65

66
    pBlock->info.rows += allNullRow ? 0 : 1;
H
Haojun Liao 已提交
67
  } else if (HASTYPE(pReader->type, CACHESCAN_RETRIEVE_LAST_ROW)) {
68 69 70 71 72 73 74 75 76
    for (int32_t i = 0; i < pReader->numOfCols; ++i) {
      SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);

      if (slotIds[i] == -1) {
        SLastCol* pColVal = (SLastCol*)taosArrayGet(pRow, 0);
        colDataAppend(pColInfoData, numOfRows, (const char*)&pColVal->ts, false);
      } else {
        int32_t   slotId = slotIds[i];
        SLastCol* pColVal = (SLastCol*)taosArrayGet(pRow, slotId);
77
        SColVal*  pVal = &pColVal->colVal;
78

79
        if (IS_VAR_DATA_TYPE(pColVal->colVal.type)) {
80 81 82
          if (!COL_VAL_IS_VALUE(&pColVal->colVal)) {
            colDataAppendNULL(pColInfoData, numOfRows);
          } else {
83 84
            varDataSetLen(pReader->transferBuf[slotId], pVal->value.nData);
            memcpy(varDataVal(pReader->transferBuf[slotId]), pVal->value.pData, pVal->value.nData);
85 86
            colDataAppend(pColInfoData, numOfRows, pReader->transferBuf[slotId], false);
          }
87
        } else {
88
          colDataAppend(pColInfoData, numOfRows, (const char*)&pVal->value.val, !COL_VAL_IS_VALUE(pVal));
89 90 91
        }
      }
    }
92

93
    pBlock->info.rows += 1;
H
Haojun Liao 已提交
94 95 96
  } else {
    tsdbError("invalid retrieve type:%d, %s", pReader->type, idStr);
    return TSDB_CODE_INVALID_PARA;
97
  }
H
Haojun Liao 已提交
98 99

  return TSDB_CODE_SUCCESS;
100 101
}

102
int32_t tsdbCacherowsReaderOpen(void* pVnode, int32_t type, void* pTableIdList, int32_t numOfTables, int32_t numOfCols,
H
Haojun Liao 已提交
103
                                uint64_t suid, void** pReader, const char* idstr) {
104 105
  *pReader = NULL;
  SCacheRowsReader* p = taosMemoryCalloc(1, sizeof(SCacheRowsReader));
106 107 108 109
  if (p == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

110 111 112
  p->type = type;
  p->pVnode = pVnode;
  p->numOfCols = numOfCols;
113
  p->suid = suid;
114

115
  if (numOfTables == 0) {
116 117 118 119
    *pReader = p;
    return TSDB_CODE_SUCCESS;
  }

120
  STableKeyInfo* pKeyInfo = &((STableKeyInfo*)pTableIdList)[0];
121
  p->pSchema = metaGetTbTSchema(p->pVnode->pMeta, pKeyInfo->uid, -1, 1);
122
  p->pTableList = pTableIdList;
123
  p->numOfTables = numOfTables;
124

125
  p->transferBuf = taosMemoryCalloc(p->pSchema->numOfCols, POINTER_BYTES);
126
  if (p->transferBuf == NULL) {
H
Haojun Liao 已提交
127
    tsdbCacherowsReaderClose(p);
H
Haojun Liao 已提交
128
    return TSDB_CODE_OUT_OF_MEMORY;
129 130
  }

131
  for (int32_t i = 0; i < p->pSchema->numOfCols; ++i) {
M
Minglei Jin 已提交
132
    if (IS_VAR_DATA_TYPE(p->pSchema->columns[i].type)) {
133
      p->transferBuf[i] = taosMemoryMalloc(p->pSchema->columns[i].bytes);
134 135 136 137
      if (p->transferBuf[i] == NULL) {
        tsdbCacherowsReaderClose(p);
        return TSDB_CODE_OUT_OF_MEMORY;
      }
138 139
    }
  }
140

141 142 143 144 145 146
  p->pLoadInfo = tCreateLastBlockLoadInfo(p->pSchema, NULL, 0);
  if (p->pLoadInfo == NULL) {
    tsdbCacherowsReaderClose(p);
    return TSDB_CODE_OUT_OF_MEMORY;
  }

H
Haojun Liao 已提交
147 148
  p->idstr = taosMemoryStrDup(idstr);

149 150 151 152
  *pReader = p;
  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
153
void* tsdbCacherowsReaderClose(void* pReader) {
154
  SCacheRowsReader* p = pReader;
155

156 157 158 159 160 161 162
  if (p->pSchema != NULL) {
    for (int32_t i = 0; i < p->pSchema->numOfCols; ++i) {
      taosMemoryFreeClear(p->transferBuf[i]);
    }

    taosMemoryFree(p->transferBuf);
    taosMemoryFree(p->pSchema);
163 164
  }

165 166
  destroyLastBlockLoadInfo(p->pLoadInfo);

K
kailixu 已提交
167
  taosMemoryFree((void*)p->idstr);
168
  taosMemoryFree(pReader);
H
Haojun Liao 已提交
169
  return NULL;
170 171
}

H
Haojun Liao 已提交
172
static int32_t doExtractCacheRow(SCacheRowsReader* pr, SLRUCache* lruCache, uint64_t uid, SArray** pRow,
H
Haojun Liao 已提交
173
                                 LRUHandle** h) {
174
  int32_t code = TSDB_CODE_SUCCESS;
175
  *pRow = NULL;
176

H
Haojun Liao 已提交
177
  if (HASTYPE(pr->type, CACHESCAN_RETRIEVE_LAST_ROW)) {
178
    code = tsdbCacheGetLastrowH(lruCache, uid, pr, h);
179
  } else {
180
    code = tsdbCacheGetLastH(lruCache, uid, pr, h);
181
  }
182

183 184 185 186 187 188 189
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }

  // no data in the table of Uid
  if (*h != NULL) {
    *pRow = (SArray*)taosLRUCacheValue(lruCache, *h);
190 191 192 193 194
  }

  return code;
}

195
static void freeItem(void* pItem) {
196
  SLastCol* pCol = (SLastCol*)pItem;
197 198 199 200 201
  if (IS_VAR_DATA_TYPE(pCol->colVal.type)) {
    taosMemoryFree(pCol->colVal.value.pData);
  }
}

202
int32_t tsdbRetrieveCacheRows(void* pReader, SSDataBlock* pResBlock, const int32_t* slotIds, SArray* pTableUidList) {
203
  if (pReader == NULL || pResBlock == NULL) {
204 205 206
    return TSDB_CODE_INVALID_PARA;
  }

207
  SCacheRowsReader* pr = pReader;
208

209
  int32_t    code = TSDB_CODE_SUCCESS;
210
  SLRUCache* lruCache = pr->pVnode->pTsdb->lruCache;
211
  LRUHandle* h = NULL;
H
Haojun Liao 已提交
212
  SArray*    pRow = NULL;
213
  bool       hasRes = false;
214
  SArray*    pLastCols = NULL;
215

216
  void** pRes = taosMemoryCalloc(pr->numOfCols, POINTER_BYTES);
217 218 219 220 221
  if (pRes == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _end;
  }

222
  for (int32_t j = 0; j < pr->numOfCols; ++j) {
223 224 225 226 227
    pRes[j] = taosMemoryCalloc(1, sizeof(SFirstLastRes) + pr->pSchema->columns[slotIds[j]].bytes + VARSTR_HEADER_SIZE);
    SFirstLastRes* p = (SFirstLastRes*)varDataVal(pRes[j]);
    p->ts = INT64_MIN;
  }

228 229 230 231 232 233
  pLastCols = taosArrayInit(pr->pSchema->numOfCols, sizeof(SLastCol));
  if (pLastCols == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _end;
  }

234
  for (int32_t i = 0; i < pr->pSchema->numOfCols; ++i) {
235
    struct STColumn* pCol = &pr->pSchema->columns[i];
236
    SLastCol         p = {.ts = INT64_MIN, .colVal.type = pCol->type};
237 238 239 240 241

    if (IS_VAR_DATA_TYPE(pCol->type)) {
      p.colVal.value.pData = taosMemoryCalloc(pCol->bytes, sizeof(char));
    }
    taosArrayPush(pLastCols, &p);
H
Haojun Liao 已提交
242
  }
243

K
kailixu 已提交
244 245 246 247 248
  code = tsdbTakeReadSnap(pr->pVnode->pTsdb, &pr->pReadSnap, "cache-l");
  if (code != TSDB_CODE_SUCCESS) {
    goto _end;
  }

249
  pr->pDataFReader = NULL;
250
  pr->pDataFReaderLast = NULL;
251

252
  // retrieve the only one last row of all tables in the uid list.
H
Haojun Liao 已提交
253
  if (HASTYPE(pr->type, CACHESCAN_RETRIEVE_TYPE_SINGLE)) {
254 255
    for (int32_t i = 0; i < pr->numOfTables; ++i) {
      STableKeyInfo* pKeyInfo = &pr->pTableList[i];
256

257
      code = doExtractCacheRow(pr, lruCache, pKeyInfo->uid, &pRow, &h);
H
Haojun Liao 已提交
258
      if (code != TSDB_CODE_SUCCESS) {
K
kailixu 已提交
259
        goto _end;
260 261
      }

262
      if (h == NULL) {
263 264 265
        continue;
      }

H
Haojun Liao 已提交
266 267
      {
        for (int32_t k = 0; k < pr->numOfCols; ++k) {
268 269 270 271 272
          int32_t slotId = slotIds[k];

          if (slotId == -1) {  // the primary timestamp
            SLastCol* p = taosArrayGet(pLastCols, 0);
            SLastCol* pCol = (SLastCol*)taosArrayGet(pRow, 0);
273
            if (pCol->ts > p->ts) {
274
              hasRes = true;
275 276
              p->ts = pCol->ts;
              p->colVal = pCol->colVal;
H
Haojun Liao 已提交
277 278 279 280 281 282 283 284 285

              // only set value for last row query
              if (HASTYPE(pr->type, CACHESCAN_RETRIEVE_LAST_ROW)) {
                if (taosArrayGetSize(pTableUidList) == 0) {
                  taosArrayPush(pTableUidList, &pKeyInfo->uid);
                } else {
                  taosArraySet(pTableUidList, 0, &pKeyInfo->uid);
                }
              }
H
Haojun Liao 已提交
286 287
            }
          } else {
288
            SLastCol* p = taosArrayGet(pLastCols, slotId);
H
Haojun Liao 已提交
289 290
            SLastCol* pColVal = (SLastCol*)taosArrayGet(pRow, slotId);

291
            if (pColVal->ts > p->ts) {
292 293 294 295
              if (!COL_VAL_IS_VALUE(&pColVal->colVal) && HASTYPE(pr->type, CACHESCAN_RETRIEVE_LAST)) {
                continue;
              }

296
              hasRes = true;
297
              p->ts = pColVal->ts;
H
Haojun Liao 已提交
298

H
Haojun Liao 已提交
299 300 301 302 303 304
              if (!IS_VAR_DATA_TYPE(pColVal->colVal.type)) {
                p->colVal = pColVal->colVal;
              } else {
                if (COL_VAL_IS_VALUE(&pColVal->colVal)) {
                  memcpy(p->colVal.value.pData, pColVal->colVal.value.pData, pColVal->colVal.value.nData);
                }
H
Haojun Liao 已提交
305

H
Haojun Liao 已提交
306 307 308 309
                p->colVal.value.nData = pColVal->colVal.value.nData;
                p->colVal.type = pColVal->colVal.type;
                p->colVal.flag = pColVal->colVal.flag;
                p->colVal.cid = pColVal->colVal.cid;
H
Haojun Liao 已提交
310 311 312
              }
            }
          }
313 314
        }
      }
315

316
      tsdbCacheRelease(lruCache, h);
317
    }
318 319

    if (hasRes) {
H
Haojun Liao 已提交
320
      saveOneRow(pLastCols, pResBlock, pr, slotIds, pRes, pr->idstr);
321 322
    }

H
Haojun Liao 已提交
323
  } else if (HASTYPE(pr->type, CACHESCAN_RETRIEVE_TYPE_ALL)) {
324 325
    for (int32_t i = pr->tableIndex; i < pr->numOfTables; ++i) {
      STableKeyInfo* pKeyInfo = &pr->pTableList[i];
326
      code = doExtractCacheRow(pr, lruCache, pKeyInfo->uid, &pRow, &h);
H
Haojun Liao 已提交
327
      if (code != TSDB_CODE_SUCCESS) {
K
kailixu 已提交
328
        goto _end;
329 330
      }

331
      if (h == NULL) {
332 333 334
        continue;
      }

H
Haojun Liao 已提交
335
      saveOneRow(pRow, pResBlock, pr, slotIds, pRes, pr->idstr);
336
      // TODO reset the pRes
337

338
      taosArrayPush(pTableUidList, &pKeyInfo->uid);
339
      tsdbCacheRelease(lruCache, h);
340

341 342
      pr->tableIndex += 1;
      if (pResBlock->info.rows >= pResBlock->info.capacity) {
343
        goto _end;
344
      }
345 346
    }
  } else {
347
    code = TSDB_CODE_INVALID_PARA;
348 349
  }

350
_end:
351
  tsdbDataFReaderClose(&pr->pDataFReaderLast);
352 353 354
  tsdbDataFReaderClose(&pr->pDataFReader);

  tsdbUntakeReadSnap(pr->pVnode->pTsdb, pr->pReadSnap, "cache-l");
355
  resetLastBlockLoadInfo(pr->pLoadInfo);
356

357 358 359 360 361
  for (int32_t j = 0; j < pr->numOfCols; ++j) {
    taosMemoryFree(pRes[j]);
  }

  taosMemoryFree(pRes);
362
  taosArrayDestroyEx(pLastCols, freeItem);
363
  return code;
364
}