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