bitcoinleveldb-table 0.1.16-alpha.0

abstractions for working with Tables -- a Table is a sorted map from strings to strings. Tables are immutable and persistent. A Table may be safely accessed from multiple threads without external synchronization.
Documentation
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
crate::ix!();



//-------------------------------------------[.cpp/bitcoin/src/leveldb/table/table_test.cc]

/**
  | Return reverse of "key".
  | 
  | Used to test non-lexicographic comparators.
  |
  */
fn reverse(key_: &Slice) -> String {
    
    todo!();
        /*
            std::string str(key.ToString());
      std::string rev("");
      for (std::string::reverse_iterator rit = str.rbegin(); rit != str.rend();
           ++rit) {
        rev.push_back(*rit);
      }
      return rev;
        */
}

struct ReverseKeyComparator {

}

impl SliceComparator for ReverseKeyComparator {

}

impl Name for ReverseKeyComparator {

    fn name(&self) -> *const u8 {
        
        todo!();
        /*
            return "leveldb.ReverseBytewiseComparator";
        */
    }
}

impl Compare for ReverseKeyComparator {

    fn compare(&self, 
        a: &Slice,
        b: &Slice) -> i32 {
        
        todo!();
        /*
            return BytewiseComparator()->Compare(Reverse(a), Reverse(b));
        */
    }
}

impl FindShortestSeparator for ReverseKeyComparator {

    fn find_shortest_separator(&self, 
        start: *mut String,
        limit: &Slice)  {
        
        todo!();
        /*
            std::string s = Reverse(*start);
        std::string l = Reverse(limit);
        BytewiseComparator()->FindShortestSeparator(&s, l);
        *start = Reverse(s);
        */
    }
}
    
impl FindShortSuccessor for ReverseKeyComparator {

    fn find_short_successor(&self, key_: *mut String)  {
        
        todo!();
        /*
            std::string s = Reverse(*key);
        BytewiseComparator()->FindShortSuccessor(&s);
        *key = Reverse(s);
        */
    }
}

lazy_static!{
    /*
    static ReverseKeyComparator reverse_key_comparator;
    */
}

fn increment(
        cmp: Box<dyn SliceComparator>,
        key_: *mut String)  {
    
    todo!();
        /*
            if (cmp == BytewiseComparator()) {
        key->push_back('\0');
      } else {
        assert(cmp == &reverse_key_comparator);
        std::string rev = Reverse(*key);
        rev.push_back('\0');
        *key = Reverse(rev);
      }
        */
}

///------------------------
struct StringSink {
    contents: String,
}

impl WritableFile for StringSink { }

impl StringSink {

    pub fn contents(&self) -> &String {
        
        todo!();
        /*
            return contents_;
        */
    }
}

impl WritableFileClose for StringSink {

    fn close(&mut self) -> crate::Status {
        
        todo!();
        /*
            return Status::OK();
        */
    }
}
    
impl WritableFileFlush for StringSink {
    fn flush(&mut self) -> crate::Status {
        
        todo!();
        /*
            return Status::OK();
        */
    }
}
    
impl WritableFileSync for StringSink {
    fn sync(&mut self) -> crate::Status {
        
        todo!();
        /*
            return Status::OK();
        */
    }
}
    
impl WritableFileAppend for StringSink {
    fn append(&mut self, data: &Slice) -> crate::Status {
        
        todo!();
        /*
            contents_.append(data.data(), data.size());
        return Status::OK();
        */
    }
}
    
impl GetName for StringSink {
    fn get_name(&self) -> &'static str {
        
        todo!();
        /*
            return "";
        */
    }
}

///---------------------------
struct StringSource {
    contents: String,
}

impl RandomAccessFile for StringSource {

}

impl RandomAccessFileRead for StringSource {

    fn read(&self, 
        offset:  u64,
        n:       usize,
        result:  *mut Slice,
        scratch: *mut u8) -> crate::Status {
        
        todo!();
        /*
            if (offset >= contents_.size()) {
          return Status::InvalidArgument("invalid Read offset");
        }
        if (offset + n > contents_.size()) {
          n = contents_.size() - offset;
        }
        memcpy(scratch, &contents_[offset], n);
        *result = Slice(scratch, n);
        return Status::OK();
        */
    }
}

impl GetName for StringSource {

    fn get_name(&self) -> &'static str {
        
        todo!();
        /*
            return "";
        */
    }
}

impl StringSource {

    pub fn new(contents: &Slice) -> Self {
    
        todo!();
        /*
        : contents(contents.data(), contents.size()),

        
        */
    }
    
    pub fn size(&self) -> u64 {
        
        todo!();
        /*
            return contents_.size();
        */
    }
}
 
///----------------------------
struct BlockConstructor {
    base:       Constructor,
    comparator: Box<dyn SliceComparator>,
    data:       String,
    block:      *mut Block,
}

impl Drop for BlockConstructor {
    fn drop(&mut self) {
        todo!();
        /*
            delete block_;
        */
    }
}

impl BlockConstructor {

    pub fn new(cmp: Box<dyn SliceComparator>) -> Self {
    
        todo!();
        /*
        : constructor(cmp),
        : comparator(cmp),
        : block(nullptr),

        
        */
    }
    
    pub fn finish_impl(&mut self, 
        options: &Options,
        data:    &KVMap) -> crate::Status {
        
        todo!();
        /*
            delete block_;
        block_ = nullptr;
        BlockBuilder builder(&options);

        for (const auto& kvp : data) {
          builder.Add(kvp.first, kvp.second);
        }
        // Open the block
        data_ = builder.Finish().ToString();
        BlockContents contents;
        contents.data = data_;
        contents.cachable = false;
        contents.heap_allocated = false;
        block_ = new Block(contents);
        return Status::OK();
        */
    }
    
    pub fn new_iterator(&self) -> *mut LevelDBIterator {
        
        todo!();
        /*
            return block_->NewIterator(comparator_);
        */
    }
}

///--------------------
struct TableConstructor {
    base:   Constructor,
    source: *mut StringSource,
    table:  *mut Table,
}

impl Drop for TableConstructor {
    fn drop(&mut self) {
        todo!();
        /*
            Reset();
        */
    }
}

impl TableConstructor {

    pub fn new(cmp: Box<dyn SliceComparator>) -> Self {
    
        todo!();
        /*
        : constructor(cmp),
        : source(nullptr),
        : table(nullptr),

        
        */
    }
    
    pub fn finish_impl(&mut self, 
        options: &Options,
        data:    &KVMap) -> crate::Status {
        
        todo!();
        /*
            Reset();
        StringSink sink;
        TableBuilder builder(options, &sink);

        for (const auto& kvp : data) {
          builder.Add(kvp.first, kvp.second);
          ASSERT_TRUE(builder.status().ok());
        }
        Status s = builder.Finish();
        ASSERT_TRUE(s.ok()) << s.ToString();

        ASSERT_EQ(sink.contents().size(), builder.FileSize());

        // Open the table
        source_ = new StringSource(sink.contents());
        Options table_options;
        table_options.comparator = options.comparator;
        return Table::Open(table_options, source_, sink.contents().size(), &table_);
        */
    }
    
    pub fn new_iterator(&self) -> *mut LevelDBIterator {
        
        todo!();
        /*
            return table_->NewIterator(ReadOptions());
        */
    }
    
    pub fn approximate_offset_of(&self, key_: &Slice) -> u64 {
        
        todo!();
        /*
            return table_->ApproximateOffsetOf(key);
        */
    }
    
    pub fn reset(&mut self)  {
        
        todo!();
        /*
            delete table_;
        delete source_;
        table_ = nullptr;
        source_ = nullptr;
        */
    }
}

/**
  | A helper class that converts internal
  | format keys into user keys
  |
  */
struct KeyConvertingIterator {
    base:   LevelDBIterator,
    status: RefCell<Status>,
    iter:   *mut LevelDBIterator,
}

impl Drop for KeyConvertingIterator {
    fn drop(&mut self) {
        todo!();
        /*
            delete iter_;
        */
    }
}

impl KeyConvertingIterator {

    pub fn new(iter: *mut LevelDBIterator) -> Self {
    
        todo!();
        /*
        : iter(iter),

        
        */
    }
    
    pub fn valid(&self) -> bool {
        
        todo!();
        /*
            return iter_->Valid();
        */
    }
    
    pub fn seek(&mut self, target: &Slice)  {
        
        todo!();
        /*
            ParsedInternalKey ikey(target, kMaxSequenceNumber, kTypeValue);
        std::string encoded;
        AppendInternalKey(&encoded, ikey);
        iter_->Seek(encoded);
        */
    }
    
    pub fn seek_to_first(&mut self)  {
        
        todo!();
        /*
            iter_->SeekToFirst();
        */
    }
    
    pub fn seek_to_last(&mut self)  {
        
        todo!();
        /*
            iter_->SeekToLast();
        */
    }
    
    pub fn next(&mut self)  {
        
        todo!();
        /*
            iter_->Next();
        */
    }
    
    pub fn prev(&mut self)  {
        
        todo!();
        /*
            iter_->Prev();
        */
    }
    
    pub fn key(&self) -> Slice {
        
        todo!();
        /*
            assert(Valid());
        ParsedInternalKey key;
        if (!ParseInternalKey(iter_->key(), &key)) {
          status_ = Status::Corruption("malformed internal key");
          return Slice("corrupted key");
        }
        return key.user_key;
        */
    }
    
    pub fn value(&self) -> Slice {
        
        todo!();
        /*
            return iter_->value();
        */
    }
    
    pub fn status(&self) -> crate::Status {
        
        todo!();
        /*
            return status_.ok() ? iter_->status() : status_;
        */
    }
}