eloqstore-sys 1.1.1

Low-level Rust FFI bindings for EloqStore
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
#pragma once

#include <glog/logging.h>

#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "coding.h"
#include "manifest_buffer.h"
#include "types.h"

namespace eloqstore
{
constexpr uint32_t num_reserved_fd = 100;

// ParseFileName: splits filename into type and suffix
// Returns {type, suffix} where:
//   - type is the prefix before first separator (e.g., "data", "manifest")
//   - suffix is everything after first separator (or empty if no separator)
// Examples:
//   "data_123" -> {"data", "123"}
//   "data_123_5" -> {"data", "123_5"}
//   "manifest" -> {"manifest", ""}
//   "manifest_5" -> {"manifest", "5"}
//   "manifest_5_123456789" -> {"manifest", "5_123456789"}
inline std::pair<std::string_view, std::string_view> ParseFileName(
    std::string_view name)
{
    size_t pos = name.find(FileNameSeparator);
    std::string_view file_type;
    std::string_view suffix;

    if (pos == std::string::npos)
    {
        file_type = name;
        suffix = std::string_view{};
    }
    else
    {
        file_type = name.substr(0, pos);
        suffix = name.substr(pos + 1);
    }

    return {file_type, suffix};
}

// Helper function to parse a number from string_view
inline bool ParseUint64(std::string_view str, uint64_t &out)
{
    if (str.empty())
    {
        return false;
    }
    errno = 0;
    char *end = nullptr;
    out = std::strtoull(str.data(), &end, 10);
    if (errno != 0 || end != str.data() + str.size())
    {
        return false;
    }
    return true;
}

// Validates and normalizes branch name
// Valid pattern: [a-zA-Z0-9-]+ (alphanumeric and hyphen only, NO underscore)
// Underscore is reserved as FileNameSeparator
// Converts to lowercase
// Returns normalized name if valid, empty string if invalid
inline std::string NormalizeBranchName(std::string_view branch_name)
{
    if (branch_name.empty())
    {
        LOG(WARNING) << "Branch name is empty";
        return "";
    }

    std::string normalized;
    normalized.reserve(branch_name.size());

    for (char c : branch_name)
    {
        if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-')
        {
            normalized.push_back(c);
        }
        else if (c >= 'A' && c <= 'Z')
        {
            // Convert uppercase to lowercase
            normalized.push_back(c + ('a' - 'A'));
        }
        else
        {
            // Invalid character (including underscore which is reserved as
            // separator)
            LOG(WARNING) << "Invalid character in branch name: '" << branch_name
                         << "' (contains '" << c << "')";
            return "";
        }
    }

    return normalized;
}

// Validates branch name (case-insensitive)
// Returns true if valid, false otherwise
// Valid pattern: [a-zA-Z0-9-]+ (alphanumeric and hyphen, case-insensitive)
inline bool IsValidBranchName(std::string_view branch_name)
{
    if (branch_name.empty())
    {
        return false;
    }

    for (char c : branch_name)
    {
        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
            (c >= '0' && c <= '9') || c == '-')
        {
            continue;  // Valid character
        }
        else
        {
            return false;  // Invalid character (dot, space, underscore, etc.)
        }
    }

    return true;  // All characters valid and not empty
}

// ParseDataFileSuffix: parses suffix from data file name
// Input suffix formats:
//   "123_main_5" -> file_id=123, branch_name="main", term=5
//   "123_feature_5" -> file_id=123, branch_name="feature", term=5
// Returns true on success, false on parse error
// Note: branch_name is output as string_view (no allocation)
inline bool ParseDataFileSuffix(std::string_view suffix,
                                FileId &file_id,
                                std::string_view &branch_name,
                                uint64_t &term)
{
    file_id = 0;
    term = 0;

    if (suffix.empty())
    {
        return false;
    }

    // Format: <file_id>_<branch_name>_<term>
    // Since underscore is reserved as separator, branch_name cannot contain
    // underscores Simple left-to-right parsing: find first two separators

    // Find first separator (after file_id)
    size_t first_sep = suffix.find(FileNameSeparator);
    if (first_sep == std::string::npos)
    {
        return false;
    }

    // Find second separator (after branch_name)
    size_t second_sep = suffix.find(FileNameSeparator, first_sep + 1);
    if (second_sep == std::string::npos)
    {
        return false;
    }

    // Extract components
    std::string_view file_id_str = suffix.substr(0, first_sep);
    std::string_view branch_str =
        suffix.substr(first_sep + 1, second_sep - first_sep - 1);
    std::string_view term_str = suffix.substr(second_sep + 1);

    // Validate and parse file_id
    uint64_t parsed_id = 0;
    if (!ParseUint64(file_id_str, parsed_id))
    {
        return false;
    }

    // Validate branch_name - files contain already-normalized names
    if (!IsValidBranchName(branch_str))
    {
        return false;  // Invalid branch name
    }

    // Validate and parse term
    uint64_t parsed_term = 0;
    if (!ParseUint64(term_str, parsed_term))
    {
        return false;
    }

    // Success
    file_id = static_cast<FileId>(parsed_id);
    branch_name = branch_str;
    term = parsed_term;
    return true;
}

// ParseManifestFileSuffix: parses suffix from manifest file name
// Input suffix formats:
//   "main_5" -> branch_name="main", term=5, tag=nullopt
//   "feature_5" -> branch_name="feature", term=5, tag=nullopt
//   "main_5_backup-2026-03-05" -> branch_name="main", term=5,
//   tag="backup-2026-03-05"
// Returns true on success, false on parse error
// Note: branch_name is output as string_view (no allocation)
inline bool ParseManifestFileSuffix(std::string_view suffix,
                                    std::string_view &branch_name,
                                    uint64_t &term,
                                    std::optional<std::string> &tag)
{
    term = 0;
    tag.reset();

    if (suffix.empty())
    {
        return false;
    }

    // Format: <branch_name>_<term> or <branch_name>_<term>_<tag>
    // Since underscore is reserved as separator, branch_name cannot contain
    // underscores. Simple left-to-right parsing.

    // Find first separator (after branch_name)
    size_t first_sep = suffix.find(FileNameSeparator);
    if (first_sep == std::string::npos)
    {
        return false;
    }

    // Extract and validate branch_name
    std::string_view branch_str = suffix.substr(0, first_sep);
    // Validate branch_name - files contain already-normalized names
    if (!IsValidBranchName(branch_str))
    {
        return false;  // Invalid branch name
    }

    // Reject old format: If branch_str is purely numeric, it's old format
    uint64_t dummy = 0;
    if (ParseUint64(branch_str, dummy))
    {
        // Branch name cannot be purely numeric - this is old format
        return false;
    }

    // Find second separator (for tag, if present)
    std::string_view remainder = suffix.substr(first_sep + 1);
    size_t second_sep = remainder.find(FileNameSeparator);

    if (second_sep == std::string::npos)
    {
        // Format: <branch_name>_<term>
        uint64_t parsed_term = 0;
        if (!ParseUint64(remainder, parsed_term))
        {
            return false;
        }
        branch_name = branch_str;
        term = parsed_term;
        return true;
    }

    // Format: <branch_name>_<term>_<tag>
    std::string_view term_str = remainder.substr(0, second_sep);
    std::string_view tag_str = remainder.substr(second_sep + 1);

    uint64_t parsed_term = 0;
    if (!ParseUint64(term_str, parsed_term) || tag_str.empty())
    {
        return false;
    }

    branch_name = branch_str;
    term = parsed_term;
    tag = std::string(tag_str);
    return true;
}

// Helper: extract manifest term directly from full filename.
// - For non-manifest files, returns 0.
// - For manifest filenames, uses ParseFileName + ParseManifestFileSuffix.
// - On parse error, returns 0.
inline uint64_t ManifestTermFromFilename(std::string_view filename)
{
    auto [type, suffix] = ParseFileName(filename);
    if (type != FileNameManifest)
    {
        return 0;
    }

    std::string_view branch_name;
    uint64_t term = 0;
    std::optional<std::string> tag;
    if (!ParseManifestFileSuffix(suffix, branch_name, term, tag))
    {
        return 0;
    }
    return term;
}

inline bool IsArchiveFile(std::string_view filename)
{
    auto [type, suffix] = ParseFileName(filename);
    if (type != FileNameManifest)
    {
        return false;
    }
    std::string_view branch_name;
    uint64_t term = 0;
    std::optional<std::string> tag;
    if (!ParseManifestFileSuffix(suffix, branch_name, term, tag))
    {
        return false;
    }
    return tag.has_value();
}

// ParseCurrentTermFilename: parses CURRENT_TERM_<branch>_<pg_id> filename
// Input formats:
//   "CURRENT_TERM_main_0" -> branch_name="main", pg_id=0
//   "CURRENT_TERM_feature_3" -> branch_name="feature", pg_id=3
// Returns true on success, false on parse error
// Note: branch_name is output as string_view (no allocation)
inline bool ParseCurrentTermFilename(std::string_view filename,
                                     std::string_view &branch_name,
                                     PartitonGroupId &pg_id)
{
    // Check if filename starts with CURRENT_TERM prefix
    constexpr std::string_view prefix = CurrentTermFileName;
    if (filename.size() <= prefix.size() ||
        filename.substr(0, prefix.size()) != prefix)
    {
        return false;
    }

    // Check for underscore separator after CURRENT_TERM
    if (filename[prefix.size()] != FileNameSeparator)
    {
        return false;
    }

    // Remainder is "<branch>_<pg_id>"
    std::string_view remainder = filename.substr(prefix.size() + 1);

    // Find the last underscore to split branch from pg_id
    // Branch names don't contain underscores, so last '_' is the separator
    auto last_sep = remainder.rfind(FileNameSeparator);
    if (last_sep == std::string_view::npos || last_sep == 0 ||
        last_sep == remainder.size() - 1)
    {
        return false;
    }

    std::string_view branch_str = remainder.substr(0, last_sep);
    std::string_view pg_id_str = remainder.substr(last_sep + 1);

    // Validate branch name
    if (!IsValidBranchName(branch_str))
    {
        return false;
    }

    // Parse pg_id
    uint32_t parsed_pg_id = 0;
    for (char c : pg_id_str)
    {
        if (c >= '0' && c <= '9')
        {
            uint32_t digit = static_cast<uint32_t>(c - '0');
            if (parsed_pg_id > (UINT32_MAX - digit) / 10)
            {
                return false;  // Overflow
            }
            parsed_pg_id = parsed_pg_id * 10 + digit;
        }
        else
        {
            return false;  // Invalid character in pg_id
        }
    }

    branch_name = branch_str;
    pg_id = parsed_pg_id;
    return true;
}

// Convenience overload: parse only the branch name (ignore pg_id)
inline bool ParseCurrentTermFilename(std::string_view filename,
                                     std::string_view &branch_name)
{
    PartitonGroupId pg_id = 0;
    return ParseCurrentTermFilename(filename, branch_name, pg_id);
}

// Branch-aware data file naming: data_<file_id>_<branch_name>_<term>
inline std::string BranchDataFileName(FileId file_id,
                                      std::string_view branch_name,
                                      uint64_t term)
{
    std::string normalized_branch = NormalizeBranchName(branch_name);
    if (normalized_branch.empty())
    {
        return "";  // Invalid branch name
    }

    std::string name;
    name.reserve(std::size(FileNameData) + normalized_branch.size() + 32);
    name.append(FileNameData);
    name.push_back(FileNameSeparator);
    name.append(std::to_string(file_id));
    name.push_back(FileNameSeparator);
    name.append(normalized_branch);
    name.push_back(FileNameSeparator);
    name.append(std::to_string(term));
    return name;
}

// Branch-aware manifest file naming: manifest_<branch_name>_<term>
inline std::string BranchManifestFileName(std::string_view branch_name,
                                          uint64_t term)
{
    std::string normalized_branch = NormalizeBranchName(branch_name);
    if (normalized_branch.empty())
    {
        return "";  // Invalid branch name
    }

    std::string name;
    name.reserve(std::size(FileNameManifest) + normalized_branch.size() + 16);
    name.append(FileNameManifest);
    name.push_back(FileNameSeparator);
    name.append(normalized_branch);
    name.push_back(FileNameSeparator);
    name.append(std::to_string(term));
    return name;
}

// Branch-aware archive naming: manifest_<branch_name>_<term>_<tag>
inline std::string BranchArchiveName(std::string_view branch_name,
                                     uint64_t term,
                                     std::string_view tag)
{
    std::string normalized_branch = NormalizeBranchName(branch_name);
    if (normalized_branch.empty())
    {
        return "";  // Invalid branch name
    }

    std::string name;
    name.reserve(std::size(FileNameManifest) + normalized_branch.size() + 32);
    name.append(FileNameManifest);
    name.push_back(FileNameSeparator);
    name.append(normalized_branch);
    name.push_back(FileNameSeparator);
    name.append(std::to_string(term));
    name.push_back(FileNameSeparator);
    name.append(tag);
    return name;
}

// Store-level, branch-aware, partition-group-aware CURRENT_TERM file naming:
// CURRENT_TERM_<branch_name>_<pg_id>
inline std::string CurrentTermFileNameForBranchAndPartitionGroup(
    std::string_view branch_name, PartitonGroupId partition_group_id)
{
    std::string normalized_branch = NormalizeBranchName(branch_name);
    if (normalized_branch.empty())
    {
        return "";  // Invalid branch name
    }

    std::string name;
    name.reserve(std::size(CurrentTermFileName) + normalized_branch.size() +
                 16);
    name.append(CurrentTermFileName);
    name.push_back(FileNameSeparator);
    name.append(normalized_branch);
    name.push_back(FileNameSeparator);
    name.append(std::to_string(partition_group_id));
    return name;
}

// Parse branch term from CURRENT_TERM.<branch_name> file content.
// Returns true on success (parsed value written to `term`), false on
// empty/invalid/overflow input (`term` is left unchanged).
inline bool ParseBranchTerm(std::string_view content, uint64_t &term)
{
    if (content.empty())
    {
        return false;
    }
    uint64_t result = 0;
    // Content should be numeric string (e.g., "0", "5", "10")
    for (char c : content)
    {
        if (c >= '0' && c <= '9')
        {
            uint64_t digit = static_cast<uint64_t>(c - '0');
            if (result > (UINT64_MAX - digit) / 10)
            {
                return false;  // Overflow
            }
            result = result * 10 + digit;
        }
        else
        {
            return false;  // Invalid content
        }
    }
    term = result;
    return true;
}

// Convert term to string for CURRENT_TERM file content
inline std::string TermToString(uint64_t term)
{
    return std::to_string(term);
}

// Check if filename is a branch manifest (not an archive)
inline bool IsBranchManifest(std::string_view filename)
{
    auto [type, suffix] = ParseFileName(filename);
    if (type != FileNameManifest)
    {
        return false;
    }
    std::string_view branch_name;
    uint64_t term = 0;
    std::optional<std::string> ts;
    if (!ParseManifestFileSuffix(suffix, branch_name, term, ts))
    {
        return false;
    }
    return !ts.has_value();
}

// Check if filename is a branch archive
inline bool IsBranchArchive(std::string_view filename)
{
    auto [type, suffix] = ParseFileName(filename);
    if (type != FileNameManifest)
    {
        return false;
    }
    std::string_view branch_name;
    uint64_t term = 0;
    std::optional<std::string> ts;
    if (!ParseManifestFileSuffix(suffix, branch_name, term, ts))
    {
        return false;
    }
    return ts.has_value();
}

// Check if filename is a branch data file
inline bool IsBranchDataFile(std::string_view filename)
{
    auto [type, suffix] = ParseFileName(filename);
    if (type != FileNameData)
    {
        return false;
    }
    FileId file_id = 0;
    std::string_view branch_name;
    uint64_t term = 0;
    return ParseDataFileSuffix(suffix, file_id, branch_name, term);
}

// Find branch range for a given file_id using binary search
// Returns iterator to the branch range, or end() if not found
// Uses std::lower_bound to find first range where max_file_id >= file_id
inline BranchFileMapping::const_iterator FindBranchRange(
    const BranchFileMapping &mapping, FileId file_id)
{
    BranchFileRange target;
    target.max_file_id = file_id;
    return std::lower_bound(mapping.begin(), mapping.end(), target);
}

// Check if file_id belongs to a specific branch
// Returns true if file_id is within the branch's range
inline bool FileIdInBranch(const BranchFileMapping &mapping,
                           FileId file_id,
                           std::string_view branch_name)
{
    auto it = FindBranchRange(mapping, file_id);
    if (it == mapping.end())
    {
        return false;
    }
    return it->branch_name == branch_name;
}

// Get branch_name and term for a given file_id in one lookup
// Returns true if file_id found in any branch range
// Uses single binary search for efficiency
inline bool GetBranchNameAndTerm(const BranchFileMapping &mapping,
                                 FileId file_id,
                                 std::string &branch_name,
                                 uint64_t &term)
{
    auto it = FindBranchRange(mapping, file_id);
    if (it == mapping.end())
    {
        return false;
    }
    branch_name = it->branch_name;
    term = it->term;
    return true;
}

// Serialize BranchFileMapping to string
// Format:
// [num_entries][branch_name_len][branch_name][term(8B)][max_file_id(8B)]...
inline std::string SerializeBranchFileMapping(const BranchFileMapping &mapping)
{
    std::string result;

    // Number of entries (fixed 8 bytes)
    uint64_t num_entries = static_cast<uint64_t>(mapping.size());
    result.append(reinterpret_cast<const char *>(&num_entries),
                  sizeof(uint64_t));

    for (const auto &range : mapping)
    {
        // Branch name length (4 bytes)
        uint32_t name_len = static_cast<uint32_t>(range.branch_name.size());
        result.append(reinterpret_cast<const char *>(&name_len),
                      sizeof(uint32_t));

        // Branch name
        result.append(range.branch_name);

        // Term (8 bytes)
        uint64_t term = range.term;
        result.append(reinterpret_cast<const char *>(&term), sizeof(uint64_t));

        // Max file_id (8 bytes)
        uint64_t max_file_id = range.max_file_id;
        result.append(reinterpret_cast<const char *>(&max_file_id),
                      sizeof(uint64_t));
    }

    return result;
}

// Deserialize BranchFileMapping from string_view
// Returns empty mapping on error
inline BranchFileMapping DeserializeBranchFileMapping(std::string_view data)
{
    BranchFileMapping mapping;

    if (data.size() < sizeof(uint64_t))
    {
        return mapping;
    }

    uint64_t num_entries = 0;
    std::memcpy(&num_entries, data.data(), sizeof(uint64_t));
    data = data.substr(sizeof(uint64_t));

    for (uint64_t i = 0; i < num_entries; ++i)
    {
        if (data.size() < sizeof(uint32_t))
        {
            return BranchFileMapping{};  // Error: invalid data
        }

        uint32_t name_len = 0;
        std::memcpy(&name_len, data.data(), sizeof(uint32_t));
        data = data.substr(sizeof(uint32_t));

        if (data.size() < name_len + sizeof(uint64_t) * 2)
        {
            return BranchFileMapping{};  // Error: invalid data
        }

        BranchFileRange range;
        range.branch_name = std::string(data.substr(0, name_len));
        data = data.substr(name_len);

        std::memcpy(&range.term, data.data(), sizeof(uint64_t));
        data = data.substr(sizeof(uint64_t));

        std::memcpy(&range.max_file_id, data.data(), sizeof(uint64_t));
        data = data.substr(sizeof(uint64_t));

        mapping.push_back(std::move(range));
    }

    return mapping;
}

// Serialize BranchManifestMetadata to string
// Format: [branch_name_len(4B)][branch_name][term(8B)][BranchFileMapping]
inline std::string SerializeBranchManifestMetadata(
    const BranchManifestMetadata &metadata)
{
    std::string result;

    // Branch name length (4 bytes)
    uint32_t name_len = static_cast<uint32_t>(metadata.branch_name.size());
    result.append(reinterpret_cast<const char *>(&name_len), sizeof(uint32_t));

    // Branch name
    result.append(metadata.branch_name);

    // Term (8 bytes)
    uint64_t term = metadata.term;
    result.append(reinterpret_cast<const char *>(&term), sizeof(uint64_t));

    // BranchFileMapping
    std::string mapping_str = SerializeBranchFileMapping(metadata.file_ranges);
    result.append(mapping_str);

    return result;
}

// Deserialize BranchManifestMetadata from string_view
// Returns true on success, false on error (metadata is left default-initialized
// on failure).
inline bool DeserializeBranchManifestMetadata(std::string_view data,
                                              BranchManifestMetadata &metadata)
{
    metadata = {};

    if (data.size() < sizeof(uint32_t))
    {
        return false;
    }

    // Branch name length
    uint32_t name_len = 0;
    std::memcpy(&name_len, data.data(), sizeof(uint32_t));
    data = data.substr(sizeof(uint32_t));

    if (data.size() < name_len + sizeof(uint64_t))
    {
        return false;
    }

    // Branch name
    metadata.branch_name = std::string(data.substr(0, name_len));
    data = data.substr(name_len);

    // Term
    std::memcpy(&metadata.term, data.data(), sizeof(uint64_t));
    data = data.substr(sizeof(uint64_t));

    // BranchFileMapping
    metadata.file_ranges = DeserializeBranchFileMapping(data);

    return true;
}

}  // namespace eloqstore