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 #ifndef I_KV_DB_FACTORY_H
17 #define I_KV_DB_FACTORY_H
18 
19 #include <mutex>
20 
21 #include "ikvdb.h"
22 #include "db_types.h"
23 #ifndef OMIT_MULTI_VER
24 #include "ikvdb_multi_ver_data_storage.h"
25 #include "ikvdb_commit_storage.h"
26 #endif
27 
28 namespace DistributedDB {
29 enum KvDBType {
30     LOCAL_KVDB_SQLITE = 0,
31     SINGER_VER_KVDB_SQLITE,
32     SINGLE_VER_KVDB_RD,
33 #ifndef OMIT_MULTI_VER
34     MULTI_VER_KVDB_SQLITE,
35 #endif // OMIT_MULTI_VER
36     UNSUPPORT_KVDB_TYPE = 4,
37 };
38 
39 class IKvDBFactory {
40 public:
~IKvDBFactory()41     virtual ~IKvDBFactory() {}
42 
43     // Get current factory object.
44     static IKvDBFactory *GetCurrent();
45 
46     // Set the factory object to 'current'
47     static void Register(IKvDBFactory *factory);
48 
49     virtual IKvDB *CreateKvDb(KvDBType kvDbType, int &errCode) = 0;
50 
51 #ifndef OMIT_MULTI_VER
52     // Create a key-value database for commit storage module.
53     virtual IKvDB *CreateCommitStorageDB(int &errCode) = 0;
54 
55     // Create the multi version storage for multi version natural store
56     virtual IKvDBMultiVerDataStorage *CreateMultiVerStorage(int &errCode) = 0;
57 
58     // Create the commit storage. The object can be deleted directly when it is not needed.
59     virtual IKvDBCommitStorage *CreateMultiVerCommitStorage(int &errCode) = 0;
60 #endif
61 private:
62     static IKvDBFactory *factory_;
63     static std::mutex instanceLock_;
64 };
65 } // namespace DistributedDB
66 
67 #endif // I_KV_DB_FACTORY_H
68