aboutsummaryrefslogtreecommitdiffstats
path: root/src/CursorManager.cpp
blob: 23f826ff680320d2a21e231bedce72a600659e38 (plain) (blame)
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
#include "CursorManager.h"
#include "Merkle.h"
#include "ObjectManager.h"
#include "TupleCodec.h"

namespace nt {
    CursorManager::CursorManager(IStorageBackend& backend, LifecycleManager* lifecycles,
                                 ObjectManager* objects)
        : backend_(backend), lifecycles_(lifecycles), objects_(objects) {
    }

    // Finds the /system/snapshots/<H> entry that owns a handle's relation, or
    // nullptr when the relation lives outside the snapshot namespace (tests use
    // synthetic paths like {"relations", "villager"}).
    static ObjectManager::registry* find_parent_snapshot(ObjectManager& objects,
                                                         HandlerManager::handle* handle) {
        if (handle == nullptr || handle->object == nullptr || handle->object->head == nullptr)
            return nullptr;

        const auto& p = handle->object->head->path;
        if (p.size() != 5)
            return nullptr;
        if (p[0] != "system" || p[1] != "snapshots" || p[3] != "relations")
            return nullptr;

        return objects.Find({p[0], p[1], p[2]});
    }

    void CursorManager::LoadPage(cursor* c) {
        auto entries = Merkle<Hash32>::Page(backend_, c->merkle_root, c->fetch_offset, PAGE_SIZE);
        c->page.clear();
        c->page_position = 0;

        for (const auto& entry : entries) {
            auto bytes = backend_.Get(bin_to_hex(entry.payload));
            if (!bytes)
                continue;
            c->page.emplace_back(TupleCodec::Deserialize(*bytes));
        }

        c->fetch_offset += entries.size();
        if (entries.size() < PAGE_SIZE)
            c->exhausted = true;
    }

    CursorManager::cursor* CursorManager::Open(HandlerManager::handle* handle,
                                               const std::string& merkle_root) {
        if (handle == nullptr || handle->object == nullptr)
            return nullptr;

        const auto label = handle->object->head->type->label;

        auto pin_snapshot = [&](cursor* c) {
            if (lifecycles_ == nullptr || objects_ == nullptr)
                return;
            auto* snap = find_parent_snapshot(*objects_, handle);
            if (snap == nullptr)
                return;
            lifecycles_->Pin(snap);
            c->pinned_snapshot = snap;
        };

        if (label == EPHEMERAL_RELATION) {
            // Cursor opens ready for a standalone scan (no bound args, offset
            // zero) so a named ephemeral can be drained like a stored relation.
            // A JOIN probe overwrites this state anyway: the operator writes
            // args and resets the cursor before the first probe. The generator
            // is copied off the registry-owned ephemeral_object_type so Next()
            // never has to dereference the handle again.
            auto* etype = static_cast<ObjectManager::ephemeral_object_type*>(
                handle->object->head->type.get());

            auto* c = new cursor();
            c->is_ephemeral = true;
            c->generator = etype->generator;
            pin_snapshot(c);
            return c;
        }

        if (label != RELATION)
            return nullptr;

        auto* c = new cursor();
        c->merkle_root = merkle_root;
        pin_snapshot(c);

        if (merkle_root.empty()) {
            // Empty relation: valid but immediately exhausted.
            c->exhausted = true;
            return c;
        }

        LoadPage(c);
        return c;
    }

    Tuple* CursorManager::Next(cursor* c) {
        if (c == nullptr)
            return nullptr;

        if (c->page_position < c->page.size())
            return &c->page[c->page_position++];

        if (c->exhausted)
            return nullptr;

        if (c->is_ephemeral) {
            c->page = c->generator(c->args, c->fetch_offset, PAGE_SIZE);
            c->page_position = 0;
            c->fetch_offset += c->page.size();
            if (c->page.size() < PAGE_SIZE)
                c->exhausted = true;
            if (c->page.empty())
                return nullptr;
            return &c->page[c->page_position++];
        }

        LoadPage(c);
        if (c->page.empty())
            return nullptr;
        return &c->page[c->page_position++];
    }

    void CursorManager::Close(cursor* c) {
        if (c == nullptr) {
            return;
        }
        if (lifecycles_ != nullptr && c->pinned_snapshot != nullptr)
            lifecycles_->Unpin(c->pinned_snapshot);
        delete c;
    }
} // namespace nt