-
-
Notifications
You must be signed in to change notification settings - Fork 709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
apply gcd on fastfield as preprocessing #1418
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7e032a9
apply gcd on fastfield as preprocessing
PSeitz 5f966d7
Apply suggestions from code review
PSeitz 90e296f
fix var name
PSeitz fff1a03
replace generic with impl T
PSeitz 2e0a7d0
use single pass for gcd
PSeitz 8dac30e
fix benchmark
PSeitz 06fd868
use filter to filter zero
PSeitz 6a9d09c
handle gcd like a composable codec
PSeitz ce8d6b2
early return
PSeitz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
use std::io::{self, Write}; | ||
|
||
use common::BinarySerializable; | ||
use fastdivide::DividerU64; | ||
use fastfield_codecs::FastFieldCodecReader; | ||
use gcd::Gcd; | ||
|
||
pub const GCD_DEFAULT: u64 = 1; | ||
pub const GCD_CODEC_ID: u8 = 4; | ||
|
||
/// Wrapper for accessing a fastfield. | ||
/// | ||
/// Holds the data and the codec to the read the data. | ||
#[derive(Clone)] | ||
pub struct GCDFastFieldCodec<CodecReader> { | ||
gcd: u64, | ||
min_value: u64, | ||
reader: CodecReader, | ||
} | ||
impl<C: FastFieldCodecReader + Clone> FastFieldCodecReader for GCDFastFieldCodec<C> { | ||
/// Opens a fast field given the bytes. | ||
fn open_from_bytes(bytes: &[u8]) -> std::io::Result<Self> { | ||
let (header, mut footer) = bytes.split_at(bytes.len() - 16); | ||
let gcd = u64::deserialize(&mut footer)?; | ||
let min_value = u64::deserialize(&mut footer)?; | ||
let reader = C::open_from_bytes(header)?; | ||
|
||
Ok(GCDFastFieldCodec { | ||
gcd, | ||
min_value, | ||
reader, | ||
}) | ||
} | ||
|
||
#[inline] | ||
fn get_u64(&self, doc: u64, data: &[u8]) -> u64 { | ||
let mut data = self.reader.get_u64(doc, data); | ||
data *= self.gcd; | ||
data += self.min_value; | ||
data | ||
} | ||
|
||
fn min_value(&self) -> u64 { | ||
self.min_value + self.reader.min_value() * self.gcd | ||
} | ||
|
||
fn max_value(&self) -> u64 { | ||
self.min_value + self.reader.max_value() * self.gcd | ||
} | ||
} | ||
|
||
pub fn write_gcd_header<W: Write>(field_write: &mut W, min_value: u64, gcd: u64) -> io::Result<()> { | ||
gcd.serialize(field_write)?; | ||
min_value.serialize(field_write)?; | ||
Ok(()) | ||
} | ||
|
||
// Find GCD for iterator of numbers | ||
pub fn find_gcd(numbers: impl Iterator<Item = u64>) -> Option<u64> { | ||
let mut numbers = numbers.filter(|n| *n != 0); | ||
let mut gcd = numbers.next()?; | ||
if gcd == 1 { | ||
return Some(1); | ||
} | ||
|
||
let mut gcd_divider = DividerU64::divide_by(gcd); | ||
for val in numbers { | ||
let remainder = val - (gcd_divider.divide(val)) * gcd; | ||
if remainder == 0 { | ||
continue; | ||
} | ||
gcd = gcd.gcd(val); | ||
if gcd == 1 { | ||
return Some(1); | ||
} | ||
|
||
gcd_divider = DividerU64::divide_by(gcd); | ||
} | ||
Some(gcd) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::collections::HashMap; | ||
use std::path::Path; | ||
|
||
use common::HasLen; | ||
|
||
use crate::directory::{CompositeFile, RamDirectory, WritePtr}; | ||
use crate::fastfield::serializer::FastFieldCodecEnableCheck; | ||
use crate::fastfield::tests::{FIELD, FIELDI64, SCHEMA, SCHEMAI64}; | ||
use crate::fastfield::{ | ||
find_gcd, CompositeFastFieldSerializer, DynamicFastFieldReader, FastFieldCodecName, | ||
FastFieldReader, FastFieldsWriter, ALL_CODECS, | ||
}; | ||
use crate::schema::Schema; | ||
use crate::Directory; | ||
|
||
fn get_index( | ||
docs: &[crate::Document], | ||
schema: &Schema, | ||
codec_enable_checker: FastFieldCodecEnableCheck, | ||
) -> crate::Result<RamDirectory> { | ||
let directory: RamDirectory = RamDirectory::create(); | ||
{ | ||
let write: WritePtr = directory.open_write(Path::new("test")).unwrap(); | ||
let mut serializer = | ||
CompositeFastFieldSerializer::from_write_with_codec(write, codec_enable_checker) | ||
.unwrap(); | ||
let mut fast_field_writers = FastFieldsWriter::from_schema(schema); | ||
for doc in docs { | ||
fast_field_writers.add_document(doc); | ||
} | ||
fast_field_writers | ||
.serialize(&mut serializer, &HashMap::new(), None) | ||
.unwrap(); | ||
serializer.close().unwrap(); | ||
} | ||
Ok(directory) | ||
} | ||
|
||
fn test_fastfield_gcd_i64_with_codec( | ||
codec_name: FastFieldCodecName, | ||
num_vals: usize, | ||
) -> crate::Result<()> { | ||
let path = Path::new("test"); | ||
let mut docs = vec![]; | ||
for i in 1..=num_vals { | ||
let val = i as i64 * 1000i64; | ||
docs.push(doc!(*FIELDI64=>val)); | ||
} | ||
let directory = get_index(&docs, &SCHEMAI64, codec_name.clone().into())?; | ||
let file = directory.open_read(path).unwrap(); | ||
// assert_eq!(file.len(), 118); | ||
let composite_file = CompositeFile::open(&file)?; | ||
let file = composite_file.open_read(*FIELD).unwrap(); | ||
let fast_field_reader = DynamicFastFieldReader::<i64>::open(file)?; | ||
assert_eq!(fast_field_reader.get(0), 1000i64); | ||
assert_eq!(fast_field_reader.get(1), 2000i64); | ||
assert_eq!(fast_field_reader.get(2), 3000i64); | ||
assert_eq!(fast_field_reader.max_value(), num_vals as i64 * 1000); | ||
assert_eq!(fast_field_reader.min_value(), 1000i64); | ||
let file = directory.open_read(path).unwrap(); | ||
|
||
// Can't apply gcd | ||
let path = Path::new("test"); | ||
docs.pop(); | ||
docs.push(doc!(*FIELDI64=>2001i64)); | ||
let directory = get_index(&docs, &SCHEMAI64, codec_name.into())?; | ||
let file2 = directory.open_read(path).unwrap(); | ||
assert!(file2.len() > file.len()); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_fastfield_gcd_i64() -> crate::Result<()> { | ||
for codec_name in ALL_CODECS { | ||
test_fastfield_gcd_i64_with_codec(codec_name.clone(), 5005)?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn test_fastfield_gcd_u64_with_codec( | ||
codec_name: FastFieldCodecName, | ||
num_vals: usize, | ||
) -> crate::Result<()> { | ||
let path = Path::new("test"); | ||
let mut docs = vec![]; | ||
for i in 1..=num_vals { | ||
let val = i as u64 * 1000u64; | ||
docs.push(doc!(*FIELD=>val)); | ||
} | ||
let directory = get_index(&docs, &SCHEMA, codec_name.clone().into())?; | ||
let file = directory.open_read(path).unwrap(); | ||
// assert_eq!(file.len(), 118); | ||
let composite_file = CompositeFile::open(&file)?; | ||
let file = composite_file.open_read(*FIELD).unwrap(); | ||
let fast_field_reader = DynamicFastFieldReader::<u64>::open(file)?; | ||
assert_eq!(fast_field_reader.get(0), 1000u64); | ||
assert_eq!(fast_field_reader.get(1), 2000u64); | ||
assert_eq!(fast_field_reader.get(2), 3000u64); | ||
assert_eq!(fast_field_reader.max_value(), num_vals as u64 * 1000); | ||
assert_eq!(fast_field_reader.min_value(), 1000u64); | ||
let file = directory.open_read(path).unwrap(); | ||
|
||
// Can't apply gcd | ||
let path = Path::new("test"); | ||
docs.pop(); | ||
docs.push(doc!(*FIELDI64=>2001u64)); | ||
let directory = get_index(&docs, &SCHEMA, codec_name.into())?; | ||
let file2 = directory.open_read(path).unwrap(); | ||
assert!(file2.len() > file.len()); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_fastfield_gcd_u64() -> crate::Result<()> { | ||
for codec_name in ALL_CODECS { | ||
test_fastfield_gcd_u64_with_codec(codec_name.clone(), 5005)?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
pub fn test_fastfield2() { | ||
let test_fastfield = DynamicFastFieldReader::<u64>::from(vec![100, 200, 300]); | ||
assert_eq!(test_fastfield.get(0), 100); | ||
assert_eq!(test_fastfield.get(1), 200); | ||
assert_eq!(test_fastfield.get(2), 300); | ||
} | ||
|
||
#[test] | ||
fn find_gcd_test() { | ||
assert_eq!(find_gcd([0].into_iter()), None); | ||
assert_eq!(find_gcd([0, 10].into_iter()), Some(10)); | ||
assert_eq!(find_gcd([10, 0].into_iter()), Some(10)); | ||
assert_eq!(find_gcd([].into_iter()), None); | ||
assert_eq!(find_gcd([15, 30, 5, 10].into_iter()), Some(5)); | ||
assert_eq!(find_gcd([15, 16, 10].into_iter()), Some(1)); | ||
assert_eq!(find_gcd([0, 5, 5, 5].into_iter()), Some(5)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great you had a i64 test.