summaryrefslogtreecommitdiff
path: root/src/database/records.cpp
blob: 0d28ca2873d20a4059b6fc1c61e105498d88d367 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include "records.hpp"

#include <stdint.h>

#include <iomanip>
#include <sstream>
#include <string>
#include <vector>

#include "cbor.h"
#include "esp_log.h"

#include "index.hpp"
#include "komihash.h"
#include "shared_string.h"
#include "track.hpp"

// As LevelDB is a key-value store, each record in the database consists of a
// key and an optional value.
//
// Values, when present, are always cbor-encoded. This is fast, compact, and
// very easy to evolve over time due to its inclusion of type information.
//
// Keys have a more complicated scheme, as for performance we rely heavily on
// LevelDB's sorted storage format. We must therefore worry about clustering of
// similar records, and the sortability of our encoding format.
//    Each kind of key consists of a a single-byte prefix, then one or more
// fields separated by null (0) bytes. Each field may be cbor-encoded, or may
// use some bespoke encoding; it depends on whether we want to be able to sort
// by that field.
//    For debugging and discussion purposes, we represent field separators
// textually as '/', and write each field as its hex encoding. e.g. a data key
// for the track with id 17 would be written as 'D / 0x11'.

namespace database {

static const char* kTag = "RECORDS";

static const char kDataPrefix = 'D';
static const char kHashPrefix = 'H';
static const char kIndexPrefix = 'I';
static const char kFieldSeparator = '\0';

/*
 * Helper function for allocating an appropriately-sized byte buffer, then
 * encoding data into it.
 *
 * 'T' should be a callable that takes a CborEncoder* as
 * an argument, and stores values within that encoder. 'T' will be called
 * exactly twice: first to detemine the buffer size, and then second to do the
 * encoding.
 *
 * 'out_buf' will be set to the location of newly allocated memory; it is up to
 * the caller to free it. Returns the size of 'out_buf'.
 */
template <typename T>
auto cbor_encode(uint8_t** out_buf, T fn) -> std::size_t {
  // First pass: work out how many bytes we will encode into.
  // FIXME: With benchmarking to help, we could consider preallocting a small
  // buffer here to do the whole encoding in one pass.
  CborEncoder size_encoder;
  cbor_encoder_init(&size_encoder, NULL, 0, 0);
  std::invoke(fn, &size_encoder);
  std::size_t buf_size = cbor_encoder_get_extra_bytes_needed(&size_encoder);

  // Second pass: do the encoding.
  CborEncoder encoder;
  *out_buf = new uint8_t[buf_size];
  cbor_encoder_init(&encoder, *out_buf, buf_size, 0);
  std::invoke(fn, &encoder);

  return buf_size;
}

OwningSlice::OwningSlice(std::string d) : data(d), slice(data) {}

/* 'D/' */
auto EncodeDataPrefix() -> OwningSlice {
  char data[2] = {kDataPrefix, kFieldSeparator};
  return OwningSlice({data, 2});
}

/* 'D/ 0xACAB' */
auto EncodeDataKey(const TrackId& id) -> OwningSlice {
  std::ostringstream output;
  output.put(kDataPrefix).put(kFieldSeparator);
  output << TrackIdToBytes(id).data;
  return OwningSlice(output.str());
}

auto EncodeDataValue(const TrackData& track) -> OwningSlice {
  uint8_t* buf;
  std::size_t buf_len = cbor_encode(&buf, [&](CborEncoder* enc) {
    CborEncoder array_encoder;
    CborError err;
    err = cbor_encoder_create_array(enc, &array_encoder, 5);
    if (err != CborNoError && err != CborErrorOutOfMemory) {
      ESP_LOGE(kTag, "encoding err %u", err);
      return;
    }
    err = cbor_encode_int(&array_encoder, track.id());
    if (err != CborNoError && err != CborErrorOutOfMemory) {
      ESP_LOGE(kTag, "encoding err %u", err);
      return;
    }
    err = cbor_encode_text_string(&array_encoder, track.filepath().c_str(),
                                  track.filepath().size());
    if (err != CborNoError && err != CborErrorOutOfMemory) {
      ESP_LOGE(kTag, "encoding err %u", err);
      return;
    }
    err = cbor_encode_uint(&array_encoder, track.tags_hash());
    if (err != CborNoError && err != CborErrorOutOfMemory) {
      ESP_LOGE(kTag, "encoding err %u", err);
      return;
    }
    err = cbor_encode_int(&array_encoder, track.play_count());
    if (err != CborNoError && err != CborErrorOutOfMemory) {
      ESP_LOGE(kTag, "encoding err %u", err);
      return;
    }
    err = cbor_encode_boolean(&array_encoder, track.is_tombstoned());
    if (err != CborNoError && err != CborErrorOutOfMemory) {
      ESP_LOGE(kTag, "encoding err %u", err);
      return;
    }
    err = cbor_encoder_close_container(enc, &array_encoder);
    if (err != CborNoError && err != CborErrorOutOfMemory) {
      ESP_LOGE(kTag, "encoding err %u", err);
      return;
    }
  });
  std::string as_str(reinterpret_cast<char*>(buf), buf_len);
  delete buf;
  return OwningSlice(as_str);
}

auto ParseDataValue(const leveldb::Slice& slice) -> std::optional<TrackData> {
  CborParser parser;
  CborValue container;
  CborError err;
  err = cbor_parser_init(reinterpret_cast<const uint8_t*>(slice.data()),
                         slice.size(), 0, &parser, &container);
  if (err != CborNoError || !cbor_value_is_container(&container)) {
    return {};
  }

  CborValue val;
  err = cbor_value_enter_container(&container, &val);
  if (err != CborNoError || !cbor_value_is_unsigned_integer(&val)) {
    return {};
  }

  uint64_t raw_int;
  err = cbor_value_get_uint64(&val, &raw_int);
  if (err != CborNoError) {
    return {};
  }
  TrackId id = raw_int;
  err = cbor_value_advance(&val);
  if (err != CborNoError || !cbor_value_is_text_string(&val)) {
    return {};
  }

  char* raw_path;
  std::size_t len;
  err = cbor_value_dup_text_string(&val, &raw_path, &len, &val);
  if (err != CborNoError || !cbor_value_is_unsigned_integer(&val)) {
    return {};
  }
  std::string path(raw_path, len);
  delete raw_path;

  err = cbor_value_get_uint64(&val, &raw_int);
  if (err != CborNoError) {
    return {};
  }
  uint64_t hash = raw_int;
  err = cbor_value_advance(&val);
  if (err != CborNoError || !cbor_value_is_unsigned_integer(&val)) {
    return {};
  }

  err = cbor_value_get_uint64(&val, &raw_int);
  if (err != CborNoError) {
    return {};
  }
  uint32_t play_count = raw_int;
  err = cbor_value_advance(&val);
  if (err != CborNoError || !cbor_value_is_boolean(&val)) {
    return {};
  }

  bool is_tombstoned;
  err = cbor_value_get_boolean(&val, &is_tombstoned);
  if (err != CborNoError) {
    return {};
  }

  return TrackData(id, path, hash, play_count, is_tombstoned);
}

/* 'H/ 0xBEEF' */
auto EncodeHashKey(const uint64_t& hash) -> OwningSlice {
  std::ostringstream output;
  output.put(kHashPrefix).put(kFieldSeparator);

  uint8_t buf[16];
  CborEncoder enc;
  cbor_encoder_init(&enc, buf, sizeof(buf), 0);
  cbor_encode_uint(&enc, hash);
  std::size_t len = cbor_encoder_get_buffer_size(&enc, buf);
  output.write(reinterpret_cast<char*>(buf), len);

  return OwningSlice(output.str());
}

auto ParseHashValue(const leveldb::Slice& slice) -> std::optional<TrackId> {
  return BytesToTrackId(slice.ToString());
}

auto EncodeHashValue(TrackId id) -> OwningSlice {
  return TrackIdToBytes(id);
}

/* 'I/' */
auto EncodeAllIndexesPrefix() -> OwningSlice {
  char data[2] = {kIndexPrefix, kFieldSeparator};
  return OwningSlice({data, 2});
}

auto AppendIndexHeader(const IndexKey::Header& header, std::ostringstream* out)
    -> void {
  *out << kIndexPrefix << kFieldSeparator;

  // Construct the header.
  uint8_t* buf;
  std::size_t buf_len = cbor_encode(&buf, [&](CborEncoder* enc) {
    CborEncoder array_encoder;
    CborError err;
    err = cbor_encoder_create_array(enc, &array_encoder, 3);
    if (err != CborNoError && err != CborErrorOutOfMemory) {
      ESP_LOGE(kTag, "encoding err %u", err);
      return;
    }
    err = cbor_encode_uint(&array_encoder, header.id);
    if (err != CborNoError && err != CborErrorOutOfMemory) {
      ESP_LOGE(kTag, "encoding err %u", err);
      return;
    }
    err = cbor_encode_uint(&array_encoder, header.depth);
    if (err != CborNoError && err != CborErrorOutOfMemory) {
      ESP_LOGE(kTag, "encoding err %u", err);
      return;
    }
    err = cbor_encode_uint(&array_encoder, header.components_hash);
    if (err != CborNoError && err != CborErrorOutOfMemory) {
      ESP_LOGE(kTag, "encoding err %u", err);
      return;
    }
    err = cbor_encoder_close_container(enc, &array_encoder);
    if (err != CborNoError && err != CborErrorOutOfMemory) {
      ESP_LOGE(kTag, "encoding err %u", err);
      return;
    }
  });
  std::string encoded{reinterpret_cast<char*>(buf), buf_len};
  delete buf;
  *out << encoded << kFieldSeparator;
}

auto EncodeIndexPrefix(const IndexKey::Header& header) -> OwningSlice {
  std::ostringstream out;
  AppendIndexHeader(header, &out);
  return OwningSlice(out.str());
}

/*
 * 'I/0xa2/0x686921/0xb9'
 *                   ^ --- trailer
 *          ^ --- component ("hi!")
 *     ^ -------- header
 *
 *  The components *must* be encoded in a way that is easy to sort
 *  lexicographically. The header and footer do not have this restriction, so
 *  cbor is fine.
 *
 *  We store grouping information within the header; which index, filtered
 *  components. We store disambiguation information in the trailer; just a track
 *  id for now, but could reasonably be something like 'release year' as well.
 */
auto EncodeIndexKey(const IndexKey& key) -> OwningSlice {
  std::ostringstream out;

  // Construct the header.
  AppendIndexHeader(key.header, &out);

  // The component should already be UTF-8 encoded, so just write it.
  if (key.item) {
    out << *key.item;
  }

  // Construct the footer.
  out << kFieldSeparator;
  if (key.track) {
    auto encoded = TrackIdToBytes(*key.track);
    out << encoded.data;
  }
  return OwningSlice(out.str());
}

auto ParseIndexKey(const leveldb::Slice& slice) -> std::optional<IndexKey> {
  IndexKey result{};

  auto prefix = EncodeAllIndexesPrefix();
  if (!slice.starts_with(prefix.data)) {
    return {};
  }

  std::string key_data = slice.ToString().substr(prefix.data.size());
  std::size_t header_length = 0;
  {
    CborParser parser;
    CborValue container;
    CborError err;
    err = cbor_parser_init(reinterpret_cast<const uint8_t*>(key_data.data()),
                           key_data.size(), 0, &parser, &container);
    if (err != CborNoError || !cbor_value_is_container(&container)) {
      return {};
    }

    CborValue val;
    err = cbor_value_enter_container(&container, &val);
    if (err != CborNoError || !cbor_value_is_unsigned_integer(&val)) {
      return {};
    }

    uint64_t raw_int;
    err = cbor_value_get_uint64(&val, &raw_int);
    if (err != CborNoError) {
      return {};
    }
    result.header.id = raw_int;
    err = cbor_value_advance(&val);
    if (err != CborNoError || !cbor_value_is_unsigned_integer(&val)) {
      return {};
    }

    err = cbor_value_get_uint64(&val, &raw_int);
    if (err != CborNoError) {
      return {};
    }
    result.header.depth = raw_int;
    err = cbor_value_advance(&val);
    if (err != CborNoError || !cbor_value_is_unsigned_integer(&val)) {
      return {};
    }

    err = cbor_value_get_uint64(&val, &raw_int);
    if (err != CborNoError) {
      return {};
    }
    result.header.components_hash = raw_int;
    err = cbor_value_advance(&val);
    if (err != CborNoError || !cbor_value_at_end(&val)) {
      return {};
    }

    const uint8_t* next_byte = cbor_value_get_next_byte(&val);
    header_length =
        next_byte - reinterpret_cast<const uint8_t*>(key_data.data());
  }

  if (header_length == 0) {
    return {};
  }

  if (header_length >= key_data.size()) {
    return {};
  }

  std::istringstream in(key_data.substr(header_length + 1));
  std::stringbuf buffer{};

  in.get(buffer, kFieldSeparator);
  if (buffer.str().size() > 0) {
    result.item = buffer.str();
  }

  buffer = {};
  in.get(buffer);
  std::string id_str = buffer.str();
  if (id_str.size() > 1) {
    result.track = BytesToTrackId(id_str.substr(1));
  }

  return result;
}

auto TrackIdToBytes(TrackId id) -> OwningSlice {
  uint8_t buf[8];
  CborEncoder enc;
  cbor_encoder_init(&enc, buf, sizeof(buf), 0);
  cbor_encode_uint(&enc, id);
  std::size_t len = cbor_encoder_get_buffer_size(&enc, buf);
  std::string as_str(reinterpret_cast<char*>(buf), len);
  return OwningSlice(as_str);
}

auto BytesToTrackId(const std::string& bytes) -> std::optional<TrackId> {
  CborParser parser;
  CborValue val;
  cbor_parser_init(reinterpret_cast<const uint8_t*>(bytes.data()), bytes.size(),
                   0, &parser, &val);
  if (!cbor_value_is_unsigned_integer(&val)) {
    return {};
  }
  uint64_t raw_id;
  cbor_value_get_uint64(&val, &raw_id);
  return raw_id;
}

}  // namespace database