changeset 148:08f5579b8598

Add id() function to Cmp trait
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 29 Jan 2017 14:28:51 +0100
parents 3cc327257435
children 11f887625190
files src/block.rs src/cmp.rs src/options.rs src/version_edit.rs
diffstat 4 files changed, 38 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/block.rs	Sun Jan 29 13:54:14 2017 +0100
+++ b/src/block.rs	Sun Jan 29 14:28:51 2017 +0100
@@ -123,7 +123,7 @@
     /// Returns SHARED, NON_SHARED, VALSIZE and [length of length spec] from the current position,
     /// where 'length spec' is the length of the three values in the entry header, as described
     /// above.
-    /// Advances self.offset to point to the beginning of the next entry.
+    /// Advances self.offset to the beginning of the next entry.
     fn parse_entry_and_advance(&mut self) -> (usize, usize, usize, usize) {
         let mut i = 0;
         let (shared, sharedlen) = usize::decode_var(&self.block[self.offset..]);
--- a/src/cmp.rs	Sun Jan 29 13:54:14 2017 +0100
+++ b/src/cmp.rs	Sun Jan 29 14:28:51 2017 +0100
@@ -7,9 +7,18 @@
 /// Comparator trait, supporting types that can be nested (i.e., add additional functionality on
 /// top of an inner comparator)
 pub trait Cmp {
+    /// Compare to byte strings, bytewise.
     fn cmp(&self, &[u8], &[u8]) -> Ordering;
+
+    /// Return the shortest byte string that compares "Greater" to the first argument and "Less" to
+    /// the second one.
     fn find_shortest_sep(&self, &[u8], &[u8]) -> Vec<u8>;
+    /// Return the shortest byte string that compares "Greater" to the argument.
     fn find_short_succ(&self, &[u8]) -> Vec<u8>;
+
+    /// A unique identifier for a comparator. A comparator wrapper (like InternalKeyCmp) may
+    /// return the id of its inner comparator.
+    fn id(&self) -> &'static str;
 }
 
 /// Lexical comparator.
@@ -21,6 +30,10 @@
         a.cmp(b)
     }
 
+    fn id(&self) -> &'static str {
+        "leveldb.BytewiseComparator"
+    }
+
     fn find_shortest_sep(&self, a: &[u8], b: &[u8]) -> Vec<u8> {
         if a == b {
             return a.to_vec();
@@ -79,6 +92,10 @@
         }
     }
 
+    fn id(&self) -> &'static str {
+        self.0.id()
+    }
+
     fn find_shortest_sep(&self, a: &[u8], b: &[u8]) -> Vec<u8> {
         let (_, seqa, keya) = key_types::parse_internal_key(a);
         let (_, _, keyb) = key_types::parse_internal_key(b);
@@ -128,6 +145,10 @@
         }
     }
 
+    fn id(&self) -> &'static str {
+        self.0.id()
+    }
+
     // The following two impls should not be used (by principle) although they should be correct.
     // They will crash the program.
     fn find_shortest_sep(&self, _: &[u8], _: &[u8]) -> Vec<u8> {
--- a/src/options.rs	Sun Jan 29 13:54:14 2017 +0100
+++ b/src/options.rs	Sun Jan 29 14:28:51 2017 +0100
@@ -64,6 +64,17 @@
     }
 }
 
+impl Options {
+    /// Set the comparator to use in all operations and structures that need to compare keys.
+    ///
+    /// DO NOT set the comparator after having written any record with a different comparator.
+    /// If the comparator used differs from the one used when writing a database that is being
+    /// opened, the library is free to panic.
+    pub fn set_comparator<C: Cmp>(&mut self, c: Box<Cmp>) {
+        self.cmp = Arc::new(c);
+    }
+}
+
 /// Supplied to DB read operations.
 pub struct ReadOptions {
     pub verify_checksums: bool,
--- a/src/version_edit.rs	Sun Jan 29 13:54:14 2017 +0100
+++ b/src/version_edit.rs	Sun Jan 29 14:28:51 2017 +0100
@@ -99,8 +99,8 @@
         self.deleted.insert((level, file_num));
     }
 
-    pub fn set_comparator_name(&mut self, name: String) {
-        self.comparator = Some(name)
+    pub fn set_comparator_name(&mut self, name: &str) {
+        self.comparator = Some(name.to_string())
     }
 
     pub fn set_log_num(&mut self, num: u64) {
@@ -303,13 +303,14 @@
     use super::CompactionPointer;
     use super::VersionEdit;
 
+    use cmp::{Cmp, DefaultCmp};
     use types::FileMetaData;
 
     #[test]
     fn test_version_edit_encode_decode() {
         let mut ve = VersionEdit::new();
 
-        ve.set_comparator_name("abcdef".to_string());
+        ve.set_comparator_name(DefaultCmp.id());
         ve.set_log_num(123);
         ve.set_next_file(456);
         ve.set_prev_log_num(789);
@@ -330,7 +331,7 @@
 
         let decoded = VersionEdit::decode_from(encoded.as_ref()).unwrap();
 
-        assert_eq!(decoded.comparator, Some("abcdef".to_string()));
+        assert_eq!(decoded.comparator, Some(DefaultCmp.id().to_string()));
         assert_eq!(decoded.log_number, Some(123));
         assert_eq!(decoded.next_file_number, Some(456));
         assert_eq!(decoded.prev_log_number, Some(789));