1 /*
2  * Copyright (c) 2021 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 "default_factory.h"
17 
18 #include <string>
19 
20 #include "db_errno.h"
21 #include "sqlite_local_kvdb.h"
22 #ifndef OMIT_MULTI_VER
23 #include "multi_ver_natural_store.h"
24 #include "multi_ver_natural_store_commit_storage.h"
25 #endif
26 #include "rd_single_ver_natural_store.h"
27 #include "sqlite_single_ver_natural_store.h"
28 #ifndef OMIT_MULTI_VER
29 #include "sqlite_multi_ver_data_storage.h"
30 #endif
31 
32 namespace DistributedDB {
CreateKvDb(KvDBType kvDbType,int & errCode)33 IKvDB *DefaultFactory::CreateKvDb(KvDBType kvDbType, int &errCode)
34 {
35     switch (kvDbType) {
36 #ifndef OMIT_MULTI_VER
37         case LOCAL_KVDB_SQLITE:
38             return CreateLocalKvDB(errCode);
39 #endif
40         case SINGER_VER_KVDB_SQLITE:
41             return CreateSingleVerNaturalStore(errCode);
42         case SINGLE_VER_KVDB_RD:
43             return CreateRdSingleVerNaturalStore(errCode);
44 #ifndef OMIT_MULTI_VER
45         case MULTI_VER_KVDB_SQLITE:
46             return CreateMultiVerNaturalStore(errCode);
47 #endif
48         default:
49             errCode = -E_INVALID_ARGS;
50             return nullptr;
51     }
52 }
53 
54 #ifndef OMIT_MULTI_VER
CreateLocalKvDB(int & errCode)55 IKvDB *DefaultFactory::CreateLocalKvDB(int &errCode)
56 {
57     IKvDB *kvDb = new (std::nothrow) SQLiteLocalKvDB();
58     errCode = ((kvDb == nullptr) ? -E_OUT_OF_MEMORY : E_OK);
59     return kvDb;
60 }
61 
62 // Create the multi-version natural store, it contains a commit version and commit storage kvdb.
CreateMultiVerNaturalStore(int & errCode)63 IKvDB *DefaultFactory::CreateMultiVerNaturalStore(int &errCode)
64 {
65     IKvDB *kvDb = new (std::nothrow) MultiVerNaturalStore();
66     errCode = ((kvDb == nullptr) ? -E_OUT_OF_MEMORY : E_OK);
67     return kvDb;
68 }
69 #endif
70 
71 // Create the single version natural store.
CreateSingleVerNaturalStore(int & errCode)72 IKvDB *DefaultFactory::CreateSingleVerNaturalStore(int &errCode)
73 {
74     IKvDB *kvDb = new (std::nothrow) SQLiteSingleVerNaturalStore();
75     errCode = ((kvDb == nullptr) ? -E_OUT_OF_MEMORY : E_OK);
76     return kvDb;
77 }
78 
79 // Create the single version natural store with gaussdb_rd
CreateRdSingleVerNaturalStore(int & errCode)80 IKvDB *DefaultFactory::CreateRdSingleVerNaturalStore(int &errCode)
81 {
82     IKvDB *kvDb = new (std::nothrow) RdSingleVerNaturalStore();
83     errCode = ((kvDb == nullptr) ? -E_OUT_OF_MEMORY : E_OK);
84     return kvDb;
85 }
86 
87 #ifndef OMIT_MULTI_VER
88 // Create a key-value database for commit storage module.
CreateCommitStorageDB(int & errCode)89 IKvDB *DefaultFactory::CreateCommitStorageDB(int &errCode)
90 {
91     return CreateLocalKvDB(errCode);
92 }
93 #endif
94 
95 #ifndef OMIT_MULTI_VER
CreateMultiVerStorage(int & errCode)96 IKvDBMultiVerDataStorage *DefaultFactory::CreateMultiVerStorage(int &errCode)
97 {
98     IKvDBMultiVerDataStorage *multiStorage = new (std::nothrow) SQLiteMultiVerDataStorage();
99     errCode = ((multiStorage == nullptr) ? -E_OUT_OF_MEMORY : E_OK);
100     return multiStorage;
101 }
102 
103 // Create the commit storage. The object can be deleted directly when it is not needed.
CreateMultiVerCommitStorage(int & errCode)104 IKvDBCommitStorage *DefaultFactory::CreateMultiVerCommitStorage(int &errCode)
105 {
106     IKvDBCommitStorage *commitStorage = new (std::nothrow) MultiVerNaturalStoreCommitStorage();
107     errCode = ((commitStorage == nullptr) ? -E_OUT_OF_MEMORY : E_OK);
108     return commitStorage;
109 }
110 #endif
111 } // namespace DistributedDB
112