mirror of
https://github.com/meilisearch/meilisearch.git
synced 2025-07-27 08:41:00 +00:00
chore: Initial commit
This commit is contained in:
65
src/bin/raptor.rs
Normal file
65
src/bin/raptor.rs
Normal file
@ -0,0 +1,65 @@
|
||||
extern crate env_logger;
|
||||
extern crate futures;
|
||||
extern crate raptor;
|
||||
extern crate tokio_minihttp;
|
||||
extern crate tokio_proto;
|
||||
extern crate tokio_service;
|
||||
extern crate url;
|
||||
|
||||
use std::io;
|
||||
|
||||
use futures::future;
|
||||
use tokio_minihttp::{Request, Response, Http};
|
||||
use tokio_proto::TcpServer;
|
||||
use tokio_service::Service;
|
||||
|
||||
use raptor::{MultiMapBuilder, MultiMap};
|
||||
|
||||
struct MainService {
|
||||
map: MultiMap,
|
||||
}
|
||||
|
||||
impl Service for MainService {
|
||||
type Request = Request;
|
||||
type Response = Response;
|
||||
type Error = io::Error;
|
||||
type Future = future::Ok<Response, io::Error>;
|
||||
|
||||
fn call(&self, request: Request) -> Self::Future {
|
||||
|
||||
let url = format!("http://raptor.net{}", request.path());
|
||||
let url = url::Url::parse(&url).unwrap();
|
||||
|
||||
if let Some((_, key)) = url.query_pairs().find(|&(ref k, _)| k == "query") {
|
||||
let values = self.map.get(&*key);
|
||||
println!("{:?}", values);
|
||||
}
|
||||
|
||||
let mut resp = Response::new();
|
||||
resp.body("Hello, world!");
|
||||
future::ok(resp)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
drop(env_logger::init());
|
||||
let addr = "0.0.0.0:8080".parse().unwrap();
|
||||
|
||||
TcpServer::new(Http, addr).serve(|| {
|
||||
|
||||
// TODO move the MultiMap construction out of this
|
||||
// closure, make it global.
|
||||
// It will permit the server to be multithreaded.
|
||||
|
||||
let mut builder = MultiMapBuilder::new();
|
||||
builder.insert("foo", 12);
|
||||
builder.insert("foo", 13);
|
||||
builder.insert("bar", 10);
|
||||
|
||||
let map = builder.build_memory().unwrap();
|
||||
|
||||
println!("Called Fn here !");
|
||||
|
||||
Ok(MainService { map })
|
||||
})
|
||||
}
|
81
src/lib.rs
Normal file
81
src/lib.rs
Normal file
@ -0,0 +1,81 @@
|
||||
extern crate fst;
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
#[macro_use] extern crate serde_derive;
|
||||
extern crate smallvec;
|
||||
|
||||
use std::io::Write;
|
||||
|
||||
pub use fst::MapBuilder;
|
||||
use smallvec::SmallVec;
|
||||
|
||||
type SmallVec16<T> = SmallVec<[T; 16]>;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
struct Product<'a> {
|
||||
product_id: u64,
|
||||
title: &'a str,
|
||||
ft: &'a str,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MultiMap {
|
||||
map: fst::Map,
|
||||
values: Box<[SmallVec16<u64>]>,
|
||||
}
|
||||
|
||||
impl MultiMap {
|
||||
pub fn contains_key<K: AsRef<[u8]>>(&self, key: K) -> bool {
|
||||
self.map.contains_key(key)
|
||||
}
|
||||
|
||||
pub fn get<K: AsRef<[u8]>>(&self, key: K) -> Option<&[u64]> {
|
||||
self.map.get(key).map(|i| &*self.values[i as usize])
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MultiMapBuilder<'a> {
|
||||
map: Vec<(&'a str, u64)>,
|
||||
values: Vec<SmallVec16<u64>>,
|
||||
}
|
||||
|
||||
impl<'a> MultiMapBuilder<'a> {
|
||||
pub fn new() -> MultiMapBuilder<'a> {
|
||||
MultiMapBuilder {
|
||||
map: Vec::new(),
|
||||
values: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, key: &'a str, value: u64) {
|
||||
match self.map.binary_search_by_key(&key, |&(k, _)| k) {
|
||||
Ok(index) => {
|
||||
let (_, index) = self.map[index];
|
||||
let values = &mut self.values[index as usize];
|
||||
if let Err(index) = values.binary_search(&value) {
|
||||
values.insert(index, value)
|
||||
}
|
||||
},
|
||||
Err(index) => {
|
||||
let values = {
|
||||
let mut vec = SmallVec16::new();
|
||||
vec.push(value);
|
||||
vec
|
||||
};
|
||||
self.values.push(values);
|
||||
let values_index = (self.values.len() - 1) as u64;
|
||||
|
||||
let value = (key, values_index);
|
||||
self.map.insert(index, value);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build_memory(self) -> fst::Result<MultiMap> {
|
||||
Ok(MultiMap {
|
||||
map: fst::Map::from_iter(self.map)?,
|
||||
values: self.values.into_boxed_slice(),
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user