1 /* 2 * Copyright (c) 2021 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 #ifndef DEVICE_KVSTORE_IMPL_H 17 #define DEVICE_KVSTORE_IMPL_H 18 19 #include "single_kvstore_impl.h" 20 21 namespace OHOS::DistributedKv { 22 union KeyEncap { 23 int len; 24 uint8_t byteLen[sizeof(int)]; 25 } __attribute__((packed)); 26 27 class DeviceKvStoreImpl : public SingleKvStoreImpl { 28 public: 29 DeviceKvStoreImpl(const Options &options, const std::string &userId, 30 const std::string &bundleName, const std::string &storeId, const std::string &appId, 31 const std::string &directory, DistributedDB::KvStoreNbDelegate *delegate); 32 ~DeviceKvStoreImpl(); 33 Status Put(const Key &key, const Value &value) override; 34 Status Delete(const Key &key) override; 35 Status Get(const Key &key, Value &value) override; 36 Status GetEntries(const Key &prefixKey, std::vector<Entry> &entries) override; 37 Status GetEntriesWithQuery(const std::string &query, std::vector<Entry> &entries) override; 38 void GetResultSet(const Key &prefixKey, std::function<void(Status, sptr<IKvStoreResultSet>)> callback) override; 39 Status PutBatch(const std::vector<Entry> &entries) override; 40 Status DeleteBatch(const std::vector<Key> &keys) override; 41 static std::string GetLocalDeviceId(); 42 protected: 43 KvStoreObserverImpl *CreateObserver(const SubscribeType subscribeType, sptr<IKvStoreObserver> observer) override; 44 KvStoreResultSetImpl *CreateResultSet( 45 DistributedDB::KvStoreResultSet *resultSet, const DistributedDB::Key &prix) override; 46 private: 47 bool AddKeyPrefixAndSuffix(const Key &in, std::vector<uint8_t> &out); 48 Status DeleteKeyPrefix(const Key &in, std::vector<uint8_t> &out); 49 void DeletePrefixAndSuffix(const Key &in, std::vector<uint8_t> &out); 50 static std::string localDeviceId_; 51 }; 52 } 53 #endif // DEVICE_KVSTORE_IMPL_H 54