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 #ifndef DOCUMENT_STORE_H
17 #define DOCUMENT_STORE_H
18 
19 #include <map>
20 #include <mutex>
21 #include <string>
22 
23 #include "collection.h"
24 #include "document_type.h"
25 #include "kv_store_executor.h"
26 
27 struct GRD_ResultSet;
28 namespace DocumentDB {
29 class DocumentStore {
30 public:
31     DocumentStore(KvStoreExecutor *);
32     ~DocumentStore();
33 
34     int CreateCollection(const std::string &name, const std::string &option, uint32_t flags);
35 
36     int DropCollection(const std::string &name, uint32_t flags);
37 
38     int UpdateDocument(const std::string &collection, const std::string &filter, const std::string &update,
39         uint32_t flags);
40 
41     int UpsertDocument(const std::string &collection, const std::string &filter, const std::string &document,
42         uint32_t flags);
43 
44     int InsertDocument(const std::string &collection, const std::string &document, uint32_t flags);
45 
46     int DeleteDocument(const std::string &collection, const std::string &filter, uint32_t flags);
47 
48     int FindDocument(const std::string &collection, const std::string &filter, const std::string &projection,
49         uint32_t flags, GRD_ResultSet *grdResultSet);
50 
51     Collection GetCollection(std::string &collectionName);
52 
53     bool IsExistResultSet(const std::string &collection);
54 
55     int EraseCollection(const std::string &collectionName);
56 
57     void OnClose(const std::function<void(void)> &notifier);
58 
59     int Close(uint32_t flags);
60 
61     int StartTransaction();
62     int Commit();
63     int Rollback();
64 
65     bool IsCollectionExists(const std::string &collectionName, int &errCode);
66 
67     std::mutex dbMutex_;
68 
69 private:
70     int UpdateDataIntoDB(std::shared_ptr<QueryContext> &context, JsonObject &filterObj, const std::string &update,
71         bool &isReplace);
72     int UpsertDataIntoDB(std::shared_ptr<QueryContext> &context, JsonObject &filterObj, const std::string &document,
73         JsonObject &documentObj, bool &isReplace);
74     int InsertDataIntoDB(const std::string &collection, const std::string &document, JsonObject &documentObj,
75         bool &isIdExist);
76     int DeleteDataFromDB(std::shared_ptr<QueryContext> &context, JsonObject &filterObj);
77     int InitFindResultSet(GRD_ResultSet *grdResultSet, std::shared_ptr<QueryContext> &context);
78     KvStoreExecutor *executor_ = nullptr;
79     std::map<std::string, Collection *> collections_;
80     std::function<void(void)> closeNotifier_;
81 };
82 } // namespace DocumentDB
83 #endif // DOCUMENT_STORE_H