mnodeAcct.c 4.3 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

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

S
Shengliang Guan 已提交
19
#define ACCT_VER 1
S
Shengliang Guan 已提交
20

S
Shengliang Guan 已提交
21 22
static SSdbRaw *mnodeAcctActionEncode(SAcctObj *pAcct) {
  SSdbRaw *pRaw = calloc(1, sizeof(SAcctObj) + sizeof(SSdbRaw));
S
Shengliang Guan 已提交
23 24 25 26
  if (pRaw == NULL) {
    terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
    return NULL;
  }
S
Shengliang Guan 已提交
27

S
Shengliang Guan 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  int32_t dataLen = 0;
  char   *pData = pRaw->data;
  SDB_SET_BINARY_VAL(pData, dataLen, pAcct->acct, TSDB_USER_LEN)
  SDB_SET_INT64_VAL(pData, dataLen, pAcct->createdTime)
  SDB_SET_INT64_VAL(pData, dataLen, pAcct->updateTime)
  SDB_SET_INT32_VAL(pData, dataLen, pAcct->acctId)
  SDB_SET_INT32_VAL(pData, dataLen, pAcct->status)
  SDB_SET_INT32_VAL(pData, dataLen, pAcct->cfg.maxUsers)
  SDB_SET_INT32_VAL(pData, dataLen, pAcct->cfg.maxDbs)
  SDB_SET_INT32_VAL(pData, dataLen, pAcct->cfg.maxTimeSeries)
  SDB_SET_INT32_VAL(pData, dataLen, pAcct->cfg.maxStreams)
  SDB_SET_INT64_VAL(pData, dataLen, pAcct->cfg.maxStorage)
  SDB_SET_INT32_VAL(pData, dataLen, pAcct->cfg.accessState)

  pRaw->dataLen = dataLen;
  pRaw->type = SDB_ACCT;
  pRaw->sver = ACCT_VER;
  return pRaw;
S
Shengliang Guan 已提交
46 47
}

S
Shengliang Guan 已提交
48
static SAcctObj *mnodeAcctActionDecode(SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
49 50 51 52
  if (pRaw->sver != ACCT_VER) {
    terrno = TSDB_CODE_SDB_INVAID_RAW_DATA_VER;
    return NULL;
  }
S
Shengliang Guan 已提交
53 54

  SAcctObj *pAcct = calloc(1, sizeof(SAcctObj));
S
Shengliang Guan 已提交
55 56 57
  if (pAcct == NULL) {
    terrno = TSDB_CODE_MND_OUT_OF_MEMORY;
    return NULL;
S
Shengliang Guan 已提交
58 59
  }

S
Shengliang Guan 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73
  int32_t code = 0;
  int32_t dataLen = pRaw->dataLen;
  char   *pData = pRaw->data;
  SDB_GET_BINARY_VAL(pData, dataLen, pAcct->acct, TSDB_USER_LEN, code)
  SDB_GET_INT64_VAL(pData, dataLen, pAcct->createdTime, code)
  SDB_GET_INT64_VAL(pData, dataLen, pAcct->updateTime, code)
  SDB_GET_INT32_VAL(pData, dataLen, pAcct->acctId, code)
  SDB_GET_INT32_VAL(pData, dataLen, pAcct->status, code)
  SDB_GET_INT32_VAL(pData, dataLen, pAcct->cfg.maxUsers, code)
  SDB_GET_INT32_VAL(pData, dataLen, pAcct->cfg.maxDbs, code)
  SDB_GET_INT32_VAL(pData, dataLen, pAcct->cfg.maxTimeSeries, code)
  SDB_GET_INT32_VAL(pData, dataLen, pAcct->cfg.maxStreams, code)
  SDB_GET_INT64_VAL(pData, dataLen, pAcct->cfg.maxStorage, code)
  SDB_GET_INT32_VAL(pData, dataLen, pAcct->cfg.accessState, code)
S
Shengliang Guan 已提交
74

S
Shengliang Guan 已提交
75 76 77 78
  if (code != 0) {
    tfree(pAcct);
    terrno = code;
    return NULL;
S
Shengliang Guan 已提交
79 80
  }

S
Shengliang Guan 已提交
81 82
  return pAcct;
}
S
Shengliang Guan 已提交
83

S
Shengliang Guan 已提交
84
static int32_t mnodeAcctActionInsert(SAcctObj *pAcct) { return 0; }
S
Shengliang Guan 已提交
85

S
Shengliang Guan 已提交
86
static int32_t mnodeAcctActionDelete(SAcctObj *pAcct) { return 0; }
S
Shengliang Guan 已提交
87

S
Shengliang Guan 已提交
88 89 90 91
static int32_t mnodeAcctActionUpdate(SAcctObj *pSrcAcct, SAcctObj *pDstAcct) {
  memcpy(pDstAcct, pSrcAcct, (int32_t)((char *)&pDstAcct->info - (char *)&pDstAcct));
  return 0;
}
S
Shengliang Guan 已提交
92

S
Shengliang Guan 已提交
93 94
static int32_t mnodeCreateDefaultAcct() {
  int32_t code = 0;
S
Shengliang Guan 已提交
95

S
Shengliang Guan 已提交
96 97 98
  SAcctObj acctObj = {0};
  tstrncpy(acctObj.acct, TSDB_DEFAULT_USER, TSDB_USER_LEN);
  acctObj.createdTime = taosGetTimestampMs();
S
Shengliang Guan 已提交
99
  acctObj.updateTime = acctObj.createdTime;
S
Shengliang Guan 已提交
100
  acctObj.acctId = 1;
S
Shengliang Guan 已提交
101 102
  acctObj.cfg = (SAcctCfg){.maxUsers = 1024,
                           .maxDbs = 1024,
S
Shengliang Guan 已提交
103
                           .maxTimeSeries = INT32_MAX,
S
Shengliang Guan 已提交
104
                           .maxStreams = 8092,
S
Shengliang Guan 已提交
105 106
                           .maxStorage = INT64_MAX,
                           .accessState = TSDB_VN_ALL_ACCCESS};
S
Shengliang Guan 已提交
107

S
Shengliang Guan 已提交
108
  SSdbRaw *pRaw = mnodeAcctActionEncode(&acctObj);
S
Shengliang Guan 已提交
109
  if (pRaw != NULL) {
S
Shengliang Guan 已提交
110
    return -1;
S
Shengliang Guan 已提交
111 112
  }

S
Shengliang Guan 已提交
113
  return sdbWrite(pRaw);
S
Shengliang Guan 已提交
114 115 116
}

int32_t mnodeInitAcct() {
S
Shengliang Guan 已提交
117 118 119 120 121 122 123 124 125
  SSdbDesc desc = {.sdbType = SDB_ACCT,
                   .keyType = SDB_KEY_BINARY,
                   .deployFp = (SdbDeployFp)mnodeCreateDefaultAcct,
                   .encodeFp = (SdbEncodeFp)mnodeAcctActionEncode,
                   .decodeFp = (SdbDecodeFp)mnodeAcctActionDecode,
                   .insertFp = (SdbInsertFp)mnodeAcctActionInsert,
                   .updateFp = (SdbUpdateFp)mnodeAcctActionUpdate,
                   .deleteFp = (SdbDeleteFp)mnodeAcctActionDelete};
  sdbSetHandler(desc);
S
Shengliang Guan 已提交
126 127 128 129 130

  return 0;
}

void mnodeCleanupAcct() {}