summaryrefslogtreecommitdiff
path: root/src/database/include/index.hpp
blob: 45dae464eda86e31a44b518e0dfe1ad2efdf5fe5 (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
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#pragma once

#include <stdint.h>

#include <cstdint>
#include <string>
#include <variant>
#include <vector>

#include "collation.hpp"
#include "leveldb/db.h"
#include "leveldb/slice.h"

#include "leveldb/write_batch.h"
#include "memory_resource.hpp"
#include "track.hpp"

namespace database {

typedef uint8_t IndexId;

struct IndexInfo {
  // Unique id for this index
  IndexId id;
  // Localised, user-friendly description of this index. e.g. "Albums by Artist"
  // or "All Tracks".
  std::pmr::string name;
  // Specifier for how this index breaks down the database.
  std::vector<Tag> components;
};

struct IndexKey {
  struct Header {
    // The index that this key was created for.
    IndexId id;
    // The number of components of IndexInfo that have already been filtered.
    // For example, if an index consists of { kGenre, kArtist }, and this key
    // represents an artist, then depth = 1.
    std::uint8_t depth;
    // The cumulative hash of all filtered components, in order. For example, if
    // an index consists of { kArtist, kAlbum, kTitle }, and we are at depth = 2
    // then this may contain hash(hash("Jacqueline"), "My Cool Album").
    std::uint64_t components_hash;

    bool operator==(const Header&) const = default;
  };
  Header header;

  // The filterable / selectable item that this key represents. "Jacqueline" for
  // kArtist, "My Cool Album" for kAlbum, etc.
  std::optional<std::pmr::string> item;
  // If this is a leaf component, the track id for this record.
  // This could reasonably be the value for a record, but we keep it as a part
  // of the key to help with disambiguation.
  std::optional<TrackId> track;
};

auto Index(locale::ICollator&, const IndexInfo&, const Track&)
    -> std::vector<std::pair<IndexKey, std::string>>;

auto ExpandHeader(const IndexKey::Header&,
                  const std::optional<std::pmr::string>&) -> IndexKey::Header;

// Predefined indexes
// TODO(jacqueline): Make these defined at runtime! :)

extern const IndexInfo kAlbumsByArtist;
extern const IndexInfo kTracksByGenre;
extern const IndexInfo kAllTracks;
extern const IndexInfo kAllAlbums;

}  // namespace database