1fc223305Sopenharmony_ci/*
2fc223305Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3fc223305Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fc223305Sopenharmony_ci * you may not use this file except in compliance with the License.
5fc223305Sopenharmony_ci * You may obtain a copy of the License at
6fc223305Sopenharmony_ci *
7fc223305Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8fc223305Sopenharmony_ci *
9fc223305Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fc223305Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fc223305Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fc223305Sopenharmony_ci * See the License for the specific language governing permissions and
13fc223305Sopenharmony_ci * limitations under the License.
14fc223305Sopenharmony_ci */
15fc223305Sopenharmony_ci
16fc223305Sopenharmony_ci#ifndef PREFERENCES_IMPL_H
17fc223305Sopenharmony_ci#define PREFERENCES_IMPL_H
18fc223305Sopenharmony_ci
19fc223305Sopenharmony_ci#include <any>
20fc223305Sopenharmony_ci#include <condition_variable>
21fc223305Sopenharmony_ci#include <filesystem>
22fc223305Sopenharmony_ci#include <list>
23fc223305Sopenharmony_ci#include <map>
24fc223305Sopenharmony_ci#include <string>
25fc223305Sopenharmony_ci#include <vector>
26fc223305Sopenharmony_ci#include <shared_mutex>
27fc223305Sopenharmony_ci#include "safe_block_queue.h"
28fc223305Sopenharmony_ci#include "concurrent_map.h"
29fc223305Sopenharmony_ci#include "preferences_base.h"
30fc223305Sopenharmony_ci
31fc223305Sopenharmony_cinamespace OHOS {
32fc223305Sopenharmony_cinamespace NativePreferences {
33fc223305Sopenharmony_ciclass PreferencesImpl : public PreferencesBase, public std::enable_shared_from_this<PreferencesImpl> {
34fc223305Sopenharmony_cipublic:
35fc223305Sopenharmony_ci    static std::shared_ptr<PreferencesImpl> GetPreferences(const Options &options)
36fc223305Sopenharmony_ci    {
37fc223305Sopenharmony_ci        return std::shared_ptr<PreferencesImpl>(new PreferencesImpl(options));
38fc223305Sopenharmony_ci    }
39fc223305Sopenharmony_ci    virtual ~PreferencesImpl();
40fc223305Sopenharmony_ci
41fc223305Sopenharmony_ci    int Init();
42fc223305Sopenharmony_ci
43fc223305Sopenharmony_ci    PreferencesValue Get(const std::string &key, const PreferencesValue &defValue) override;
44fc223305Sopenharmony_ci
45fc223305Sopenharmony_ci    int Put(const std::string &key, const PreferencesValue &value) override;
46fc223305Sopenharmony_ci
47fc223305Sopenharmony_ci    bool HasKey(const std::string &key) override;
48fc223305Sopenharmony_ci
49fc223305Sopenharmony_ci    std::map<std::string, PreferencesValue> GetAll() override;
50fc223305Sopenharmony_ci
51fc223305Sopenharmony_ci    int Delete(const std::string &key) override;
52fc223305Sopenharmony_ci
53fc223305Sopenharmony_ci    int Clear() override;
54fc223305Sopenharmony_ci
55fc223305Sopenharmony_ci    void Flush() override;
56fc223305Sopenharmony_ci
57fc223305Sopenharmony_ci    int FlushSync() override;
58fc223305Sopenharmony_ci
59fc223305Sopenharmony_ci    std::pair<int, PreferencesValue> GetValue(const std::string &key, const PreferencesValue &defValue) override;
60fc223305Sopenharmony_ci
61fc223305Sopenharmony_ci    std::pair<int, std::map<std::string, PreferencesValue>> GetAllData() override;
62fc223305Sopenharmony_ciprivate:
63fc223305Sopenharmony_ci    explicit PreferencesImpl(const Options &options);
64fc223305Sopenharmony_ci
65fc223305Sopenharmony_ci    void NotifyPreferencesObserver(const std::list<std::string> &keysModified,
66fc223305Sopenharmony_ci        const std::map<std::string, PreferencesValue> &writeToDiskMap);
67fc223305Sopenharmony_ci    bool StartLoadFromDisk();
68fc223305Sopenharmony_ci
69fc223305Sopenharmony_ci    /* thread function */
70fc223305Sopenharmony_ci    static void LoadFromDisk(std::shared_ptr<PreferencesImpl> pref);
71fc223305Sopenharmony_ci    void AwaitLoadFile();
72fc223305Sopenharmony_ci    bool WriteSettingXml(const Options &options, const std::map<std::string, PreferencesValue> &writeToDiskMap);
73fc223305Sopenharmony_ci    static int WriteToDiskFile(std::shared_ptr<PreferencesImpl> pref);
74fc223305Sopenharmony_ci    static bool ReadSettingXml(std::shared_ptr<PreferencesImpl> pref);
75fc223305Sopenharmony_ci
76fc223305Sopenharmony_ci    std::atomic<bool> loaded_;
77fc223305Sopenharmony_ci
78fc223305Sopenharmony_ci    /* Current memory state (always increasing) */
79fc223305Sopenharmony_ci    int64_t currentMemoryStateGeneration_;
80fc223305Sopenharmony_ci    /* Latest memory state that was committed to disk */
81fc223305Sopenharmony_ci    int64_t diskStateGeneration_;
82fc223305Sopenharmony_ci
83fc223305Sopenharmony_ci    std::list<std::string> modifiedKeys_;
84fc223305Sopenharmony_ci
85fc223305Sopenharmony_ci    ConcurrentMap<std::string, PreferencesValue> valuesCache_;
86fc223305Sopenharmony_ci
87fc223305Sopenharmony_ci    std::shared_ptr<SafeBlockQueue<uint64_t>> queue_;
88fc223305Sopenharmony_ci};
89fc223305Sopenharmony_ci} // End of namespace NativePreferences
90fc223305Sopenharmony_ci} // End of namespace OHOS
91fc223305Sopenharmony_ci#endif // End of #ifndef PREFERENCES_IMPL_H