bundleNameId编码方式替换为递增方式

Signed-off-by: m30043719 <maxiaodong25@huawei.com>
This commit is contained in:
m30043719 2024-06-29 16:08:03 +08:00
parent 264201a6fb
commit bcd3f9f02a
6 changed files with 261 additions and 49 deletions

View File

@ -68,6 +68,7 @@ public:
void UpdateDistributedData();
int32_t CloudSync();
void DmsPutBatch(const std::vector<DmsBundleInfo> &dmsBundleInfos);
bool UpdatePublicRecords(const std::string &localUdid);
private:
std::string DeviceAndNameToKey(const std::string &udid, const std::string &bundleName) const;
@ -88,6 +89,8 @@ private:
OHOS::DistributedKv::Status GetResultSatus(std::promise<OHOS::DistributedKv::Status> &resultStatusSignal);
bool DelReduData(const std::string &networkId, const std::vector<OHOS::DistributedKv::Entry> &reduRiskEntries);
bool CheckSyncData(const std::string &networkId);
bool RebuildLocalData();
bool GetLastBundleNameId(uint16_t &bundleNameId);
private:
static std::mutex mutex_;

View File

@ -23,6 +23,16 @@
namespace OHOS {
namespace DistributedSchedule {
struct PublicRecordsInfo : public Parcelable {
uint16_t maxBundleNameId = 0;
bool ReadFromParcel(Parcel &parcel);
bool Marshalling(Parcel &parcel) const override;
static PublicRecordsInfo *Unmarshalling(Parcel &parcel);
std::string ToString() const;
bool FromJsonString(const std::string &jsonString);
};
struct DmsAbilityInfo : public Parcelable {
std::string abilityName;
std::vector<std::string> continueType;
@ -30,7 +40,7 @@ struct DmsAbilityInfo : public Parcelable {
std::string moduleName;
bool ReadFromParcel(Parcel &parcel);
virtual bool Marshalling(Parcel &parcel) const override;
bool Marshalling(Parcel &parcel) const override;
static DmsAbilityInfo *Unmarshalling(Parcel &parcel);
};
@ -52,7 +62,7 @@ struct DmsBundleInfo : public Parcelable {
std::vector<uint8_t> userIdArr;
bool ReadFromParcel(Parcel &parcel);
virtual bool Marshalling(Parcel &parcel) const override;
bool Marshalling(Parcel &parcel) const override;
static DmsBundleInfo *Unmarshalling(Parcel &parcel);
std::string ToString() const;
bool FromJsonString(const std::string &jsonString);

View File

@ -666,6 +666,7 @@ bool DistributedSchedStub::IsUsingQos(const std::string& remoteDeviceId)
HILOGW("remote dms not support qos, using rpc");
return false;
}
HILOGI("remote device satisfied qos condition");
return true;
}

View File

@ -32,6 +32,7 @@ namespace DistributedSchedule {
namespace {
const std::string TAG = "DmsBmDataStorage";
const std::string BMS_KV_BASE_DIR = "/data/service/el1/public/database/DistributedSchedule";
const std::string PUBLIC_RECORDS = "publicRecords";
const int32_t EL1 = 1;
const int32_t MAX_TIMES = 600; // 1min
const int32_t SLEEP_INTERVAL = 100 * 1000; // 100ms
@ -115,6 +116,28 @@ bool DmsBmStorage::SaveStorageDistributeInfo(const std::string &bundleName)
return true;
}
bool DmsBmStorage::UpdatePublicRecords(const std::string &localUdid)
{
if (!CheckKvStore()) {
HILOGE("kvStore is nullptr");
return false;
}
std::string keyOfPublic = localUdid + AppExecFwk::Constants::FILE_UNDERLINE + PUBLIC_RECORDS;
Key publicKey(keyOfPublic);
PublicRecordsInfo publicRecordsInfo;
GetLastBundleNameId(publicRecordsInfo.maxBundleNameId);
publicRecordsInfo.maxBundleNameId =
std::max((bundleNameIdTables_.rbegin())->first, publicRecordsInfo.maxBundleNameId);
HILOGI("maxBundleNameId = %{public}d", publicRecordsInfo.maxBundleNameId);
Value publicValue(publicRecordsInfo.ToString());
Status status = kvStorePtr_->Put(publicKey, publicValue);
if (status != Status::SUCCESS) {
HILOGE("put to kvStore error: %{public}d", status);
return false;
}
return true;
}
bool DmsBmStorage::InnerSaveStorageDistributeInfo(const DmsBundleInfo &distributedBundleInfo,
const std::string &localUdid)
@ -139,6 +162,7 @@ bool DmsBmStorage::InnerSaveStorageDistributeInfo(const DmsBundleInfo &distribut
HILOGE("put to kvStore error: %{public}d", status);
return false;
}
UpdatePublicRecords(localUdid);
return true;
}
@ -191,7 +215,6 @@ bool DmsBmStorage::DeleteStorageDistributeInfo(const std::string &bundleName)
HILOGE("delete key error: %{public}d", status);
return false;
}
DelBundleNameId(bundleName);
HILOGI("delete value to kvStore success");
return true;
}
@ -259,10 +282,11 @@ bool DmsBmStorage::DealGetBundleName(const std::string &networkId, const uint16_
return false;
}
std::vector<Entry> reduRiskEntries;
std::string keyOfPublic = udid + AppExecFwk::Constants::FILE_UNDERLINE + PUBLIC_RECORDS;
for (auto entry : allEntries) {
std::string key = entry.key.ToString();
std::string value = entry.value.ToString();
if (key.find(udid) == std::string::npos) {
if (key.find(udid) == std::string::npos || key.find(keyOfPublic) != std::string::npos) {
continue;
}
DmsBundleInfo distributedBundleInfo;
@ -396,33 +420,26 @@ bool DmsBmStorage::GetBundleNameId(const std::string& bundleName, uint16_t &bund
HILOGE("kvStore is nullptr");
return false;
}
std::string udid;
DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalUdid(udid);
if (udid == "") {
HILOGE("can not get udid by networkId");
std::string localUdid;
DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalUdid(localUdid);
if (localUdid == "") {
HILOGE("can not get localUdid by networkId");
return false;
}
Key allEntryKeyPrefix("");
std::vector<Entry> allEntries;
Status status = kvStorePtr_->GetEntries(allEntryKeyPrefix, allEntries);
Key key(DeviceAndNameToKey(localUdid, bundleName));
Value value;
Status status = kvStorePtr_->Get(key, value);
if (status != Status::SUCCESS) {
HILOGE("GetEntries error: %{public}d", status);
HILOGW("BundleNameId not found, Get result: %{public}d", status);
return false;
}
for (auto entry : allEntries) {
std::string key = entry.key.ToString();
std::string value = entry.value.ToString();
if (key.find(udid) == std::string::npos) {
continue;
}
DmsBundleInfo distributedBundleInfo;
if (distributedBundleInfo.FromJsonString(value) && distributedBundleInfo.bundleName == bundleName) {
bundleNameId = distributedBundleInfo.bundleNameId;
HILOGD("end.");
return true;
}
DmsBundleInfo distributedBundleInfo;
if (distributedBundleInfo.FromJsonString(value.ToString()) && distributedBundleInfo.bundleName == bundleName) {
bundleNameId = distributedBundleInfo.bundleNameId;
HILOGD("end.");
return true;
}
HILOGE("get distributed bundleName no matching data: %{public}s %{public}d", GetAnonymStr(udid).c_str(),
HILOGE("get distributed bundleName no matching data: %{public}s %{public}d", GetAnonymStr(localUdid).c_str(),
bundleNameId);
return false;
}
@ -456,7 +473,7 @@ bool DmsBmStorage::CheckKvStore()
Status DmsBmStorage::GetKvStore()
{
HILOGD("called.");
HILOGI("called.");
Options options = {
.createIfMissing = true,
.encrypt = false,
@ -479,7 +496,7 @@ Status DmsBmStorage::GetKvStore()
HILOGE("This db meta changed, remove and rebuild it");
dataManager_.DeleteKvStore(appId_, storeId_, BMS_KV_BASE_DIR + appId_.appId);
}
HILOGD("end.");
HILOGI("end.");
return status;
}
@ -494,17 +511,97 @@ void DmsBmStorage::TryTwice(const std::function<Status()> &func) const
HILOGD("end.");
}
bool DmsBmStorage::GetLastBundleNameId(uint16_t &bundleNameId)
{
HILOGI("call.");
std::string localUdid;
DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalUdid(localUdid);
if (localUdid == "") {
HILOGE("GetLocalUdid failed");
return false;
}
if (!CheckKvStore()) {
HILOGE("kvStore is nullptr");
return false;
}
std::string keyOfPublic = localUdid + AppExecFwk::Constants::FILE_UNDERLINE + PUBLIC_RECORDS;
Key publicKey(keyOfPublic);
Value value;
Status status = kvStorePtr_->Get(publicKey, value);
if (status != Status::SUCCESS) {
HILOGE("This information not be found in the database,Get error: %{public}d", status);
return false;
}
PublicRecordsInfo publicRecordsInfo;
if (publicRecordsInfo.FromJsonString(value.ToString())) {
bundleNameId = publicRecordsInfo.maxBundleNameId;
HILOGI("bundleNameId: %{public}d", bundleNameId);
return true;
}
return false;
}
uint16_t DmsBmStorage::CreateBundleNameId(const std::string &bundleName)
{
std::lock_guard<std::mutex> lock_l(mutex_);
for (uint16_t bundleNameId = 0; bundleNameId < MAX_BUNDLEID; ++bundleNameId) {
if (bundleNameIdTables_.find(bundleNameId) == bundleNameIdTables_.end()) {
bundleNameIdTables_.insert(std::make_pair(bundleNameId, bundleName));
return bundleNameId;
}
HILOGI("called.");
uint16_t bundleNameId = 0;
if (GetBundleNameId(bundleName, bundleNameId)) {
HILOGI("The bundleNameId already exists in the bundleName.");
return bundleNameId;
}
HILOGE("CreateBundleNameId failed");
return MAX_BUNDLEID;
if (bundleNameIdTables_.empty()) {
HILOGI("Encode from the first one.");
std::lock_guard<std::mutex> lock_l(mutex_);
bundleNameIdTables_.insert(std::make_pair(bundleNameId, bundleName));
return bundleNameId;
}
uint16_t lastBundleNameId = 0;
GetLastBundleNameId(lastBundleNameId);
lastBundleNameId = std::max((bundleNameIdTables_.rbegin())->first, lastBundleNameId);
if (lastBundleNameId < MAX_BUNDLEID) {
HILOGI("Add bundleNameId.");
std::lock_guard<std::mutex> lock_l(mutex_);
bundleNameIdTables_.insert(std::make_pair(lastBundleNameId + 1, bundleName));
return lastBundleNameId + 1;
}
HILOGE("The bundleNameId exceeds the maximum value! Delete the local data and re-create the data.");
RebuildLocalData();
GetBundleNameId(bundleName, bundleNameId);
return bundleNameId;
}
bool DmsBmStorage::RebuildLocalData()
{
HILOGE("called.");
std::string localUdid;
DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalUdid(localUdid);
if (localUdid == "") {
HILOGE("GetLocalUdid failed");
return false;
}
if (!CheckKvStore()) {
HILOGE("kvStore is nullptr");
return false;
}
Key allEntryKeyPrefix(localUdid);
std::vector<Entry> allEntries;
Status status = kvStorePtr_->GetEntries(allEntryKeyPrefix, allEntries);
if (status != Status::SUCCESS) {
HILOGE("GetEntries error: %{public}d", status);
return false;
}
std::vector<Key> keyArr;
for (const auto &entry : allEntries) {
keyArr.push_back(entry.key);
}
status = kvStorePtr_->DeleteBatch(keyArr);
if (status != Status::SUCCESS) {
HILOGE("DeleteBatch error: %{public}d", status);
return false;
}
bundleNameIdTables_.clear();
UpdateDistributedData();
return true;
}
DmsBundleInfo DmsBmStorage::ConvertToDistributedBundleInfo(const AppExecFwk::BundleInfo &bundleInfo)
@ -632,16 +729,16 @@ void DmsBmStorage::DmsPutBatch(const std::vector<DmsBundleInfo> &dmsBundleInfos)
HILOGE("kvStore is nullptr");
return;
}
std::string udid;
DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalUdid(udid);
if (udid == "") {
std::string localUdid;
DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalUdid(localUdid);
if (localUdid == "") {
HILOGE("GetLocalUdid failed");
return;
}
std::vector<Entry> entries;
for (const auto &dmsBundleInfo : dmsBundleInfos) {
Entry entrie;
std::string keyOfData = DeviceAndNameToKey(udid, dmsBundleInfo.bundleName);
std::string keyOfData = DeviceAndNameToKey(localUdid, dmsBundleInfo.bundleName);
Key key(keyOfData);
entrie.key = key;
Value value(dmsBundleInfo.ToString());
@ -649,15 +746,24 @@ void DmsBmStorage::DmsPutBatch(const std::vector<DmsBundleInfo> &dmsBundleInfos)
HILOGI("need be put: %{public}s", dmsBundleInfo.bundleName.c_str());
entries.push_back(entrie);
}
Entry entrie;
std::string keyOfData = localUdid + AppExecFwk::Constants::FILE_UNDERLINE + PUBLIC_RECORDS;
Key key(keyOfData);
entrie.key = key;
PublicRecordsInfo publicRecordsInfo;
GetLastBundleNameId(publicRecordsInfo.maxBundleNameId);
publicRecordsInfo.maxBundleNameId =
std::max((bundleNameIdTables_.rbegin())->first, publicRecordsInfo.maxBundleNameId);
Value value(publicRecordsInfo.ToString());
entrie.value = value;
HILOGI("need be put: %{public}d", publicRecordsInfo.maxBundleNameId);
entries.push_back(entrie);
Status status = kvStorePtr_->PutBatch(entries);
if (status == Status::IPC_ERROR) {
status = kvStorePtr_->PutBatch(entries);
HILOGW("distribute database ipc error and try to call again, result = %{public}d", status);
}
if (status != Status::SUCCESS) {
HILOGE("PutBatch to kvStore error: %{public}d", status);
return;
}
HILOGI("end.");
}
@ -676,9 +782,9 @@ std::map<std::string, DmsBundleInfo> DmsBmStorage::GetAllOldDistributionBundleIn
HILOGE("kvStorePtr_ is null");
return oldDistributedBundleInfos;
}
std::string udid;
DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalUdid(udid);
if (udid == "") {
std::string localUdid;
DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalUdid(localUdid);
if (localUdid == "") {
HILOGE("GetLocalUdid failed");
return oldDistributedBundleInfos;
}
@ -689,22 +795,23 @@ std::map<std::string, DmsBundleInfo> DmsBmStorage::GetAllOldDistributionBundleIn
HILOGE("GetEntries error: %{public}d", status);
return oldDistributedBundleInfos;
}
std::string keyOfPublic = localUdid + AppExecFwk::Constants::FILE_UNDERLINE + PUBLIC_RECORDS;
for (const auto &entry : allEntries) {
std::string key = entry.key.ToString();
if (key.find(udid) == std::string::npos) {
if (key.find(localUdid) == std::string::npos || key.find(keyOfPublic) != std::string::npos) {
continue;
}
std::string value = entry.value.ToString();
DmsBundleInfo distributedBundleInfo;
if (distributedBundleInfo.FromJsonString(value)) {
AddBundleNameId(distributedBundleInfo.bundleNameId, distributedBundleInfo.bundleName);
if (std::find(bundleNames.begin(), bundleNames.end(), distributedBundleInfo.bundleName) ==
bundleNames.end() || distributedBundleInfo.bundleName == "") {
HILOGE("Find %{public}s failed,need be delete", distributedBundleInfo.bundleName.c_str());
kvStorePtr_->Delete(entry.key);
DelBundleNameId(distributedBundleInfo.bundleName);
continue;
}
oldDistributedBundleInfos.emplace(distributedBundleInfo.bundleName, distributedBundleInfo);
AddBundleNameId(distributedBundleInfo.bundleNameId, distributedBundleInfo.bundleName);
} else {
HILOGE("DistributionInfo FromJsonString key:%{public}s failed", key.c_str());
}

View File

@ -41,6 +41,78 @@ const std::string JSON_KEY_DMS_CONTINUETYPE = "continueType";
const std::string JSON_KEY_DMS_CONTINUETYPEID = "continueTypeId";
const std::string JSON_KEY_DMS_USERID = "userIdArr";
const std::string JSON_KEY_DMS_MODULENAME = "moduleName";
const std::string JSON_KEY_DMS_MAX_BUNDLENAME_ID = "maxBundleNameId";
}
bool PublicRecordsInfo::ReadFromParcel(Parcel &parcel)
{
maxBundleNameId = parcel.ReadUint16();
return true;
}
bool PublicRecordsInfo::Marshalling(Parcel &parcel) const
{
parcel.WriteUint16(maxBundleNameId);
return true;
}
PublicRecordsInfo *PublicRecordsInfo::Unmarshalling(Parcel &parcel)
{
PublicRecordsInfo *info = new (std::nothrow) PublicRecordsInfo();
if (info && !info->ReadFromParcel(parcel)) {
APP_LOGW("read from parcel failed");
delete info;
info = nullptr;
}
return info;
}
std::string PublicRecordsInfo::ToString() const
{
nlohmann::json jsonObject;
jsonObject[JSON_KEY_DMS_MAX_BUNDLENAME_ID] = maxBundleNameId;
return jsonObject.dump();
}
void to_json(nlohmann::json &jsonObject, const PublicRecordsInfo &publicRecordsInfo)
{
APP_LOGI("call");
jsonObject = nlohmann::json {
{JSON_KEY_DMS_MAX_BUNDLENAME_ID, publicRecordsInfo.maxBundleNameId},
};
APP_LOGI("end");
}
void from_json(const nlohmann::json &jsonObject, PublicRecordsInfo &publicRecordsInfo)
{
APP_LOGI("call");
const auto &jsonObjectEnd = jsonObject.end();
int32_t parseResult = ERR_OK;
GetValueIfFindKey<uint16_t>(jsonObject,
jsonObjectEnd,
JSON_KEY_DMS_CONTINUETYPEID,
publicRecordsInfo.maxBundleNameId,
JsonType::NUMBER,
false,
parseResult,
ArrayType::NOT_ARRAY);
if (parseResult != ERR_OK) {
APP_LOGE("read PublicRecordsInfo from jsonObject error, error code : %{public}d", parseResult);
}
APP_LOGI("end");
}
bool PublicRecordsInfo::FromJsonString(const std::string &jsonString)
{
nlohmann::json jsonObject = nlohmann::json::parse(jsonString, nullptr, false);
if (jsonObject.is_discarded()) {
return false;
}
const auto &jsonObjectEnd = jsonObject.end();
int32_t parseResult = ERR_OK;
GetValueIfFindKey<uint16_t>(jsonObject, jsonObjectEnd, JSON_KEY_DMS_MAX_BUNDLENAME_ID, maxBundleNameId,
JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
return parseResult == ERR_OK;
}
bool DmsAbilityInfo::ReadFromParcel(Parcel &parcel)

View File

@ -201,6 +201,25 @@ HWTEST_F(DistributedBmStorageTest, GetBundleIdTest_001, TestSize.Level0)
DTEST_LOG << "DistributedBmStorageTest GetBundleIdTest_001 end" << std::endl;
}
/**
* @tc.name: GetBundleIdTest_002
* @tc.desc: test delete DistributedBmStorage
* @tc.type: FUNC
*/
HWTEST_F(DistributedBmStorageTest, GetBundleIdTest_002, TestSize.Level0)
{
DTEST_LOG << "DistributedBmStorageTest GetBundleIdTest_002 start" << std::endl;
auto distributedDataStorage = GetDmsBmStorage();
EXPECT_NE(distributedDataStorage, nullptr);
if (distributedDataStorage != nullptr) {
const std::string bundleName = "browser";
uint16_t bundleNameId = 1;
bool ret = dmsBmStorage_->GetInstance()->GetBundleNameId(bundleName, bundleNameId);
EXPECT_EQ(ret, false);
}
DTEST_LOG << "DistributedBmStorageTest GetBundleIdTest_002 end" << std::endl;
}
/**
* @tc.name: UpdateDistributedDataTest_001
* @tc.desc: test delete DistributedBmStorage