2018-12-30 13:04:02 +01:00
|
|
|
#![cfg_attr(feature = "nightly", feature(test))]
|
|
|
|
|
2018-11-27 19:11:33 +01:00
|
|
|
pub mod automaton;
|
2018-12-02 16:45:17 +01:00
|
|
|
pub mod database;
|
2018-11-08 12:05:59 +01:00
|
|
|
pub mod data;
|
2018-05-27 15:23:43 +02:00
|
|
|
pub mod rank;
|
2018-09-27 16:32:17 +02:00
|
|
|
pub mod tokenizer;
|
2019-01-10 11:59:42 +01:00
|
|
|
mod attribute;
|
2018-10-09 18:23:35 +02:00
|
|
|
mod common_words;
|
2018-07-10 21:29:17 +02:00
|
|
|
|
2018-12-23 16:46:49 +01:00
|
|
|
use std::fmt;
|
|
|
|
|
2018-12-13 11:52:34 +01:00
|
|
|
pub use rocksdb;
|
|
|
|
|
2018-09-27 16:59:41 +02:00
|
|
|
pub use self::tokenizer::Tokenizer;
|
2018-10-09 18:23:35 +02:00
|
|
|
pub use self::common_words::CommonWords;
|
2019-01-10 11:59:42 +01:00
|
|
|
pub use self::attribute::{Attribute, AttributeError};
|
2018-05-13 15:12:15 +02:00
|
|
|
|
2018-12-22 12:00:24 +01:00
|
|
|
/// Represent an internally generated document unique identifier.
|
|
|
|
///
|
|
|
|
/// It is used to inform the database the document you want to deserialize.
|
|
|
|
/// Helpful for custom ranking.
|
|
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
|
2018-12-25 12:26:38 +01:00
|
|
|
pub struct DocumentId(u64);
|
2018-05-27 15:23:43 +02:00
|
|
|
|
2018-12-23 16:46:49 +01:00
|
|
|
/// Represent a word position in bytes along with the length of it.
|
|
|
|
///
|
|
|
|
/// It can represent words byte index to maximum 2^22 and
|
|
|
|
/// up to words of length 1024.
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub struct WordArea(u32);
|
|
|
|
|
|
|
|
impl WordArea {
|
2019-01-09 20:14:08 +01:00
|
|
|
/// Construct a `WordArea` from a word position in expresed as
|
|
|
|
/// a number of characters and the length of it.
|
2018-12-23 16:46:49 +01:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
2019-01-09 20:14:08 +01:00
|
|
|
/// The char index must not be greater than 2^22
|
2018-12-23 16:46:49 +01:00
|
|
|
/// and the length not greater than 1024.
|
2019-01-09 20:14:08 +01:00
|
|
|
fn new(char_index: u32, length: u16) -> Result<WordArea, WordAreaError> {
|
|
|
|
if char_index & 0b1111_1111_1100_0000_0000_0000_0000 != 0 {
|
2018-12-28 16:15:22 +01:00
|
|
|
return Err(WordAreaError::ByteIndexTooBig)
|
|
|
|
}
|
|
|
|
|
|
|
|
if length & 0b1111_1100_0000_0000 != 0 {
|
|
|
|
return Err(WordAreaError::LengthTooBig)
|
|
|
|
}
|
|
|
|
|
2019-01-09 20:14:08 +01:00
|
|
|
let char_index = char_index << 10;
|
|
|
|
Ok(WordArea(char_index | u32::from(length)))
|
2018-12-28 16:15:22 +01:00
|
|
|
}
|
|
|
|
|
2019-01-09 20:14:08 +01:00
|
|
|
fn new_faillible(char_index: u32, length: u16) -> WordArea {
|
|
|
|
match WordArea::new(char_index, length) {
|
2018-12-28 16:15:22 +01:00
|
|
|
Ok(word_area) => word_area,
|
|
|
|
Err(WordAreaError::ByteIndexTooBig) => {
|
|
|
|
panic!("word area byte index must not be greater than 2^22")
|
|
|
|
},
|
|
|
|
Err(WordAreaError::LengthTooBig) => {
|
|
|
|
panic!("word area length must not be greater than 1024")
|
|
|
|
},
|
|
|
|
}
|
2018-12-23 16:46:49 +01:00
|
|
|
}
|
|
|
|
|
2018-12-29 12:26:33 +01:00
|
|
|
#[inline]
|
2019-01-09 20:14:08 +01:00
|
|
|
pub fn char_index(self) -> u32 {
|
2018-12-23 16:46:49 +01:00
|
|
|
self.0 >> 10
|
|
|
|
}
|
|
|
|
|
2018-12-29 12:26:33 +01:00
|
|
|
#[inline]
|
2018-12-31 23:20:18 +01:00
|
|
|
pub fn length(self) -> u16 {
|
2018-12-23 16:46:49 +01:00
|
|
|
(self.0 & 0b0000_0000_0000_0000_0011_1111_1111) as u16
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for WordArea {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.debug_struct("WordArea")
|
2019-01-09 20:14:08 +01:00
|
|
|
.field("char_index", &self.char_index())
|
2018-12-23 16:46:49 +01:00
|
|
|
.field("length", &self.length())
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 16:15:22 +01:00
|
|
|
enum WordAreaError {
|
|
|
|
ByteIndexTooBig,
|
|
|
|
LengthTooBig,
|
|
|
|
}
|
|
|
|
|
2018-05-27 11:15:05 +02:00
|
|
|
/// This structure represent the position of a word
|
|
|
|
/// in a document and its attributes.
|
|
|
|
///
|
|
|
|
/// This is stored in the map, generated at index time,
|
|
|
|
/// extracted and interpreted at search time.
|
2018-12-23 16:46:49 +01:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2018-07-10 21:29:17 +02:00
|
|
|
#[repr(C)]
|
2018-05-27 11:15:05 +02:00
|
|
|
pub struct DocIndex {
|
|
|
|
/// The document identifier where the word was found.
|
2018-10-17 13:35:34 +02:00
|
|
|
pub document_id: DocumentId,
|
2018-05-27 11:15:05 +02:00
|
|
|
|
2018-12-23 16:46:49 +01:00
|
|
|
/// The attribute in the document where the word was found
|
|
|
|
/// along with the index in it.
|
|
|
|
pub attribute: Attribute,
|
2018-05-27 11:15:05 +02:00
|
|
|
|
2018-12-23 16:46:49 +01:00
|
|
|
/// The position in bytes where the word was found
|
|
|
|
/// along with the length of it.
|
2018-05-27 11:15:05 +02:00
|
|
|
///
|
2018-12-23 16:46:49 +01:00
|
|
|
/// It informs on the original word area in the text indexed
|
|
|
|
/// without needing to run the tokenizer again.
|
|
|
|
pub word_area: WordArea,
|
2018-05-13 15:12:15 +02:00
|
|
|
}
|
|
|
|
|
2018-05-27 11:15:05 +02:00
|
|
|
/// This structure represent a matching word with informations
|
|
|
|
/// on the location of the word in the document.
|
|
|
|
///
|
|
|
|
/// The order of the field is important because it defines
|
|
|
|
/// the way these structures are ordered between themselves.
|
|
|
|
///
|
|
|
|
/// The word in itself is not important.
|
2018-05-27 15:23:43 +02:00
|
|
|
// TODO do data oriented programming ? very arrays ?
|
2018-12-23 16:46:49 +01:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2018-05-27 11:15:05 +02:00
|
|
|
pub struct Match {
|
2018-05-27 15:23:43 +02:00
|
|
|
/// The word index in the query sentence.
|
|
|
|
/// Same as the `attribute_index` but for the query words.
|
|
|
|
///
|
|
|
|
/// Used to retrieve the automaton that match this word.
|
|
|
|
pub query_index: u32,
|
|
|
|
|
2018-05-27 11:15:05 +02:00
|
|
|
/// The distance the word has with the query word
|
|
|
|
/// (i.e. the Levenshtein distance).
|
|
|
|
pub distance: u8,
|
|
|
|
|
2018-12-23 16:46:49 +01:00
|
|
|
/// The attribute in the document where the word was found
|
|
|
|
/// along with the index in it.
|
|
|
|
pub attribute: Attribute,
|
2018-07-06 20:58:06 +02:00
|
|
|
|
|
|
|
/// Whether the word that match is an exact match or a prefix.
|
|
|
|
pub is_exact: bool,
|
2018-12-23 16:46:49 +01:00
|
|
|
|
|
|
|
/// The position in bytes where the word was found
|
|
|
|
/// along with the length of it.
|
|
|
|
///
|
|
|
|
/// It informs on the original word area in the text indexed
|
|
|
|
/// without needing to run the tokenizer again.
|
|
|
|
pub word_area: WordArea,
|
2018-05-13 15:12:15 +02:00
|
|
|
}
|
2018-05-27 15:23:43 +02:00
|
|
|
|
|
|
|
impl Match {
|
|
|
|
pub fn zero() -> Self {
|
|
|
|
Match {
|
|
|
|
query_index: 0,
|
|
|
|
distance: 0,
|
2018-12-28 16:15:22 +01:00
|
|
|
attribute: Attribute::new_faillible(0, 0),
|
2018-07-06 20:58:06 +02:00
|
|
|
is_exact: false,
|
2018-12-28 16:15:22 +01:00
|
|
|
word_area: WordArea::new_faillible(0, 0),
|
2018-05-27 15:23:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn max() -> Self {
|
|
|
|
Match {
|
|
|
|
query_index: u32::max_value(),
|
|
|
|
distance: u8::max_value(),
|
2019-01-10 11:59:42 +01:00
|
|
|
attribute: Attribute::max_value(),
|
2018-07-06 20:58:06 +02:00
|
|
|
is_exact: true,
|
2018-12-23 16:46:49 +01:00
|
|
|
word_area: WordArea(u32::max_value()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use quickcheck::{quickcheck, TestResult};
|
|
|
|
use std::mem;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn docindex_mem_size() {
|
|
|
|
assert_eq!(mem::size_of::<DocIndex>(), 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
quickcheck! {
|
2019-01-09 20:14:08 +01:00
|
|
|
fn qc_word_area(gen_char_index: u32, gen_length: u16) -> TestResult {
|
|
|
|
if gen_char_index > 2_u32.pow(22) || gen_length > 2_u16.pow(10) {
|
2018-12-23 16:46:49 +01:00
|
|
|
return TestResult::discard()
|
|
|
|
}
|
|
|
|
|
2019-01-09 20:14:08 +01:00
|
|
|
let word_area = WordArea::new_faillible(gen_char_index, gen_length);
|
2018-12-23 16:46:49 +01:00
|
|
|
|
2019-01-09 20:14:08 +01:00
|
|
|
let valid_char_index = word_area.char_index() == gen_char_index;
|
2018-12-23 16:46:49 +01:00
|
|
|
let valid_length = word_area.length() == gen_length;
|
|
|
|
|
|
|
|
TestResult::from_bool(valid_char_index && valid_length)
|
|
|
|
}
|
|
|
|
|
2019-01-09 20:14:08 +01:00
|
|
|
fn qc_word_area_ord(gen_char_index: u32, gen_length: u16) -> TestResult {
|
|
|
|
if gen_char_index >= 2_u32.pow(22) || gen_length >= 2_u16.pow(10) {
|
2018-12-23 16:46:49 +01:00
|
|
|
return TestResult::discard()
|
|
|
|
}
|
|
|
|
|
2019-01-09 20:14:08 +01:00
|
|
|
let a = WordArea::new_faillible(gen_char_index, gen_length);
|
|
|
|
let b = WordArea::new_faillible(gen_char_index + 1, gen_length + 1);
|
2018-12-23 16:46:49 +01:00
|
|
|
|
|
|
|
TestResult::from_bool(a < b)
|
2018-05-27 15:23:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|