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 #include "grd_api_manager.h"
17 
18 #ifndef _WIN32
19 #include <dlfcn.h>
20 #endif
21 
22 #include "check_common.h"
23 #include "doc_errno.h"
24 #include "document_store_manager.h"
25 #include "grd_base/grd_error.h"
26 #include "grd_db_api_inner.h"
27 #include "grd_document_api_inner.h"
28 #include "grd_kv_api_inner.h"
29 #include "grd_resultset_api_inner.h"
30 #include "grd_sequence_api_inner.h"
31 #include "grd_type_inner.h"
32 #include "rd_log_print.h"
33 
34 #ifndef _WIN32
35 static void *g_library = nullptr;
36 #endif
37 
38 static bool g_isGmdbLib = false;
39 
40 namespace DocumentDB {
GRD_DBApiInitCommon(GRD_APIInfo & GRD_DBApiInfo)41 void GRD_DBApiInitCommon(GRD_APIInfo &GRD_DBApiInfo)
42 {
43     GRD_DBApiInfo.DBOpenApi = GRD_DBOpenInner;
44     GRD_DBApiInfo.DBCloseApi = GRD_DBCloseInner;
45     GRD_DBApiInfo.FlushApi = GRD_FlushInner;
46     GRD_DBApiInfo.IndexPreloadApi = GRD_IndexPreloadInner;
47     GRD_DBApiInfo.CreateCollectionApi = GRD_CreateCollectionInner;
48     GRD_DBApiInfo.DropCollectionApi = GRD_DropCollectionInner;
49     GRD_DBApiInfo.InsertDocApi = GRD_InsertDocInner;
50     GRD_DBApiInfo.FindDocApi = GRD_FindDocInner;
51     GRD_DBApiInfo.UpdateDocApi = GRD_UpdateDocInner;
52     GRD_DBApiInfo.UpsertDocApi = GRD_UpsertDocInner;
53     GRD_DBApiInfo.DeleteDocApi = GRD_DeleteDocInner;
54     GRD_DBApiInfo.NextApi = GRD_NextInner;
55     GRD_DBApiInfo.PrevApi = GRD_PrevInner;
56     GRD_DBApiInfo.GetValueApi = GRD_GetValueInner;
57     GRD_DBApiInfo.FetchApi = GRD_FetchInner;
58     GRD_DBApiInfo.FreeValueApi = GRD_FreeValueInner;
59     GRD_DBApiInfo.FreeResultSetApi = GRD_FreeResultSetInner;
60     GRD_DBApiInfo.KVPutApi = GRD_KVPutInner;
61     GRD_DBApiInfo.KVGetApi = GRD_KVGetInner;
62     GRD_DBApiInfo.KVDelApi = GRD_KVDelInner;
63     GRD_DBApiInfo.KVScanApi = GRD_KVScanInner;
64     GRD_DBApiInfo.KVFilterApi = GRD_KVFilterInner;
65     GRD_DBApiInfo.KVGetSizeApi = GRD_KVGetSizeInner;
66     GRD_DBApiInfo.GetItemApi = GRD_GetItemInner;
67     GRD_DBApiInfo.KVFreeItemApi = GRD_KVFreeItemInner;
68     GRD_DBApiInfo.KVBatchPrepareApi = GRD_KVBatchPrepareInner;
69     GRD_DBApiInfo.KVBatchPushbackApi = GRD_KVBatchPushbackInner;
70     GRD_DBApiInfo.KVBatchPutApi = GRD_KVBatchPutInner;
71     GRD_DBApiInfo.KVBatchDelApi = GRD_KVBatchDelInner;
72     GRD_DBApiInfo.KVBatchDestoryApi = GRD_KVBatchDestroyInner;
73 }
74 
GRD_DBApiInitEnhance(GRD_APIInfo & GRD_DBApiInfo)75 void GRD_DBApiInitEnhance(GRD_APIInfo &GRD_DBApiInfo)
76 {
77 #ifndef _WIN32
78     GRD_DBApiInfo.DBOpenApi = (DBOpen)dlsym(g_library, "GRD_DBOpen");
79     GRD_DBApiInfo.DBCloseApi = (DBClose)dlsym(g_library, "GRD_DBClose");
80     GRD_DBApiInfo.DBBackupApi = (DBBackup)dlsym(g_library, "GRD_DBBackup");
81     GRD_DBApiInfo.DBRestoreApi = (DBRestore)dlsym(g_library, "GRD_DBRestore");
82     GRD_DBApiInfo.FlushApi = (DBFlush)dlsym(g_library, "GRD_Flush");
83     GRD_DBApiInfo.IndexPreloadApi = (IndexPreload)dlsym(g_library, "GRD_IndexPreload");
84     GRD_DBApiInfo.CreateCollectionApi = (CreateCollection)dlsym(g_library, "GRD_CreateCollection");
85     GRD_DBApiInfo.DropCollectionApi = (DropCollection)dlsym(g_library, "GRD_DropCollection");
86     GRD_DBApiInfo.InsertDocApi = (InsertDoc)dlsym(g_library, "GRD_InsertDoc");
87     GRD_DBApiInfo.FindDocApi = (FindDoc)dlsym(g_library, "GRD_FindDoc");
88     GRD_DBApiInfo.UpdateDocApi = (UpdateDoc)dlsym(g_library, "GRD_UpdateDoc");
89     GRD_DBApiInfo.UpsertDocApi = (UpsertDoc)dlsym(g_library, "GRD_UpsertDoc");
90     GRD_DBApiInfo.DeleteDocApi = (DeleteDoc)dlsym(g_library, "GRD_DeleteDoc");
91     GRD_DBApiInfo.NextApi = (ResultNext)dlsym(g_library, "GRD_Next");
92     GRD_DBApiInfo.PrevApi = (ResultPrev)dlsym(g_library, "GRD_Prev");
93     GRD_DBApiInfo.GetValueApi = (GetValue)dlsym(g_library, "GRD_GetValue");
94     GRD_DBApiInfo.FetchApi = (Fetch)dlsym(g_library, "GRD_Fetch");
95     GRD_DBApiInfo.FreeValueApi = (FreeValue)dlsym(g_library, "GRD_FreeValue");
96     GRD_DBApiInfo.FreeResultSetApi = (FreeResultSet)dlsym(g_library, "GRD_FreeResultSet");
97     GRD_DBApiInfo.KVPutApi = (KVPut)dlsym(g_library, "GRD_KVPut");
98     GRD_DBApiInfo.KVGetApi = (KVGet)dlsym(g_library, "GRD_KVGet");
99     GRD_DBApiInfo.KVDelApi = (KVDel)dlsym(g_library, "GRD_KVDel");
100     GRD_DBApiInfo.KVScanApi = (KVScan)dlsym(g_library, "GRD_KVScan");
101     GRD_DBApiInfo.KVFilterApi = (KVFilter)dlsym(g_library, "GRD_KVFilter");
102     GRD_DBApiInfo.KVGetSizeApi = (KVGetSize)dlsym(g_library, "GRD_KVGetSize");
103     GRD_DBApiInfo.GetItemApi = (GetItem)dlsym(g_library, "GRD_GetItem");
104     GRD_DBApiInfo.KVFreeItemApi = (KVFreeItem)dlsym(g_library, "GRD_KVFreeItem");
105     GRD_DBApiInfo.KVBatchPrepareApi = (KVBatchPrepare)dlsym(g_library, "GRD_KVBatchPrepare");
106     GRD_DBApiInfo.KVBatchPushbackApi = (KVBatchPushback)dlsym(g_library, "GRD_KVBatchPushback");
107     GRD_DBApiInfo.KVBatchPutApi = (KVBatchPut)dlsym(g_library, "GRD_KVBatchPut");
108     GRD_DBApiInfo.KVBatchDelApi = (KVBatchDel)dlsym(g_library, "GRD_KVBatchDel");
109     GRD_DBApiInfo.KVBatchDestoryApi = (KVBatchDestory)dlsym(g_library, "GRD_KVBatchDestroy");
110 #endif
111 }
112 
InitApiInfo(const char * configStr)113 void InitApiInfo(const char *configStr)
114 {
115     g_isGmdbLib = (configStr != nullptr);
116 }
117 
GetApiInfoInstance()118 GRD_APIInfo GetApiInfoInstance()
119 {
120     GRD_APIInfo GRD_TempApiStruct;
121 #ifndef _WIN32
122     std::string libPath = g_isGmdbLib ? "libarkdata_db_core.z.so" : "libgaussdb_rd.z.so";
123     g_library = dlopen(libPath.c_str(), RTLD_LAZY);
124     if (!g_library) {
125         GRD_DBApiInitCommon(GRD_TempApiStruct); // When calling specific function, read whether init is successful.
126     } else {
127         GRD_DBApiInitEnhance(GRD_TempApiStruct);
128     }
129 #endif
130     return GRD_TempApiStruct;
131 }
132 } // namespace DocumentDB
133