esp8266ndn
NDN Arduino library for ESP8266 and more
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
kv.hpp
Go to the documentation of this file.
1#ifndef NDNPH_STORE_KV_HPP
2#define NDNPH_STORE_KV_HPP
3
4#include "../port/fs/port.hpp"
5#include "../tlv/value.hpp"
6
7namespace ndnph {
8
10class KvStore {
11public:
13 explicit KvStore() = default;
14
17 : m_fs(&fs) {}
18
23 template<typename... Arg>
24 bool open(Arg&&... arg) {
25 if (m_fs == nullptr) {
26 m_ownFs.reset(new port::FileStore());
27 m_fs = m_ownFs.get();
28 }
29 return m_fs->open(std::forward<Arg>(arg)...);
30 }
31
38 tlv::Value get(const char* key, Region& region) {
39 if (m_fs == nullptr || !checkKey(key)) {
40 return tlv::Value();
41 }
42 int size = m_fs->read(key, nullptr, 0);
43 if (size <= 0) {
44 return tlv::Value();
45 }
46
47 uint8_t* buf = region.alloc(size);
48 if (buf == nullptr) {
49 return tlv::Value();
50 }
51 int size2 = m_fs->read(key, buf, size);
52 if (size2 != size) {
53 region.free(buf, size);
54 return tlv::Value();
55 }
56 return tlv::Value(buf, size);
57 }
58
69 bool set(const char* key, tlv::Value value) {
70 if (m_fs == nullptr || !checkKey(key)) {
71 return false;
72 }
73 return m_fs->write(key, value.begin(), value.size());
74 }
75
81 bool del(const char* key) {
82 if (m_fs == nullptr || !checkKey(key)) {
83 return false;
84 }
85 return m_fs->unlink(key);
86 }
87
88private:
89 static bool checkKey(const char* key) {
90 size_t keyLen = 0;
91 if (key == nullptr || (keyLen = strlen(key)) == 0) {
92 return false;
93 }
94 for (size_t i = 0; i < keyLen; ++i) {
95 char ch = key[i];
96 if (!isdigit(ch) && !islower(ch) && ch != '_') {
97 return false;
98 }
99 }
100 return true;
101 }
102
103private:
104 std::unique_ptr<port::FileStore> m_ownFs;
105 port::FileStore* m_fs = nullptr;
106};
107
108} // namespace ndnph
109
110#endif // NDNPH_STORE_KV_HPP
File storage on microcontroller filesystem.
Definition fs.hpp:11
int read(const char *filename, uint8_t *buffer, size_t count)
bool unlink(const char *filename)
bool open(const char *path)
bool write(const char *filename, const uint8_t *buffer, size_t count)
File based key-value store.
Definition kv.hpp:10
bool del(const char *key)
Delete a key.
Definition kv.hpp:81
KvStore(port::FileStore &fs)
Constructor to use existing FileStore instance.
Definition kv.hpp:16
tlv::Value get(const char *key, Region &region)
Retrieve a value.
Definition kv.hpp:38
bool open(Arg &&... arg)
Open the FileStore backend.
Definition kv.hpp:24
bool set(const char *key, tlv::Value value)
Store a value.
Definition kv.hpp:69
KvStore()=default
Constructor to use internal FileStore instance.
Region-based memory allocator thats owns memory of NDNph objects.
Definition region.hpp:9
uint8_t * alloc(size_t size)
Allocate a buffer with no alignment requirement.
Definition region.hpp:27
bool free(const uint8_t *first, const uint8_t *last)
Deallocate (part of) last allocated buffer.
Definition region.hpp:51
A sequence of bytes, usually TLV-VALUE.
Definition value.hpp:11
const uint8_t * begin() const
Definition value.hpp:38
size_t size() const
Definition value.hpp:46
Definition fs.hpp:33