mndAcct.c 4.2 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 "mndInt.h"
S
Shengliang Guan 已提交
18

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

S
Shengliang Guan 已提交
21
static SSdbRaw *mnodeAcctActionEncode(SAcctObj *pAcct) {
S
Shengliang Guan 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
  SSdbRaw *pRaw = sdbAllocRaw(SDB_ACCT, SDB_ACCT_VER, sizeof(SAcctObj));
  if (pRaw == NULL) return NULL;

  int32_t dataPos = 0;
  SDB_SET_BINARY(pRaw, dataPos, pAcct->acct, TSDB_USER_LEN)
  SDB_SET_INT64(pRaw, dataPos, pAcct->createdTime)
  SDB_SET_INT64(pRaw, dataPos, pAcct->updateTime)
  SDB_SET_INT32(pRaw, dataPos, pAcct->acctId)
  SDB_SET_INT32(pRaw, dataPos, pAcct->status)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxUsers)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxDbs)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxTimeSeries)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.maxStreams)
  SDB_SET_INT64(pRaw, dataPos, pAcct->cfg.maxStorage)
  SDB_SET_INT32(pRaw, dataPos, pAcct->cfg.accessState)
  SDB_SET_DATALEN(pRaw, dataPos);
S
Shengliang Guan 已提交
38

S
Shengliang Guan 已提交
39
  return pRaw;
S
Shengliang Guan 已提交
40 41
}

S
Shengliang Guan 已提交
42 43 44
static SSdbRow *mnodeAcctActionDecode(SSdbRaw *pRaw) {
  int8_t sver = 0;
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) return NULL;
S
Shengliang Guan 已提交
45

S
Shengliang Guan 已提交
46 47
  if (sver != SDB_ACCT_VER) {
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
S
Shengliang Guan 已提交
48
    return NULL;
S
Shengliang Guan 已提交
49 50
  }

S
Shengliang Guan 已提交
51
  SSdbRow  *pRow = sdbAllocRow(sizeof(SAcctObj));
S
Shengliang Guan 已提交
52 53
  SAcctObj *pAcct = sdbGetRowObj(pRow);
  if (pAcct == NULL) return NULL;
S
Shengliang Guan 已提交
54

S
Shengliang Guan 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68
  int32_t dataPos = 0;
  SDB_GET_BINARY(pRaw, pRow, dataPos, pAcct->acct, TSDB_USER_LEN)
  SDB_GET_INT64(pRaw, pRow, dataPos, &pAcct->createdTime)
  SDB_GET_INT64(pRaw, pRow, dataPos, &pAcct->updateTime)
  SDB_GET_INT32(pRaw, pRow, dataPos, &pAcct->acctId)
  SDB_GET_INT32(pRaw, pRow, dataPos, &pAcct->status)
  SDB_GET_INT32(pRaw, pRow, dataPos, &pAcct->cfg.maxUsers)
  SDB_GET_INT32(pRaw, pRow, dataPos, &pAcct->cfg.maxDbs)
  SDB_GET_INT32(pRaw, pRow, dataPos, &pAcct->cfg.maxTimeSeries)
  SDB_GET_INT32(pRaw, pRow, dataPos, &pAcct->cfg.maxStreams)
  SDB_GET_INT64(pRaw, pRow, dataPos, &pAcct->cfg.maxStorage)
  SDB_GET_INT32(pRaw, pRow, dataPos, &pAcct->cfg.accessState)

  return pRow;
S
Shengliang Guan 已提交
69
}
S
Shengliang Guan 已提交
70

S
Shengliang Guan 已提交
71
static int32_t mnodeAcctActionInsert(SSdb *pSdb, SAcctObj *pAcct) { return 0; }
S
Shengliang Guan 已提交
72

S
Shengliang Guan 已提交
73
static int32_t mnodeAcctActionDelete(SSdb *pSdb, SAcctObj *pAcct) { return 0; }
S
Shengliang Guan 已提交
74

S
Shengliang Guan 已提交
75
static int32_t mnodeAcctActionUpdate(SSdb *pSdb, SAcctObj *pSrcAcct, SAcctObj *pDstAcct) {
S
Shengliang Guan 已提交
76 77 78
  SAcctObj tObj;
  int32_t  len = (int32_t)((int8_t *)&tObj.info - (int8_t *)&tObj);
  memcpy(pDstAcct, pSrcAcct, len);
S
Shengliang Guan 已提交
79 80
  return 0;
}
S
Shengliang Guan 已提交
81

S
Shengliang Guan 已提交
82
static int32_t mnodeCreateDefaultAcct(SSdb *pSdb) {
S
Shengliang Guan 已提交
83
  int32_t code = 0;
S
Shengliang Guan 已提交
84

S
Shengliang Guan 已提交
85 86 87
  SAcctObj acctObj = {0};
  tstrncpy(acctObj.acct, TSDB_DEFAULT_USER, TSDB_USER_LEN);
  acctObj.createdTime = taosGetTimestampMs();
S
Shengliang Guan 已提交
88
  acctObj.updateTime = acctObj.createdTime;
S
Shengliang Guan 已提交
89
  acctObj.acctId = 1;
S
Shengliang Guan 已提交
90 91
  acctObj.cfg = (SAcctCfg){.maxUsers = 1024,
                           .maxDbs = 1024,
S
Shengliang Guan 已提交
92
                           .maxTimeSeries = INT32_MAX,
S
Shengliang Guan 已提交
93
                           .maxStreams = 8092,
S
Shengliang Guan 已提交
94 95
                           .maxStorage = INT64_MAX,
                           .accessState = TSDB_VN_ALL_ACCCESS};
S
Shengliang Guan 已提交
96

S
Shengliang Guan 已提交
97
  SSdbRaw *pRaw = mnodeAcctActionEncode(&acctObj);
S
Shengliang Guan 已提交
98
  if (pRaw == NULL) return -1;
S
Shengliang Guan 已提交
99
  sdbSetRawStatus(pRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
100

S
Shengliang Guan 已提交
101
  return sdbWrite(pSdb, pRaw);
S
Shengliang Guan 已提交
102 103
}

S
Shengliang Guan 已提交
104
int32_t mndInitAcct(SMnode *pMnode) {
S
Shengliang Guan 已提交
105 106
  SSdbTable table = {.sdbType = SDB_ACCT,
                     .keyType = SDB_KEY_BINARY,
S
Shengliang Guan 已提交
107
                     .deployFp = mnodeCreateDefaultAcct,
S
Shengliang Guan 已提交
108 109 110 111 112
                     .encodeFp = (SdbEncodeFp)mnodeAcctActionEncode,
                     .decodeFp = (SdbDecodeFp)mnodeAcctActionDecode,
                     .insertFp = (SdbInsertFp)mnodeAcctActionInsert,
                     .updateFp = (SdbUpdateFp)mnodeAcctActionUpdate,
                     .deleteFp = (SdbDeleteFp)mnodeAcctActionDelete};
S
Shengliang Guan 已提交
113

114
  sdbSetTable(pMnode->pSdb, table);
S
Shengliang Guan 已提交
115 116 117
  return 0;
}

S
Shengliang Guan 已提交
118
void mndCleanupAcct(SMnode *pMnode) {}