|
|
@ -12,9 +12,11 @@ |
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use std::collections::HashMap; |
|
|
|
|
|
|
|
|
|
|
|
use pretty_assertions::assert_eq; |
|
|
|
use pretty_assertions::assert_eq; |
|
|
|
|
|
|
|
|
|
|
|
use rocksdb::WriteBatch; |
|
|
|
use rocksdb::{WriteBatch, WriteBatchIterator}; |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn test_write_batch_clear() { |
|
|
|
fn test_write_batch_clear() { |
|
|
@ -25,3 +27,42 @@ fn test_write_batch_clear() { |
|
|
|
assert_eq!(batch.len(), 0); |
|
|
|
assert_eq!(batch.len(), 0); |
|
|
|
assert!(batch.is_empty()); |
|
|
|
assert!(batch.is_empty()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
|
|
|
fn test_write_batch_with_serialized_data() { |
|
|
|
|
|
|
|
struct Iterator { |
|
|
|
|
|
|
|
data: HashMap<Vec<u8>, Vec<u8>>, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl WriteBatchIterator for Iterator { |
|
|
|
|
|
|
|
fn put(&mut self, key: Box<[u8]>, value: Box<[u8]>) { |
|
|
|
|
|
|
|
match self.data.remove(key.as_ref()) { |
|
|
|
|
|
|
|
Some(expect) => { |
|
|
|
|
|
|
|
assert_eq!(value.as_ref(), expect.as_slice()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
None => { |
|
|
|
|
|
|
|
panic!("key not exists"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn delete(&mut self, _: Box<[u8]>) { |
|
|
|
|
|
|
|
panic!("invalid delete operation"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut kvs: HashMap<Vec<u8>, Vec<u8>> = HashMap::default(); |
|
|
|
|
|
|
|
kvs.insert(vec![1], vec![2]); |
|
|
|
|
|
|
|
kvs.insert(vec![2], vec![3]); |
|
|
|
|
|
|
|
kvs.insert(vec![1, 2, 3, 4, 5], vec![4]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut b1 = WriteBatch::default(); |
|
|
|
|
|
|
|
for (k, v) in &kvs { |
|
|
|
|
|
|
|
b1.put(k, v); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
let data = b1.data(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let b2 = WriteBatch::from_data(data); |
|
|
|
|
|
|
|
let mut it = Iterator { data: kvs }; |
|
|
|
|
|
|
|
b2.iterate(&mut it); |
|
|
|
|
|
|
|
} |
|
|
|