-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_index0.c
378 lines (353 loc) · 12.9 KB
/
linear_index0.c
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
#include "linear_index0.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
typedef struct {
uint32_t Offset;
uint32_t Index;
uint32_t Hash;
uint32_t Value;
} linear_node0_t;
typedef struct {
uint32_t Signature, Version;
uint32_t NumOffsets, NumEntries;
uint32_t NumNodes, NextFree;
uint32_t Count, Extra;
linear_node0_t Nodes[];
} linear_header0_t;
struct linear_index0_t {
#ifdef RADB_MEM_PER_STORE
void *Allocator;
void *(*alloc)(void *, size_t);
void *(*alloc_atomic)(void *, size_t);
void (*free)(void *, void *);
#endif
const char *Prefix;
linear_header0_t *Header;
void *Keys;
linear_compare_t Compare;
linear_insert_t Insert;
size_t HeaderSize;
int HeaderFd;
};
#ifdef RADB_MEM_GC
#include <gc/gc.h>
#endif
#ifdef RADB_MEM_PER_STORE
static inline const char *radb_strdup(const char *String, void *Allocator, void *(*alloc_atomic)(void *, size_t)) {
size_t Length = strlen(String);
char *Copy = alloc_atomic(Allocator, Length + 1);
strcpy(Copy, String);
return Copy;
}
#endif
#define MAKE_VERSION(MAJOR, MINOR) (0xFF000000 + (MAJOR << 16) + (MINOR << 8))
#define LINEAR_INDEX_SIGNATURE 0x494C4152
#define LINEAR_INDEX_VERSION MAKE_VERSION(1, 0)
#define PAGE_SIZE 4096
linear_index0_t *linear_index0_create(const char *Prefix, void *Keys RADB_MEM_PARAMS) {
#if defined(RADB_MEM_MALLOC)
linear_index0_t *Store = malloc(sizeof(linear_index0_t));
Store->Prefix = strdup(Prefix);
#elif defined(RADB_MEM_GC)
linear_index0_t *Store = GC_malloc(sizeof(linear_index0_t));
Store->Prefix = GC_strdup(Prefix);
#else
linear_index0_t *Store = alloc(Allocator, sizeof(linear_index0_t));
Store->Prefix = radb_strdup(Prefix, Allocator, alloc_atomic);
Store->Allocator = Allocator;
Store->alloc = alloc;
Store->alloc_atomic = alloc_atomic;
Store->free = free;
#endif
char FileName[strlen(Prefix) + 10];
sprintf(FileName, "%s.index2", Prefix);
Store->HeaderFd = open(FileName, O_RDWR | O_CREAT | O_TRUNC, 0777);
Store->HeaderSize = PAGE_SIZE;
ftruncate(Store->HeaderFd, Store->HeaderSize);
Store->Header = mmap(NULL, Store->HeaderSize, PROT_READ | PROT_WRITE, MAP_SHARED, Store->HeaderFd, 0);
Store->Header->Signature = LINEAR_INDEX_SIGNATURE;
Store->Header->Version = LINEAR_INDEX_VERSION;
Store->Header->NumNodes = (PAGE_SIZE - sizeof(linear_header0_t)) / sizeof(linear_node0_t);
Store->Header->NumOffsets = 1;
Store->Header->NumEntries = 0;
Store->Header->NextFree = INVALID_INDEX;
Store->Header->Count = 0;
Store->Header->Nodes[0].Index = INVALID_INDEX;
Store->Keys = Keys;
return Store;
}
linear_index0_open_t linear_index0_open2(const char *Prefix, void *Keys RADB_MEM_PARAMS) {
struct stat Stat[1];
char FileName[strlen(Prefix) + 10];
sprintf(FileName, "%s.index2", Prefix);
if (stat(FileName, Stat)) return (linear_index0_open_t){NULL, RADB_FILE_NOT_FOUND};
#if defined(RADB_MEM_MALLOC)
linear_index0_t *Store = malloc(sizeof(linear_index0_t));
Store->Prefix = strdup(Prefix);
#elif defined(RADB_MEM_GC)
linear_index0_t *Store = GC_malloc(sizeof(linear_index0_t));
Store->Prefix = GC_strdup(Prefix);
#else
linear_index0_t *Store = alloc(Allocator, sizeof(linear_index0_t));
Store->Prefix = radb_strdup(Prefix, Allocator, alloc_atomic);
Store->Allocator = Allocator;
Store->alloc = alloc;
Store->alloc_atomic = alloc_atomic;
Store->free = free;
#endif
Store->HeaderFd = open(FileName, O_RDWR, 0777);
Store->HeaderSize = Stat->st_size;
Store->Header = mmap(NULL, Store->HeaderSize, PROT_READ | PROT_WRITE, MAP_SHARED, Store->HeaderFd, 0);
if (Store->Header->Signature != LINEAR_INDEX_SIGNATURE) {
munmap(Store->Header, Store->HeaderSize);
close(Store->HeaderFd);
return (linear_index0_open_t){NULL, RADB_HEADER_MISMATCH};
}
Store->Keys = Keys;
return (linear_index0_open_t){Store, RADB_SUCCESS};
}
linear_index0_t *linear_index0_open(const char *Prefix, void *Keys RADB_MEM_PARAMS) {
return linear_index0_open2(Prefix, Keys RADB_MEM_ARGS).Index;
}
void linear_index0_close(linear_index0_t *Store) {
msync(Store->Header, Store->HeaderSize, MS_SYNC);
munmap(Store->Header, Store->HeaderSize);
close(Store->HeaderFd);
#if defined(RADB_MEM_MALLOC)
free((void *)Store->Prefix);
free(Store);
#elif defined(RADB_MEM_GC)
#else
Store->free(Store->Allocator, (void *)Store->Prefix);
Store->free(Store->Allocator, Store);
#endif
}
void linear_index0_set_compare(linear_index0_t *Store, linear_compare_t Compare) {
Store->Compare = Compare;
}
void linear_index0_set_insert(linear_index0_t *Store, linear_insert_t Insert) {
Store->Insert = Insert;
}
void linear_index0_set_extra(linear_index0_t *Store, uint32_t Value) {
Store->Header->Extra = Value;
}
uint32_t linear_index0_get_extra(linear_index0_t *Store) {
return Store->Header->Extra;
}
void *linear_index0_keys(linear_index0_t *Store) {
return Store->Keys;
}
size_t linear_index0_count(linear_index0_t *Store) {
return Store->Header->Count;
}
size_t linear_index0_search(linear_index0_t *Store, uint32_t Hash, const void *Full) {
size_t NumOffset = Store->Header->NumOffsets;
size_t Scale = NumOffset > 1 ? 1 << (64 - __builtin_clzl(NumOffset - 1)) : 1;
size_t Index = Hash & (Scale - 1);
if (Index >= NumOffset) Index -= (Scale >> 1);
linear_node0_t *Nodes = Store->Header->Nodes;
size_t Offset = Nodes[Index].Offset;
if (Offset == INVALID_INDEX) return INVALID_INDEX;
linear_node0_t *Last = Nodes + Store->Header->NumEntries;
for (linear_node0_t *Entry = Nodes + Offset; Entry < Last; ++Entry) {
if (Entry->Index == INVALID_INDEX) {
return INVALID_INDEX;
} else if (Entry->Index != Index) {
return INVALID_INDEX;
} else if (Entry->Hash == Hash && !Store->Compare(Store->Keys, Full, Entry->Value)) {
return Entry->Value;
}
}
return INVALID_INDEX;
}
static linear_node0_t *linear_index0_grow_nodes(linear_index0_t *Store, size_t Target) {
if (Target > Store->Header->NumNodes) {
size_t Required = Target - Store->Header->NumNodes;
size_t Allocation = ((Required * sizeof(linear_node0_t) + PAGE_SIZE - 1) / PAGE_SIZE) * PAGE_SIZE;
size_t HeaderSize = Store->HeaderSize + Allocation;
ftruncate(Store->HeaderFd, HeaderSize);
#ifdef Linux
Store->Header = mremap(Store->Header, Store->HeaderSize, HeaderSize, MREMAP_MAYMOVE);
#else
munmap(Store->Header, Store->HeaderSize);
Store->Header = mmap(NULL, HeaderSize, PROT_READ | PROT_WRITE, MAP_SHARED, Store->HeaderFd, 0);
#endif
Store->Header->NumNodes = (HeaderSize - sizeof(linear_header0_t)) / sizeof(linear_node0_t);
Store->HeaderSize = HeaderSize;
}
return Store->Header->Nodes;
}
static void linear_index0_add_offset(linear_index0_t *Store) {
size_t NumOffsets = Store->Header->NumOffsets;
if (NumOffsets >= Store->Header->Count) return;
size_t Scale = 1 << (64 - __builtin_clzl(NumOffsets));
size_t Shift = Scale >> 1;
size_t Index = Scale > NumOffsets ? NumOffsets - Shift : NumOffsets & (Scale - 1);
linear_node0_t *Nodes = linear_index0_grow_nodes(Store, NumOffsets + 1);
size_t Offset = Nodes[Index].Offset;
if (Offset == INVALID_INDEX) {
Nodes[Store->Header->NumOffsets++].Offset = INVALID_INDEX;
return;
}
linear_node0_t *First = Nodes + Offset, *Last = First, *A = First;
linear_node0_t *Limit = Nodes + Store->Header->NumEntries;
while (Last < Limit) {
if (Last->Index != Index) break;
++Last;
}
linear_node0_t *B = Last;
Store->Header->NumOffsets = ++NumOffsets;
while (A < B) {
size_t NewIndex = A->Hash & (Scale - 1);
if (NewIndex >= NumOffsets) NewIndex -= Shift;
if (NewIndex == Index) {
++A;
} else {
--B;
uint32_t TempHash = A->Hash;
A->Hash = B->Hash;
B->Hash = TempHash;
uint32_t TempValue = A->Value;
A->Value = B->Value;
B->Value = TempValue;
B->Index = NewIndex;
}
}
if (B == Last) {
Nodes[NumOffsets - 1].Offset = INVALID_INDEX;
} else {
if (B == First) Nodes[Index].Offset = INVALID_INDEX;
Nodes[NumOffsets - 1].Offset = B - Nodes;
}
}
static index_result_t linear_index0_add_entry(linear_index0_t *Store, uint32_t Index, uint32_t Hash, const void *Full) {
linear_node0_t *Nodes = linear_index0_grow_nodes(Store, Store->Header->NumEntries + 1);
size_t Offset = Store->Header->NumEntries++;
linear_node0_t *Entry = Nodes + Offset;
Entry->Index = Index;
Entry->Hash = Hash;
uint32_t Insert = Entry->Value = Store->Insert(Store->Keys, Full);
linear_index0_add_offset(Store);
return (index_result_t){Insert, 1};
}
index_result_t linear_index0_insert2(linear_index0_t *Store, uint32_t Hash, const void *Full) {
size_t NumOffsets = Store->Header->NumOffsets;
size_t Scale = NumOffsets > 1 ? 1 << (64 - __builtin_clzl(NumOffsets - 1)) : 1;
size_t Index = Hash & (Scale - 1);
if (Index >= NumOffsets) Index -= (Scale >> 1);
linear_node0_t *Nodes = Store->Header->Nodes;
size_t Offset = Nodes[Index].Offset;
if (Offset == INVALID_INDEX) {
++Store->Header->Count;
size_t Free = Store->Header->NextFree;
if (Free != INVALID_INDEX && Nodes[Free].Index == INVALID_INDEX) {
Store->Header->NextFree = Nodes[Free].Value;
Nodes[Index].Offset = Free;
Nodes[Free].Index = Index;
Nodes[Free].Hash = Hash;
uint32_t Insert = Nodes[Free].Value = Store->Insert(Store->Keys, Full);
linear_index0_add_offset(Store);
return (index_result_t){Insert, 1};
} else {
Nodes[Index].Offset = Store->Header->NumEntries;
return linear_index0_add_entry(Store, Index, Hash, Full);
}
}
linear_node0_t *Last = Nodes + Store->Header->NumEntries;
for (linear_node0_t *Entry = Nodes + Offset; Entry < Last; ++Entry) {
if (Entry->Index == INVALID_INDEX) {
++Store->Header->Count;
Entry->Index = Index;
Entry->Hash = Hash;
uint32_t Insert = Entry->Value = Store->Insert(Store->Keys, Full);
linear_index0_add_offset(Store);
return (index_result_t){Insert, 1};
} else if (Entry->Index != Index) {
++Store->Header->Count;
if (Offset > 0 && Nodes[Offset - 1].Index == INVALID_INDEX) {
Nodes[Index].Offset = Offset - 1;
linear_node0_t *Entry2 = Nodes + (Offset - 1);
Entry2->Index = Index;
Entry2->Hash = Hash;
uint32_t Insert = Entry2->Value = Store->Insert(Store->Keys, Full);
linear_index0_add_offset(Store);
return (index_result_t){Insert, 1};
} else {
size_t Count = (Entry - Nodes) - Offset;
Nodes = linear_index0_grow_nodes(Store, Store->Header->NumEntries + Count + 1);
Nodes[Index].Offset = Store->Header->NumEntries;
linear_node0_t *Source = Nodes + Offset;
linear_node0_t *Target = Nodes + Store->Header->NumEntries;
Store->Header->NumEntries += (Count + 1);
for (int I = 0; I < Count; ++I, ++Source, ++Target) {
Target->Index = Index;
Target->Hash = Source->Hash;
Target->Value = Source->Value;
Source->Index = INVALID_INDEX;
}
Nodes[Offset].Value = Store->Header->NextFree;
Store->Header->NextFree = Offset;
Target->Index = Index;
Target->Hash = Hash;
uint32_t Insert = Target->Value = Store->Insert(Store->Keys, Full);
linear_index0_add_offset(Store);
return (index_result_t){Insert, 1};
}
} else if (Entry->Hash == Hash && !Store->Compare(Store->Keys, Full, Entry->Value)) {
return (index_result_t){Entry->Value, 0};
}
}
++Store->Header->Count;
return linear_index0_add_entry(Store, Index, Hash, Full);
}
size_t linear_index0_insert(linear_index0_t *Store, uint32_t Hash, const void *Full) {
return linear_index0_insert2(Store, Hash, Full).Index;
}
index_result_t linear_index0_delete2(linear_index0_t *Store, uint32_t Hash, const void *Full) {
size_t NumOffsets = Store->Header->NumOffsets;
size_t Scale = NumOffsets > 1 ? 1 << (64 - __builtin_clzl(NumOffsets - 1)) : 1;
size_t Index = Hash & (Scale - 1);
if (Index >= NumOffsets) Index -= (Scale >> 1);
linear_node0_t *Nodes = Store->Header->Nodes;
size_t Offset = Nodes[Index].Offset;
if (Offset == INVALID_INDEX) return (index_result_t){INVALID_INDEX, 0};
linear_node0_t *Last = Nodes + Store->Header->NumEntries;
for (linear_node0_t *Entry = Nodes + Offset; Entry < Last; ++Entry) {
if (Entry->Index == INVALID_INDEX) {
return (index_result_t){INVALID_INDEX, 0};
} else if (Entry->Index != Index) {
return (index_result_t){INVALID_INDEX, 0};
} else if (Entry->Hash == Hash && !Store->Compare(Store->Keys, Full, Entry->Value)) {
--Store->Header->Count;
uint32_t Value = Entry->Value;
linear_node0_t *Base = Nodes + Offset;
if (Entry > Base) {
Entry->Hash = Base->Hash;
Entry->Value = Base->Value;
Base->Index = INVALID_INDEX;
Nodes[Index].Offset = Offset + 1;
} else if ((Entry + 1) == Last) {
Entry->Index = INVALID_INDEX;
Nodes[Index].Offset = INVALID_INDEX;
--Store->Header->NumEntries;
} else if (Entry[1].Index != Index) {
Entry->Index = INVALID_INDEX;
Nodes[Index].Offset = INVALID_INDEX;
} else {
Entry->Index = INVALID_INDEX;
Nodes[Index].Offset = Offset + 1;
}
return (index_result_t){Value, 1};
}
}
return (index_result_t){INVALID_INDEX, 0};
}
size_t linear_index0_delete(linear_index0_t *Store, uint32_t Hash, const void *Full) {
return linear_index0_delete2(Store, Hash, Full).Index;
}