scsl/Dictionary.cc

188 lines
4.0 KiB
C++
Raw Permalink Normal View History

///
/// \file Dictionary.cc
/// \author K.Isom <kyle@imap.cc>
/// \date 2023-10-05
/// \brief Key-value store built on top of Arena and TLV.
///
/// Copyright 2023 K. Isom <kyle@imap.cc>
///
/// Permission to use, copy, modify, and/or distribute this software for
/// any purpose with or without fee is hereby granted, provided that
/// the above copyright notice and this permission notice appear in all /// copies.
///
/// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
/// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
/// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
/// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
/// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
/// OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
/// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
/// PERFORMANCE OF THIS SOFTWARE.
///
2023-10-18 23:19:09 +00:00
#include <cassert>
2023-10-06 03:13:46 +00:00
#include <cstdlib>
#include <cstring>
2023-10-06 06:17:09 +00:00
#include "Dictionary.h"
2023-10-06 03:13:46 +00:00
2023-10-15 01:38:01 +00:00
#if defined(SCSL_DESKTOP_BUILD)
2023-10-06 06:08:35 +00:00
#include <iostream>
#endif
2023-10-15 01:38:01 +00:00
namespace scsl {
2023-10-06 03:13:46 +00:00
bool
2023-10-06 06:17:09 +00:00
Dictionary::Lookup(const char *key, uint8_t klen, TLV::Record &res)
2023-10-06 03:13:46 +00:00
{
2023-10-06 06:08:35 +00:00
res.Tag = this->kTag;
uint8_t *cursor = TLV::FindTag(this->arena, nullptr, res);
2023-10-06 03:13:46 +00:00
while (cursor != nullptr) {
2023-10-06 03:13:46 +00:00
if ((klen == res.Len) &&
(memcmp(res.Val, key, klen) == 0)) {
2023-10-06 06:08:35 +00:00
TLV::ReadFromMemory(res, cursor);
assert(res.Tag == this->vTag);
return res.Tag == this->vTag;
2023-10-06 03:13:46 +00:00
}
2023-10-06 06:08:35 +00:00
cursor = TLV::FindTag(this->arena, cursor, res);
2023-10-06 03:13:46 +00:00
}
return false;
}
int
2023-10-06 06:17:09 +00:00
Dictionary::Set(const char *key, uint8_t klen, const char *val, uint8_t vlen)
2023-10-06 03:13:46 +00:00
{
TLV::Record rec;
uint8_t *cursor = nullptr;
2023-10-06 03:13:46 +00:00
2023-10-06 06:08:35 +00:00
SetRecord(rec, this->kTag, klen, key);
2023-10-06 03:13:46 +00:00
cursor = this->seek(key, klen);
if (cursor != nullptr) {
2023-10-06 06:08:35 +00:00
TLV::DeleteRecord(this->arena, cursor);
TLV::DeleteRecord(this->arena, cursor);
2023-10-06 03:13:46 +00:00
}
2023-10-06 06:08:35 +00:00
if (!spaceAvailable(klen, vlen)) {
2023-10-06 04:18:21 +00:00
return -1;
}
cursor = TLV::WriteToMemory(this->arena, nullptr, rec);
if (cursor == nullptr) {
2023-10-06 03:13:46 +00:00
return -1;
}
2023-10-06 06:08:35 +00:00
SetRecord(rec, this->vTag, vlen, val);
if (TLV::WriteToMemory(this->arena, nullptr, rec) == nullptr) {
2023-10-06 03:13:46 +00:00
return -1;
}
return 0;
}
/// seek searches the Dictionary for the key.
2023-10-06 03:13:46 +00:00
uint8_t *
2023-10-06 06:17:09 +00:00
Dictionary::seek(const char *key, uint8_t klen)
2023-10-06 03:13:46 +00:00
{
TLV::Record rec;
2023-10-06 06:08:35 +00:00
rec.Tag = this->kTag;
uint8_t *cursor = TLV::LocateTag(this->arena, nullptr, rec);
2023-10-06 03:13:46 +00:00
while (cursor != nullptr) {
2023-10-06 06:08:35 +00:00
if ((klen == rec.Len) && (this->kTag == rec.Tag)) {
if (memcmp(rec.Val, key, klen) == 0) {
return cursor;
}
2023-10-06 03:13:46 +00:00
}
2023-10-06 06:08:35 +00:00
cursor = TLV::SkipRecord(rec, cursor);
cursor = TLV::LocateTag(this->arena, cursor, rec);
2023-10-06 03:13:46 +00:00
}
return nullptr;
2023-10-06 03:13:46 +00:00
}
bool
Dictionary::Contains(const char *key, uint8_t klen)
2023-10-06 03:13:46 +00:00
{
return this->seek(key, klen) != nullptr;
2023-10-06 03:13:46 +00:00
}
bool
Dictionary::Delete(const char *key, uint8_t klen)
{
auto *cursor = this->seek(key, klen);
if (cursor == nullptr) {
return false;
}
TLV::DeleteRecord(this->arena, cursor);
TLV::DeleteRecord(this->arena, cursor);
return true;
}
2023-10-06 04:18:21 +00:00
bool
2023-10-06 06:17:09 +00:00
Dictionary::spaceAvailable(uint8_t klen, uint8_t vlen)
2023-10-06 04:18:21 +00:00
{
size_t required = 0;
uintptr_t remaining = 0;
uint8_t *cursor = nullptr;
2023-10-06 04:18:21 +00:00
cursor = TLV::FindEmpty(this->arena, nullptr);
if (cursor == nullptr) {
2023-10-06 04:18:21 +00:00
return false;
}
2023-10-06 06:08:35 +00:00
required += klen + 2;
required += vlen + 2;
2023-10-06 04:18:21 +00:00
2023-10-15 01:38:01 +00:00
remaining = (uintptr_t)cursor - (uintptr_t) arena.Start();
remaining = arena.Size() - remaining;
2023-10-06 04:18:21 +00:00
return ((size_t)remaining >= required);
}
2023-10-06 06:08:35 +00:00
std::ostream &
operator<<(std::ostream &os, const Dictionary &dictionary)
2023-10-06 06:08:35 +00:00
{
2023-10-15 01:38:01 +00:00
#if defined(SCSL_DESKTOP_BUILD)
uint8_t *cursor = (dictionary.arena).Start();
2023-10-06 06:08:35 +00:00
TLV::Record rec;
TLV::ReadFromMemory(rec, cursor);
if (rec.Tag == TLV::TAG_EMPTY) {
os << "\t(NONE)" << std::endl;
return os;
2023-10-06 06:08:35 +00:00
}
while ((cursor != nullptr) && (rec.Tag != TLV::TAG_EMPTY)) {
os << "\t" << rec.Val << "->";
2023-10-06 06:08:35 +00:00
cursor = TLV::SkipRecord(rec, cursor);
TLV::ReadFromMemory(rec, cursor);
os << rec.Val << std::endl;
2023-10-06 06:08:35 +00:00
cursor = TLV::SkipRecord(rec, cursor);
TLV::ReadFromMemory(rec, cursor);
}
#endif
2023-10-06 06:08:35 +00:00
return os;
2023-10-06 06:08:35 +00:00
}
2023-10-06 06:08:35 +00:00
int
Dictionary::DumpToFile(const char *path)
2023-10-06 06:08:35 +00:00
{
return this->arena.Write(path);
2023-10-06 06:08:35 +00:00
}
2023-10-15 01:38:01 +00:00
} // namespace scsl