• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static int g_openCount = 0;
37 static std::mutex g_apiInfoMutex;
38 #endif
39 
40 static bool g_isGmdbLib = false;
41 
42 namespace DocumentDB {
GRD_DBApiInitCommon(GRD_APIInfo & GRD_DBApiInfo)43 void GRD_DBApiInitCommon(GRD_APIInfo &GRD_DBApiInfo)
44 {
45     GRD_DBApiInfo.DBOpenApi = GRD_DBOpenInner;
46     GRD_DBApiInfo.DBCloseApi = GRD_DBCloseInner;
47     GRD_DBApiInfo.FlushApi = GRD_FlushInner;
48     GRD_DBApiInfo.IndexPreloadApi = GRD_IndexPreloadInner;
49     GRD_DBApiInfo.CreateCollectionApi = GRD_CreateCollectionInner;
50     GRD_DBApiInfo.DropCollectionApi = GRD_DropCollectionInner;
51     GRD_DBApiInfo.InsertDocApi = GRD_InsertDocInner;
52     GRD_DBApiInfo.FindDocApi = GRD_FindDocInner;
53     GRD_DBApiInfo.UpdateDocApi = GRD_UpdateDocInner;
54     GRD_DBApiInfo.UpsertDocApi = GRD_UpsertDocInner;
55     GRD_DBApiInfo.DeleteDocApi = GRD_DeleteDocInner;
56     GRD_DBApiInfo.NextApi = GRD_NextInner;
57     GRD_DBApiInfo.PrevApi = GRD_PrevInner;
58     GRD_DBApiInfo.GetValueApi = GRD_GetValueInner;
59     GRD_DBApiInfo.FetchApi = GRD_FetchInner;
60     GRD_DBApiInfo.FreeValueApi = GRD_FreeValueInner;
61     GRD_DBApiInfo.FreeResultSetApi = GRD_FreeResultSetInner;
62     GRD_DBApiInfo.KVPutApi = GRD_KVPutInner;
63     GRD_DBApiInfo.KVGetApi = GRD_KVGetInner;
64     GRD_DBApiInfo.KVDelApi = GRD_KVDelInner;
65     GRD_DBApiInfo.KVScanApi = GRD_KVScanInner;
66     GRD_DBApiInfo.KVFilterApi = GRD_KVFilterInner;
67     GRD_DBApiInfo.KVGetSizeApi = GRD_KVGetSizeInner;
68     GRD_DBApiInfo.GetItemApi = GRD_GetItemInner;
69     GRD_DBApiInfo.KVFreeItemApi = GRD_KVFreeItemInner;
70     GRD_DBApiInfo.KVBatchPrepareApi = GRD_KVBatchPrepareInner;
71     GRD_DBApiInfo.KVBatchPushbackApi = GRD_KVBatchPushbackInner;
72     GRD_DBApiInfo.KVBatchPutApi = GRD_KVBatchPutInner;
73     GRD_DBApiInfo.KVBatchDelApi = GRD_KVBatchDelInner;
74     GRD_DBApiInfo.KVBatchDestroyApi = GRD_KVBatchDestroyInner;
75 }
76 
GRD_DBApiInitEnhance(GRD_APIInfo & GRD_DBApiInfo)77 void GRD_DBApiInitEnhance(GRD_APIInfo &GRD_DBApiInfo)
78 {
79 #ifndef _WIN32
80     GRD_DBApiInfo.DBOpenApi = (DBOpen)dlsym(g_library, "GRD_DBOpen");
81     GRD_DBApiInfo.DBCloseApi = (DBClose)dlsym(g_library, "GRD_DBClose");
82     GRD_DBApiInfo.DBBackupApi = (DBBackup)dlsym(g_library, "GRD_DBBackup");
83     GRD_DBApiInfo.DBRestoreApi = (DBRestore)dlsym(g_library, "GRD_DBRestore");
84     GRD_DBApiInfo.FlushApi = (DBFlush)dlsym(g_library, "GRD_Flush");
85     GRD_DBApiInfo.IndexPreloadApi = (IndexPreload)dlsym(g_library, "GRD_IndexPreload");
86     GRD_DBApiInfo.CreateCollectionApi = (CreateCollection)dlsym(g_library, "GRD_CreateCollection");
87     GRD_DBApiInfo.DropCollectionApi = (DropCollection)dlsym(g_library, "GRD_DropCollection");
88     GRD_DBApiInfo.InsertDocApi = (InsertDoc)dlsym(g_library, "GRD_InsertDoc");
89     GRD_DBApiInfo.FindDocApi = (FindDoc)dlsym(g_library, "GRD_FindDoc");
90     GRD_DBApiInfo.UpdateDocApi = (UpdateDoc)dlsym(g_library, "GRD_UpdateDoc");
91     GRD_DBApiInfo.UpsertDocApi = (UpsertDoc)dlsym(g_library, "GRD_UpsertDoc");
92     GRD_DBApiInfo.DeleteDocApi = (DeleteDoc)dlsym(g_library, "GRD_DeleteDoc");
93     GRD_DBApiInfo.NextApi = (ResultNext)dlsym(g_library, "GRD_Next");
94     GRD_DBApiInfo.PrevApi = (ResultPrev)dlsym(g_library, "GRD_Prev");
95     GRD_DBApiInfo.GetValueApi = (GetValue)dlsym(g_library, "GRD_GetValue");
96     GRD_DBApiInfo.FetchApi = (Fetch)dlsym(g_library, "GRD_Fetch");
97     GRD_DBApiInfo.FreeValueApi = (FreeValue)dlsym(g_library, "GRD_FreeValue");
98     GRD_DBApiInfo.FreeResultSetApi = (FreeResultSet)dlsym(g_library, "GRD_FreeResultSet");
99     GRD_DBApiInfo.KVPutApi = (KVPut)dlsym(g_library, "GRD_KVPut");
100     GRD_DBApiInfo.KVGetApi = (KVGet)dlsym(g_library, "GRD_KVGet");
101     GRD_DBApiInfo.KVDelApi = (KVDel)dlsym(g_library, "GRD_KVDel");
102     GRD_DBApiInfo.KVScanApi = (KVScan)dlsym(g_library, "GRD_KVScan");
103     GRD_DBApiInfo.KVFilterApi = (KVFilter)dlsym(g_library, "GRD_KVFilter");
104     GRD_DBApiInfo.KVGetSizeApi = (KVGetSize)dlsym(g_library, "GRD_KVGetSize");
105     GRD_DBApiInfo.GetItemApi = (GetItem)dlsym(g_library, "GRD_GetItem");
106     GRD_DBApiInfo.KVFreeItemApi = (KVFreeItem)dlsym(g_library, "GRD_KVFreeItem");
107     GRD_DBApiInfo.KVBatchPrepareApi = (KVBatchPrepare)dlsym(g_library, "GRD_KVBatchPrepare");
108     GRD_DBApiInfo.KVBatchPushbackApi = (KVBatchPushback)dlsym(g_library, "GRD_KVBatchPushback");
109     GRD_DBApiInfo.KVBatchPutApi = (KVBatchPut)dlsym(g_library, "GRD_KVBatchPut");
110     GRD_DBApiInfo.KVBatchDelApi = (KVBatchDel)dlsym(g_library, "GRD_KVBatchDel");
111     GRD_DBApiInfo.KVBatchDestroyApi = (KVBatchDestroy)dlsym(g_library, "GRD_KVBatchDestroy");
112 #endif
113 }
114 
InitApiInfo(const char * configStr)115 void InitApiInfo(const char *configStr)
116 {
117     g_isGmdbLib = (configStr != nullptr);
118 }
119 
120 static GRD_APIInfo GRD_ApiInfo;
121 
GetApiInfo(void)122 GRD_APIInfo *GetApiInfo(void)
123 {
124     return &GRD_ApiInfo;
125 }
126 
GetApiInfoInstance(void)127 void GetApiInfoInstance(void)
128 {
129 #ifndef _WIN32
130     std::lock_guard<std::mutex> lock(g_apiInfoMutex);
131     g_openCount++;
132     if (g_library != nullptr) {
133         return;
134     }
135     std::string libPath = g_isGmdbLib ? "libarkdata_db_core.z.so" : "libgaussdb_rd.z.so";
136     g_library = dlopen(libPath.c_str(), RTLD_LAZY);
137     if (!g_library) {
138         GRD_DBApiInitCommon(GRD_ApiInfo); // When calling specific function, read whether init is successful.
139     } else {
140         GRD_DBApiInitEnhance(GRD_ApiInfo);
141     }
142 #endif
143 }
144 
UnloadApiInfo(GRD_APIInfo * GRD_DBApiInfo)145 void UnloadApiInfo(GRD_APIInfo *GRD_DBApiInfo)
146 {
147 #ifndef _WIN32
148     std::lock_guard<std::mutex> lock(g_apiInfoMutex);
149     g_openCount--;
150     if (g_library == nullptr) {
151         return;
152     }
153     if (g_openCount == 0) {
154         dlclose(g_library);
155         *GRD_DBApiInfo = {0};
156         g_library = nullptr;
157     }
158 #endif
159 }
160 } // namespace DocumentDB
161