feat: Create a strong DocumentId type

Forcing it to be something internal will permit to avoid possible miss comparisons to be done with other types.
This commit is contained in:
Clément Renault 2018-12-22 12:00:24 +01:00
parent a756ca5e3f
commit 4ebae7784c
No known key found for this signature in database
GPG key ID: 0151CDAB43460DAE
9 changed files with 46 additions and 29 deletions

View file

@ -201,13 +201,16 @@ impl<W: Write, X: Write> PositiveBlobBuilder<W, X> {
#[cfg(test)]
mod tests {
use super::*;
use std::error::Error;
use crate::DocumentId;
#[test]
fn serialize_deserialize() -> Result<(), Box<Error>> {
let a = DocIndex { document_id: 0, attribute: 3, attribute_index: 11 };
let b = DocIndex { document_id: 1, attribute: 4, attribute_index: 21 };
let c = DocIndex { document_id: 2, attribute: 8, attribute_index: 2 };
let a = DocIndex { document_id: DocumentId(0), attribute: 3, attribute_index: 11 };
let b = DocIndex { document_id: DocumentId(1), attribute: 4, attribute_index: 21 };
let c = DocIndex { document_id: DocumentId(2), attribute: 8, attribute_index: 2 };
let mut builder = PositiveBlobBuilder::memory();
@ -228,9 +231,9 @@ mod tests {
#[test]
fn serde_serialize_deserialize() -> Result<(), Box<Error>> {
let a = DocIndex { document_id: 0, attribute: 3, attribute_index: 11 };
let b = DocIndex { document_id: 1, attribute: 4, attribute_index: 21 };
let c = DocIndex { document_id: 2, attribute: 8, attribute_index: 2 };
let a = DocIndex { document_id: DocumentId(0), attribute: 3, attribute_index: 11 };
let b = DocIndex { document_id: DocumentId(1), attribute: 4, attribute_index: 21 };
let c = DocIndex { document_id: DocumentId(2), attribute: 8, attribute_index: 2 };
let mut builder = PositiveBlobBuilder::memory();

View file

@ -100,7 +100,7 @@ where D: Deref<Target=DB>
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut options = ReadOptions::new();
let lower = DocumentKey::new(0);
let lower = DocumentKey::new(DocumentId(0));
options.set_iterate_lower_bound(lower.as_ref());
let mut iter = self.snapshot.iter_opt(options);

View file

@ -19,7 +19,7 @@ impl DocumentKey {
let mut wtr = Cursor::new(&mut buffer[..]);
wtr.write_all(b"doc-").unwrap();
wtr.write_u64::<NativeEndian>(id).unwrap();
wtr.write_u64::<NativeEndian>(id.0).unwrap();
DocumentKey(buffer)
}
@ -43,7 +43,8 @@ impl DocumentKey {
}
pub fn document_id(&self) -> DocumentId {
(&self.0[4..]).read_u64::<NativeEndian>().unwrap()
let id = (&self.0[4..]).read_u64::<NativeEndian>().unwrap();
DocumentId(id)
}
}
@ -88,7 +89,8 @@ impl DocumentKeyAttr {
}
pub fn document_id(&self) -> DocumentId {
(&self.0[4..]).read_u64::<NativeEndian>().unwrap()
let id = (&self.0[4..]).read_u64::<NativeEndian>().unwrap();
DocumentId(id)
}
pub fn attribute(&self) -> SchemaAttr {

View file

@ -194,6 +194,7 @@ mod tests {
use serde_derive::{Serialize, Deserialize};
use tempfile::tempdir;
use crate::DocumentId;
use crate::tokenizer::DefaultBuilder;
use crate::database::update::PositiveUpdateBuilder;
use crate::database::schema::{SchemaBuilder, STORED, INDEXED};
@ -238,8 +239,8 @@ mod tests {
let mut update = {
let mut builder = PositiveUpdateBuilder::new(update_path, schema, tokenizer_builder);
builder.update(0, &doc0).unwrap();
builder.update(1, &doc1).unwrap();
builder.update(DocumentId(0), &doc0).unwrap();
builder.update(DocumentId(1), &doc1).unwrap();
builder.build()?
};
@ -248,8 +249,8 @@ mod tests {
database.ingest_update_file(update)?;
let view = database.view();
let de_doc0: SimpleDoc = view.retrieve_document(0)?;
let de_doc1: SimpleDoc = view.retrieve_document(1)?;
let de_doc0: SimpleDoc = view.retrieve_document(DocumentId(0))?;
let de_doc1: SimpleDoc = view.retrieve_document(DocumentId(1))?;
assert_eq!(doc0, de_doc0);
assert_eq!(doc1, de_doc1);

View file

@ -30,7 +30,7 @@ impl<W: io::Write> UnorderedNegativeBlobBuilder<W> {
pub fn into_inner(mut self) -> io::Result<W> {
for id in self.doc_ids {
self.wrt.write_u64::<NativeEndian>(id)?;
self.wrt.write_u64::<NativeEndian>(id.0)?;
}
Ok(self.wrt)
}