binary format

This commit is contained in:
cademfly 2023-03-30 13:59:48 +08:00
parent 22b87dc26c
commit 824c87a6c7
2 changed files with 190 additions and 129 deletions

View File

@ -280,8 +280,8 @@ typedef struct {
SHashObj* readDbs; SHashObj* readDbs;
SHashObj* writeDbs; SHashObj* writeDbs;
SHashObj* topics; SHashObj* topics;
SHashObj* readStbs; SHashObj* readTbs;
SHashObj* writeStbs; SHashObj* writeTbs;
SRWLatch lock; SRWLatch lock;
} SUserObj; } SUserObj;

View File

@ -125,14 +125,14 @@ SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
int32_t numOfReadDbs = taosHashGetSize(pUser->readDbs); int32_t numOfReadDbs = taosHashGetSize(pUser->readDbs);
int32_t numOfWriteDbs = taosHashGetSize(pUser->writeDbs); int32_t numOfWriteDbs = taosHashGetSize(pUser->writeDbs);
int32_t numOfReadStbs = taosHashGetSize(pUser->readStbs); int32_t numOfReadStbs = taosHashGetSize(pUser->readTbs);
int32_t numOfWriteStbs = taosHashGetSize(pUser->writeStbs); int32_t numOfWriteStbs = taosHashGetSize(pUser->writeTbs);
int32_t numOfTopics = taosHashGetSize(pUser->topics); int32_t numOfTopics = taosHashGetSize(pUser->topics);
int32_t size = sizeof(SUserObj) + USER_RESERVE_SIZE + int32_t size = sizeof(SUserObj) + USER_RESERVE_SIZE +
(numOfReadDbs + numOfWriteDbs ) * TSDB_DB_FNAME_LEN + (numOfReadDbs + numOfWriteDbs ) * TSDB_DB_FNAME_LEN +
numOfTopics * TSDB_TOPIC_FNAME_LEN; numOfTopics * TSDB_TOPIC_FNAME_LEN;
char *stb = taosHashIterate(pUser->readStbs, NULL); char *stb = taosHashIterate(pUser->readTbs, NULL);
while (stb != NULL) { while (stb != NULL) {
size_t keyLen = 0; size_t keyLen = 0;
void *key = taosHashGetKey(stb, &keyLen); void *key = taosHashGetKey(stb, &keyLen);
@ -143,10 +143,10 @@ SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
valueLen = strlen(stb); valueLen = strlen(stb);
size += sizeof(int32_t); size += sizeof(int32_t);
size += valueLen; size += valueLen;
stb = taosHashIterate(pUser->readStbs, stb); stb = taosHashIterate(pUser->readTbs, stb);
} }
stb = taosHashIterate(pUser->writeStbs, NULL); stb = taosHashIterate(pUser->writeTbs, NULL);
while (stb != NULL) { while (stb != NULL) {
size_t keyLen = 0; size_t keyLen = 0;
void *key = taosHashGetKey(stb, &keyLen); void *key = taosHashGetKey(stb, &keyLen);
@ -157,7 +157,7 @@ SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
valueLen = strlen(stb); valueLen = strlen(stb);
size += sizeof(int32_t); size += sizeof(int32_t);
size += keyLen; size += keyLen;
stb = taosHashIterate(pUser->writeStbs, stb); stb = taosHashIterate(pUser->writeTbs, stb);
} }
SSdbRaw *pRaw = sdbAllocRaw(SDB_USER, USER_VER_NUMBER, size); SSdbRaw *pRaw = sdbAllocRaw(SDB_USER, USER_VER_NUMBER, size);
@ -198,7 +198,7 @@ SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
topic = taosHashIterate(pUser->topics, topic); topic = taosHashIterate(pUser->topics, topic);
} }
stb = taosHashIterate(pUser->readStbs, NULL); stb = taosHashIterate(pUser->readTbs, NULL);
while (stb != NULL) { while (stb != NULL) {
size_t keyLen = 0; size_t keyLen = 0;
void *key = taosHashGetKey(stb, &keyLen); void *key = taosHashGetKey(stb, &keyLen);
@ -206,13 +206,13 @@ SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER); SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
size_t valueLen = 0; size_t valueLen = 0;
valueLen = strlen(stb); valueLen = strlen(stb)+1;
SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER) SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER); SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
stb = taosHashIterate(pUser->readStbs, stb); stb = taosHashIterate(pUser->readTbs, stb);
} }
stb = taosHashIterate(pUser->writeStbs, NULL); stb = taosHashIterate(pUser->writeTbs, NULL);
while (stb != NULL) { while (stb != NULL) {
size_t keyLen = 0; size_t keyLen = 0;
void *key = taosHashGetKey(stb, &keyLen); void *key = taosHashGetKey(stb, &keyLen);
@ -220,10 +220,10 @@ SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER); SDB_SET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
size_t valueLen = 0; size_t valueLen = 0;
valueLen = strlen(stb); valueLen = strlen(stb)+1;
SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER) SDB_SET_INT32(pRaw, dataPos, valueLen, _OVER)
SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER); SDB_SET_BINARY(pRaw, dataPos, stb, valueLen, _OVER);
stb = taosHashIterate(pUser->writeStbs, stb); stb = taosHashIterate(pUser->writeTbs, stb);
} }
SDB_SET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER) SDB_SET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
@ -292,8 +292,8 @@ static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) {
pUser->writeDbs = pUser->writeDbs =
taosHashInit(numOfWriteDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK); taosHashInit(numOfWriteDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
pUser->topics = taosHashInit(numOfTopics, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK); pUser->topics = taosHashInit(numOfTopics, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
pUser->readStbs = taosHashInit(numOfReadStbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK); pUser->readTbs = taosHashInit(numOfReadStbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
pUser->writeStbs = pUser->writeTbs =
taosHashInit(numOfWriteStbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK); taosHashInit(numOfWriteStbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
if (pUser->readDbs == NULL || pUser->writeDbs == NULL || pUser->topics == NULL) goto _OVER; if (pUser->readDbs == NULL || pUser->writeDbs == NULL || pUser->topics == NULL) goto _OVER;
@ -325,15 +325,17 @@ static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) {
int32_t keyLen = 0; int32_t keyLen = 0;
SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER); SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
char *key = taosMemoryCalloc(keyLen + 1, sizeof(char)); char *key = taosMemoryCalloc(keyLen, sizeof(char));
memset(key, 0, keyLen);
SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER); SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
int32_t valuelen = 0; int32_t valuelen = 0;
SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER); SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
char *value = taosMemoryCalloc(valuelen + 1, sizeof(char)); char *value = taosMemoryCalloc(valuelen, sizeof(char));
memset(value, 0, keyLen);
SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER) SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
taosHashPut(pUser->readStbs, key, keyLen, value, valuelen); taosHashPut(pUser->readTbs, key, keyLen, value, valuelen);
taosMemoryFree(key); taosMemoryFree(key);
taosMemoryFree(value); taosMemoryFree(value);
@ -343,15 +345,17 @@ static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) {
int32_t keyLen = 0; int32_t keyLen = 0;
SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER); SDB_GET_INT32(pRaw, dataPos, &keyLen, _OVER);
char *key = taosMemoryCalloc(keyLen + 1, sizeof(char)); char *key = taosMemoryCalloc(keyLen, sizeof(char));
memset(key, 0, keyLen);
SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER); SDB_GET_BINARY(pRaw, dataPos, key, keyLen, _OVER);
int32_t valuelen = 0; int32_t valuelen = 0;
SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER); SDB_GET_INT32(pRaw, dataPos, &valuelen, _OVER);
char *value = taosMemoryCalloc(valuelen + 1, sizeof(char)); char *value = taosMemoryCalloc(valuelen, sizeof(char));
memset(value, 0, keyLen);
SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER) SDB_GET_BINARY(pRaw, dataPos, value, valuelen, _OVER)
taosHashPut(pUser->writeStbs, key, keyLen, value, valuelen); taosHashPut(pUser->writeTbs, key, keyLen, value, valuelen);
taosMemoryFree(key); taosMemoryFree(key);
taosMemoryFree(value); taosMemoryFree(value);
@ -370,8 +374,8 @@ _OVER:
taosHashCleanup(pUser->readDbs); taosHashCleanup(pUser->readDbs);
taosHashCleanup(pUser->writeDbs); taosHashCleanup(pUser->writeDbs);
taosHashCleanup(pUser->topics); taosHashCleanup(pUser->topics);
taosHashCleanup(pUser->readStbs); taosHashCleanup(pUser->readTbs);
taosHashCleanup(pUser->writeStbs); taosHashCleanup(pUser->writeTbs);
} }
taosMemoryFreeClear(pRow); taosMemoryFreeClear(pRow);
return NULL; return NULL;
@ -404,8 +408,8 @@ static int32_t mndUserDupObj(SUserObj *pUser, SUserObj *pNew) {
taosRLockLatch(&pUser->lock); taosRLockLatch(&pUser->lock);
pNew->readDbs = mndDupDbHash(pUser->readDbs); pNew->readDbs = mndDupDbHash(pUser->readDbs);
pNew->writeDbs = mndDupDbHash(pUser->writeDbs); pNew->writeDbs = mndDupDbHash(pUser->writeDbs);
pNew->readStbs = mndDupTopicHash(pUser->readStbs); pNew->readTbs = mndDupTopicHash(pUser->readTbs);
pNew->writeStbs = mndDupTopicHash(pUser->writeStbs); pNew->writeTbs = mndDupTopicHash(pUser->writeTbs);
pNew->topics = mndDupTopicHash(pUser->topics); pNew->topics = mndDupTopicHash(pUser->topics);
taosRUnLockLatch(&pUser->lock); taosRUnLockLatch(&pUser->lock);
@ -419,13 +423,13 @@ static void mndUserFreeObj(SUserObj *pUser) {
taosHashCleanup(pUser->readDbs); taosHashCleanup(pUser->readDbs);
taosHashCleanup(pUser->writeDbs); taosHashCleanup(pUser->writeDbs);
taosHashCleanup(pUser->topics); taosHashCleanup(pUser->topics);
taosHashCleanup(pUser->readStbs); taosHashCleanup(pUser->readTbs);
taosHashCleanup(pUser->writeStbs); taosHashCleanup(pUser->writeTbs);
pUser->readDbs = NULL; pUser->readDbs = NULL;
pUser->writeDbs = NULL; pUser->writeDbs = NULL;
pUser->topics = NULL; pUser->topics = NULL;
pUser->readStbs = NULL; pUser->readTbs = NULL;
pUser->writeStbs = NULL; pUser->writeTbs = NULL;
} }
static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser) { static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser) {
@ -445,8 +449,8 @@ static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew) {
TSWAP(pOld->readDbs, pNew->readDbs); TSWAP(pOld->readDbs, pNew->readDbs);
TSWAP(pOld->writeDbs, pNew->writeDbs); TSWAP(pOld->writeDbs, pNew->writeDbs);
TSWAP(pOld->topics, pNew->topics); TSWAP(pOld->topics, pNew->topics);
TSWAP(pOld->readStbs, pNew->readStbs); TSWAP(pOld->readTbs, pNew->readTbs);
TSWAP(pOld->writeStbs, pNew->writeStbs); TSWAP(pOld->writeTbs, pNew->writeTbs);
taosWUnLockLatch(&pOld->lock); taosWUnLockLatch(&pOld->lock);
return 0; return 0;
@ -767,7 +771,7 @@ static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) {
mndReleaseStb(pMnode, pStb); mndReleaseStb(pMnode, pStb);
goto _OVER; goto _OVER;
} }
if (taosHashPut(newUser.readStbs, tbFName, len, tbFName, TSDB_TABLE_NAME_LEN) != 0) { if (taosHashPut(newUser.readTbs, tbFName, len, tbFName, len) != 0) {
mndReleaseStb(pMnode, pStb); mndReleaseStb(pMnode, pStb);
goto _OVER; goto _OVER;
} }
@ -777,7 +781,7 @@ static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) {
pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb); pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
if (pIter == NULL) break; if (pIter == NULL) break;
int32_t len = strlen(pStb->name) + 1; int32_t len = strlen(pStb->name) + 1;
taosHashPut(newUser.readStbs, pStb->name, len, pStb->name, TSDB_TABLE_NAME_LEN); taosHashPut(newUser.readTbs, pStb->name, len, pStb->name, len);
sdbRelease(pSdb, pStb); sdbRelease(pSdb, pStb);
} }
} }
@ -794,7 +798,7 @@ static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) {
mndReleaseStb(pMnode, pStb); mndReleaseStb(pMnode, pStb);
goto _OVER; goto _OVER;
} }
if (taosHashPut(newUser.writeStbs, tbFName, len, tbFName, TSDB_TABLE_NAME_LEN) != 0) { if (taosHashPut(newUser.writeTbs, tbFName, len, tbFName, len) != 0) {
mndReleaseStb(pMnode, pStb); mndReleaseStb(pMnode, pStb);
goto _OVER; goto _OVER;
} }
@ -804,12 +808,85 @@ static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) {
pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb); pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
if (pIter == NULL) break; if (pIter == NULL) break;
int32_t len = strlen(pStb->name) + 1; int32_t len = strlen(pStb->name) + 1;
taosHashPut(newUser.writeStbs, pStb->name, len, pStb->name, TSDB_DB_FNAME_LEN); taosHashPut(newUser.writeTbs, pStb->name, len, pStb->name, TSDB_DB_FNAME_LEN);
sdbRelease(pSdb, pStb); sdbRelease(pSdb, pStb);
} }
} }
} }
if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_READ_TABLE ||
alterReq.alterType == TSDB_ALTER_USER_REMOVE_READ_TAG ||
alterReq.alterType == TSDB_ALTER_USER_REMOVE_ALL_TABLE) {
if (strcmp(alterReq.objname, "1.*") != 0) {
char tbFName[TSDB_TABLE_FNAME_LEN] = {0};
snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq.objname, alterReq.tabName);
int32_t len = strlen(tbFName) + 1;
SStbObj *pStb = mndAcquireStb(pMnode, tbFName);
if (pStb == NULL) {
mndReleaseStb(pMnode, pStb);
goto _OVER;
}
if (taosHashRemove(newUser.readTbs, tbFName, len) != 0) {
mndReleaseStb(pMnode, pStb);
goto _OVER;
}
} else {
while (1) {
SStbObj *pStb = NULL;
pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
if (pIter == NULL) break;
int32_t len = strlen(pStb->name) + 1;
if(strcmp(pStb->db, alterReq.objname) == 0){
if (taosHashRemove(newUser.readTbs, pStb->name, len) != 0) {
mndReleaseStb(pMnode, pStb);
goto _OVER;
}
}
//taosHashPut(newUser.writeStbs, pStb->name, len, pStb->name, TSDB_DB_FNAME_LEN);
//sdbRelease(pSdb, pStb);
}
//taosHashClear(newUser.readStbs);
}
}
if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_WRITE_TABLE ||
alterReq.alterType == TSDB_ALTER_USER_REMOVE_WRITE_TAG ||
alterReq.alterType == TSDB_ALTER_USER_REMOVE_ALL_TABLE) {
if (strcmp(alterReq.objname, "1.*") != 0) {
char tbFName[TSDB_TABLE_FNAME_LEN] = {0};
snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq.objname, alterReq.tabName);
int32_t len = strlen(tbFName) + 1;
SStbObj *pStb = mndAcquireStb(pMnode, tbFName);
if (pStb == NULL) {
mndReleaseStb(pMnode, pStb);
goto _OVER;
}
if (taosHashRemove(newUser.writeTbs, tbFName, len) != 0) {
mndReleaseStb(pMnode, pStb);
goto _OVER;
}
} else {
while (1) {
SStbObj *pStb = NULL;
pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
if (pIter == NULL) break;
int32_t len = strlen(pStb->name) + 1;
if(strcmp(pStb->db, alterReq.objname) == 0){
if (taosHashRemove(newUser.writeTbs, pStb->name, len) != 0) {
mndReleaseStb(pMnode, pStb);
goto _OVER;
}
}
}
}
}
if (alterReq.alterType == TSDB_ALTER_USER_ADD_SUBSCRIBE_TOPIC) { if (alterReq.alterType == TSDB_ALTER_USER_ADD_SUBSCRIBE_TOPIC) {
int32_t len = strlen(alterReq.objname) + 1; int32_t len = strlen(alterReq.objname) + 1;
SMqTopicObj *pTopic = mndAcquireTopic(pMnode, alterReq.objname); SMqTopicObj *pTopic = mndAcquireTopic(pMnode, alterReq.objname);
@ -822,9 +899,9 @@ static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) {
if (alterReq.alterType == TSDB_ALTER_USER_ADD_READ_TAG) { if (alterReq.alterType == TSDB_ALTER_USER_ADD_READ_TAG) {
char tbFName[TSDB_TABLE_FNAME_LEN] = {0}; char tbFName[TSDB_TABLE_FNAME_LEN] = {0};
snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq.objname, alterReq.tabName); snprintf(tbFName, TSDB_TABLE_FNAME_LEN, "%s.%s", alterReq.objname, alterReq.tabName);
int32_t len = strlen(tbFName) + 1; int32_t len = strlen(tbFName) + 1;
SStbObj *pStb = mndAcquireStb(pMnode, tbFName); SStbObj *pStb = mndAcquireStb(pMnode, tbFName);
if (pStb == NULL) { if (pStb == NULL) {
mndReleaseStb(pMnode, pStb); mndReleaseStb(pMnode, pStb);
@ -834,8 +911,9 @@ static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) {
mndReleaseStb(pMnode, pStb); mndReleaseStb(pMnode, pStb);
goto _OVER; goto _OVER;
} }
int32_t condLen = strlen(alterReq.tagCond);
if (taosHashPut(newUser.readStbs, tbFName, len, alterReq.tagCond, strlen(alterReq.tagCond)) != 0) { int32_t condLen = strlen(alterReq.tagCond) + 1;
if (taosHashPut(newUser.readTbs, tbFName, len, alterReq.tagCond, condLen) != 0) {
mndReleaseStb(pMnode, pStb); mndReleaseStb(pMnode, pStb);
goto _OVER; goto _OVER;
} }
@ -844,8 +922,8 @@ static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) {
if (alterReq.alterType == TSDB_ALTER_USER_ADD_WRITE_TAG) { if (alterReq.alterType == TSDB_ALTER_USER_ADD_WRITE_TAG) {
char tbFName[TSDB_TABLE_FNAME_LEN] = {0}; char tbFName[TSDB_TABLE_FNAME_LEN] = {0};
snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq.objname, alterReq.tabName); snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq.objname, alterReq.tabName);
int32_t len = strlen(tbFName) + 1; int32_t len = strlen(tbFName) + 1;
SStbObj *pStb = mndAcquireStb(pMnode, tbFName); SStbObj *pStb = mndAcquireStb(pMnode, tbFName);
if (pStb == NULL) { if (pStb == NULL) {
mndReleaseStb(pMnode, pStb); mndReleaseStb(pMnode, pStb);
@ -855,8 +933,8 @@ static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) {
mndReleaseStb(pMnode, pStb); mndReleaseStb(pMnode, pStb);
goto _OVER; goto _OVER;
} }
int32_t condLen = strlen(alterReq.tagCond); int32_t condLen = strlen(alterReq.tagCond) + 1;
if (taosHashPut(newUser.writeStbs, tbFName, len, alterReq.tagCond, condLen) != 0) { if (taosHashPut(newUser.writeTbs, tbFName, len, alterReq.tagCond, condLen) != 0) {
mndReleaseStb(pMnode, pStb); mndReleaseStb(pMnode, pStb);
goto _OVER; goto _OVER;
} }
@ -1045,6 +1123,71 @@ static void mndCancelGetNextUser(SMnode *pMnode, void *pIter) {
sdbCancelFetch(pSdb, pIter); sdbCancelFetch(pSdb, pIter);
} }
static void mndLoopHash(SHashObj * hash, char *priType, SSDataBlock *pBlock, int32_t *numOfRows, char *user, SShowObj *pShow){
char *value = taosHashIterate(hash, NULL);
int32_t cols = 0;
while (value != NULL) {
cols = 0;
char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
STR_WITH_MAXSIZE_TO_VARSTR(userName, user, pShow->pMeta->pSchemas[cols].bytes);
SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, *numOfRows, (const char *)userName, false);
char privilege[20] = {0};
STR_WITH_MAXSIZE_TO_VARSTR(privilege, priType, pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, *numOfRows, (const char *)privilege, false);
size_t keyLen = 0;
void *key = taosHashGetKey(value, &keyLen);
char tableName[TSDB_TABLE_NAME_LEN] = {0};
mndExtractTbNameFromStbFullName(key, tableName, TSDB_TABLE_NAME_LEN);
if(strcmp(key, value) == 0){
char *obj = taosMemoryMalloc(TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE);
STR_WITH_MAXSIZE_TO_VARSTR(obj, tableName, pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, *numOfRows, (const char *)obj, false);
taosMemoryFree(obj);
}
else{
SNode *pAst = NULL;
int32_t sqlLen = 0;
char *sql = NULL;
if(nodesStringToNode(value, &pAst) == 0) {
sql = taosMemoryMalloc(TSDB_EXPLAIN_RESULT_ROW_SIZE);
nodesNodeToSQL(pAst, sql, TSDB_EXPLAIN_RESULT_ROW_SIZE, &sqlLen);
}
else{
sqlLen = 5;
sql = taosMemoryMalloc(sqlLen + 1);
sprintf(sql, "error");
}
int32_t contentLen = sqlLen + TSDB_TABLE_NAME_LEN + 3;
char *content = taosMemoryMalloc(contentLen);
if(sql != NULL){
sprintf(content, "%s(%s)", tableName, sql);
taosMemoryFree(sql);
}
char *obj = taosMemoryMalloc(contentLen + VARSTR_HEADER_SIZE);
STR_WITH_MAXSIZE_TO_VARSTR(obj, content, pShow->pMeta->pSchemas[cols].bytes);
taosMemoryFree(content);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, *numOfRows, (const char *)obj, false);
taosMemoryFree(obj);
}
(*numOfRows)++;
value = taosHashIterate(hash, value);
}
}
static int32_t mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) { static int32_t mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
SMnode *pMnode = pReq->info.node; SMnode *pMnode = pReq->info.node;
SSdb *pSdb = pMnode->pSdb; SSdb *pSdb = pMnode->pSdb;
@ -1132,91 +1275,9 @@ static int32_t mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock
db = taosHashIterate(pUser->writeDbs, db); db = taosHashIterate(pUser->writeDbs, db);
} }
char *stb = taosHashIterate(pUser->readStbs, NULL); mndLoopHash(pUser->readTbs, "read", pBlock, &numOfRows, pUser->user, pShow);
while (stb != NULL) {
cols = 0;
char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, numOfRows, (const char *)userName, false);
char privilege[20] = {0}; mndLoopHash(pUser->writeTbs, "write", pBlock, &numOfRows, pUser->user, pShow);
STR_WITH_MAXSIZE_TO_VARSTR(privilege, "read", pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, numOfRows, (const char *)privilege, false);
size_t superTableLen = 0;
void *superTable = taosHashGetKey(stb, &superTableLen);
char objName[TSDB_TABLE_NAME_LEN] = {0};
mndExtractTbNameFromStbFullName(superTable, objName, TSDB_TABLE_NAME_LEN);
SNode *pAst = NULL;
int32_t code = nodesStringToNode(stb, &pAst);
char *buf = taosMemoryCalloc(TSDB_EXPLAIN_RESULT_ROW_SIZE, sizeof(char));
int32_t sqlLen = 0;
nodesNodeToSQL(pAst, buf, TSDB_EXPLAIN_RESULT_ROW_SIZE * sizeof(char), &sqlLen);
char *value = taosMemoryCalloc(sqlLen + TSDB_TABLE_NAME_LEN + 1, sizeof(char));
sprintf(value, "%s(%s)", objName, buf);
char *obj = taosMemoryCalloc(sqlLen + TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE + 1, sizeof(char));
STR_WITH_MAXSIZE_TO_VARSTR(obj, value, pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, numOfRows, (const char *)obj, false);
taosMemoryFree(value);
taosMemoryFree(buf);
taosMemoryFree(obj);
numOfRows++;
stb = taosHashIterate(pUser->readStbs, stb);
}
stb = taosHashIterate(pUser->writeStbs, NULL);
while (stb != NULL) {
cols = 0;
char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, numOfRows, (const char *)userName, false);
char privilege[20] = {0};
STR_WITH_MAXSIZE_TO_VARSTR(privilege, "write", pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, numOfRows, (const char *)privilege, false);
size_t superTableLen = 0;
void *superTable = taosHashGetKey(stb, &superTableLen);
char objName[TSDB_TABLE_NAME_LEN] = {0};
mndExtractTbNameFromStbFullName(superTable, objName, TSDB_TABLE_NAME_LEN);
SNode *pAst = NULL;
int32_t code = nodesStringToNode(stb, &pAst);
char *buf = taosMemoryCalloc(TSDB_EXPLAIN_RESULT_ROW_SIZE, sizeof(char));
int32_t sqlLen = 0;
nodesNodeToSQL(pAst, buf, TSDB_EXPLAIN_RESULT_ROW_SIZE * sizeof(char), &sqlLen);
char *value = taosMemoryCalloc(sqlLen + TSDB_TABLE_NAME_LEN + 1, sizeof(char));
sprintf(value, "%s(%s)", objName, buf);
char *obj = taosMemoryCalloc(sqlLen + TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE + 1, sizeof(char));
STR_WITH_MAXSIZE_TO_VARSTR(obj, value, pShow->pMeta->pSchemas[cols].bytes);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
colDataSetVal(pColInfo, numOfRows, (const char *)obj, false);
taosMemoryFree(value);
taosMemoryFree(buf);
taosMemoryFree(obj);
numOfRows++;
stb = taosHashIterate(pUser->writeStbs, stb);
}
char *topic = taosHashIterate(pUser->topics, NULL); char *topic = taosHashIterate(pUser->topics, NULL);
while (topic != NULL) { while (topic != NULL) {
@ -1340,8 +1401,8 @@ int32_t mndUserRemoveDb(SMnode *pMnode, STrans *pTrans, char *db) {
if (inRead || inWrite) { if (inRead || inWrite) {
(void)taosHashRemove(newUser.readDbs, db, len); (void)taosHashRemove(newUser.readDbs, db, len);
(void)taosHashRemove(newUser.writeDbs, db, len); (void)taosHashRemove(newUser.writeDbs, db, len);
(void)taosHashRemove(newUser.readStbs, db, len); (void)taosHashRemove(newUser.readTbs, db, len);
(void)taosHashRemove(newUser.writeStbs, db, len); (void)taosHashRemove(newUser.writeTbs, db, len);
SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser); SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) break; if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) break;