From 3911fd64b5904e6851dc7f6f76a5582c3e90a44f Mon Sep 17 00:00:00 2001 From: Elbert Ronnie Date: Sun, 30 Oct 2022 03:27:30 +0530 Subject: [PATCH 1/2] Implement Uuid codec for heed --- index-scheduler/src/index_mapper.rs | 30 +++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/index-scheduler/src/index_mapper.rs b/index-scheduler/src/index_mapper.rs index 80e4127c0..52b269b06 100644 --- a/index-scheduler/src/index_mapper.rs +++ b/index-scheduler/src/index_mapper.rs @@ -1,12 +1,16 @@ +use std::borrow::Cow; use std::collections::hash_map::Entry; use std::collections::HashMap; +use std::convert::TryInto; use std::path::{Path, PathBuf}; use std::sync::{Arc, RwLock}; use std::{fs, thread}; use log::error; -use meilisearch_types::heed::types::{SerdeBincode, Str}; -use meilisearch_types::heed::{Database, Env, EnvOpenOptions, RoTxn, RwTxn}; +use meilisearch_types::heed::types::Str; +use meilisearch_types::heed::{ + BytesDecode, BytesEncode, Database, Env, EnvOpenOptions, RoTxn, RwTxn, +}; use meilisearch_types::milli::update::IndexerConfig; use meilisearch_types::milli::Index; use uuid::Uuid; @@ -28,9 +32,8 @@ pub struct IndexMapper { /// Keep track of the opened indexes. Used mainly by the index resolver. index_map: Arc>>, - // TODO create a UUID Codec that uses the 16 bytes representation /// Map an index name with an index uuid currently available on disk. - pub(crate) index_mapping: Database>, + pub(crate) index_mapping: Database, /// Path to the folder where the LMDB environments of each index are. base_path: PathBuf, @@ -228,3 +231,22 @@ impl IndexMapper { &self.indexer_config } } + +/// A heed codec for value of struct Uuid. +pub struct UuidCodec; + +impl<'a> BytesDecode<'a> for UuidCodec { + type DItem = Uuid; + + fn bytes_decode(bytes: &'a [u8]) -> Option { + bytes.try_into().ok().map(Uuid::from_bytes) + } +} + +impl BytesEncode<'_> for UuidCodec { + type EItem = Uuid; + + fn bytes_encode(item: &Self::EItem) -> Option> { + Some(Cow::Borrowed(item.as_bytes())) + } +} From 0219ef25fe607fb0e229a3b0fc131a9cdb456719 Mon Sep 17 00:00:00 2001 From: Elbert Ronnie Date: Mon, 31 Oct 2022 12:25:19 +0530 Subject: [PATCH 2/2] Moved the struct UuidCodec to a new file --- index-scheduler/src/index_mapper.rs | 26 ++------------------------ index-scheduler/src/lib.rs | 1 + index-scheduler/src/uuid_codec.rs | 24 ++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 24 deletions(-) create mode 100644 index-scheduler/src/uuid_codec.rs diff --git a/index-scheduler/src/index_mapper.rs b/index-scheduler/src/index_mapper.rs index 52b269b06..b75267927 100644 --- a/index-scheduler/src/index_mapper.rs +++ b/index-scheduler/src/index_mapper.rs @@ -1,21 +1,18 @@ -use std::borrow::Cow; use std::collections::hash_map::Entry; use std::collections::HashMap; -use std::convert::TryInto; use std::path::{Path, PathBuf}; use std::sync::{Arc, RwLock}; use std::{fs, thread}; use log::error; use meilisearch_types::heed::types::Str; -use meilisearch_types::heed::{ - BytesDecode, BytesEncode, Database, Env, EnvOpenOptions, RoTxn, RwTxn, -}; +use meilisearch_types::heed::{Database, Env, EnvOpenOptions, RoTxn, RwTxn}; use meilisearch_types::milli::update::IndexerConfig; use meilisearch_types::milli::Index; use uuid::Uuid; use self::IndexStatus::{Available, BeingDeleted}; +use crate::uuid_codec::UuidCodec; use crate::{Error, Result}; const INDEX_MAPPING: &str = "index-mapping"; @@ -231,22 +228,3 @@ impl IndexMapper { &self.indexer_config } } - -/// A heed codec for value of struct Uuid. -pub struct UuidCodec; - -impl<'a> BytesDecode<'a> for UuidCodec { - type DItem = Uuid; - - fn bytes_decode(bytes: &'a [u8]) -> Option { - bytes.try_into().ok().map(Uuid::from_bytes) - } -} - -impl BytesEncode<'_> for UuidCodec { - type EItem = Uuid; - - fn bytes_encode(item: &Self::EItem) -> Option> { - Some(Cow::Borrowed(item.as_bytes())) - } -} diff --git a/index-scheduler/src/lib.rs b/index-scheduler/src/lib.rs index a25f74a69..1807bdb40 100644 --- a/index-scheduler/src/lib.rs +++ b/index-scheduler/src/lib.rs @@ -25,6 +25,7 @@ mod index_mapper; #[cfg(test)] mod insta_snapshot; mod utils; +mod uuid_codec; pub type Result = std::result::Result; pub type TaskId = u32; diff --git a/index-scheduler/src/uuid_codec.rs b/index-scheduler/src/uuid_codec.rs new file mode 100644 index 000000000..70a92ca94 --- /dev/null +++ b/index-scheduler/src/uuid_codec.rs @@ -0,0 +1,24 @@ +use std::borrow::Cow; +use std::convert::TryInto; + +use meilisearch_types::heed::{BytesDecode, BytesEncode}; +use uuid::Uuid; + +/// A heed codec for value of struct Uuid. +pub struct UuidCodec; + +impl<'a> BytesDecode<'a> for UuidCodec { + type DItem = Uuid; + + fn bytes_decode(bytes: &'a [u8]) -> Option { + bytes.try_into().ok().map(Uuid::from_bytes) + } +} + +impl BytesEncode<'_> for UuidCodec { + type EItem = Uuid; + + fn bytes_encode(item: &Self::EItem) -> Option> { + Some(Cow::Borrowed(item.as_bytes())) + } +}