• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "sqlite_single_ver_natural_store.h"
27 #ifndef OMIT_MULTI_VER
28 #include "sqlite_multi_ver_data_storage.h"
29 #endif
30 
31 namespace DistributedDB {
CreateKvDb(KvDBType kvDbType,int & errCode)32 IKvDB *DefaultFactory::CreateKvDb(KvDBType kvDbType, int &errCode)
33 {
34     switch (kvDbType) {
35         case LOCAL_KVDB:
36             return CreateLocalKvDB(errCode);
37         case SINGER_VER_KVDB:
38             return CreateSingleVerNaturalStore(errCode);
39 #ifndef OMIT_MULTI_VER
40         case MULTI_VER_KVDB:
41             return CreateMultiVerNaturalStore(errCode);
42 #endif
43         default:
44             errCode = -E_INVALID_ARGS;
45             return nullptr;
46     }
47 }
48 
CreateLocalKvDB(int & errCode)49 IKvDB *DefaultFactory::CreateLocalKvDB(int &errCode)
50 {
51     IKvDB *kvDb = new (std::nothrow) SQLiteLocalKvDB();
52     errCode = ((kvDb == nullptr) ? -E_OUT_OF_MEMORY : E_OK);
53     return kvDb;
54 }
55 
56 #ifndef OMIT_MULTI_VER
57 // Create the multi-version natural store, it contains a commit version and commit storage kvdb.
CreateMultiVerNaturalStore(int & errCode)58 IKvDB *DefaultFactory::CreateMultiVerNaturalStore(int &errCode)
59 {
60     IKvDB *kvDb = new (std::nothrow) MultiVerNaturalStore();
61     errCode = ((kvDb == nullptr) ? -E_OUT_OF_MEMORY : E_OK);
62     return kvDb;
63 }
64 #endif
65 
66 // Create the single version natural store.
CreateSingleVerNaturalStore(int & errCode)67 IKvDB *DefaultFactory::CreateSingleVerNaturalStore(int &errCode)
68 {
69     IKvDB *kvDb = new (std::nothrow) SQLiteSingleVerNaturalStore();
70     errCode = ((kvDb == nullptr) ? -E_OUT_OF_MEMORY : E_OK);
71     return kvDb;
72 }
73 
74 // Create a key-value database for commit storage module.
CreateCommitStorageDB(int & errCode)75 IKvDB *DefaultFactory::CreateCommitStorageDB(int &errCode)
76 {
77     return CreateLocalKvDB(errCode);
78 }
79 
80 #ifndef OMIT_MULTI_VER
CreateMultiVerStorage(int & errCode)81 IKvDBMultiVerDataStorage *DefaultFactory::CreateMultiVerStorage(int &errCode)
82 {
83     IKvDBMultiVerDataStorage *multiStorage = new (std::nothrow) SQLiteMultiVerDataStorage();
84     errCode = ((multiStorage == nullptr) ? -E_OUT_OF_MEMORY : E_OK);
85     return multiStorage;
86 }
87 
88 // Create the commit storage. The object can be deleted directly when it is not needed.
CreateMultiVerCommitStorage(int & errCode)89 IKvDBCommitStorage *DefaultFactory::CreateMultiVerCommitStorage(int &errCode)
90 {
91     IKvDBCommitStorage *commitStorage = new (std::nothrow) MultiVerNaturalStoreCommitStorage();
92     errCode = ((commitStorage == nullptr) ? -E_OUT_OF_MEMORY : E_OK);
93     return commitStorage;
94 }
95 #endif
96 } // namespace DistributedDB
97