Make append buffer contiguous and update iterators
This commit is contained in:
@@ -5,19 +5,19 @@
|
||||
#include "pch.h"
|
||||
|
||||
struct AppendBuffer : Buffer {
|
||||
using TChunk = char[APPEND_CHUNK_SIZE];
|
||||
std::vector<TChunk *> buf;
|
||||
TChunk *t_current;
|
||||
uint64_t current_offset{0};
|
||||
uint64_t t_offset{0};
|
||||
|
||||
AppendBuffer();
|
||||
char *buf = nullptr;
|
||||
uint64_t allocated_capacity = 0;
|
||||
uint64_t current_size = 0;
|
||||
int fd = -1;
|
||||
|
||||
AppendBuffer(std::filesystem::path base_dir);
|
||||
~AppendBuffer();
|
||||
|
||||
uint64_t append(char c);
|
||||
uint64_t append(const char *text, uint64_t length);
|
||||
uint64_t append(const char c);
|
||||
uint64_t append(const char *text, uint64_t len);
|
||||
const char *read(uint64_t pos) override;
|
||||
uint64_t length() override;
|
||||
|
||||
const char *read(uint64_t pos, uint64_t *out_len);
|
||||
inline uint64_t length();
|
||||
private:
|
||||
void grow(uint64_t len);
|
||||
};
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include "pch.h"
|
||||
|
||||
struct Buffer {
|
||||
virtual const char *read(uint64_t pos, uint64_t *out_len) = 0;
|
||||
virtual inline uint64_t length() = 0;
|
||||
virtual const char *read(uint64_t pos) = 0;
|
||||
virtual uint64_t length() = 0;
|
||||
virtual ~Buffer() = default;
|
||||
};
|
||||
|
||||
@@ -12,6 +12,6 @@ struct OriginalBuffer : Buffer {
|
||||
~OriginalBuffer();
|
||||
|
||||
void initialize();
|
||||
const char *read(uint64_t pos, uint64_t *out_len);
|
||||
uint64_t length();
|
||||
const char *read(uint64_t pos) override;
|
||||
uint64_t length() override;
|
||||
};
|
||||
|
||||
@@ -2,10 +2,4 @@
|
||||
|
||||
#include "pch.h"
|
||||
|
||||
constexpr uint64_t APPEND_CHUNK_SIZE = 64 * 1024;
|
||||
constexpr uint64_t PETAL_SIZE_MAX = 32 * 1024;
|
||||
|
||||
static_assert(
|
||||
APPEND_CHUNK_SIZE > PETAL_SIZE_MAX,
|
||||
"PETAL_SIZE_MAX must be smaller than APPEND_CHUNK_SIZE"
|
||||
);
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "../shard.h"
|
||||
#include "pch.h"
|
||||
|
||||
enum struct Direction : uint8_t {
|
||||
Forward,
|
||||
Backward
|
||||
};
|
||||
@@ -1,22 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "../shard.h"
|
||||
#include "chunk.h"
|
||||
#include "pch.h"
|
||||
#include "petal.h"
|
||||
|
||||
struct LineIterator {
|
||||
ChunkIterator it;
|
||||
Direction dir;
|
||||
|
||||
PetalIterator it;
|
||||
const char *chunk;
|
||||
uint64_t len;
|
||||
uint64_t offset = 0;
|
||||
uint64_t chunk_offset = 0;
|
||||
bool at_end = false;
|
||||
Direction dir;
|
||||
uint64_t current_offset = 0;
|
||||
uint64_t last_line_offset = 0;
|
||||
|
||||
LineIterator(Shard *r, uint64_t start_line, Direction dir);
|
||||
LineIterator(Shard *root, uint64_t line_num, Direction dir);
|
||||
~LineIterator() = default;
|
||||
|
||||
bool next(std::string &line);
|
||||
bool next(std::string *line);
|
||||
uint64_t byte_offset();
|
||||
};
|
||||
|
||||
@@ -1,30 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include "../shard.h"
|
||||
#include "iter.h"
|
||||
#include "pch.h"
|
||||
|
||||
enum struct Direction : uint8_t {
|
||||
Forward,
|
||||
Backward
|
||||
};
|
||||
|
||||
struct ChunkIterator {
|
||||
struct PetalIterator {
|
||||
Direction dir;
|
||||
Shard *root = nullptr;
|
||||
std::vector<Shard *> stack;
|
||||
Petal *petal = nullptr;
|
||||
uint64_t petal_offset = 0;
|
||||
uint64_t global_offset = 0;
|
||||
uint64_t last_offset = 0;
|
||||
uint64_t petal_offset = 0;
|
||||
uint64_t global_line = 0;
|
||||
bool at_end = false;
|
||||
|
||||
ChunkIterator(Shard *r, Direction dir);
|
||||
|
||||
~ChunkIterator();
|
||||
PetalIterator(Shard *r, Direction dir);
|
||||
~PetalIterator() = default;
|
||||
|
||||
void seek_offset(uint64_t offset);
|
||||
void seek_line(uint64_t offset);
|
||||
bool next(const char **data, uint64_t *out_len);
|
||||
uint64_t byte_offset();
|
||||
uint64_t line_offset();
|
||||
|
||||
private:
|
||||
Petal *_next(uint64_t *offset);
|
||||
};
|
||||
+57
-8
@@ -7,7 +7,7 @@
|
||||
#include "utils/utils.h"
|
||||
|
||||
struct Shard {
|
||||
enum struct ShardKind : uint8_t {
|
||||
enum struct Kind : uint8_t {
|
||||
Branch,
|
||||
Petal
|
||||
} kind;
|
||||
@@ -19,7 +19,7 @@ struct Shard {
|
||||
|
||||
std::atomic_uint64_t refs;
|
||||
|
||||
Shard(ShardKind kind, uint64_t length, uint64_t lines, uint8_t height)
|
||||
Shard(Kind kind, uint64_t length, uint64_t lines, uint8_t height)
|
||||
: kind(kind), height(height), length(length), lines(lines), refs(1) {};
|
||||
|
||||
static void retain(Shard *n);
|
||||
@@ -27,14 +27,13 @@ struct Shard {
|
||||
|
||||
static Shard *from_file(std::filesystem::path path, OriginalBuffer *b);
|
||||
static std::vector<Shard *> from_swap(std::filesystem::path path, OriginalBuffer *b);
|
||||
static Shard *new_empty(OriginalBuffer *b);
|
||||
|
||||
static std::pair<Shard *, Shard *> split(Shard *n, uint64_t offset);
|
||||
static Shard *concat(Shard *left, Shard *right);
|
||||
static Shard *merge(Shard *a, Shard *b);
|
||||
static Shard *merge_leaves(Shard *a, Shard *b);
|
||||
static Shard *append_leaf(Shard *root, Shard *leaf);
|
||||
static Shard *build_balanced(Shard **pieces, uint64_t lo, uint64_t hi);
|
||||
static Shard *append(Shard *root, Shard *leaf);
|
||||
static Shard *build(Shard **pieces, uint64_t lo, uint64_t hi);
|
||||
};
|
||||
|
||||
struct Branch : Shard {
|
||||
@@ -43,7 +42,7 @@ struct Branch : Shard {
|
||||
|
||||
Branch(Shard *l, Shard *r)
|
||||
: Shard(
|
||||
ShardKind::Branch,
|
||||
Kind::Branch,
|
||||
l->length + r->length,
|
||||
l->lines + r->lines,
|
||||
1 + std::max(l->height, r->height)
|
||||
@@ -56,10 +55,60 @@ struct Branch : Shard {
|
||||
|
||||
struct Petal : Shard {
|
||||
Buffer *source;
|
||||
|
||||
uint64_t pos;
|
||||
|
||||
Petal(uint64_t length, uint64_t lines, Buffer *source, uint64_t pos)
|
||||
: Shard(ShardKind::Petal, length, lines, 1),
|
||||
: Shard(Kind::Petal, length, lines, 1),
|
||||
source(source), pos(pos) {};
|
||||
};
|
||||
|
||||
extern inline void dump_shard(Shard *node, int depth = 0) {
|
||||
if (!node) {
|
||||
std::cout << std::string(depth * 2, ' ') << "<null>\n";
|
||||
return;
|
||||
}
|
||||
|
||||
std::string indent(depth * 2, ' ');
|
||||
|
||||
std::cout << indent
|
||||
<< "Shard@" << node
|
||||
<< " kind=";
|
||||
|
||||
switch (node->kind) {
|
||||
case Shard::Kind::Branch:
|
||||
std::cout << "Branch";
|
||||
break;
|
||||
case Shard::Kind::Petal:
|
||||
std::cout << "Petal";
|
||||
break;
|
||||
}
|
||||
|
||||
std::cout
|
||||
<< " height=" << unsigned(node->height)
|
||||
<< " length=" << node->length
|
||||
<< " lines=" << node->lines
|
||||
<< " refs=" << node->refs.load()
|
||||
<< "\n";
|
||||
|
||||
if (node->kind == Shard::Kind::Branch) {
|
||||
auto *branch = static_cast<Branch *>(node);
|
||||
|
||||
std::cout << indent << " left:\n";
|
||||
dump_shard(branch->left, depth + 2);
|
||||
|
||||
std::cout << indent << " right:\n";
|
||||
dump_shard(branch->right, depth + 2);
|
||||
} else {
|
||||
auto *petal = static_cast<Petal *>(node);
|
||||
|
||||
std::cout
|
||||
<< indent << " source=" << petal->source
|
||||
<< " pos=" << petal->pos
|
||||
<< " length=" << petal->length
|
||||
<< " lines=" << petal->lines
|
||||
<< "\n";
|
||||
}
|
||||
|
||||
if (!depth)
|
||||
std::cout << "\n\n";
|
||||
}
|
||||
|
||||
@@ -48,7 +48,6 @@ struct Vase {
|
||||
std::filesystem::path swapdir;
|
||||
|
||||
Vase(std::filesystem::path path, std::filesystem::path swapdir);
|
||||
|
||||
~Vase();
|
||||
|
||||
uint64_t length();
|
||||
@@ -80,7 +79,6 @@ struct Vase {
|
||||
|
||||
bool undo();
|
||||
bool redo();
|
||||
|
||||
void snapshot();
|
||||
void prune_history(uint64_t n);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user