scsl/Dictionary.h

45 lines
935 B
C
Raw Normal View History

2023-10-06 06:17:09 +00:00
#ifndef KLIB_DICTIONARY_H
#define KLIB_DICTIONARY_H
2023-10-06 03:13:46 +00:00
#include "Arena.h"
#include "TLV.h"
2023-10-06 06:17:09 +00:00
#define DICTIONARY_TAG_KEY 1
#define DICTIONARY_TAG_VAL 2
2023-10-06 03:13:46 +00:00
2023-10-06 06:17:09 +00:00
/*
* A Dictionary is a collection of key-value pairs, similar to how
* a dictionary is a mapping of names to definitions.
*/
class Dictionary {
2023-10-06 03:13:46 +00:00
public:
2023-10-06 06:17:09 +00:00
Dictionary(Arena &arena) :
2023-10-06 06:08:35 +00:00
arena(arena),
2023-10-06 06:17:09 +00:00
kTag(DICTIONARY_TAG_KEY),
vTag(DICTIONARY_TAG_VAL) {} ;
Dictionary(Arena &arena, uint8_t kt, uint8_t vt) :
2023-10-06 06:08:35 +00:00
arena(arena),
kTag(kt),
vTag(vt) {};
bool Lookup(const char *key, uint8_t klen, TLV::Record &res);
int Set(const char *key, uint8_t klen, const char *val,
uint8_t vlen);
bool Has(const char *key, uint8_t klen);
void DumpKVPairs();
void DumpToFile(const char *path);
2023-10-06 03:13:46 +00:00
private:
uint8_t *seek(const char *key, uint8_t klen);
2023-10-06 06:08:35 +00:00
bool spaceAvailable(uint8_t klen, uint8_t vlen);
2023-10-06 03:13:46 +00:00
Arena &arena;
2023-10-06 06:08:35 +00:00
uint8_t kTag;
uint8_t vTag;
2023-10-06 03:13:46 +00:00
};
#endif