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
16 #include "collection.h"
17
18 #include "check_common.h"
19 #include "rd_db_constant.h"
20 #include "doc_errno.h"
21 #include "document_key.h"
22 #include "rd_log_print.h"
23
24 namespace DocumentDB {
Collection(const std::string & name,KvStoreExecutor * executor)25 Collection::Collection(const std::string &name, KvStoreExecutor *executor) : executor_(executor)
26 {
27 std::string lowerCaseName = name;
28 std::transform(lowerCaseName.begin(), lowerCaseName.end(), lowerCaseName.begin(), [](unsigned char c) {
29 return std::tolower(c);
30 });
31 name_ = RdDBConstant::COLL_PREFIX + lowerCaseName;
32 }
33
~Collection()34 Collection::~Collection()
35 {
36 executor_ = nullptr;
37 }
38
InsertUntilSuccess(Key & key,const std::string & id,Value & valSet)39 int Collection::InsertUntilSuccess(Key &key, const std::string &id, Value &valSet)
40 {
41 DocKey docKey;
42 key.assign(id.begin(), id.end());
43 int errCode = executor_->InsertData(name_, key, valSet);
44 while (errCode == -E_DATA_CONFLICT) { // if id alreay exist, create new one.
45 DocumentKey::GetOidDocKey(docKey);
46 key.assign(docKey.key.begin(), docKey.key.end());
47 errCode = executor_->InsertData(name_, key, valSet);
48 }
49 return errCode;
50 }
InsertDocument(const std::string & id,const std::string & document,bool & isIdExist)51 int Collection::InsertDocument(const std::string &id, const std::string &document, bool &isIdExist)
52 {
53 if (executor_ == nullptr) {
54 return -E_INNER_ERROR;
55 }
56 int errCode = E_OK;
57 bool isCollectionExist = IsCollectionExists(errCode);
58 if (errCode != E_OK) {
59 return errCode;
60 }
61 if (!isCollectionExist) {
62 return -E_INVALID_ARGS;
63 }
64 Key key;
65 Value valSet(document.begin(), document.end());
66 if (!isIdExist) {
67 return InsertUntilSuccess(key, id, valSet);
68 }
69 key.assign(id.begin(), id.end());
70 return executor_->InsertData(name_, key, valSet);
71 }
72
GetDocumentById(Key & key,Value & document) const73 int Collection::GetDocumentById(Key &key, Value &document) const
74 {
75 if (executor_ == nullptr) {
76 return -E_INNER_ERROR;
77 }
78 return executor_->GetDataById(name_, key, document);
79 }
80
DeleteDocument(Key & key)81 int Collection::DeleteDocument(Key &key)
82 {
83 if (executor_ == nullptr) {
84 return -E_INNER_ERROR;
85 }
86 int errCode = E_OK;
87 bool isCollectionExist = IsCollectionExists(errCode);
88 if (errCode != E_OK) {
89 return errCode;
90 }
91 if (!isCollectionExist) {
92 return -E_INVALID_ARGS;
93 }
94 return executor_->DelData(name_, key);
95 }
96
GetMatchedDocument(const JsonObject & filterObj,Key & key,std::pair<std::string,std::string> & values,int isIdExist) const97 int Collection::GetMatchedDocument(const JsonObject &filterObj, Key &key, std::pair<std::string, std::string> &values,
98 int isIdExist) const
99 {
100 if (executor_ == nullptr) {
101 return -E_INNER_ERROR;
102 }
103 return executor_->GetDataByFilter(name_, key, filterObj, values, isIdExist);
104 }
105
IsCollectionExists(int & errCode)106 int Collection::IsCollectionExists(int &errCode)
107 {
108 return executor_->IsCollectionExists(name_, errCode);
109 }
110
UpsertDocument(const std::string & id,const std::string & newDocument,bool & isDataExist)111 int Collection::UpsertDocument(const std::string &id, const std::string &newDocument, bool &isDataExist)
112 {
113 if (executor_ == nullptr) {
114 return -E_INNER_ERROR;
115 }
116 int errCode = E_OK;
117 bool isCollExist = executor_->IsCollectionExists(name_, errCode);
118 if (errCode != E_OK) {
119 GLOGE("Check collection failed. %d", errCode);
120 return -errCode;
121 }
122 if (!isCollExist) {
123 GLOGE("Collection not created.");
124 return -E_INVALID_ARGS;
125 }
126 Key key;
127 Value valSet(newDocument.begin(), newDocument.end());
128 if (!isDataExist) {
129 return InsertUntilSuccess(key, id, valSet);
130 }
131 key.assign(id.begin(), id.end());
132 return executor_->PutData(name_, key, valSet);
133 }
134
UpdateDocument(const std::string & id,const std::string & newDocument)135 int Collection::UpdateDocument(const std::string &id, const std::string &newDocument)
136 {
137 if (executor_ == nullptr) {
138 return -E_INNER_ERROR;
139 }
140 int errCode = E_OK;
141 bool isCollExist = executor_->IsCollectionExists(name_, errCode);
142 if (errCode != E_OK) {
143 GLOGE("Check collection failed. %d", errCode);
144 return -errCode;
145 }
146 if (!isCollExist) {
147 GLOGE("Collection not created.");
148 return -E_INVALID_ARGS;
149 }
150 Key keyId(id.begin(), id.end());
151 Value valSet(newDocument.begin(), newDocument.end());
152 return executor_->PutData(name_, keyId, valSet);
153 }
154 } // namespace DocumentDB
155