Expand description
§Front-coded string dictionary: Fast and compact indexed string set
This is a Rust library to store an indexed set of strings and support fast queires. The data structure is a plain front-coded string dictionary described in Martínez-Prieto et al., Practical compressed string dictionaries, INFOSYS 2016.
§Features
- Indexed set. Fcsd implements an indexed set of strings in a compressed format.
nstrings in the set are indexed with integers from[0..n-1]and assigned in the lexicographical order. - Simple and fast compression/decompression. Fcsd maintains a set of strings in a compressed space through front coding, a differential compression technique for strings, allowing for fast decompression operations.
- Random access. Fcsd maintains strings through a bucketization technique enabling to directly decompress arbitrary strings and perform binary search for strings.
- Zero-copy loading. A set is kept in its serialized form, so
Set::from_bytescan query it directly from any byte buffer, such as a memory-mapped file, without copying. no_stdsupport. Disable default features to use Fcsd in an allocator-enabledno_stdenvironment.Set::from_bytesworks withoutstd; reader/writer-based serialization requires the defaultstdfeature.
§Example
use fcsd::Set;
// Input string keys should be sorted and unique.
let keys = ["ICDM", "ICML", "SIGIR", "SIGKDD", "SIGMOD"];
// Builds an indexed set.
let set = Set::new(keys).unwrap();
assert_eq!(set.len(), keys.len());
// Gets indexes associated with given keys.
let mut locator = set.locator();
assert_eq!(locator.run(b"ICML"), Some(1));
assert_eq!(locator.run(b"SIGMOD"), Some(4));
assert_eq!(locator.run(b"SIGSPATIAL"), None);
// Decodes string keys from given indexes.
let mut decoder = set.decoder();
assert_eq!(decoder.run(0), b"ICDM".to_vec());
assert_eq!(decoder.run(3), b"SIGKDD".to_vec());
// Enumerates indexes and keys stored in the set.
let mut iter = set.iter();
assert_eq!(iter.next(), Some((0, b"ICDM".to_vec())));
assert_eq!(iter.next(), Some((1, b"ICML".to_vec())));
assert_eq!(iter.next(), Some((2, b"SIGIR".to_vec())));
assert_eq!(iter.next(), Some((3, b"SIGKDD".to_vec())));
assert_eq!(iter.next(), Some((4, b"SIGMOD".to_vec())));
assert_eq!(iter.next(), None);
// Enumerates indexes and keys starting with a prefix.
let mut iter = set.predictive_iter(b"SIG");
assert_eq!(iter.next(), Some((2, b"SIGIR".to_vec())));
assert_eq!(iter.next(), Some((3, b"SIGKDD".to_vec())));
assert_eq!(iter.next(), Some((4, b"SIGMOD".to_vec())));
assert_eq!(iter.next(), None);
// Serialization / Deserialization
let mut data = Vec::<u8>::new();
set.serialize_into(&mut data).unwrap();
assert_eq!(data.len(), set.size_in_bytes());
let other = Set::deserialize_from(&data[..]).unwrap();
assert_eq!(data.len(), other.size_in_bytes());
// Zero-copy loading from bytes
let view = Set::from_bytes(data.as_slice()).unwrap();
assert_eq!(view.locator().run(b"SIGKDD"), Some(3));§Memory-mapped files
Set::from_bytes accepts any S: AsRef<[u8]>, so a memory map can be passed as is (here with the memmap2 crate). Here, keys.fcsd contains a previously serialized set:
let file = std::fs::File::open("keys.fcsd").unwrap();
let mmap = unsafe { memmap2::Mmap::map(&file).unwrap() };
let set = fcsd::Set::from_bytes(mmap).unwrap();Set::from_bytes validates every key, which reads the whole file. For trusted data, Set::from_bytes_trusted checks only the header and metadata, so pages are loaded lazily as queries touch them.
§Benchmarks
See the benchmark suite and measured results for comparisons with fst and yada on words and Wikipedia URLs.
§Licensing
This library is free software provided under MIT.
Re-exports§
Modules§
Structs§
- Set
- Fast and compact indexed string set using front coding.
Constants§
- DEFAULT_
BUCKET_ SIZE - Default parameter for the number of keys in each bucket.
- END_
MARKER - Special terminator, which must not be contained in stored keys.
- FORMAT_
VERSION - Version of the serialization format.