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
/*!
  | Decodes the blocks generated by
  | block_builder.cc.
  |
  */

crate::ix!();

//-------------------------------------------[.cpp/bitcoin/src/leveldb/table/block.h]

pub struct Block {

    data:           *const u8,
    size:           usize,

    /**
      | Offset in data_ of restart array
      |
      */
    restart_offset: u32,

    /**
      | Block owns data_[]
      |
      */
    owned:          bool,
}

unsafe impl Send for Block {}
unsafe impl Sync for Block {}

//-------------------------------------------[.cpp/bitcoin/src/leveldb/table/block.cc]
impl Drop for Block {
    fn drop(&mut self) {
        todo!();
        /*
            if (owned_) {
        delete[] data_;
      }
        */
    }
}

impl Block {

    pub fn size(&self) -> usize {
        
        todo!();
        /*
            return size_;
        */
    }
    
    #[inline] pub fn num_restarts(&self) -> u32 {
        
        todo!();
        /*
            assert(size_ >= sizeof(uint32_t));
      return DecodeFixed32(data_ + size_ - sizeof(uint32_t));
        */
    }
    
    /**
      | Initialize the block with the specified
      | contents.
      |
      */
    pub fn new(contents: &BlockContents) -> Self {
    
        todo!();
        /*
        : data(contents.data.data()),
        : size(contents.data.size()),
        : owned(contents.heap_allocated),

            if (size_ < sizeof(uint32_t)) {
        size_ = 0;  // Error marker
      } else {
        size_t max_restarts_allowed = (size_ - sizeof(uint32_t)) / sizeof(uint32_t);
        if (NumRestarts() > max_restarts_allowed) {
          // The size is too small for NumRestarts()
          size_ = 0;
        } else {
          restart_offset_ = size_ - (1 + NumRestarts()) * sizeof(uint32_t);
        }
      }
        */
    }
    
    pub fn new_iterator(&mut self, comparator: Box<dyn SliceComparator>) -> *mut LevelDBIterator {
        
        todo!();
        /*
            if (size_ < sizeof(uint32_t)) {
        return NewErrorIterator(Status::Corruption("bad block contents"));
      }
      const uint32_t num_restarts = NumRestarts();
      if (num_restarts == 0) {
        return NewEmptyIterator();
      } else {
        return new Iter(comparator, data_, restart_offset_, num_restarts);
      }
        */
    }
}

/**
  | Helper routine: decode the next block entry
  | starting at "p", storing the number of shared
  | key bytes, non_shared key bytes, and the length
  | of the value in "*shared", "*non_shared", and
  | "*value_length", respectively.  Will not
  | dereference past "limit".
  |
  | If any errors are detected, returns nullptr.
  | Otherwise, returns a pointer to the key delta
  | (just past the three decoded values).
  */
#[inline] pub fn decode_entry(
        p:            *const u8,
        limit:        *const u8,
        shared:       *mut u32,
        non_shared:   *mut u32,
        value_length: *mut u32) -> *const u8 {
    
    todo!();
        /*
            if (limit - p < 3) return nullptr;
      *shared = reinterpret_cast<const uint8_t*>(p)[0];
      *non_shared = reinterpret_cast<const uint8_t*>(p)[1];
      *value_length = reinterpret_cast<const uint8_t*>(p)[2];
      if ((*shared | *non_shared | *value_length) < 128) {
        // Fast path: all three values are encoded in one byte each
        p += 3;
      } else {
        if ((p = GetVarint32Ptr(p, limit, shared)) == nullptr) return nullptr;
        if ((p = GetVarint32Ptr(p, limit, non_shared)) == nullptr) return nullptr;
        if ((p = GetVarint32Ptr(p, limit, value_length)) == nullptr) return nullptr;
      }

      if (static_cast<uint32_t>(limit - p) < (*non_shared + *value_length)) {
        return nullptr;
      }
      return p;
        */
}

///--------------------
pub struct BlockIter {

    base:          LevelDBIterator,

    comparator:    Box<dyn SliceComparator>,

    /**
      | underlying block contents
      |
      */
    data:          *const u8,

    /**
      | Offset of restart array (list of fixed32)
      |
      */
    restarts:      u32,

    /**
      | Number of uint32_t entries in restart
      | array
      |
      */
    num_restarts:  u32,

    /**
      | current_ is offset in data_ of current
      | entry. >= restarts_ if !Valid
      |
      */
    current:       u32,

    /**
      | Index of restart block in which current_
      | falls
      |
      */
    restart_index: u32,

    key_:          String,
    value:         Slice,
    status:        Status,
}

impl BlockIter {

    #[inline] pub fn compare(&self, 
        a: &Slice,
        b: &Slice) -> i32 {
        
        todo!();
        /*
            return comparator_->Compare(a, b);
        */
    }

    /**
      | Return the offset in data_ just past
      | the end of the current entry.
      |
      */
    #[inline] pub fn next_entry_offset(&self) -> u32 {
        
        todo!();
        /*
            return (value_.data() + value_.size()) - data_;
        */
    }
    
    pub fn get_restart_point(&mut self, index: u32) -> u32 {
        
        todo!();
        /*
            assert(index < num_restarts_);
        return DecodeFixed32(data_ + restarts_ + index * sizeof(uint32_t));
        */
    }
    
    pub fn seek_to_restart_point(&mut self, index: u32)  {
        
        todo!();
        /*
            key_.clear();
        restart_index_ = index;
        // current_ will be fixed by ParseNextKey();

        // ParseNextKey() starts at the end of value_, so set value_ accordingly
        uint32_t offset = GetRestartPoint(index);
        value_ = Slice(data_ + offset, 0);
        */
    }
    
    pub fn new(
        comparator:   Box<dyn SliceComparator>,
        data:         *const u8,
        restarts:     u32,
        num_restarts: u32) -> Self {
    
        todo!();
        /*
        : comparator(comparator),
        : data(data),
        : restarts(restarts),
        : num_restarts(num_restarts),
        : current(restarts_),
        : restart_index(num_restarts_),

            assert(num_restarts_ > 0);
        */
    }
    
    pub fn valid(&self) -> bool {
        
        todo!();
        /*
            return current_ < restarts_;
        */
    }
    
    pub fn status(&self) -> crate::Status {
        
        todo!();
        /*
            return status_;
        */
    }
    
    pub fn key(&self) -> Slice {
        
        todo!();
        /*
            assert(Valid());
        return key_;
        */
    }
    
    pub fn value(&self) -> Slice {
        
        todo!();
        /*
            assert(Valid());
        return value_;
        */
    }
    
    pub fn next(&mut self)  {
        
        todo!();
        /*
            assert(Valid());
        ParseNextKey();
        */
    }
    
    pub fn prev(&mut self)  {
        
        todo!();
        /*
            assert(Valid());

        // Scan backwards to a restart point before current_
        const uint32_t original = current_;
        while (GetRestartPoint(restart_index_) >= original) {
          if (restart_index_ == 0) {
            // No more entries
            current_ = restarts_;
            restart_index_ = num_restarts_;
            return;
          }
          restart_index_--;
        }

        SeekToRestartPoint(restart_index_);
        do {
          // Loop until end of current entry hits the start of original entry
        } while (ParseNextKey() && NextEntryOffset() < original);
        */
    }
    
    pub fn seek(&mut self, target: &Slice)  {
        
        todo!();
        /*
            // Binary search in restart array to find the last restart point
        // with a key < target
        uint32_t left = 0;
        uint32_t right = num_restarts_ - 1;
        while (left < right) {
          uint32_t mid = (left + right + 1) / 2;
          uint32_t region_offset = GetRestartPoint(mid);
          uint32_t shared, non_shared, value_length;
          const char* key_ptr =
              DecodeEntry(data_ + region_offset, data_ + restarts_, &shared,
                          &non_shared, &value_length);
          if (key_ptr == nullptr || (shared != 0)) {
            CorruptionError();
            return;
          }
          Slice mid_key(key_ptr, non_shared);
          if (Compare(mid_key, target) < 0) {
            // Key at "mid" is smaller than "target".  Therefore all
            // blocks before "mid" are uninteresting.
            left = mid;
          } else {
            // Key at "mid" is >= "target".  Therefore all blocks at or
            // after "mid" are uninteresting.
            right = mid - 1;
          }
        }

        // Linear search (within restart block) for first key >= target
        SeekToRestartPoint(left);
        while (true) {
          if (!ParseNextKey()) {
            return;
          }
          if (Compare(key_, target) >= 0) {
            return;
          }
        }
        */
    }
    
    pub fn seek_to_first(&mut self)  {
        
        todo!();
        /*
            SeekToRestartPoint(0);
        ParseNextKey();
        */
    }
    
    pub fn seek_to_last(&mut self)  {
        
        todo!();
        /*
            SeekToRestartPoint(num_restarts_ - 1);
        while (ParseNextKey() && NextEntryOffset() < restarts_) {
          // Keep skipping
        }
        */
    }
    
    pub fn corruption_error(&mut self)  {
        
        todo!();
        /*
            current_ = restarts_;
        restart_index_ = num_restarts_;
        status_ = Status::Corruption("bad entry in block");
        key_.clear();
        value_.clear();
        */
    }
    
    pub fn parse_next_key(&mut self) -> bool {
        
        todo!();
        /*
            current_ = NextEntryOffset();
        const char* p = data_ + current_;
        const char* limit = data_ + restarts_;  // Restarts come right after data
        if (p >= limit) {
          // No more entries to return.  Mark as invalid.
          current_ = restarts_;
          restart_index_ = num_restarts_;
          return false;
        }

        // Decode next entry
        uint32_t shared, non_shared, value_length;
        p = DecodeEntry(p, limit, &shared, &non_shared, &value_length);
        if (p == nullptr || key_.size() < shared) {
          CorruptionError();
          return false;
        } else {
          key_.resize(shared);
          key_.append(p, non_shared);
          value_ = Slice(p + non_shared, value_length);
          while (restart_index_ + 1 < num_restarts_ &&
                 GetRestartPoint(restart_index_ + 1) < current_) {
            ++restart_index_;
          }
          return true;
        }
        */
    }
}