/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef OHOS_DISTRIBUTED_DATA_SERVICE_KVDB_STORE_CACHE_H #define OHOS_DISTRIBUTED_DATA_SERVICE_KVDB_STORE_CACHE_H #include #include #include #include #include "concurrent_map.h" #include "executor_pool.h" #include "ikvstore_observer.h" #include "kv_store_nb_delegate.h" #include "metadata/store_meta_data.h" #include "refbase.h" namespace OHOS::DistributedKv { class StoreCache { public: template struct Less { public: bool operator()(const sptr &x, const sptr &y) const { return x.GetRefPtr() < y.GetRefPtr(); } }; using DBStatus = DistributedDB::DBStatus; using DBStore = DistributedDB::KvStoreNbDelegate; using Store = std::shared_ptr; using DBManager = DistributedDB::KvStoreDelegateManager; using DBObserver = DistributedDB::KvStoreObserver; using DBChangeData = DistributedDB::KvStoreChangedData; using DBEntry = DistributedDB::Entry; using Observers = std::set, Less>; using StoreMetaData = OHOS::DistributedData::StoreMetaData; using Time = std::chrono::steady_clock::time_point; using DBOption = DistributedDB::KvStoreNbDelegate::Option; using DBSecurity = DistributedDB::SecurityOption; using DBPassword = DistributedDB::CipherPassword; struct DBStoreDelegate : public DBObserver { DBStoreDelegate(DBStore *delegate, std::shared_ptr observers); ~DBStoreDelegate(); operator std::shared_ptr (); bool operator<(const Time &time) const; bool Close(DBManager &manager); void OnChange(const DBChangeData &data) override; void SetObservers(std::shared_ptr observers); private: std::vector Convert(const std::list &dbEntries); mutable Time time_; DBStore *delegate_ = nullptr; std::shared_ptr observers_ = nullptr; std::shared_mutex mutex_; }; Store GetStore(const StoreMetaData &data, std::shared_ptr observers, DBStatus &status); void CloseStore(uint32_t tokenId, const std::string &storeId); void CloseExcept(const std::set &users); void SetObserver(uint32_t tokenId, const std::string &storeId, std::shared_ptr observers); static DBOption GetDBOption(const StoreMetaData &data, const DBPassword &password); static DBSecurity GetDBSecurity(int32_t secLevel); static DBPassword GetDBPassword(const StoreMetaData &data); void SetThreadPool(std::shared_ptr executors); private: void GarbageCollect(); static constexpr int64_t INTERVAL = 1; ConcurrentMap> stores_; std::shared_ptr executors_; }; } // namespace OHOS::DistributedKv #endif // OHOS_DISTRIBUTED_DATA_SERVICE_KVDB_STORE_CACHE_H