tsdbCacheRead.c 5.7 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 22 23 24
typedef struct SLastrowReader {
  SVnode*   pVnode;
  STSchema* pSchema;
  uint64_t  uid;
25 26 27 28 29 30
  //  int32_t*  pSlotIds;
  char**  transferBuf;  // todo remove it soon
  int32_t numOfCols;
  int32_t type;
  int32_t tableIndex;  // currently returned result tables
  SArray* pTableList;  // table id list
31 32
} SLastrowReader;

33
static void saveOneRow(STSRow* pRow, SSDataBlock* pBlock, SLastrowReader* pReader, const int32_t* slotIds) {
34 35 36
  int32_t numOfRows = pBlock->info.rows;

  SColVal colVal = {0};
37
  for (int32_t i = 0; i < pReader->numOfCols; ++i) {
38 39
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);

40 41 42 43 44 45
    if (slotIds[i] == -1) {
      colDataAppend(pColInfoData, numOfRows, (const char*)&pRow->ts, false);
    } else {
      tTSRowGetVal(pRow, pReader->pSchema, slotIds[i], &colVal);

      if (IS_VAR_DATA_TYPE(colVal.type)) {
46
        if (colVal.isNull || colVal.isNone) {
47 48 49 50 51 52 53
          colDataAppendNULL(pColInfoData, numOfRows);
        } else {
          varDataSetLen(pReader->transferBuf[i], colVal.value.nData);
          memcpy(varDataVal(pReader->transferBuf[i]), colVal.value.pData, colVal.value.nData);
          colDataAppend(pColInfoData, numOfRows, pReader->transferBuf[i], false);
        }
      } else {
54
        colDataAppend(pColInfoData, numOfRows, (const char*)&colVal.value, colVal.isNull || colVal.isNone);
55 56
      }
    }
57 58 59 60 61
  }

  pBlock->info.rows += 1;
}

62
int32_t tsdbLastRowReaderOpen(void* pVnode, int32_t type, SArray* pTableIdList, int32_t numOfCols, void** pReader) {
63 64 65 66 67
  SLastrowReader* p = taosMemoryCalloc(1, sizeof(SLastrowReader));
  if (p == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

68 69 70
  p->type = type;
  p->pVnode = pVnode;
  p->numOfCols = numOfCols;
71 72 73
  p->transferBuf = taosMemoryCalloc(p->numOfCols, POINTER_BYTES);

  STableKeyInfo* pKeyInfo = taosArrayGet(pTableIdList, 0);
74 75
  p->pSchema = metaGetTbTSchema(p->pVnode->pMeta, pKeyInfo->uid, -1);
  p->pTableList = pTableIdList;
76

M
Minglei Jin 已提交
77 78
  for (int32_t i = 0; i < p->numOfCols; ++i) {
    if (IS_VAR_DATA_TYPE(p->pSchema->columns[i].type)) {
79
      p->transferBuf[i] = taosMemoryMalloc(p->pSchema->columns[i].bytes);
80 81
    }
  }
82

83 84 85 86 87 88 89
  *pReader = p;
  return TSDB_CODE_SUCCESS;
}

int32_t tsdbLastrowReaderClose(void* pReader) {
  SLastrowReader* p = pReader;

90
  for (int32_t i = 0; i < p->numOfCols; ++i) {
91 92 93 94 95 96 97 98 99 100
    taosMemoryFreeClear(p->transferBuf[i]);
  }

  taosMemoryFree(p->transferBuf);
  taosMemoryFree(pReader);
  return TSDB_CODE_SUCCESS;
}

int32_t tsdbRetrieveLastRow(void* pReader, SSDataBlock* pResBlock, const int32_t* slotIds) {
  if (pReader == NULL || pResBlock == NULL) {
101 102 103
    return TSDB_CODE_INVALID_PARA;
  }

104 105
  SLastrowReader* pr = pReader;

106
  SLRUCache* lruCache = pr->pVnode->pTsdb->lruCache;
107 108 109
  LRUHandle* h = NULL;
  STSRow*    pRow = NULL;
  size_t     numOfTables = taosArrayGetSize(pr->pTableList);
110 111

  // retrieve the only one last row of all tables in the uid list.
112
  if (pr->type == LASTROW_RETRIEVE_TYPE_SINGLE) {
113 114 115
    int64_t lastKey = INT64_MIN;
    bool    internalResult = false;
    for (int32_t i = 0; i < numOfTables; ++i) {
116
      STableKeyInfo* pKeyInfo = taosArrayGet(pr->pTableList, i);
117

118
      int32_t code = tsdbCacheGetLastrowH(lruCache, pKeyInfo->uid, pr->pVnode->pTsdb, &h);
119
      // int32_t code = tsdbCacheGetLastH(lruCache, pKeyInfo->uid, pr->pVnode->pTsdb, &h);
120 121 122 123
      if (code != TSDB_CODE_SUCCESS) {
        return code;
      }

124
      if (h == NULL) {
125 126 127
        continue;
      }

128
      pRow = (STSRow*)taosLRUCacheValue(lruCache, h);
129 130
      // SArray* pLast = (SArray*)taosLRUCacheValue(lruCache, h);
      // tsdbCacheLastArray2Row(pLast, &pRow, pr->pSchema);
131 132 133 134 135 136 137
      if (pRow->ts > lastKey) {
        // Set result row into the same rowIndex repeatly, so we need to check if the internal result row has already
        // appended or not.
        if (internalResult) {
          pResBlock->info.rows -= 1;
        }

138
        saveOneRow(pRow, pResBlock, pr, slotIds);
139 140 141
        internalResult = true;
        lastKey = pRow->ts;
      }
142

143
      // taosMemoryFree(pRow);
144
      tsdbCacheRelease(lruCache, h);
145
    }
146 147 148
  } else if (pr->type == LASTROW_RETRIEVE_TYPE_ALL) {
    for (int32_t i = pr->tableIndex; i < numOfTables; ++i) {
      STableKeyInfo* pKeyInfo = taosArrayGet(pr->pTableList, i);
149

150
      int32_t code = tsdbCacheGetLastrowH(lruCache, pKeyInfo->uid, pr->pVnode->pTsdb, &h);
151
      // int32_t code = tsdbCacheGetLastH(lruCache, pKeyInfo->uid, pr->pVnode->pTsdb, &h);
152 153 154 155 156
      if (code != TSDB_CODE_SUCCESS) {
        return code;
      }

      // no data in the table of Uid
157
      if (h == NULL) {
158 159 160
        continue;
      }

161
      pRow = (STSRow*)taosLRUCacheValue(lruCache, h);
162 163 164
      // SArray* pLast = (SArray*)taosLRUCacheValue(lruCache, h);
      // tsdbCacheLastArray2Row(pLast, &pRow, pr->pSchema);

165 166
      saveOneRow(pRow, pResBlock, pr, slotIds);

167
      // taosMemoryFree(pRow);
168
      tsdbCacheRelease(lruCache, h);
169

170 171 172 173
      pr->tableIndex += 1;
      if (pResBlock->info.rows >= pResBlock->info.capacity) {
        return TSDB_CODE_SUCCESS;
      }
174 175 176 177 178 179 180
    }
  } else {
    return TSDB_CODE_INVALID_PARA;
  }

  return TSDB_CODE_SUCCESS;
}