You need to sign in or sign up before continuing.
sdbRaw.c 5.2 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "sdb.h"
S
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19
SSdbRaw *sdbAllocRaw(ESdbType type, int8_t sver, int32_t dataLen) {
wafwerar's avatar
wafwerar 已提交
20
  SSdbRaw *pRaw = taosMemoryCalloc(1, dataLen + sizeof(SSdbRaw));
S
Shengliang Guan 已提交
21 22 23 24 25
  if (pRaw == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
26
  pRaw->type = type;
S
Shengliang Guan 已提交
27 28
  pRaw->sver = sver;
  pRaw->dataLen = dataLen;
S
Shengliang Guan 已提交
29

30
  mTrace("raw:%p, is created, len:%d table:%s", pRaw, dataLen, sdbTableName(type));
S
Shengliang Guan 已提交
31 32 33
  return pRaw;
}

S
Shengliang Guan 已提交
34
void sdbFreeRaw(SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
35
  mTrace("raw:%p, is freed", pRaw);
wafwerar's avatar
wafwerar 已提交
36
  taosMemoryFree(pRaw);
S
Shengliang Guan 已提交
37
}
S
Shengliang Guan 已提交
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 67 68

int32_t sdbSetRawInt8(SSdbRaw *pRaw, int32_t dataPos, int8_t val) {
  if (pRaw == NULL) {
    terrno = TSDB_CODE_INVALID_PTR;
    return -1;
  }

  if (dataPos + sizeof(int8_t) > pRaw->dataLen) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_LEN;
    return -1;
  }

  *(int8_t *)(pRaw->pData + dataPos) = val;
  return 0;
}

int32_t sdbSetRawInt32(SSdbRaw *pRaw, int32_t dataPos, int32_t val) {
  if (pRaw == NULL) {
    terrno = TSDB_CODE_INVALID_PTR;
    return -1;
  }

  if (dataPos + sizeof(int32_t) > pRaw->dataLen) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_LEN;
    return -1;
  }

  *(int32_t *)(pRaw->pData + dataPos) = val;
  return 0;
}

S
Shengliang Guan 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
int32_t sdbSetRawInt16(SSdbRaw *pRaw, int32_t dataPos, int16_t val) {
  if (pRaw == NULL) {
    terrno = TSDB_CODE_INVALID_PTR;
    return -1;
  }

  if (dataPos + sizeof(int16_t) > pRaw->dataLen) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_LEN;
    return -1;
  }

  *(int16_t *)(pRaw->pData + dataPos) = val;
  return 0;
}

S
Shengliang Guan 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
int32_t sdbSetRawInt64(SSdbRaw *pRaw, int32_t dataPos, int64_t val) {
  if (pRaw == NULL) {
    terrno = TSDB_CODE_INVALID_PTR;
    return -1;
  }

  if (dataPos + sizeof(int64_t) > pRaw->dataLen) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_LEN;
    return -1;
  }

  *(int64_t *)(pRaw->pData + dataPos) = val;
  return 0;
}

int32_t sdbSetRawBinary(SSdbRaw *pRaw, int32_t dataPos, const char *pVal, int32_t valLen) {
  if (pRaw == NULL) {
    terrno = TSDB_CODE_INVALID_PTR;
    return -1;
  }

  if (dataPos + valLen > pRaw->dataLen) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_LEN;
    return -1;
  }

110 111 112
  if (pVal != NULL) {
    memcpy(pRaw->pData + dataPos, pVal, valLen);
  }
S
Shengliang Guan 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  return 0;
}

int32_t sdbSetRawDataLen(SSdbRaw *pRaw, int32_t dataLen) {
  if (pRaw == NULL) {
    terrno = TSDB_CODE_INVALID_PTR;
    return -1;
  }

  if (dataLen > pRaw->dataLen) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_LEN;
    return -1;
  }

  pRaw->dataLen = dataLen;
  return 0;
}

int32_t sdbSetRawStatus(SSdbRaw *pRaw, ESdbStatus status) {
  if (pRaw == NULL) {
    terrno = TSDB_CODE_INVALID_PTR;
    return -1;
  }

S
Shengliang Guan 已提交
137 138 139 140 141
  if (status == SDB_STATUS_INIT) {
    terrno = TSDB_CODE_INVALID_PARA;
    return -1;
  }

S
Shengliang Guan 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  pRaw->status = status;
  return 0;
}

int32_t sdbGetRawInt8(SSdbRaw *pRaw, int32_t dataPos, int8_t *val) {
  if (pRaw == NULL) {
    terrno = TSDB_CODE_INVALID_PTR;
    return -1;
  }

  if (dataPos + sizeof(int8_t) > pRaw->dataLen) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_LEN;
    return -1;
  }

  *val = *(int8_t *)(pRaw->pData + dataPos);
  return 0;
}

int32_t sdbGetRawInt32(SSdbRaw *pRaw, int32_t dataPos, int32_t *val) {
  if (pRaw == NULL) {
    terrno = TSDB_CODE_INVALID_PTR;
    return -1;
  }

  if (dataPos + sizeof(int32_t) > pRaw->dataLen) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_LEN;
    return -1;
  }

  *val = *(int32_t *)(pRaw->pData + dataPos);
S
Shengliang Guan 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
  return 0;
}

int32_t sdbGetRawInt16(SSdbRaw *pRaw, int32_t dataPos, int16_t *val) {
  if (pRaw == NULL) {
    terrno = TSDB_CODE_INVALID_PTR;
    return -1;
  }

  if (dataPos + sizeof(int16_t) > pRaw->dataLen) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_LEN;
    return -1;
  }

  *val = *(int16_t *)(pRaw->pData + dataPos);
S
Shengliang Guan 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
  return 0;
}

int32_t sdbGetRawInt64(SSdbRaw *pRaw, int32_t dataPos, int64_t *val) {
  if (pRaw == NULL) {
    terrno = TSDB_CODE_INVALID_PTR;
    return -1;
  }

  if (dataPos + sizeof(int64_t) > pRaw->dataLen) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_LEN;
    return -1;
  }

  *val = *(int64_t *)(pRaw->pData + dataPos);
  return 0;
}

int32_t sdbGetRawBinary(SSdbRaw *pRaw, int32_t dataPos, char *pVal, int32_t valLen) {
  if (pRaw == NULL) {
    terrno = TSDB_CODE_INVALID_PTR;
    return -1;
  }

  if (dataPos + valLen > pRaw->dataLen) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_LEN;
    return -1;
  }
dengyihao's avatar
dengyihao 已提交
216 217 218
  if (pVal != NULL) {
    memcpy(pVal, pRaw->pData + dataPos, valLen);
  }
S
Shengliang Guan 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  return 0;
}

int32_t sdbGetRawSoftVer(SSdbRaw *pRaw, int8_t *sver) {
  if (pRaw == NULL) {
    terrno = TSDB_CODE_INVALID_PTR;
    return -1;
  }

  *sver = pRaw->sver;
  return 0;
}

int32_t sdbGetRawTotalSize(SSdbRaw *pRaw) {
  if (pRaw == NULL) {
    terrno = TSDB_CODE_INVALID_PTR;
    return -1;
  }

  return sizeof(SSdbRaw) + pRaw->dataLen;
dengyihao's avatar
dengyihao 已提交
239
}