!706 rdb目录切至database/rdb

Merge pull request !706 from zuojiangjiang/master
This commit is contained in:
openharmony_ci 2022-07-27 06:07:26 +00:00 committed by Gitee
commit bd80635842
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
9 changed files with 171 additions and 87 deletions

View File

@ -188,7 +188,7 @@ bool ITypesUtil::Marshalling(const DistributedRdb::RdbSyncerParam &param, Messag
ZLOGE("RdbStoreParam write bundle name failed");
return false;
}
if (!parcel.WriteString(param.relativePath_)) {
if (!parcel.WriteString(param.hapName_)) {
ZLOGE("RdbStoreParam write directory failed");
return false;
}
@ -196,7 +196,11 @@ bool ITypesUtil::Marshalling(const DistributedRdb::RdbSyncerParam &param, Messag
ZLOGE("RdbStoreParam write store name failed");
return false;
}
if (!parcel.WriteString(param.encryptLevel_)) {
if (!parcel.WriteInt32(param.area_)) {
ZLOGE("RdbStoreParam write security level failed");
return false;
}
if (!parcel.WriteInt32(param.level_)) {
ZLOGE("RdbStoreParam write security level failed");
return false;
}
@ -217,7 +221,7 @@ bool ITypesUtil::Unmarshalling(DistributedRdb::RdbSyncerParam &param, MessagePar
ZLOGE("RdbStoreParam read bundle name failed");
return false;
}
if (!parcel.ReadString(param.relativePath_)) {
if (!parcel.ReadString(param.hapName_)) {
ZLOGE("RdbStoreParam read directory failed");
return false;
}
@ -225,7 +229,11 @@ bool ITypesUtil::Unmarshalling(DistributedRdb::RdbSyncerParam &param, MessagePar
ZLOGE("RdbStoreParam read store name failed");
return false;
}
if (!parcel.ReadString(param.encryptLevel_)) {
if (!parcel.ReadInt32(param.area_)) {
ZLOGE("RdbStoreParam read security level failed");
return false;
}
if (!parcel.ReadInt32(param.level_)) {
ZLOGE("RdbStoreParam read security level failed");
return false;
}

View File

@ -293,7 +293,6 @@ HWTEST_F(DeviceKvStoreTest, GetPrefixQueryEntriesAndResultSet, TestSize.Level1)
// prepare 10
size_t sum = 10;
int sumGet = 10;
std::string prefix = "prefix_";
for (size_t i = 0; i < sum; i++) {
kvStore_->Put({prefix + std::to_string(i)}, {std::to_string(i)});
@ -301,10 +300,10 @@ HWTEST_F(DeviceKvStoreTest, GetPrefixQueryEntriesAndResultSet, TestSize.Level1)
DataQuery dataQuery;
dataQuery.KeyPrefix(GetKey(prefix));
dataQuery.Limit(10, 0);
int sumGet = 0;
kvStore_->GetCount(dataQuery, sumGet);
EXPECT_EQ(sumGet, sum) << "count is not equal 10.";
dataQuery.Limit(10, 0);
std::vector<Entry> results;
kvStore_->GetEntries(dataQuery, results);
EXPECT_EQ(results.size(), sum) << "entries size is not equal 10.";
@ -334,6 +333,59 @@ HWTEST_F(DeviceKvStoreTest, GetPrefixQueryEntriesAndResultSet, TestSize.Level1)
EXPECT_EQ(status, Status::SUCCESS) << "Close resultSet failed.";
}
/**
* @tc.name: GetInKeysQueryResultSet
* @tc.desc: get entries and result set by prefix query.
* @tc.type: FUNC
* @tc.require: I5DE2A
* @tc.author: Sven Wang
*/
HWTEST_F(DeviceKvStoreTest, GetInKeysQueryResultSet, TestSize.Level1)
{
EXPECT_NE(kvStore_, nullptr) << "kvStore is nullptr.";
if (options_.baseDir.empty()) {
return;
}
// prepare 10
size_t sum = 10;
std::string prefix = "prefix_";
for (size_t i = 0; i < sum; i++) {
kvStore_->Put({prefix + std::to_string(i)}, {std::to_string(i)});
}
DataQuery dataQuery;
dataQuery.InKeys({"prefix_0", "prefix_1", "prefix_3", "prefix_9"});
int sumGet = 0;
kvStore_->GetCount(dataQuery, sumGet);
EXPECT_EQ(sumGet, 4) << "count is not equal 4.";
dataQuery.Limit(10, 0);
std::shared_ptr<KvStoreResultSet> resultSet;
Status status = kvStore_->GetResultSet(dataQuery, resultSet);
EXPECT_EQ(status, Status::SUCCESS);
EXPECT_EQ(resultSet->GetCount(), sumGet) << "resultSet size is not equal 4.";
resultSet->IsFirst();
resultSet->IsAfterLast();
resultSet->IsBeforeFirst();
resultSet->MoveToPosition(1);
resultSet->IsLast();
resultSet->MoveToPrevious();
resultSet->MoveToNext();
resultSet->MoveToLast();
resultSet->MoveToFirst();
resultSet->GetPosition();
Entry entry;
resultSet->GetEntry(entry);
for (size_t i = 0; i < sum; i++) {
kvStore_->Delete({GetKey(prefix + std::to_string(i))});
}
status = kvStore_->CloseResultSet(resultSet);
EXPECT_EQ(status, Status::SUCCESS) << "Close resultSet failed.";
}
/**
* @tc.name: GetPrefixEntriesAndResultSet
* @tc.desc: get entries and result set by prefix.

View File

@ -34,11 +34,11 @@ enum RdbDistributedType {
struct RdbSyncerParam {
std::string bundleName_;
std::string relativePath_;
std::string hapName_;
std::string storeName_;
std::string encryptLevel_;
std::string realPath_;
int type_ = RDB_DEVICE_COLLABORATION;
int32_t area_ = 0;
int32_t level_ = 0;
int32_t type_ = RDB_DEVICE_COLLABORATION;
bool isAutoSync_ = false;
};

View File

@ -121,7 +121,7 @@ std::string DirectoryManager::GetStore(const StoreMetaData &metaData) const
return "kvdb";
}
// rdb use empty session
return "";
return "rdb";
}
std::string DirectoryManager::GetSecurity(const StoreMetaData &metaData) const

View File

@ -121,28 +121,10 @@ bool RdbServiceImpl::CheckAccess(const RdbSyncerParam &param)
storeInfo.uid = GetCallingUid();
storeInfo.tokenId = GetCallingTokenID();
storeInfo.bundleName = param.bundleName_;
storeInfo.storeId = param.storeName_;
storeInfo.storeId = RdbSyncer::RemoveSuffix(param.storeName_);
return !CheckerManager::GetInstance().GetAppId(storeInfo).empty();
}
RdbSyncerParam RdbServiceImpl::ToServiceParam(const RdbSyncerParam &param)
{
ZLOGI("%{public}s", param.relativePath_.c_str());
auto serviceParam = param;
Security::AccessToken::AccessTokenID callerToken = GetCallingTokenID();
auto accessToken = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(callerToken);
if (accessToken == Security::AccessToken::TOKEN_NATIVE || accessToken == Security::AccessToken::TOKEN_SHELL) {
ZLOGD("native access");
serviceParam.realPath_ = "/data/service/el1/public/database/" + param.bundleName_ + '/' + param.relativePath_;
} else if (accessToken == Security::AccessToken::TOKEN_HAP) {
ZLOGD("hap access %{public}s", param.encryptLevel_.c_str());
auto userId = AccountDelegate::GetInstance()->GetDeviceAccountIdByUID(GetCallingUid());
serviceParam.realPath_ = "/data/app/" + param.encryptLevel_ + '/' + userId + "/database/" +
param.bundleName_ + '/' + param.relativePath_;
}
return serviceParam;
}
std::string RdbServiceImpl::ObtainDistributedTableName(const std::string &device, const std::string &table)
{
ZLOGI("device=%{public}s table=%{public}s", Anonymous::Change(device).c_str(), table.c_str());
@ -220,9 +202,9 @@ std::shared_ptr<RdbSyncer> RdbServiceImpl::GetRdbSyncer(const RdbSyncerParam &pa
pid_t uid = GetCallingUid();
uint32_t tokenId = GetCallingTokenID();
std::shared_ptr<RdbSyncer> syncer;
syncers_.Compute(pid, [this, &param, pid, uid, tokenId, &syncer] (const auto& key, StoreSyncersType& syncers) {
auto it = syncers.find(param.storeName_);
auto storeId = RdbSyncer::RemoveSuffix(param.storeName_);
auto it = syncers.find(storeId);
if (it != syncers.end()) {
syncer = it->second;
timer_.Unregister(syncer->GetTimerId());
@ -238,12 +220,11 @@ std::shared_ptr<RdbSyncer> RdbServiceImpl::GetRdbSyncer(const RdbSyncerParam &pa
ZLOGE("no available syncer");
return !syncers.empty();
}
auto syncer_ = std::make_shared<RdbSyncer>(ToServiceParam(param),
new (std::nothrow) RdbStoreObserverImpl(this, pid));
auto syncer_ = std::make_shared<RdbSyncer>(param, new (std::nothrow) RdbStoreObserverImpl(this, pid));
if (syncer_->Init(pid, uid, tokenId) != 0) {
return !syncers.empty();
}
syncers[param.storeName_] = syncer_;
syncers[storeId] = syncer_;
syncer = syncer_;
syncerNum_++;
uint32_t timerId = timer_.Register([this, syncer]() { SyncerTimeout(syncer); }, SYNCER_TIMEOUT, true);
@ -334,10 +315,11 @@ std::string RdbServiceImpl::GenIdentifier(const RdbSyncerParam &param)
{
pid_t uid = GetCallingUid();
uint32_t token = GetCallingTokenID();
CheckerManager::StoreInfo storeInfo{ uid, token, param.bundleName_, param.storeName_ };
auto storeId = RdbSyncer::RemoveSuffix(param.storeName_);
CheckerManager::StoreInfo storeInfo{ uid, token, param.bundleName_, storeId};
std::string userId = AccountDelegate::GetInstance()->GetDeviceAccountIdByUID(uid);
std::string appId = CheckerManager::GetInstance().GetAppId(storeInfo);
std::string identifier = RelationalStoreManager::GetRelationalStoreIdentifier(userId, appId, param.storeName_);
std::string identifier = RelationalStoreManager::GetRelationalStoreIdentifier(userId, appId, storeId);
return TransferStringToHex(identifier);
}

View File

@ -59,8 +59,6 @@ private:
bool CheckAccess(const RdbSyncerParam& param);
RdbSyncerParam ToServiceParam(const RdbSyncerParam& param);
bool ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam &param);
void SyncerTimeout(std::shared_ptr<RdbSyncer> syncer);

View File

@ -12,24 +12,26 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "RdbSyncer"
#include "rdb_syncer.h"
#include "accesstoken_kit.h"
#include "account/account_delegate.h"
#include "checker/checker_manager.h"
#include "log_print.h"
#include "metadata/store_meta_data.h"
#include "metadata/meta_data_manager.h"
#include "directory_manager.h"
#include "kvstore_utils.h"
#include "log_print.h"
#include "metadata/meta_data_manager.h"
#include "metadata/store_meta_data.h"
#include "types.h"
#include "utils/constant.h"
#include "utils/converter.h"
using OHOS::DistributedKv::KvStoreUtils;
using OHOS::DistributedKv::AccountDelegate;
using OHOS::DistributedData::StoreMetaData;
using OHOS::DistributedData::MetaDataManager;
using OHOS::AppDistributedKv::CommunicationProvider;
using namespace OHOS::Security::AccessToken;
using namespace OHOS::DistributedData;
namespace OHOS::DistributedRdb {
RdbSyncer::RdbSyncer(const RdbSyncerParam& param, RdbStoreObserverImpl* observer)
: param_(param), observer_(observer)
@ -84,14 +86,9 @@ std::string RdbSyncer::GetAppId() const
return DistributedData::CheckerManager::GetInstance().GetAppId({ uid_, token_, param_.bundleName_ });
}
std::string RdbSyncer::GetPath() const
{
return param_.realPath_;
}
std::string RdbSyncer::GetStoreId() const
{
return param_.storeName_;
return RemoveSuffix(param_.storeName_);
}
int32_t RdbSyncer::Init(pid_t pid, pid_t uid, uint32_t token)
@ -100,43 +97,63 @@ int32_t RdbSyncer::Init(pid_t pid, pid_t uid, uint32_t token)
pid_ = pid;
uid_ = uid;
token_ = token;
if (CreateMetaData() != RDB_OK) {
StoreMetaData meta;
if (CreateMetaData(meta) != RDB_OK) {
ZLOGE("create meta data failed");
return RDB_ERROR;
}
if (InitDBDelegate(meta) != RDB_OK) {
ZLOGE("delegate is nullptr");
return RDB_ERROR;
}
ZLOGI("success");
return RDB_OK;
}
int32_t RdbSyncer::CreateMetaData()
int32_t RdbSyncer::CreateMetaData(StoreMetaData &meta)
{
StoreMetaData newMeta;
newMeta.storeType = static_cast<int32_t>(RDB_DEVICE_COLLABORATION);
newMeta.appId = GetAppId();
newMeta.appType = "harmony";
newMeta.bundleName = GetBundleName();
newMeta.deviceId = CommunicationProvider::GetInstance().GetLocalDevice().uuid;
newMeta.storeId = GetStoreId();
newMeta.uid = uid_;
newMeta.tokenId = token_;
newMeta.user = AccountDelegate::GetInstance()->GetDeviceAccountIdByUID(uid_);
newMeta.account = AccountDelegate::GetInstance()->GetCurrentAccountId();
newMeta.dataDir = GetPath();
meta.uid = uid_;
meta.tokenId = token_;
meta.instanceId = GetInstIndex(token_, param_.bundleName_);
meta.bundleName = param_.bundleName_;
meta.deviceId = CommunicationProvider::GetInstance().GetLocalDevice().uuid;
meta.storeId = RemoveSuffix(param_.storeName_);
meta.user = AccountDelegate::GetInstance()->GetDeviceAccountIdByUID(uid_);
meta.storeType = param_.type_;
meta.securityLevel = param_.level_;
meta.area = param_.area_;
meta.appId = CheckerManager::GetInstance().GetAppId(Converter::ConvertToStoreInfo(meta));
meta.appType = "harmony";
meta.hapName = param_.hapName_;
meta.dataDir = DirectoryManager::GetInstance().GetStorePath(meta) + "/" + param_.storeName_;
meta.account = AccountDelegate::GetInstance()->GetCurrentAccountId();
auto metaKey = StoreMetaData::GetKey({ newMeta.user, "default", newMeta.bundleName, newMeta.storeId });
StoreMetaData oldMeta;
if (MetaDataManager::GetInstance().LoadMeta(metaKey, oldMeta)) {
if (newMeta == oldMeta) {
ZLOGI("ignore same meta");
return RDB_OK;
}
StoreMetaData old;
bool isCreated = MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), old);
if (isCreated && (old.storeType != meta.storeType || Constant::NotEqual(old.isEncrypt, meta.isEncrypt) ||
old.area != meta.area)) {
ZLOGE("meta bundle:%{public}s store:%{public}s type:%{public}d->%{public}d encrypt:%{public}d->%{public}d "
"area:%{public}d->%{public}d",
meta.bundleName.c_str(), meta.storeId.c_str(), old.storeType, meta.storeType, old.isEncrypt,
meta.isEncrypt, old.area, meta.area);
return RDB_ERROR;
}
auto saved = MetaDataManager::GetInstance().SaveMeta(metaKey, newMeta);
auto saved = MetaDataManager::GetInstance().SaveMeta(meta.GetKey(), meta);
return saved ? RDB_OK : RDB_ERROR;
}
DistributedDB::RelationalStoreDelegate* RdbSyncer::GetDelegate()
std::string RdbSyncer::RemoveSuffix(const std::string& name)
{
std::string suffix(".db");
auto pos = name.rfind(suffix);
if (pos == std::string::npos || pos < name.length() - suffix.length()) {
return name;
}
return std::string(name, 0, pos);
}
int32_t RdbSyncer::InitDBDelegate(const StoreMetaData &meta)
{
std::lock_guard<std::mutex> lock(mutex_);
if (manager_ == nullptr) {
@ -144,21 +161,45 @@ DistributedDB::RelationalStoreDelegate* RdbSyncer::GetDelegate()
}
if (manager_ == nullptr) {
ZLOGE("malloc manager failed");
return nullptr;
return RDB_ERROR;
}
if (delegate_ == nullptr) {
DistributedDB::RelationalStoreDelegate::Option option;
option.observer = observer_;
ZLOGI("path=%{public}s storeId=%{public}s", GetPath().c_str(), GetStoreId().c_str());
DistributedDB::DBStatus status = manager_->OpenStore(GetPath(), GetStoreId(), option, delegate_);
std::string fileName = meta.dataDir;
ZLOGI("path=%{public}s storeId=%{public}s", fileName.c_str(), meta.storeId.c_str());
auto status = manager_->OpenStore(fileName, meta.storeId, option, delegate_);
if (status != DistributedDB::DBStatus::OK) {
ZLOGE("open store failed status=%{public}d", status);
return nullptr;
return RDB_ERROR;
}
ZLOGI("open store success");
}
return RDB_OK;
}
int32_t RdbSyncer::GetInstIndex(uint32_t tokenId, const std::string &bundleName) const
{
if (AccessTokenKit::GetTokenTypeFlag(tokenId) != TOKEN_HAP) {
return 0;
}
HapTokenInfo tokenInfo;
tokenInfo.instIndex = -1;
int errCode = AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo);
if (errCode != RET_SUCCESS) {
ZLOGE("GetHapTokenInfo error:%{public}d, tokenId:0x%{public}x appId:%{public}s", errCode, tokenId,
bundleName.c_str());
return -1;
}
return tokenInfo.instIndex;
}
DistributedDB::RelationalStoreDelegate* RdbSyncer::GetDelegate()
{
std::lock_guard<std::mutex> lock(mutex_);
return delegate_;
}

View File

@ -19,15 +19,16 @@
#include <mutex>
#include <string>
#include "rdb_types.h"
#include "metadata/store_meta_data.h"
#include "rdb_notifier.h"
#include "rdb_store_observer_impl.h"
#include "relational_store_manager.h"
#include "rdb_types.h"
#include "relational_store_delegate.h"
#include "relational_store_manager.h"
namespace OHOS::DistributedRdb {
class RdbSyncer {
public:
using StoreMetaData = OHOS::DistributedData::StoreMetaData;
RdbSyncer(const RdbSyncerParam& param, RdbStoreObserverImpl* observer);
~RdbSyncer() noexcept;
@ -49,6 +50,7 @@ public:
int32_t DoAsync(const SyncOption& option, const RdbPredicates& predicates, const SyncCallback& callback);
static std::string RemoveSuffix(const std::string& name);
private:
std::string GetUserId() const;
@ -56,9 +58,10 @@ private:
std::string GetAppId() const;
std::string GetPath() const;
int32_t CreateMetaData(StoreMetaData &meta);
int32_t InitDBDelegate(const StoreMetaData &meta);
int32_t CreateMetaData();
int32_t GetInstIndex(uint32_t tokenId, const std::string &bundleName) const;
DistributedDB::RelationalStoreDelegate* GetDelegate();

View File

@ -185,7 +185,7 @@ HWTEST_F(DirectoryManagerTest, GetRDBBackupPath, TestSize.Level0)
AccessTokenKit::GetHapTokenInfo(metaData.tokenId, tokenInfo);
metaData.appId = tokenInfo.appID;
auto path = DirectoryManager::GetInstance().GetStoreBackupPath(metaData);
EXPECT_EQ(path, metaData.dataDir + "/backup");
EXPECT_EQ(path, metaData.dataDir + "/rdb/backup");
}
/**
* @tc.name: GetKVDBBackupPath