mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-25 19:45:05 +08:00
feat: Use BTreeMap
instead of a custom algo
This commit is contained in:
parent
22c26a5dd0
commit
bb08a9842c
27
src/map.rs
27
src/map.rs
@ -2,6 +2,8 @@ use bincode;
|
|||||||
use fst::{self, Automaton};
|
use fst::{self, Automaton};
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use serde::ser::Serialize;
|
use serde::ser::Serialize;
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
use std::collections::btree_map::Entry;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{Write, BufReader};
|
use std::io::{Write, BufReader};
|
||||||
use std::ops::Range;
|
use std::ops::Range;
|
||||||
@ -109,7 +111,7 @@ impl<T> Values<T> {
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct MapBuilder<T> {
|
pub struct MapBuilder<T> {
|
||||||
map: Vec<(String, u64)>,
|
map: BTreeMap<String, u64>,
|
||||||
// This makes many memory indirections but it is only used
|
// This makes many memory indirections but it is only used
|
||||||
// at index time, not kept for query time.
|
// at index time, not kept for query time.
|
||||||
values: Vec<Vec<T>>,
|
values: Vec<Vec<T>>,
|
||||||
@ -118,26 +120,23 @@ pub struct MapBuilder<T> {
|
|||||||
impl<T> MapBuilder<T> {
|
impl<T> MapBuilder<T> {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
map: Vec::new(),
|
map: BTreeMap::new(),
|
||||||
values: Vec::new(),
|
values: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert<S: Into<String>>(&mut self, key: S, value: T) {
|
pub fn insert<S: Into<String>>(&mut self, key: S, value: T) {
|
||||||
let key = key.into();
|
let key = key.into();
|
||||||
match self.map.binary_search_by_key(&key.as_str(), |&(ref k, _)| k) {
|
match self.map.entry(key) {
|
||||||
Ok(index) => {
|
Entry::Vacant(e) => {
|
||||||
let (_, index) = self.map[index];
|
|
||||||
let values = &mut self.values[index as usize];
|
|
||||||
|
|
||||||
values.push(value);
|
|
||||||
},
|
|
||||||
Err(index) => {
|
|
||||||
self.values.push(vec![value]);
|
self.values.push(vec![value]);
|
||||||
let values_index = (self.values.len() - 1) as u64;
|
let index = (self.values.len() - 1) as u64;
|
||||||
|
e.insert(index);
|
||||||
let value = (key, values_index);
|
},
|
||||||
self.map.insert(index, value);
|
Entry::Occupied(e) => {
|
||||||
|
let index = *e.get();
|
||||||
|
let values = &mut self.values[index as usize];
|
||||||
|
values.push(value);
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user