mirror of
https://gitee.com/openharmony/communication_dsoftbus
synced 2024-11-24 01:09:51 +00:00
commit
ff5f50f6d5
@ -23,6 +23,9 @@ extern "C" {
|
||||
#endif
|
||||
int32_t LnnCreateKvAdapter(int32_t *dbId, const char *appId, int32_t appIdLen, const char *storeId, int32_t storeIdLen);
|
||||
int32_t LnnDestroyKvAdapter(int32_t dbId);
|
||||
void LnnRegisterDataChangeListener(int32_t dbId, const char *appId, int32_t appIdLen, const char *storeId,
|
||||
int32_t storeIdLen);
|
||||
void LnnUnRegisterDataChangeListener(int32_t dbId);
|
||||
int32_t LnnPutDBData(int32_t dbId, const char *key, int32_t keyLen, const char *value, int32_t valueLen);
|
||||
int32_t LnnPutDBDataBatch(int32_t dbId, const CloudSyncInfo *localInfo);
|
||||
int32_t LnnDeleteDBData(int32_t dbId, const char *key, int32_t keyLen);
|
||||
|
@ -55,7 +55,7 @@ int32_t LnnCreateKvAdapter(int32_t *dbId, const char *appId, int32_t appIdLen, c
|
||||
std::shared_ptr<KVAdapter> kvAdapter = nullptr;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_kvAdapterWrapperMutex);
|
||||
kvAdapter = std::make_shared<KVAdapter>(appIdStr, storeIdStr, std::make_shared<KvDataChangeListener>());
|
||||
kvAdapter = std::make_shared<KVAdapter>(appIdStr, storeIdStr);
|
||||
int32_t initRet = kvAdapter->Init();
|
||||
if (initRet != SOFTBUS_OK) {
|
||||
LNN_LOGE(LNN_LEDGER, "kvAdapter init failed, ret=%{public}d", initRet);
|
||||
@ -188,7 +188,7 @@ int32_t LnnGetDBData(int32_t dbId, const char *key, int32_t keyLen, char **value
|
||||
LNN_LOGE(LNN_LEDGER, "strdup failed");
|
||||
return SOFTBUS_MALLOC_ERR;
|
||||
}
|
||||
LNN_LOGI(LNN_LEDGER, "kvAdapter get success, dbId=%{public}d", dbId);
|
||||
LNN_LOGD(LNN_LEDGER, "kvAdapter get success, dbId=%{public}d", dbId);
|
||||
return SOFTBUS_OK;
|
||||
}
|
||||
|
||||
@ -363,3 +363,46 @@ void ComplexCloudSyncInfoToMap(const CloudSyncInfo *localInfo, std::map<std::str
|
||||
LNN_LOGE(LNN_LEDGER, "convert cipher and rpa info to map fail");
|
||||
}
|
||||
}
|
||||
|
||||
void LnnRegisterDataChangeListener(int32_t dbId, const char *appId, int32_t appIdLen, const char *storeId,
|
||||
int32_t storeIdLen)
|
||||
{
|
||||
int32_t status;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_kvAdapterWrapperMutex);
|
||||
if (dbId < MIN_DBID_COUNT || dbId >= g_dbId || appId == nullptr || appIdLen < MIN_STRING_LEN ||
|
||||
appIdLen > MAX_STRING_LEN || storeId == nullptr || storeIdLen < MIN_STRING_LEN ||
|
||||
storeIdLen > MAX_STRING_LEN) {
|
||||
LNN_LOGE(LNN_LEDGER, "invalid param");
|
||||
return;
|
||||
}
|
||||
std::string appIdStr(appId, appIdLen);
|
||||
std::string storeIdStr(storeId, storeIdLen);
|
||||
auto kvAdapter = FindKvStorePtr(dbId);
|
||||
if (kvAdapter == nullptr) {
|
||||
LNN_LOGE(LNN_LEDGER, "kvAdapter is not exist, dbId=%{public}d", dbId);
|
||||
return;
|
||||
}
|
||||
status = kvAdapter->RegisterDataChangeListener(std::make_shared<KvDataChangeListener>(appIdStr, storeIdStr));
|
||||
}
|
||||
if (status != SOFTBUS_OK) {
|
||||
LNN_LOGE(LNN_LEDGER, "RegisterDataChangeListener failed");
|
||||
return;
|
||||
}
|
||||
LNN_LOGI(LNN_LEDGER, "RegisterDataChangeListener success");
|
||||
}
|
||||
|
||||
void LnnUnRegisterDataChangeListener(int32_t dbId)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_kvAdapterWrapperMutex);
|
||||
if (dbId < MIN_DBID_COUNT || dbId >= g_dbId) {
|
||||
LNN_LOGI(LNN_LEDGER, "Invalid dbId ");
|
||||
return;
|
||||
}
|
||||
auto kvAdapter = FindKvStorePtr(dbId);
|
||||
if (kvAdapter == nullptr) {
|
||||
LNN_LOGE(LNN_LEDGER, "kvAdapter is not exist, dbId=%{public}d", dbId);
|
||||
return;
|
||||
}
|
||||
kvAdapter->DeRegisterDataChangeListener();
|
||||
}
|
||||
|
@ -79,3 +79,17 @@ int32_t LnnCloudSync(int32_t dbId)
|
||||
(void)dbId;
|
||||
return SOFTBUS_NOT_IMPLEMENT;
|
||||
}
|
||||
|
||||
void LnnRegisterDataChangeListener(int32_t dbId, const char *appId, int32_t appIdLen, const char *storeId,
|
||||
int32_t storeIdLen)
|
||||
{
|
||||
(void)dbId;
|
||||
(void)appId;
|
||||
(void)appIdLen;
|
||||
(void)storeId;
|
||||
(void)storeIdLen;
|
||||
}
|
||||
void LnnUnRegisterDataChangeListener(int32_t dbId)
|
||||
{
|
||||
(void)dbId;
|
||||
}
|
||||
|
@ -26,8 +26,7 @@
|
||||
namespace OHOS {
|
||||
class KVAdapter {
|
||||
public:
|
||||
KVAdapter(const std::string &appId, const std::string &storeId,
|
||||
const std::shared_ptr<DistributedKv::KvStoreObserver> &dataChangeListener);
|
||||
KVAdapter(const std::string &appId, const std::string &storeId);
|
||||
virtual ~KVAdapter();
|
||||
|
||||
int32_t Init();
|
||||
@ -39,12 +38,13 @@ public:
|
||||
int32_t Get(const std::string &key, std::string &value);
|
||||
int32_t DeleteKvStore();
|
||||
int32_t CloudSync();
|
||||
int32_t RegisterDataChangeListener(const std::shared_ptr<DistributedKv::KvStoreObserver> &dataChangeListener);
|
||||
void DeRegisterDataChangeListener();
|
||||
static void CloudSyncCallback(DistributedKv::ProgressDetail &&detail);
|
||||
|
||||
private:
|
||||
DistributedKv::Status GetKvStorePtr();
|
||||
int32_t DeleteKvStorePtr();
|
||||
int32_t RegisterDataChangeListener();
|
||||
int32_t UnRegisterDataChangeListener();
|
||||
int32_t DeleteDataChangeListener();
|
||||
|
||||
|
@ -23,7 +23,7 @@
|
||||
namespace OHOS {
|
||||
class KvDataChangeListener : public DistributedKv::KvStoreObserver {
|
||||
public:
|
||||
KvDataChangeListener();
|
||||
KvDataChangeListener(const std::string &appId, const std::string &storeId);
|
||||
~KvDataChangeListener();
|
||||
|
||||
void OnChange(const DistributedKv::DataOrigin &origin, Keys &&keys) override;
|
||||
@ -35,6 +35,10 @@ private:
|
||||
void HandleUpdateChange(const std::vector<DistributedKv::Entry> &updateRecords);
|
||||
void HandleDeleteChange(const std::vector<DistributedKv::Entry> &deleteRecords);
|
||||
std::string GetKeyPrefix(const std::string &key);
|
||||
|
||||
private:
|
||||
std::string appId_;
|
||||
std::string storeId_;
|
||||
};
|
||||
} // namespace OHOS
|
||||
#endif // LNN_KV_DATA_CHANGE_LISTENER_H
|
||||
|
@ -36,12 +36,10 @@ constexpr int32_t MAX_MAP_SIZE = 10000;
|
||||
const std::string DATABASE_DIR = "/data/service/el1/public/database/dsoftbus";
|
||||
} // namespace
|
||||
|
||||
KVAdapter::KVAdapter(const std::string &appId, const std::string &storeId,
|
||||
const std::shared_ptr<DistributedKv::KvStoreObserver> &dataChangeListener)
|
||||
KVAdapter::KVAdapter(const std::string &appId, const std::string &storeId)
|
||||
{
|
||||
this->appId_.appId = appId;
|
||||
this->storeId_.storeId = storeId;
|
||||
this->dataChangeListener_ = dataChangeListener;
|
||||
LNN_LOGI(LNN_LEDGER, "KVAdapter Constructor Success, appId: %{public}s, storeId: %{public}s", appId.c_str(),
|
||||
storeId.c_str());
|
||||
}
|
||||
@ -61,7 +59,6 @@ int32_t KVAdapter::Init()
|
||||
if (kvStorePtr_ && status == DistributedKv::Status::SUCCESS) {
|
||||
int64_t endTime = GetTickCount();
|
||||
LNN_LOGI(LNN_LEDGER, "Init KvStorePtr Success, spend %{public}" PRId64 " ms", endTime - beginTime);
|
||||
RegisterDataChangeListener();
|
||||
return SOFTBUS_OK;
|
||||
}
|
||||
LNN_LOGI(LNN_LEDGER, "CheckKvStore, left times: %{public}d, status: %{public}d", tryTimes, status);
|
||||
@ -82,19 +79,23 @@ int32_t KVAdapter::Init()
|
||||
int32_t KVAdapter::DeInit()
|
||||
{
|
||||
LNN_LOGI(LNN_LEDGER, "DBAdapter DeInit");
|
||||
UnRegisterDataChangeListener();
|
||||
DeleteDataChangeListener();
|
||||
DeleteKvStorePtr();
|
||||
return SOFTBUS_OK;
|
||||
}
|
||||
|
||||
int32_t KVAdapter::RegisterDataChangeListener()
|
||||
int32_t KVAdapter::RegisterDataChangeListener(
|
||||
const std::shared_ptr<DistributedKv::KvStoreObserver> &dataChangeListener)
|
||||
{
|
||||
LNN_LOGI(LNN_LEDGER, "Register db data change listener");
|
||||
if (!IsCloudSyncEnabled()) {
|
||||
LNN_LOGW(LNN_LEDGER, "not support cloud sync");
|
||||
return SOFTBUS_ERR;
|
||||
}
|
||||
if (dataChangeListener == nullptr) {
|
||||
LNN_LOGE(LNN_LEDGER, "dataChangeListener is null");
|
||||
return SOFTBUS_INVALID_PARAM;
|
||||
}
|
||||
this->dataChangeListener_ = dataChangeListener;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(kvAdapterMutex_);
|
||||
if (kvStorePtr_ == nullptr) {
|
||||
@ -391,4 +392,11 @@ void KVAdapter::CloudSyncCallback(DistributedKv::ProgressDetail &&detail)
|
||||
}
|
||||
}
|
||||
|
||||
void KVAdapter::DeRegisterDataChangeListener()
|
||||
{
|
||||
LNN_LOGI(LNN_LEDGER, "call!");
|
||||
UnRegisterDataChangeListener();
|
||||
DeleteDataChangeListener();
|
||||
}
|
||||
|
||||
} // namespace OHOS
|
||||
|
@ -27,16 +27,14 @@
|
||||
|
||||
namespace OHOS {
|
||||
namespace {
|
||||
const std::string APP_ID = "dsoftbus";
|
||||
const std::string STORE_ID = "dsoftbus_kv_db";
|
||||
constexpr int32_t APP_ID_LEN = 8;
|
||||
constexpr int32_t STORE_ID_LEN = 14;
|
||||
constexpr int32_t MAX_DB_RECORD_SIZE = 10000;
|
||||
} // namespace
|
||||
|
||||
KvDataChangeListener::KvDataChangeListener()
|
||||
KvDataChangeListener::KvDataChangeListener(const std::string &appId, const std::string &storeId)
|
||||
{
|
||||
LNN_LOGI(LNN_LEDGER, "construct!");
|
||||
this->appId_ = appId;
|
||||
this->storeId_ = storeId;
|
||||
}
|
||||
|
||||
KvDataChangeListener::~KvDataChangeListener()
|
||||
@ -75,7 +73,7 @@ std::vector<DistributedKv::Entry> KvDataChangeListener::ConvertCloudChangeDataTo
|
||||
{
|
||||
int32_t dbId = 0;
|
||||
char *anonyKey = nullptr;
|
||||
LnnCreateKvAdapter(&dbId, APP_ID.c_str(), APP_ID_LEN, STORE_ID.c_str(), STORE_ID_LEN);
|
||||
LnnCreateKvAdapter(&dbId, appId_.c_str(), appId_.length(), storeId_.c_str(), storeId_.length());
|
||||
LNN_LOGI(LNN_LEDGER, "call! dbId=%{public}d", dbId);
|
||||
std::vector<DistributedKv::Entry> entries;
|
||||
if (keys.empty()) {
|
||||
|
@ -1023,6 +1023,7 @@ void LnnInitCloudSyncModule(void)
|
||||
LNN_LOGE(LNN_BUILDER, "Lnn Init Cloud Sync Module fail");
|
||||
return;
|
||||
}
|
||||
LnnRegisterDataChangeListener(dbId, APPID, strlen(APPID), STOREID, strlen(STOREID));
|
||||
g_dbId = dbId;
|
||||
}
|
||||
|
||||
@ -1030,6 +1031,7 @@ void LnnDeInitCloudSyncModule(void)
|
||||
{
|
||||
LNN_LOGI(LNN_BUILDER, "enter.");
|
||||
int32_t dbId = g_dbId;
|
||||
LnnUnRegisterDataChangeListener(dbId);
|
||||
if (LnnDestroyKvAdapter(dbId) != SOFTBUS_OK) {
|
||||
LNN_LOGE(LNN_BUILDER, "DeInit Cloud Sync module fail");
|
||||
}
|
||||
|
@ -43,8 +43,7 @@ protected:
|
||||
|
||||
void KVAdapterTest::SetUpTestCase(void)
|
||||
{
|
||||
kvStore = make_shared<KVAdapter>(APP_ID, STORE_ID,
|
||||
make_shared<KvDataChangeListener>());
|
||||
kvStore = make_shared<KVAdapter>(APP_ID, STORE_ID);
|
||||
}
|
||||
|
||||
void KVAdapterTest::TearDownTestCase(void)
|
||||
|
Loading…
Reference in New Issue
Block a user