• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (executor_ == nullptr) {
44         return -E_INNER_ERROR;
45     }
46     int errCode = executor_->InsertData(name_, key, valSet);
47     while (errCode == -E_DATA_CONFLICT) { // if id alreay exist, create new one.
48         DocumentKey::GetOidDocKey(docKey);
49         key.assign(docKey.key.begin(), docKey.key.end());
50         errCode = executor_->InsertData(name_, key, valSet);
51     }
52     return errCode;
53 }
InsertDocument(const std::string & id,const std::string & document,bool & isIdExist)54 int Collection::InsertDocument(const std::string &id, const std::string &document, bool &isIdExist)
55 {
56     if (executor_ == nullptr) {
57         return -E_INNER_ERROR;
58     }
59     int errCode = E_OK;
60     bool isCollectionExist = IsCollectionExists(errCode);
61     if (errCode != E_OK) {
62         return errCode;
63     }
64     if (!isCollectionExist) {
65         return -E_INVALID_ARGS;
66     }
67     Key key;
68     Value valSet(document.begin(), document.end());
69     if (!isIdExist) {
70         return InsertUntilSuccess(key, id, valSet);
71     }
72     key.assign(id.begin(), id.end());
73     return executor_->InsertData(name_, key, valSet);
74 }
75 
GetDocumentById(Key & key,Value & document) const76 int Collection::GetDocumentById(Key &key, Value &document) const
77 {
78     if (executor_ == nullptr) {
79         return -E_INNER_ERROR;
80     }
81     return executor_->GetDataById(name_, key, document);
82 }
83 
DeleteDocument(Key & key)84 int Collection::DeleteDocument(Key &key)
85 {
86     if (executor_ == nullptr) {
87         return -E_INNER_ERROR;
88     }
89     int errCode = E_OK;
90     bool isCollectionExist = IsCollectionExists(errCode);
91     if (errCode != E_OK) {
92         return errCode;
93     }
94     if (!isCollectionExist) {
95         return -E_INVALID_ARGS;
96     }
97     return executor_->DelData(name_, key);
98 }
99 
GetMatchedDocument(const JsonObject & filterObj,Key & key,std::pair<std::string,std::string> & values,int isIdExist) const100 int Collection::GetMatchedDocument(const JsonObject &filterObj, Key &key, std::pair<std::string, std::string> &values,
101     int isIdExist) const
102 {
103     if (executor_ == nullptr) {
104         return -E_INNER_ERROR;
105     }
106     return executor_->GetDataByFilter(name_, key, filterObj, values, isIdExist);
107 }
108 
IsCollectionExists(int & errCode)109 int Collection::IsCollectionExists(int &errCode)
110 {
111     return executor_->IsCollectionExists(name_, errCode);
112 }
113 
UpsertDocument(const std::string & id,const std::string & newDocument,bool & isDataExist)114 int Collection::UpsertDocument(const std::string &id, const std::string &newDocument, bool &isDataExist)
115 {
116     if (executor_ == nullptr) {
117         return -E_INNER_ERROR;
118     }
119     int errCode = E_OK;
120     bool isCollExist = executor_->IsCollectionExists(name_, errCode);
121     if (errCode != E_OK) {
122         GLOGE("Check collection failed. %d", errCode);
123         return -errCode;
124     }
125     if (!isCollExist) {
126         GLOGE("Collection not created.");
127         return -E_INVALID_ARGS;
128     }
129     Key key;
130     Value valSet(newDocument.begin(), newDocument.end());
131     if (!isDataExist) {
132         return InsertUntilSuccess(key, id, valSet);
133     }
134     key.assign(id.begin(), id.end());
135     return executor_->PutData(name_, key, valSet);
136 }
137 
UpdateDocument(const std::string & id,const std::string & newDocument)138 int Collection::UpdateDocument(const std::string &id, const std::string &newDocument)
139 {
140     if (executor_ == nullptr) {
141         return -E_INNER_ERROR;
142     }
143     int errCode = E_OK;
144     bool isCollExist = executor_->IsCollectionExists(name_, errCode);
145     if (errCode != E_OK) {
146         GLOGE("Check collection failed. %d", errCode);
147         return -errCode;
148     }
149     if (!isCollExist) {
150         GLOGE("Collection not created.");
151         return -E_INVALID_ARGS;
152     }
153     Key keyId(id.begin(), id.end());
154     Value valSet(newDocument.begin(), newDocument.end());
155     return executor_->PutData(name_, keyId, valSet);
156 }
157 } // namespace DocumentDB
158