1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "grd_kv/grd_kv_api.h"
16
17 #include "grd_api_manager.h"
18 #include "grd_base/grd_error.h"
19 #include "grd_resultset_inner.h"
20 #include "grd_type_inner.h"
21 #include "rd_log_print.h"
22 using namespace DocumentDB;
23
24 static GRD_APIInfo GRD_KVApiInfo;
25
GRD_KVPut(GRD_DB * db,const char * collectionName,const GRD_KVItemT * key,const GRD_KVItemT * value)26 GRD_API int32_t GRD_KVPut(GRD_DB *db, const char *collectionName, const GRD_KVItemT *key, const GRD_KVItemT *value)
27 {
28 if (GRD_KVApiInfo.KVPutApi == nullptr) {
29 GRD_KVApiInfo = GetApiInfoInstance();
30 }
31 if (GRD_KVApiInfo.KVPutApi == nullptr) {
32 return GRD_INNER_ERR;
33 }
34 return GRD_KVApiInfo.KVPutApi(db, collectionName, key, value);
35 }
36
GRD_KVGet(GRD_DB * db,const char * collectionName,const GRD_KVItemT * key,const GRD_KVItemT * value)37 GRD_API int32_t GRD_KVGet(GRD_DB *db, const char *collectionName, const GRD_KVItemT *key, const GRD_KVItemT *value)
38 {
39 if (GRD_KVApiInfo.KVGetApi == nullptr) {
40 GRD_KVApiInfo = GetApiInfoInstance();
41 }
42 if (GRD_KVApiInfo.KVGetApi == nullptr) {
43 return GRD_INNER_ERR;
44 }
45 return GRD_KVApiInfo.KVGetApi(db, collectionName, key, value);
46 }
47
GRD_KVDel(GRD_DB * db,const char * collectionName,const GRD_KVItemT * key)48 GRD_API int32_t GRD_KVDel(GRD_DB *db, const char *collectionName, const GRD_KVItemT *key)
49 {
50 if (GRD_KVApiInfo.KVDelApi == nullptr) {
51 GRD_KVApiInfo = GetApiInfoInstance();
52 }
53 if (GRD_KVApiInfo.KVDelApi == nullptr) {
54 return GRD_INNER_ERR;
55 }
56 return GRD_KVApiInfo.KVDelApi(db, collectionName, key);
57 }
58
GRD_KVScan(GRD_DB * db,const char * collectionName,const GRD_KVItemT * key,GRD_KvScanModeE mode,GRD_ResultSet ** resultSet)59 GRD_API int32_t GRD_KVScan(GRD_DB *db, const char *collectionName, const GRD_KVItemT *key, GRD_KvScanModeE mode,
60 GRD_ResultSet **resultSet)
61 {
62 if (GRD_KVApiInfo.KVScanApi == nullptr) {
63 GRD_KVApiInfo = GetApiInfoInstance();
64 }
65 if (GRD_KVApiInfo.KVScanApi == nullptr) {
66 return GRD_INNER_ERR;
67 }
68 return GRD_KVApiInfo.KVScanApi(db, collectionName, key, mode, resultSet);
69 }
70
GRD_KVFilter(GRD_DB * db,const char * collectionName,const GRD_FilterOptionT * scanParams,GRD_ResultSet ** resultSet)71 GRD_API int32_t GRD_KVFilter(GRD_DB *db, const char *collectionName, const GRD_FilterOptionT *scanParams,
72 GRD_ResultSet **resultSet)
73 {
74 if (GRD_KVApiInfo.KVFilterApi == nullptr) {
75 GRD_KVApiInfo = GetApiInfoInstance();
76 }
77 if (GRD_KVApiInfo.KVFilterApi == nullptr) {
78 return GRD_INNER_ERR;
79 }
80 return GRD_KVApiInfo.KVFilterApi(db, collectionName, scanParams, resultSet);
81 }
82
GRD_KVGetSize(GRD_ResultSet * resultSet,uint32_t * keyLen,uint32_t * valueLen)83 GRD_API int32_t GRD_KVGetSize(GRD_ResultSet *resultSet, uint32_t *keyLen, uint32_t *valueLen)
84 {
85 if (GRD_KVApiInfo.KVGetSizeApi == nullptr) {
86 GRD_KVApiInfo = GetApiInfoInstance();
87 }
88 if (GRD_KVApiInfo.KVGetSizeApi == nullptr) {
89 return GRD_INNER_ERR;
90 }
91 return GRD_KVApiInfo.KVGetSizeApi(resultSet, keyLen, valueLen);
92 }
93
GRD_GetItem(GRD_ResultSet * resultSet,void * key,void * value)94 GRD_API int32_t GRD_GetItem(GRD_ResultSet *resultSet, void *key, void *value)
95 {
96 if (GRD_KVApiInfo.GetItemApi == nullptr) {
97 GRD_KVApiInfo = GetApiInfoInstance();
98 }
99 if (GRD_KVApiInfo.GetItemApi == nullptr) {
100 return GRD_INNER_ERR;
101 }
102 return GRD_KVApiInfo.GetItemApi(resultSet, key, value);
103 }
104
GRD_KVFreeItem(GRD_KVItemT * item)105 GRD_API int32_t GRD_KVFreeItem(GRD_KVItemT *item)
106 {
107 if (GRD_KVApiInfo.KVFreeItemApi == nullptr) {
108 GRD_KVApiInfo = GetApiInfoInstance();
109 }
110 if (GRD_KVApiInfo.KVFreeItemApi == nullptr) {
111 return GRD_INNER_ERR;
112 }
113 return GRD_KVApiInfo.KVFreeItemApi(item);
114 }
115
GRD_KVBatchPrepare(uint16_t itemNum,GRD_KVBatchT ** batch)116 GRD_API int32_t GRD_KVBatchPrepare(uint16_t itemNum, GRD_KVBatchT **batch)
117 {
118 if (GRD_KVApiInfo.KVBatchPrepareApi == nullptr) {
119 GRD_KVApiInfo = GetApiInfoInstance();
120 }
121 if (GRD_KVApiInfo.KVBatchPrepareApi == nullptr) {
122 return GRD_INNER_ERR;
123 }
124 return GRD_KVApiInfo.KVBatchPrepareApi(itemNum, batch);
125 }
126
GRD_KVBatchPushback(const void * key,uint32_t keyLen,const void * data,uint32_t dataLen,GRD_KVBatchT * batch)127 GRD_API int32_t GRD_KVBatchPushback(const void *key, uint32_t keyLen, const void *data, uint32_t dataLen,
128 GRD_KVBatchT *batch)
129 {
130 if (GRD_KVApiInfo.KVBatchPushbackApi == nullptr) {
131 GRD_KVApiInfo = GetApiInfoInstance();
132 }
133 if (GRD_KVApiInfo.KVBatchPushbackApi == nullptr) {
134 return GRD_INNER_ERR;
135 }
136 return GRD_KVApiInfo.KVBatchPushbackApi(key, keyLen, data, dataLen, batch);
137 }
138
GRD_KVBatchPut(GRD_DB * db,const char * collectionName,GRD_KVBatchT * batch)139 GRD_API int32_t GRD_KVBatchPut(GRD_DB *db, const char *collectionName, GRD_KVBatchT *batch)
140 {
141 if (GRD_KVApiInfo.KVBatchPutApi == nullptr) {
142 GRD_KVApiInfo = GetApiInfoInstance();
143 }
144 if (GRD_KVApiInfo.KVBatchPutApi == nullptr) {
145 return GRD_INNER_ERR;
146 }
147 return GRD_KVApiInfo.KVBatchPutApi(db, collectionName, batch);
148 }
149
GRD_KVBatchDel(GRD_DB * db,const char * collectionName,GRD_KVBatchT * batch)150 GRD_API int32_t GRD_KVBatchDel(GRD_DB *db, const char *collectionName, GRD_KVBatchT *batch)
151 {
152 if (GRD_KVApiInfo.KVBatchDelApi == nullptr) {
153 GRD_KVApiInfo = GetApiInfoInstance();
154 }
155 if (GRD_KVApiInfo.KVBatchDelApi == nullptr) {
156 return GRD_INNER_ERR;
157 }
158 return GRD_KVApiInfo.KVBatchDelApi(db, collectionName, batch);
159 }
160
GRD_KVBatchDestroy(GRD_KVBatchT * batch)161 GRD_API int32_t GRD_KVBatchDestroy(GRD_KVBatchT *batch)
162 {
163 if (GRD_KVApiInfo.KVBatchDestoryApi == nullptr) {
164 GRD_KVApiInfo = GetApiInfoInstance();
165 }
166 if (GRD_KVApiInfo.KVBatchDestoryApi == nullptr) {
167 return GRD_INNER_ERR;
168 }
169 return GRD_KVApiInfo.KVBatchDestoryApi(batch);
170 }
171