Implement majority of Storage functionality.

It's also now thread-safe.
This commit is contained in:
Peter Johnson
2015-07-18 01:29:51 -07:00
parent 9b7e265762
commit 2437f06c7f
3 changed files with 183 additions and 25 deletions

View File

@@ -12,9 +12,11 @@
#include <functional>
#include <iosfwd>
#include <memory>
#include <mutex>
#include "llvm/StringMap.h"
#include "nt_Value.h"
#include "support/ConcurrentQueue.h"
#include "ntcore_cpp.h"
#include "SequenceNumber.h"
namespace nt {
@@ -41,9 +43,28 @@ class Storage {
typedef llvm::StringMap<StorageEntry> EntriesMap;
struct Update {
std::string name;
enum Kind { kAssign, kValueUpdate, kFlagsUpdate, kDelete, kDeleteAll };
Kind kind;
};
typedef ConcurrentQueue<Update> UpdateQueue;
std::mutex& mutex() { return m_mutex; }
EntriesMap& entries() { return m_entries; }
const EntriesMap& entries() const { return m_entries; }
UpdateQueue& updates() { return m_updates; }
std::shared_ptr<Value> GetEntryValue(StringRef name) const;
bool SetEntryValue(StringRef name, std::shared_ptr<Value> value);
void SetEntryTypeValue(StringRef name, std::shared_ptr<Value> value);
void SetEntryFlags(StringRef name, unsigned int flags);
unsigned int GetEntryFlags(StringRef name) const;
void DeleteEntry(StringRef name);
void DeleteAllEntries();
std::vector<EntryInfo> GetEntryInfo(StringRef prefix, unsigned int types);
void SavePersistent(std::ostream& os) const;
bool LoadPersistent(
std::istream& is,
@@ -54,7 +75,9 @@ class Storage {
Storage(const Storage&) = delete;
Storage& operator=(const Storage&) = delete;
mutable std::mutex m_mutex;
EntriesMap m_entries;
UpdateQueue m_updates;
static std::unique_ptr<Storage> m_instance;
};