bitcoinleveldb_batch/
write_batch.rs

1/*!
2  | WriteBatch holds a collection of updates to
3  | apply atomically to a DB.
4  |
5  | The updates are applied in the order in which
6  | they are added to the WriteBatch.  For example,
7  | the value of "key" will be "v3" after the
8  | following batch is written:
9  |
10  |    batch.Put("key", "v1");
11  |    batch.Delete("key");
12  |    batch.Put("key", "v2");
13  |    batch.Put("key", "v3");
14  |
15  | Multiple threads can invoke const methods on
16  | a WriteBatch without external synchronization,
17  | but if any of the threads may call a non-const
18  | method, all threads accessing the same
19  | WriteBatch must use external synchronization.
20  */
21
22crate::ix!();
23
24/**
25  | WriteBatch header has an 8-byte sequence
26  | number followed by a 4-byte count.
27  |
28  */
29pub const HEADER: usize = 12;
30
31//-------------------------------------------[.cpp/bitcoin/src/leveldb/include/leveldb/write_batch.h]
32//-------------------------------------------[.cpp/bitcoin/src/leveldb/db/write_batch.cc]
33
34pub trait WriteBatchPut {
35    fn put(&mut self, 
36            key_:   &Slice,
37            value: &Slice);
38}
39
40pub trait WriteBatchDelete {
41    fn delete(&mut self, key_: &Slice);
42}
43
44pub trait WriteBatchHandler: 
45WriteBatchPut 
46+ WriteBatchDelete { }
47
48/**
49  | WriteBatch::rep_ :=
50  |    sequence: fixed64
51  |    count: fixed32
52  |    data: record[count]
53  | record :=
54  |    kTypeValue varstring varstring         |
55  |    kTypeDeletion varstring
56  | varstring :=
57  |    len: varint32
58  |    data: uint8[len]
59  */
60pub struct WriteBatch {
61
62    /**
63      | See comment in write_batch.cc for the
64      | format of rep_
65      |
66      */
67    rep: String,
68}
69
70impl WriteBatch {
71
72    pub fn new() -> Self {
73    
74        todo!();
75        /*
76            Clear();
77        */
78    }
79    
80    /**
81      | Clear all updates buffered in this batch.
82      |
83      */
84    pub fn clear(&mut self)  {
85        
86        todo!();
87        /*
88            rep_.clear();
89      rep_.resize(kHeader);
90        */
91    }
92    
93    /**
94      | The size of the database changes caused by
95      | this batch.
96      |
97      | This number is tied to implementation
98      | details, and may change across releases. It
99      | is intended for LevelDB usage metrics.
100      */
101    pub fn approximate_size(&self) -> usize {
102        
103        todo!();
104        /*
105            return rep_.size();
106        */
107    }
108    
109    /**
110      | Support for iterating over the contents
111      | of a batch.
112      |
113      */
114    pub fn iterate(&self, handler: *mut dyn WriteBatchHandler) -> crate::Status {
115        
116        todo!();
117        /*
118            Slice input(rep_);
119      if (input.size() < kHeader) {
120        return Status::Corruption("malformed WriteBatch (too small)");
121      }
122
123      input.remove_prefix(kHeader);
124      Slice key, value;
125      int found = 0;
126      while (!input.empty()) {
127        found++;
128        char tag = input[0];
129        input.remove_prefix(1);
130        switch (tag) {
131          case kTypeValue:
132            if (GetLengthPrefixedSlice(&input, &key) &&
133                GetLengthPrefixedSlice(&input, &value)) {
134              handler->Put(key, value);
135            } else {
136              return Status::Corruption("bad WriteBatch Put");
137            }
138            break;
139          case kTypeDeletion:
140            if (GetLengthPrefixedSlice(&input, &key)) {
141              handler->Delete(key);
142            } else {
143              return Status::Corruption("bad WriteBatch Delete");
144            }
145            break;
146          default:
147            return Status::Corruption("unknown WriteBatch tag");
148        }
149      }
150      if (found != WriteBatchInternal::Count(this)) {
151        return Status::Corruption("WriteBatch has wrong count");
152      } else {
153        return Status::OK();
154      }
155        */
156    }
157    
158    /**
159      | Store the mapping "key->value" in the
160      | database.
161      |
162      */
163    pub fn put(&mut self, 
164        key_:   &Slice,
165        value: &Slice)  {
166        
167        todo!();
168        /*
169            WriteBatchInternal::SetCount(this, WriteBatchInternal::Count(this) + 1);
170      rep_.push_back(static_cast<char>(kTypeValue));
171      PutLengthPrefixedSlice(&rep_, key);
172      PutLengthPrefixedSlice(&rep_, value);
173        */
174    }
175    
176    /**
177      | If the database contains a mapping for
178      | "key", erase it. Else do nothing.
179      |
180      */
181    pub fn delete(&mut self, key_: &Slice)  {
182        
183        todo!();
184        /*
185            WriteBatchInternal::SetCount(this, WriteBatchInternal::Count(this) + 1);
186      rep_.push_back(static_cast<char>(kTypeDeletion));
187      PutLengthPrefixedSlice(&rep_, key);
188        */
189    }
190    
191    /**
192      | Copies the operations in "source" to this
193      | batch.
194      |
195      | This runs in O(source size) time. However,
196      | the constant factor is better than calling
197      | Iterate() over the source batch with
198      | a Handler that replicates the operations into
199      | this batch.
200      */
201    pub fn append(&mut self, source: &WriteBatch)  {
202        
203        todo!();
204        /*
205            WriteBatchInternal::Append(this, &source);
206        */
207    }
208}