|
| 1 | +// Copyright 2018 foundationdb-rs developers, https://github.com/bluejekyll/foundationdb-rs/graphs/contributors |
| 2 | +// Copyright 2013-2018 Apple, Inc and the FoundationDB project authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or |
| 5 | +// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or |
| 6 | +// http://opensource.org/licenses/MIT>, at your option. This file may not be |
| 7 | +// copied, modified, or distributed except according to those terms. |
| 8 | + |
| 9 | +//! subspace provides a convenient way to use FoundationDB tuples to define namespaces for |
| 10 | +//! different categories of data. The namespace is specified by a prefix tuple which is prepended |
| 11 | +//! to all tuples packed by the subspace. When unpacking a key with the subspace, the prefix tuple |
| 12 | +//! will be removed from the result. |
| 13 | +//! |
| 14 | +//! As a best practice, API clients should use at least one subspace for application data. For |
| 15 | +//! general guidance on subspace usage, see the Subspaces section of the Developer Guide |
| 16 | +//! (https://apple.github.io/foundationdb/developer-guide.html#subspaces). |
| 17 | +
|
| 18 | +use tuple::{Tuple, TupleError}; |
| 19 | + |
| 20 | +/// Subspace represents a well-defined region of keyspace in a FoundationDB database. |
| 21 | +#[derive(Debug, Clone)] |
| 22 | +pub struct Subspace { |
| 23 | + prefix: Vec<u8>, |
| 24 | +} |
| 25 | + |
| 26 | +impl Subspace { |
| 27 | + /// `all` returns the Subspace corresponding to all keys in a FoundationDB database. |
| 28 | + pub fn all() -> Subspace { |
| 29 | + Self { prefix: Vec::new() } |
| 30 | + } |
| 31 | + |
| 32 | + /// `from_bytes` returns a new Subspace from the provided bytes. |
| 33 | + pub fn from_bytes(bytes: &[u8]) -> Self { |
| 34 | + Self { |
| 35 | + prefix: bytes.to_vec(), |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /// `from_bytes` returns a new Subspace from the provided Tuple. |
| 40 | + pub fn new<T: Tuple>(t: &T) -> Self { |
| 41 | + let prefix = Tuple::encode_to_vec(t); |
| 42 | + Self { prefix } |
| 43 | + } |
| 44 | + |
| 45 | + /// `subspace` returns a new Subspace whose prefix extends this Subspace with a given tuple. |
| 46 | + pub fn subspace<T: Tuple>(&self, t: &T) -> Self { |
| 47 | + Self { |
| 48 | + prefix: self.pack(t), |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /// `bytes` returns the literal bytes of the prefix of this Subspace. |
| 53 | + pub fn bytes(&self) -> &[u8] { |
| 54 | + self.prefix.as_slice() |
| 55 | + } |
| 56 | + |
| 57 | + /// `pack` returns the key encoding the specified Tuple with the prefix of this Subspace |
| 58 | + /// prepended. |
| 59 | + pub fn pack<T: Tuple>(&self, t: &T) -> Vec<u8> { |
| 60 | + let mut packed = Tuple::encode_to_vec(t); |
| 61 | + let mut out = Vec::with_capacity(self.prefix.len() + packed.len()); |
| 62 | + out.extend_from_slice(&self.prefix); |
| 63 | + out.append(&mut packed); |
| 64 | + out |
| 65 | + } |
| 66 | + |
| 67 | + /// `unpack` returns the Tuple encoded by the given key with the prefix of this Subspace |
| 68 | + /// removed. `unpack` will return an error if the key is not in this Subspace or does not |
| 69 | + /// encode a well-formed Tuple. |
| 70 | + pub fn unpack<T: Tuple>(&self, key: &[u8]) -> Result<T, TupleError> { |
| 71 | + if !self.contains(key) { |
| 72 | + return Err(TupleError::InvalidData); |
| 73 | + } |
| 74 | + let key = &key[self.prefix.len()..]; |
| 75 | + Tuple::decode(&key) |
| 76 | + } |
| 77 | + |
| 78 | + /// `contains` returns true if the provided key starts with the prefix of this Subspace, |
| 79 | + /// indicating that the Subspace logically contains the key. |
| 80 | + pub fn contains(&self, key: &[u8]) -> bool { |
| 81 | + key.starts_with(&self.prefix) |
| 82 | + } |
| 83 | + |
| 84 | + /// `range` returns first and last key of given Subspace |
| 85 | + pub fn range(&self) -> (Vec<u8>, Vec<u8>) { |
| 86 | + let mut begin = self.prefix.clone(); |
| 87 | + begin.push(0x00); |
| 88 | + |
| 89 | + let mut end = self.prefix.clone(); |
| 90 | + end.push(0xff); |
| 91 | + |
| 92 | + (begin, end) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +#[cfg(test)] |
| 97 | +mod tests { |
| 98 | + use super::*; |
| 99 | + use tuple; |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn sub() { |
| 103 | + let ss0 = Subspace::new(&(1,)); |
| 104 | + let ss1 = ss0.subspace(&(2,)); |
| 105 | + |
| 106 | + let ss2 = Subspace::new(&(1, 2)); |
| 107 | + |
| 108 | + assert_eq!(ss1.bytes(), ss2.bytes()); |
| 109 | + } |
| 110 | + |
| 111 | + #[test] |
| 112 | + fn pack_unpack() { |
| 113 | + let ss0 = Subspace::new(&(1,)); |
| 114 | + let tup = (2, 3); |
| 115 | + |
| 116 | + let packed = ss0.pack(&tup); |
| 117 | + let expected = Tuple::encode_to_vec(&(1, 2, 3)); |
| 118 | + assert_eq!(expected, packed); |
| 119 | + |
| 120 | + let tup_unpack: (i64, i64) = ss0.unpack(&packed).unwrap(); |
| 121 | + assert_eq!(tup, tup_unpack); |
| 122 | + |
| 123 | + assert!(ss0.unpack::<(i64, i64, i64)>(&packed).is_err()); |
| 124 | + } |
| 125 | + |
| 126 | + #[test] |
| 127 | + fn contains() { |
| 128 | + let ss0 = Subspace::new(&(1,)); |
| 129 | + let ss1 = Subspace::new(&(2,)); |
| 130 | + let tup = (2, 3); |
| 131 | + |
| 132 | + assert!(ss0.contains(&ss0.pack(&tup))); |
| 133 | + assert!(!ss1.contains(&ss0.pack(&tup))); |
| 134 | + } |
| 135 | + |
| 136 | + #[test] |
| 137 | + fn unpack_malformed() { |
| 138 | + let ss0 = Subspace::new(&((),)); |
| 139 | + |
| 140 | + let malformed = { |
| 141 | + let mut v = ss0.bytes().to_vec(); |
| 142 | + v.push(0xff); |
| 143 | + v |
| 144 | + }; |
| 145 | + |
| 146 | + assert!(ss0.unpack::<tuple::TupleValue>(&malformed).is_err()); |
| 147 | + } |
| 148 | + |
| 149 | + #[test] |
| 150 | + fn range() { |
| 151 | + let ss = Subspace::new(&(1,)); |
| 152 | + let tup = (2, 3); |
| 153 | + let packed = ss.pack(&tup); |
| 154 | + |
| 155 | + let (begin, end) = ss.range(); |
| 156 | + assert!(packed >= begin && packed <= end); |
| 157 | + } |
| 158 | +} |
0 commit comments