From 0219ef25fe607fb0e229a3b0fc131a9cdb456719 Mon Sep 17 00:00:00 2001 From: Elbert Ronnie Date: Mon, 31 Oct 2022 12:25:19 +0530 Subject: [PATCH] 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())) + } +}