tsdbCacheRead.c 6.1 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
  char**    transferBuf;  // todo remove it soon
  int32_t   numOfCols;
  int32_t   type;
  int32_t   tableIndex;  // currently returned result tables
  SArray*   pTableList;  // table id list
30 31
} SLastrowReader;

32
static void saveOneRow(STSRow* pRow, SSDataBlock* pBlock, SLastrowReader* pReader, const int32_t* slotIds) {
33
  ASSERT(pReader->numOfCols <= taosArrayGetSize(pBlock->pDataBlock));
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
    if (slotIds[i] == -1) {
      colDataAppend(pColInfoData, numOfRows, (const char*)&pRow->ts, false);
    } else {
43 44 45
      int32_t slotId = slotIds[i];

      tTSRowGetVal(pRow, pReader->pSchema, slotId, &colVal);
46 47

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

  pBlock->info.rows += 1;
}

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

70 71 72
  p->type = type;
  p->pVnode = pVnode;
  p->numOfCols = numOfCols;
73

74 75 76 77 78
  if (taosArrayGetSize(pTableIdList) == 0) {
    *pReader = p;
    return TSDB_CODE_SUCCESS;
  }

79
  STableKeyInfo* pKeyInfo = taosArrayGet(pTableIdList, 0);
80 81
  p->pSchema = metaGetTbTSchema(p->pVnode->pMeta, pKeyInfo->uid, -1);
  p->pTableList = pTableIdList;
82

83
  p->transferBuf = taosMemoryCalloc(p->pSchema->numOfCols, POINTER_BYTES);
84
  for (int32_t i = 0; i < p->pSchema->numOfCols; ++i) {
M
Minglei Jin 已提交
85
    if (IS_VAR_DATA_TYPE(p->pSchema->columns[i].type)) {
86
      p->transferBuf[i] = taosMemoryMalloc(p->pSchema->columns[i].bytes);
87 88
    }
  }
89

90 91 92 93 94 95 96
  *pReader = p;
  return TSDB_CODE_SUCCESS;
}

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

97 98 99 100 101 102 103
  if (p->pSchema != NULL) {
    for (int32_t i = 0; i < p->pSchema->numOfCols; ++i) {
      taosMemoryFreeClear(p->transferBuf[i]);
    }

    taosMemoryFree(p->transferBuf);
    taosMemoryFree(p->pSchema);
104 105 106 107 108 109
  }

  taosMemoryFree(pReader);
  return TSDB_CODE_SUCCESS;
}

110
int32_t tsdbRetrieveLastRow(void* pReader, SSDataBlock* pResBlock, const int32_t* slotIds, SArray* pTableUidList) {
111
  if (pReader == NULL || pResBlock == NULL) {
112 113 114
    return TSDB_CODE_INVALID_PARA;
  }

115 116
  SLastrowReader* pr = pReader;

117
  SLRUCache* lruCache = pr->pVnode->pTsdb->lruCache;
118 119 120
  LRUHandle* h = NULL;
  STSRow*    pRow = NULL;
  size_t     numOfTables = taosArrayGetSize(pr->pTableList);
121 122

  // retrieve the only one last row of all tables in the uid list.
123
  if (pr->type == LASTROW_RETRIEVE_TYPE_SINGLE) {
124 125 126
    int64_t lastKey = INT64_MIN;
    bool    internalResult = false;
    for (int32_t i = 0; i < numOfTables; ++i) {
127
      STableKeyInfo* pKeyInfo = taosArrayGet(pr->pTableList, i);
128

129
      int32_t code = tsdbCacheGetLastrowH(lruCache, pKeyInfo->uid, pr->pVnode->pTsdb, &h);
130
      // int32_t code = tsdbCacheGetLastH(lruCache, pKeyInfo->uid, pr->pVnode->pTsdb, &h);
131 132 133 134
      if (code != TSDB_CODE_SUCCESS) {
        return code;
      }

135
      if (h == NULL) {
136 137 138
        continue;
      }

139
      pRow = (STSRow*)taosLRUCacheValue(lruCache, h);
140 141
      // SArray* pLast = (SArray*)taosLRUCacheValue(lruCache, h);
      // tsdbCacheLastArray2Row(pLast, &pRow, pr->pSchema);
142 143 144 145 146
      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;
147
          taosArrayClear(pTableUidList);
148 149
        }

150
        saveOneRow(pRow, pResBlock, pr, slotIds);
151
        taosArrayPush(pTableUidList, &pKeyInfo->uid);
152 153 154
        internalResult = true;
        lastKey = pRow->ts;
      }
155

156
      tsdbCacheRelease(lruCache, h);
157
    }
158 159 160
  } else if (pr->type == LASTROW_RETRIEVE_TYPE_ALL) {
    for (int32_t i = pr->tableIndex; i < numOfTables; ++i) {
      STableKeyInfo* pKeyInfo = taosArrayGet(pr->pTableList, i);
161

162
      int32_t code = tsdbCacheGetLastrowH(lruCache, pKeyInfo->uid, pr->pVnode->pTsdb, &h);
163
      // int32_t code = tsdbCacheGetLastH(lruCache, pKeyInfo->uid, pr->pVnode->pTsdb, &h);
164 165 166 167 168
      if (code != TSDB_CODE_SUCCESS) {
        return code;
      }

      // no data in the table of Uid
169
      if (h == NULL) {
170 171 172
        continue;
      }

173
      pRow = (STSRow*)taosLRUCacheValue(lruCache, h);
174 175 176
      // SArray* pLast = (SArray*)taosLRUCacheValue(lruCache, h);
      // tsdbCacheLastArray2Row(pLast, &pRow, pr->pSchema);

177
      saveOneRow(pRow, pResBlock, pr, slotIds);
178
      taosArrayPush(pTableUidList, &pKeyInfo->uid);
179

180
      tsdbCacheRelease(lruCache, h);
181

182 183 184 185
      pr->tableIndex += 1;
      if (pResBlock->info.rows >= pResBlock->info.capacity) {
        return TSDB_CODE_SUCCESS;
      }
186 187 188 189 190 191 192
    }
  } else {
    return TSDB_CODE_INVALID_PARA;
  }

  return TSDB_CODE_SUCCESS;
}