2020-06-23 01:04:10 +08:00
|
|
|
use std::collections::hash_map::Entry;
|
2020-06-29 19:54:47 +08:00
|
|
|
use std::collections::{HashMap, BTreeSet};
|
2020-06-23 01:04:10 +08:00
|
|
|
use std::convert::{TryFrom, TryInto};
|
2020-07-01 23:24:55 +08:00
|
|
|
use std::hash::{Hash, BuildHasher};
|
2020-07-04 18:34:10 +08:00
|
|
|
use std::io;
|
2020-07-02 04:45:43 +08:00
|
|
|
use std::iter::FromIterator;
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::time::Instant;
|
|
|
|
|
|
|
|
use anyhow::{ensure, Context};
|
|
|
|
use fst::{Streamer, set::OpBuilder};
|
2020-05-30 21:35:33 +08:00
|
|
|
use heed::types::*;
|
2020-07-02 04:45:43 +08:00
|
|
|
use heed::{Env, EnvOpenOptions};
|
|
|
|
use rayon::prelude::*;
|
2020-06-30 01:48:02 +08:00
|
|
|
use roaring::RoaringBitmap;
|
2020-06-05 02:25:51 +08:00
|
|
|
use slice_group_by::StrGroupBy;
|
2020-05-26 02:39:53 +08:00
|
|
|
use structopt::StructOpt;
|
2020-07-02 04:45:43 +08:00
|
|
|
use tempfile::TempDir;
|
2020-05-26 02:39:53 +08:00
|
|
|
|
2020-06-30 01:48:02 +08:00
|
|
|
use mega_mini_indexer::cache::ArcCache;
|
2020-07-01 17:19:40 +08:00
|
|
|
use mega_mini_indexer::{BEU32, Index, DocumentId, FastMap4};
|
2020-06-05 22:32:14 +08:00
|
|
|
|
2020-07-02 04:45:43 +08:00
|
|
|
const ONE_MILLION: u32 = 1_000_000;
|
2020-06-05 22:32:14 +08:00
|
|
|
const MAX_POSITION: usize = 1000;
|
|
|
|
const MAX_ATTRIBUTES: usize = u32::max_value() as usize / MAX_POSITION;
|
2020-05-26 02:39:53 +08:00
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
#[global_allocator]
|
|
|
|
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
|
|
|
|
|
2020-06-05 02:25:51 +08:00
|
|
|
pub fn simple_alphanumeric_tokens(string: &str) -> impl Iterator<Item = &str> {
|
|
|
|
let is_alphanumeric = |s: &&str| s.chars().next().map_or(false, char::is_alphanumeric);
|
|
|
|
string.linear_group_by_key(|c| c.is_alphanumeric()).filter(is_alphanumeric)
|
|
|
|
}
|
|
|
|
|
2020-05-26 02:39:53 +08:00
|
|
|
#[derive(Debug, StructOpt)]
|
2020-06-05 00:19:52 +08:00
|
|
|
#[structopt(name = "mm-indexer", about = "The indexer side of the MMI project.")]
|
2020-05-26 02:39:53 +08:00
|
|
|
struct Opt {
|
|
|
|
/// The database path where the database is located.
|
|
|
|
/// It is created if it doesn't already exist.
|
|
|
|
#[structopt(long = "db", parse(from_os_str))]
|
|
|
|
database: PathBuf,
|
|
|
|
|
2020-07-05 00:12:41 +08:00
|
|
|
/// The number of words that can fit in cache, the bigger this number is the less
|
|
|
|
/// the indexer will touch the databases on disk but the more it uses memory.
|
|
|
|
#[structopt(long, default_value = "100000")]
|
|
|
|
arc_cache_size: usize,
|
|
|
|
|
2020-06-29 19:54:47 +08:00
|
|
|
/// CSV file to index.
|
2020-07-04 18:34:10 +08:00
|
|
|
csv_file: PathBuf,
|
2020-05-30 21:35:33 +08:00
|
|
|
}
|
|
|
|
|
2020-07-01 23:24:55 +08:00
|
|
|
fn put_evicted_into_heed<I>(wtxn: &mut heed::RwTxn, index: &Index, iter: I) -> anyhow::Result<()>
|
|
|
|
where
|
|
|
|
I: IntoIterator<Item = (String, (RoaringBitmap, FastMap4<u32, RoaringBitmap>))>
|
|
|
|
{
|
|
|
|
for (word, (positions, positions_docids)) in iter {
|
|
|
|
index.word_positions.put(wtxn, &word, &positions)?;
|
|
|
|
|
|
|
|
for (position, docids) in positions_docids {
|
|
|
|
let mut key = word.as_bytes().to_vec();
|
|
|
|
key.extend_from_slice(&position.to_be_bytes());
|
|
|
|
index.word_position_docids.put(wtxn, &key, &docids)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn merge_hashmaps<K, V, S, F>(mut a: HashMap<K, V, S>, mut b: HashMap<K, V, S>, mut merge: F) -> HashMap<K, V, S>
|
|
|
|
where
|
|
|
|
K: Hash + Eq,
|
|
|
|
S: BuildHasher,
|
|
|
|
F: FnMut(&K, &mut V, V)
|
|
|
|
{
|
|
|
|
for (k, v) in a.iter_mut() {
|
|
|
|
if let Some(vb) = b.remove(k) {
|
|
|
|
(merge)(k, v, vb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
a.extend(b);
|
|
|
|
|
|
|
|
a
|
|
|
|
}
|
|
|
|
|
2020-07-01 23:49:46 +08:00
|
|
|
fn index_csv<R: io::Read>(
|
|
|
|
wtxn: &mut heed::RwTxn,
|
|
|
|
mut rdr: csv::Reader<R>,
|
|
|
|
index: &Index,
|
2020-07-05 00:12:41 +08:00
|
|
|
arc_cache_size: usize,
|
2020-07-01 23:49:46 +08:00
|
|
|
num_threads: usize,
|
|
|
|
thread_index: usize,
|
|
|
|
) -> anyhow::Result<()>
|
|
|
|
{
|
2020-06-29 19:54:47 +08:00
|
|
|
eprintln!("Indexing into LMDB...");
|
2020-05-26 02:39:53 +08:00
|
|
|
|
2020-07-05 00:12:41 +08:00
|
|
|
let mut words_cache = ArcCache::<_, (RoaringBitmap, FastMap4<_, RoaringBitmap>)>::new(arc_cache_size);
|
2020-06-30 00:15:03 +08:00
|
|
|
|
2020-05-26 02:39:53 +08:00
|
|
|
// Write the headers into a Vec of bytes.
|
|
|
|
let headers = rdr.headers()?;
|
|
|
|
let mut writer = csv::WriterBuilder::new().has_headers(false).from_writer(Vec::new());
|
|
|
|
writer.write_byte_record(headers.as_byte_record())?;
|
|
|
|
let headers = writer.into_inner()?;
|
|
|
|
|
2020-07-02 04:45:43 +08:00
|
|
|
let mut document_id = 0usize;
|
|
|
|
let mut before = Instant::now();
|
2020-06-29 19:54:47 +08:00
|
|
|
let mut document = csv::StringRecord::new();
|
|
|
|
|
2020-05-26 02:39:53 +08:00
|
|
|
while rdr.read_record(&mut document)? {
|
2020-07-02 04:45:43 +08:00
|
|
|
document_id = document_id + 1;
|
2020-05-30 21:35:33 +08:00
|
|
|
let document_id = DocumentId::try_from(document_id).context("Generated id is too big")?;
|
2020-05-26 02:39:53 +08:00
|
|
|
|
2020-07-02 04:45:43 +08:00
|
|
|
if thread_index == 0 && document_id % ONE_MILLION == 0 {
|
|
|
|
eprintln!("Document {}m just processed ({:.02?} elapsed).", document_id / ONE_MILLION, before.elapsed());
|
|
|
|
before = Instant::now();
|
|
|
|
}
|
|
|
|
|
2020-06-05 22:32:14 +08:00
|
|
|
for (attr, content) in document.iter().enumerate().take(MAX_ATTRIBUTES) {
|
2020-06-06 02:12:52 +08:00
|
|
|
for (pos, word) in simple_alphanumeric_tokens(&content).enumerate().take(MAX_POSITION) {
|
2020-05-30 21:35:33 +08:00
|
|
|
if !word.is_empty() && word.len() < 500 { // LMDB limits
|
2020-06-30 00:15:03 +08:00
|
|
|
let word = word.to_lowercase(); // TODO cow_to_lowercase
|
2020-06-06 02:12:52 +08:00
|
|
|
let position = (attr * 1000 + pos) as u32;
|
2020-06-05 22:32:14 +08:00
|
|
|
|
2020-07-01 23:49:46 +08:00
|
|
|
// If this indexing process is not concerned by this word, then ignore it.
|
|
|
|
if fxhash::hash32(&word) as usize % num_threads != thread_index { continue; }
|
|
|
|
|
2020-07-01 17:19:40 +08:00
|
|
|
match words_cache.get_mut(&word) {
|
2020-07-01 23:24:55 +08:00
|
|
|
(Some(entry), evicted) => {
|
|
|
|
let (ids, positions) = entry;
|
2020-06-30 01:48:02 +08:00
|
|
|
ids.insert(position);
|
2020-07-01 17:19:40 +08:00
|
|
|
positions.entry(position).or_default().insert(document_id);
|
2020-07-01 23:24:55 +08:00
|
|
|
put_evicted_into_heed(wtxn, index, evicted)?;
|
2020-07-01 17:19:40 +08:00
|
|
|
},
|
2020-07-01 23:24:55 +08:00
|
|
|
(None, _evicted) => {
|
2020-07-01 17:19:40 +08:00
|
|
|
let mut key = word.as_bytes().to_vec();
|
|
|
|
key.extend_from_slice(&position.to_be_bytes());
|
2020-06-29 19:54:47 +08:00
|
|
|
|
2020-07-01 17:19:40 +08:00
|
|
|
let mut words_positions = index.word_positions.get(wtxn, &word)?.unwrap_or_default();
|
|
|
|
let mut words_position_docids = index.word_position_docids.get(wtxn, &key)?.unwrap_or_default();
|
2020-06-29 19:54:47 +08:00
|
|
|
|
2020-07-01 17:19:40 +08:00
|
|
|
words_positions.insert(position);
|
|
|
|
words_position_docids.insert(document_id);
|
2020-06-05 22:32:14 +08:00
|
|
|
|
2020-07-01 17:19:40 +08:00
|
|
|
let mut map = FastMap4::default();
|
|
|
|
map.insert(position, words_position_docids);
|
|
|
|
let value = (words_positions, map);
|
|
|
|
|
2020-07-01 23:24:55 +08:00
|
|
|
let evicted = words_cache.insert(word.clone(), value, |(pa, pda), (pb, pdb)| {
|
|
|
|
(pa | pb, merge_hashmaps(pda, pdb, |_, a, b| RoaringBitmap::union_with(a, &b)))
|
|
|
|
});
|
2020-07-01 17:19:40 +08:00
|
|
|
|
2020-07-01 23:24:55 +08:00
|
|
|
put_evicted_into_heed(wtxn, index, evicted)?;
|
2020-06-30 00:15:03 +08:00
|
|
|
}
|
2020-06-30 01:48:02 +08:00
|
|
|
}
|
2020-05-30 21:35:33 +08:00
|
|
|
}
|
2020-05-26 02:39:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-04 18:34:10 +08:00
|
|
|
if document_id as usize % num_threads == thread_index {
|
2020-07-02 04:45:43 +08:00
|
|
|
// We write the document in the database.
|
|
|
|
let mut writer = csv::WriterBuilder::new().has_headers(false).from_writer(Vec::new());
|
|
|
|
writer.write_byte_record(document.as_byte_record())?;
|
|
|
|
let document = writer.into_inner()?;
|
|
|
|
index.documents.put(wtxn, &BEU32::new(document_id), &document)?;
|
|
|
|
}
|
2020-05-26 02:39:53 +08:00
|
|
|
}
|
|
|
|
|
2020-07-01 23:24:55 +08:00
|
|
|
put_evicted_into_heed(wtxn, index, words_cache)?;
|
2020-06-30 00:15:03 +08:00
|
|
|
|
2020-06-01 00:20:49 +08:00
|
|
|
// We store the words from the postings.
|
2020-05-31 01:56:57 +08:00
|
|
|
let mut new_words = BTreeSet::default();
|
2020-06-29 19:54:47 +08:00
|
|
|
let iter = index.word_positions.as_polymorph().iter::<_, Str, DecodeIgnore>(wtxn)?;
|
|
|
|
for result in iter {
|
|
|
|
let (word, ()) = result?;
|
2020-07-01 16:35:07 +08:00
|
|
|
new_words.insert(word);
|
2020-05-26 02:39:53 +08:00
|
|
|
}
|
|
|
|
|
2020-06-29 19:54:47 +08:00
|
|
|
let new_words_fst = fst::Set::from_iter(new_words)?;
|
2020-05-31 01:56:57 +08:00
|
|
|
|
2020-06-29 19:54:47 +08:00
|
|
|
index.put_fst(wtxn, &new_words_fst)?;
|
|
|
|
index.put_headers(wtxn, &headers)?;
|
2020-05-26 02:39:53 +08:00
|
|
|
|
2020-07-04 23:02:27 +08:00
|
|
|
let before = Instant::now();
|
|
|
|
compute_words_attributes_docids(wtxn, index)?;
|
|
|
|
eprintln!("Computing the attributes documents ids took {:.02?}.", before.elapsed());
|
|
|
|
|
2020-06-02 03:09:32 +08:00
|
|
|
Ok(())
|
2020-05-26 02:39:53 +08:00
|
|
|
}
|
|
|
|
|
2020-06-23 01:04:10 +08:00
|
|
|
fn compute_words_attributes_docids(wtxn: &mut heed::RwTxn, index: &Index) -> anyhow::Result<()> {
|
|
|
|
let fst = match index.fst(&wtxn)? {
|
|
|
|
Some(fst) => fst.map_data(|s| s.to_vec())?,
|
|
|
|
None => return Ok(()),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut word_attributes = HashMap::new();
|
|
|
|
let mut stream = fst.stream();
|
|
|
|
while let Some(word) = stream.next() {
|
|
|
|
word_attributes.clear();
|
|
|
|
|
|
|
|
// Loop on the word attributes and unions all the documents ids by attribute.
|
|
|
|
for result in index.word_position_docids.prefix_iter(wtxn, word)? {
|
|
|
|
let (key, docids) = result?;
|
|
|
|
let (_key_word, key_pos) = key.split_at(key.len() - 4);
|
|
|
|
let key_pos = key_pos.try_into().map(u32::from_be_bytes)?;
|
|
|
|
// If the key corresponds to the word (minus the attribute)
|
|
|
|
if key.len() == word.len() + 4 {
|
|
|
|
let attribute = key_pos / 1000;
|
|
|
|
match word_attributes.entry(attribute) {
|
|
|
|
Entry::Vacant(entry) => { entry.insert(docids); },
|
|
|
|
Entry::Occupied(mut entry) => entry.get_mut().union_with(&docids),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write this word attributes unions into LMDB.
|
|
|
|
let mut key = word.to_vec();
|
|
|
|
for (attribute, docids) in word_attributes.drain() {
|
|
|
|
key.truncate(word.len());
|
|
|
|
key.extend_from_slice(&attribute.to_be_bytes());
|
|
|
|
index.word_attribute_docids.put(wtxn, &key, &docids)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-07-04 18:34:10 +08:00
|
|
|
use std::collections::binary_heap::{BinaryHeap, PeekMut};
|
|
|
|
use std::cmp::{Ordering, Reverse};
|
|
|
|
|
|
|
|
// ------------ Value
|
|
|
|
|
|
|
|
struct Value<'t, KC, DC>
|
|
|
|
where
|
|
|
|
KC: heed::BytesDecode<'t>,
|
|
|
|
DC: heed::BytesDecode<'t>,
|
|
|
|
{
|
|
|
|
iter: heed::RoIter<'t, KC, DC>,
|
|
|
|
value: Option<heed::Result<(KC::DItem, DC::DItem)>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'t, KC, DC> Value<'t, KC, DC>
|
|
|
|
where
|
|
|
|
KC: heed::BytesDecode<'t>,
|
|
|
|
DC: heed::BytesDecode<'t>,
|
|
|
|
{
|
|
|
|
fn new(mut iter: heed::RoIter<'t, KC, DC>) -> Option<Value<'t, KC, DC>> {
|
|
|
|
iter.next().map(|value| Value { iter, value: Some(value) })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn peek_value(&mut self) -> Option<heed::Result<(KC::DItem, DC::DItem)>> {
|
|
|
|
std::mem::replace(&mut self.value, self.iter.next())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'t, KC, DC> Ord for Value<'t, KC, DC>
|
|
|
|
where
|
|
|
|
KC: heed::BytesDecode<'t>,
|
|
|
|
DC: heed::BytesDecode<'t>,
|
|
|
|
KC::DItem: Ord,
|
|
|
|
{
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
let a = self.value.as_ref().unwrap();
|
|
|
|
let b = other.value.as_ref().unwrap();
|
|
|
|
match (a, b) {
|
|
|
|
(Ok((a, _)), Ok((b, _))) => a.cmp(&b),
|
|
|
|
(Err(_), Err(_)) => Ordering::Equal,
|
|
|
|
(Err(_), _) => Ordering::Less,
|
|
|
|
(_, Err(_)) => Ordering::Greater,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'t, KC, DC> Eq for Value<'t, KC, DC>
|
|
|
|
where
|
|
|
|
KC: heed::BytesDecode<'t>,
|
|
|
|
DC: heed::BytesDecode<'t>,
|
|
|
|
KC::DItem: Ord,
|
|
|
|
{ }
|
|
|
|
|
|
|
|
impl<'t, KC, DC> PartialEq for Value<'t, KC, DC>
|
|
|
|
where
|
|
|
|
KC: heed::BytesDecode<'t>,
|
|
|
|
DC: heed::BytesDecode<'t>,
|
|
|
|
KC::DItem: Ord,
|
|
|
|
{
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.cmp(other) == Ordering::Equal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'t, KC, DC> PartialOrd for Value<'t, KC, DC>
|
|
|
|
where
|
|
|
|
KC: heed::BytesDecode<'t>,
|
|
|
|
DC: heed::BytesDecode<'t>,
|
|
|
|
KC::DItem: Ord,
|
|
|
|
{
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------
|
|
|
|
|
|
|
|
struct MergeIter<'t, KC, DC>
|
|
|
|
where
|
|
|
|
KC: heed::BytesDecode<'t>,
|
|
|
|
DC: heed::BytesDecode<'t>,
|
|
|
|
{
|
|
|
|
iters: BinaryHeap<Reverse<Value<'t, KC, DC>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'t, KC, DC> MergeIter<'t, KC, DC>
|
|
|
|
where
|
|
|
|
KC: heed::BytesDecode<'t>,
|
|
|
|
DC: heed::BytesDecode<'t>,
|
|
|
|
KC::DItem: Ord,
|
|
|
|
{
|
|
|
|
fn new(iters: Vec<heed::RoIter<'t, KC, DC>>) -> MergeIter<'t, KC, DC> {
|
|
|
|
let iters = iters.into_iter().filter_map(Value::new).map(Reverse).collect();
|
|
|
|
MergeIter { iters }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'t, KC, DC> Iterator for MergeIter<'t, KC, DC>
|
|
|
|
where
|
|
|
|
KC: heed::BytesDecode<'t>,
|
|
|
|
DC: heed::BytesDecode<'t>,
|
|
|
|
KC::DItem: Ord,
|
|
|
|
{
|
|
|
|
type Item = heed::Result<(KC::DItem, DC::DItem)>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
let mut peek = self.iters.peek_mut()?;
|
|
|
|
let result = peek.0.peek_value().unwrap();
|
|
|
|
|
|
|
|
if peek.0.value.is_none() {
|
|
|
|
PeekMut::pop(peek);
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 04:45:43 +08:00
|
|
|
fn merge_databases(
|
2020-07-04 18:34:10 +08:00
|
|
|
others: Vec<(TempDir, Env, Index)>,
|
2020-07-02 04:45:43 +08:00
|
|
|
wtxn: &mut heed::RwTxn,
|
|
|
|
index: &Index,
|
|
|
|
) -> anyhow::Result<()>
|
|
|
|
{
|
|
|
|
eprintln!("Merging the temporary databases...");
|
|
|
|
|
2020-07-04 18:34:10 +08:00
|
|
|
let rtxns: Result<Vec<_>, _> = others.iter().map(|(_, env, _)| env.read_txn()).collect();
|
|
|
|
let rtxns = rtxns?;
|
|
|
|
|
|
|
|
// merge the word positions
|
|
|
|
let sources: Result<Vec<_>, _> = others.iter().zip(&rtxns).map(|((.., i), t)| i.word_positions.iter(t)).collect();
|
|
|
|
let sources = sources?;
|
|
|
|
let mut dest = index.word_positions.iter_mut(wtxn)?;
|
|
|
|
let before = Instant::now();
|
|
|
|
for result in MergeIter::new(sources) {
|
|
|
|
let (k, v) = result?;
|
|
|
|
dest.append(&k, &v)?;
|
|
|
|
}
|
|
|
|
eprintln!("Merging the word_positions database took {:.02?}.", before.elapsed());
|
|
|
|
drop(dest);
|
|
|
|
|
|
|
|
// merge the word position documents ids
|
|
|
|
let sources: Result<Vec<_>, _> = others.iter().zip(&rtxns).map(|((.., i), t)| i.word_position_docids.iter(t)).collect();
|
|
|
|
let sources = sources?;
|
|
|
|
let mut dest = index.word_position_docids.iter_mut(wtxn)?;
|
|
|
|
let before = Instant::now();
|
|
|
|
for result in MergeIter::new(sources) {
|
|
|
|
let (k, v) = result?;
|
|
|
|
dest.append(&k, &v)?;
|
|
|
|
}
|
|
|
|
eprintln!("Merging the word_position_docids database took {:.02?}.", before.elapsed());
|
|
|
|
drop(dest);
|
|
|
|
|
2020-07-04 23:02:27 +08:00
|
|
|
// merge the word attribute documents ids
|
|
|
|
let sources: Result<Vec<_>, _> = others.iter().zip(&rtxns).map(|((.., i), t)| i.word_attribute_docids.iter(t)).collect();
|
|
|
|
let sources = sources?;
|
|
|
|
let mut dest = index.word_attribute_docids.iter_mut(wtxn)?;
|
|
|
|
|
|
|
|
let before = Instant::now();
|
|
|
|
let mut current = None as Option<(&[u8], RoaringBitmap)>;
|
|
|
|
for result in MergeIter::new(sources) {
|
|
|
|
let (k, v) = result?;
|
|
|
|
match current.as_mut() {
|
|
|
|
Some((ck, cv)) if ck == &k => cv.union_with(&v),
|
|
|
|
Some((ck, cv)) => {
|
|
|
|
dest.append(&ck, &cv)?;
|
|
|
|
current = Some((k, v));
|
|
|
|
},
|
|
|
|
None => current = Some((k, v)),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some((ck, cv)) = current.take() {
|
|
|
|
dest.append(&ck, &cv)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
eprintln!("Merging the word_attribute_docids database took {:.02?}.", before.elapsed());
|
|
|
|
drop(dest);
|
|
|
|
|
2020-07-04 18:34:10 +08:00
|
|
|
// merge the documents
|
|
|
|
let sources: Result<Vec<_>, _> = others.iter().zip(&rtxns).map(|((.., i), t)| {
|
|
|
|
i.documents.as_polymorph().iter::<_, ByteSlice, ByteSlice>(t)
|
|
|
|
}).collect();
|
|
|
|
let sources = sources?;
|
|
|
|
let mut dest = index.documents.as_polymorph().iter_mut::<_, ByteSlice, ByteSlice>(wtxn)?;
|
|
|
|
let before = Instant::now();
|
|
|
|
for result in MergeIter::new(sources) {
|
|
|
|
let (k, v) = result?;
|
|
|
|
dest.append(&k, &v)?;
|
|
|
|
}
|
|
|
|
eprintln!("Merging the documents database took {:.02?}.", before.elapsed());
|
|
|
|
drop(dest);
|
2020-07-02 04:45:43 +08:00
|
|
|
|
2020-07-04 18:34:10 +08:00
|
|
|
let mut fsts = Vec::new();
|
|
|
|
for ((_dir, _env, oindex), rtxn) in others.into_iter().zip(&rtxns) {
|
2020-07-02 04:45:43 +08:00
|
|
|
// merge and check the headers are equal
|
|
|
|
let headers = oindex.headers(&rtxn)?.context("A database is missing the headers")?;
|
|
|
|
match index.headers(wtxn)? {
|
|
|
|
Some(h) => ensure!(h == headers, "headers are not equal"),
|
|
|
|
None => index.put_headers(wtxn, &headers)?,
|
|
|
|
};
|
|
|
|
|
|
|
|
// retrieve the FSTs to merge them together in one run.
|
|
|
|
let fst = oindex.fst(&rtxn)?.context("A database is missing its FST")?;
|
|
|
|
let fst = fst.map_data(|s| s.to_vec())?;
|
|
|
|
fsts.push(fst);
|
|
|
|
}
|
|
|
|
|
2020-07-04 18:34:10 +08:00
|
|
|
let before = Instant::now();
|
|
|
|
|
2020-07-02 04:45:43 +08:00
|
|
|
// Merge all the FSTs to create a final one and write it in the final database.
|
|
|
|
if let Some(fst) = index.fst(wtxn)? {
|
|
|
|
let fst = fst.map_data(|s| s.to_vec())?;
|
|
|
|
fsts.push(fst);
|
|
|
|
}
|
|
|
|
|
|
|
|
let builder = OpBuilder::from_iter(&fsts);
|
|
|
|
let op = builder.r#union();
|
|
|
|
let mut builder = fst::set::SetBuilder::memory();
|
|
|
|
builder.extend_stream(op)?;
|
|
|
|
let fst = builder.into_set();
|
|
|
|
|
|
|
|
index.put_fst(wtxn, &fst)?;
|
|
|
|
|
2020-07-04 18:34:10 +08:00
|
|
|
eprintln!("Merging the FSTs took {:.02?}.", before.elapsed());
|
|
|
|
|
2020-07-02 04:45:43 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn open_env_index(path: impl AsRef<Path>) -> anyhow::Result<(Env, Index)> {
|
2020-05-30 21:35:33 +08:00
|
|
|
let env = EnvOpenOptions::new()
|
|
|
|
.map_size(100 * 1024 * 1024 * 1024) // 100 GB
|
|
|
|
.max_readers(10)
|
2020-06-23 01:04:10 +08:00
|
|
|
.max_dbs(10)
|
2020-07-02 04:45:43 +08:00
|
|
|
.open(path)?;
|
2020-05-26 18:18:29 +08:00
|
|
|
|
2020-06-01 00:20:49 +08:00
|
|
|
let index = Index::new(&env)?;
|
2020-06-02 00:27:26 +08:00
|
|
|
|
2020-07-02 04:45:43 +08:00
|
|
|
Ok((env, index))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> anyhow::Result<()> {
|
|
|
|
let opt = Opt::from_args();
|
2020-06-29 19:54:47 +08:00
|
|
|
|
2020-07-04 18:34:10 +08:00
|
|
|
std::fs::create_dir_all(&opt.database)?;
|
|
|
|
let (env, index) = open_env_index(&opt.database)?;
|
2020-07-02 04:45:43 +08:00
|
|
|
|
2020-07-04 18:34:10 +08:00
|
|
|
let num_threads = rayon::current_num_threads();
|
2020-06-29 19:54:47 +08:00
|
|
|
|
2020-07-04 18:34:10 +08:00
|
|
|
let result: anyhow::Result<_> =
|
|
|
|
(0..num_threads).into_par_iter().map(|i| {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
let (env, index) = open_env_index(&dir)?;
|
2020-06-29 19:54:47 +08:00
|
|
|
|
2020-07-02 04:45:43 +08:00
|
|
|
let mut wtxn = env.write_txn()?;
|
2020-07-04 18:34:10 +08:00
|
|
|
let rdr = csv::Reader::from_path(&opt.csv_file)?;
|
2020-07-05 00:12:41 +08:00
|
|
|
index_csv(&mut wtxn, rdr, &index, opt.arc_cache_size, num_threads, i)?;
|
2020-07-02 04:45:43 +08:00
|
|
|
|
|
|
|
wtxn.commit()?;
|
|
|
|
|
2020-07-04 18:34:10 +08:00
|
|
|
Ok((dir, env, index))
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let mut wtxn = env.write_txn()?;
|
|
|
|
let parts = result?;
|
|
|
|
merge_databases(parts, &mut wtxn, &index)?;
|
|
|
|
let count = index.documents.len(&wtxn)?;
|
|
|
|
|
|
|
|
wtxn.commit()?;
|
2020-06-29 19:54:47 +08:00
|
|
|
|
2020-07-04 18:34:10 +08:00
|
|
|
eprintln!("Wrote {} documents into LMDB", count);
|
2020-05-26 02:39:53 +08:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|