-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathblock.v
More file actions
127 lines (115 loc) · 2.71 KB
/
Copy pathblock.v
File metadata and controls
127 lines (115 loc) · 2.71 KB
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
module leveldb
struct BlockBuilder {
restart_interval int
mut:
buf []u8
restarts []u32
counter int
last_key []u8
entries int
}
fn new_block_builder(restart_interval int) &BlockBuilder {
return &BlockBuilder{
restart_interval: restart_interval
restarts: [u32(0)]
}
}
fn (mut b BlockBuilder) add(key []u8, value []u8) {
mut shared_len := 0
if b.counter < b.restart_interval {
n := if b.last_key.len < key.len { b.last_key.len } else { key.len }
for shared_len < n && b.last_key[shared_len] == key[shared_len] {
shared_len++
}
} else {
b.restarts << u32(b.buf.len)
b.counter = 0
}
append_uvarint(mut b.buf, u64(shared_len))
append_uvarint(mut b.buf, u64(key.len - shared_len))
append_uvarint(mut b.buf, u64(value.len))
b.buf << key[shared_len..]
b.buf << value
b.last_key = key.clone()
b.counter++
b.entries++
}
fn (b &BlockBuilder) size_estimate() int {
return b.buf.len + b.restarts.len * 4 + 4
}
fn (b &BlockBuilder) empty() bool {
return b.entries == 0
}
fn (mut b BlockBuilder) finish() []u8 {
mut out := b.buf.clone()
for r in b.restarts {
append_u32_le(mut out, r)
}
append_u32_le(mut out, u32(b.restarts.len))
return out
}
fn (mut b BlockBuilder) reset() {
b.buf.clear()
b.restarts = [u32(0)]
b.counter = 0
b.last_key = []u8{}
b.entries = 0
}
struct BlockEntry {
key []u8
value []u8
}
fn decode_block(data []u8) ![]BlockEntry {
if data.len < 4 {
return error('leveldb: block too short')
}
num_restarts := int(read_u32_le(data, data.len - 4))
data_end := data.len - 4 - num_restarts * 4
if data_end < 0 {
return error('leveldb: corrupted block restarts')
}
mut entries := []BlockEntry{}
mut pos := 0
mut last_key := []u8{}
for pos < data_end {
shared_len, n1 := read_uvarint(data, pos)!
pos += n1
non_shared, n2 := read_uvarint(data, pos)!
pos += n2
vlen, n3 := read_uvarint(data, pos)!
pos += n3
if pos + int(non_shared) + int(vlen) > data_end || int(shared_len) > last_key.len {
return error('leveldb: corrupted block entry')
}
mut key := []u8{cap: int(shared_len) + int(non_shared)}
key << last_key[..int(shared_len)]
key << data[pos..pos + int(non_shared)]
pos += int(non_shared)
value := data[pos..pos + int(vlen)].clone()
pos += int(vlen)
entries << BlockEntry{
key: key
value: value
}
last_key = unsafe { key }
}
return entries
}
struct BlockHandle {
offset u64
size u64
}
fn (h BlockHandle) encode() []u8 {
mut out := []u8{cap: 20}
append_uvarint(mut out, h.offset)
append_uvarint(mut out, h.size)
return out
}
fn decode_block_handle(data []u8) !(BlockHandle, int) {
offset, n1 := read_uvarint(data, 0)!
size, n2 := read_uvarint(data[n1..], 0)!
return BlockHandle{
offset: offset
size: size
}, n1 + n2
}