sdbRaw.c 5.0 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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
#include "sdbInt.h"

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

S
Shengliang Guan 已提交
26
  pRaw->type = type;
S
Shengliang Guan 已提交
27 28 29 30 31 32 33 34 35 36 37 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
  pRaw->sver = sver;
  pRaw->dataLen = dataLen;
  return pRaw;
}

void sdbFreeRaw(SSdbRaw *pRaw) { free(pRaw); }

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 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
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 已提交
79 80 81 82 83 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
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;
  }

  memcpy(pRaw->pData + dataPos, pVal, valLen);
  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;
  }

  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 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  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 已提交
176 177 178 179 180 181 182 183 184 185 186 187 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 216 217 218 219 220 221 222 223 224 225 226
  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;
  }

  memcpy(pVal, pRaw->pData + dataPos, valLen);
  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;
}