summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibSQL/Serializer.h
blob: 8a2be68e3d23a1a9127463139038253cf42e330c (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
/*
 * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/ByteBuffer.h>
#include <AK/Debug.h>
#include <AK/DeprecatedString.h>
#include <AK/Format.h>
#include <LibSQL/Forward.h>
#include <LibSQL/Heap.h>
#include <string.h>

namespace SQL {

class Serializer {
public:
    Serializer() = default;

    Serializer(RefPtr<Heap> heap)
        : m_heap(heap)
    {
    }

    void get_block(u32 pointer)
    {
        auto buffer_or_error = m_heap->read_block(pointer);
        if (buffer_or_error.is_error())
            VERIFY_NOT_REACHED();
        m_buffer = buffer_or_error.value();
        m_current_offset = 0;
    }

    void reset()
    {
        m_buffer.clear();
        m_current_offset = 0;
    }

    void rewind()
    {
        m_current_offset = 0;
    }

    template<typename T, typename... Args>
    T deserialize_block(u32 pointer, Args&&... args)
    {
        get_block(pointer);
        return deserialize<T>(forward<Args>(args)...);
    }

    template<typename T>
    void deserialize_block_to(u32 pointer, T& t)
    {
        get_block(pointer);
        return deserialize_to<T>(t);
    }

    template<typename T>
    void deserialize_to(T& t)
    {
        if constexpr (IsArithmetic<T>)
            memcpy(&t, read(sizeof(T)), sizeof(T));
        else
            t.deserialize(*this);
    }

    void deserialize_to(DeprecatedString& text);

    template<typename T, typename... Args>
    NonnullOwnPtr<T> make_and_deserialize(Args&&... args)
    {
        auto ptr = make<T>(forward<Args>(args)...);
        ptr->deserialize(*this);
        return ptr;
    }

    template<typename T, typename... Args>
    NonnullRefPtr<T> adopt_and_deserialize(Args&&... args)
    {
        auto ptr = adopt_ref(*new T(forward<Args>(args)...));
        ptr->deserialize(*this);
        return ptr;
    }

    template<typename T, typename... Args>
    T deserialize(Args&&... args)
    {
        T t(forward<Args>(args)...);
        deserialize_to(t);
        return t;
    }

    template<typename T>
    void serialize(T const& t)
    {
        if constexpr (IsArithmetic<T>)
            write((u8 const*)(&t), sizeof(T));
        else
            t.serialize(*this);
    }

    void serialize(DeprecatedString const&);

    template<typename T>
    bool serialize_and_write(T const& t)
    {
        VERIFY(!m_heap.is_null());
        reset();
        serialize<T>(t);
        m_heap->add_to_wal(t.pointer(), m_buffer);
        return true;
    }

    [[nodiscard]] size_t offset() const { return m_current_offset; }
    u32 new_record_pointer()
    {
        return m_heap->new_record_pointer();
    }

    bool has_block(u32 pointer) const
    {
        return pointer < m_heap->size();
    }

    Heap& heap()
    {
        return *m_heap;
    }

private:
    void write(u8 const* ptr, size_t sz)
    {
        if constexpr (SQL_DEBUG)
            dump(ptr, sz, "(out) =>");
        m_buffer.append(ptr, sz);
        m_current_offset += sz;
    }

    u8 const* read(size_t sz)
    {
        auto buffer_ptr = m_buffer.offset_pointer(m_current_offset);
        if constexpr (SQL_DEBUG)
            dump(buffer_ptr, sz, "<= (in)");
        m_current_offset += sz;
        return buffer_ptr;
    }

    static void dump(u8 const* ptr, size_t sz, DeprecatedString const& prefix)
    {
        StringBuilder builder;
        builder.appendff("{0} {1:04x} | ", prefix, sz);
        Vector<DeprecatedString> bytes;
        for (auto ix = 0u; ix < sz; ++ix)
            bytes.append(DeprecatedString::formatted("{0:02x}", *(ptr + ix)));
        StringBuilder bytes_builder;
        bytes_builder.join(' ', bytes);
        builder.append(bytes_builder.to_deprecated_string());
        dbgln(builder.to_deprecated_string());
    }

    ByteBuffer m_buffer {};
    size_t m_current_offset { 0 };
    RefPtr<Heap> m_heap { nullptr };
};

}