|
| 1 | +use crate::error::AIProxyError; |
| 2 | +use crate::AHNLICH_AI_RESERVED_META_KEY; |
| 3 | +use ahnlich_types::ai::AIModel; |
| 4 | +use ahnlich_types::ai::AIStoreInfo; |
| 5 | +use ahnlich_types::ai::AIStoreType; |
| 6 | +use ahnlich_types::keyval::StoreInput; |
| 7 | +use ahnlich_types::keyval::StoreKey; |
| 8 | +use ahnlich_types::keyval::StoreName; |
| 9 | +use ahnlich_types::keyval::StoreValue; |
| 10 | +use ahnlich_types::metadata::MetadataKey; |
| 11 | +use ahnlich_types::metadata::MetadataValue; |
| 12 | +use flurry::HashMap as ConcurrentHashMap; |
| 13 | +use serde::Deserialize; |
| 14 | +use serde::Serialize; |
| 15 | +use std::collections::HashMap as StdHashMap; |
| 16 | +use std::collections::HashSet as StdHashSet; |
| 17 | +use std::sync::atomic::AtomicBool; |
| 18 | +use std::sync::Arc; |
| 19 | + |
| 20 | +/// Contains all the stores that have been created in memory |
| 21 | +#[derive(Debug)] |
| 22 | +pub struct AIStoreHandler { |
| 23 | + /// Making use of a concurrent hashmap, we should be able to create an engine that manages stores |
| 24 | + stores: AIStores, |
| 25 | + pub write_flag: Arc<AtomicBool>, |
| 26 | +} |
| 27 | + |
| 28 | +pub type AIStores = Arc<ConcurrentHashMap<StoreName, Arc<AIStore>>>; |
| 29 | + |
| 30 | +impl AIStoreHandler { |
| 31 | + pub fn new(write_flag: Arc<AtomicBool>) -> Self { |
| 32 | + Self { |
| 33 | + stores: Arc::new(ConcurrentHashMap::new()), |
| 34 | + write_flag, |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + #[tracing::instrument(skip(self))] |
| 39 | + pub(crate) fn create_store( |
| 40 | + &self, |
| 41 | + store_name: StoreName, |
| 42 | + store_type: AIStoreType, |
| 43 | + model: AIModel, |
| 44 | + ) -> Result<(), AIProxyError> { |
| 45 | + self.stores |
| 46 | + .try_insert( |
| 47 | + store_name.clone(), |
| 48 | + Arc::new(AIStore::create( |
| 49 | + store_type, |
| 50 | + store_name.clone(), |
| 51 | + model.clone(), |
| 52 | + )), |
| 53 | + &self.stores.guard(), |
| 54 | + ) |
| 55 | + .map_err(|_| AIProxyError::StoreAlreadyExists(store_name.clone()))?; |
| 56 | + |
| 57 | + Ok(()) |
| 58 | + } |
| 59 | + |
| 60 | + /// matches LISTSTORES - to return statistics of all stores |
| 61 | + #[tracing::instrument(skip(self))] |
| 62 | + pub(crate) fn list_stores(&self) -> StdHashSet<AIStoreInfo> { |
| 63 | + self.stores |
| 64 | + .iter(&self.stores.guard()) |
| 65 | + .map(|(store_name, store)| AIStoreInfo { |
| 66 | + name: store_name.clone(), |
| 67 | + model: store.model.clone(), |
| 68 | + r#type: store.r#type.clone(), |
| 69 | + embedding_size: store.model.embedding_size().into(), |
| 70 | + }) |
| 71 | + .collect() |
| 72 | + } |
| 73 | + |
| 74 | + /// Returns a store using the store name, else returns an error |
| 75 | + #[tracing::instrument(skip(self))] |
| 76 | + pub(crate) fn get(&self, store_name: &StoreName) -> Result<Arc<AIStore>, AIProxyError> { |
| 77 | + let store = self |
| 78 | + .stores |
| 79 | + .get(store_name, &self.stores.guard()) |
| 80 | + .cloned() |
| 81 | + .ok_or(AIProxyError::StoreNotFound(store_name.clone()))?; |
| 82 | + Ok(store) |
| 83 | + } |
| 84 | + |
| 85 | + /// Converts storeinput into a tuple of storekey and storevalue. |
| 86 | + /// Fails if the type of storeinput does not match the store type |
| 87 | + #[tracing::instrument(skip(self))] |
| 88 | + pub(crate) fn store_input_to_store_key_val( |
| 89 | + &self, |
| 90 | + store_name: &StoreName, |
| 91 | + store_input: StoreInput, |
| 92 | + store_value: &StoreValue, |
| 93 | + ) -> Result<(StoreKey, StoreValue), AIProxyError> { |
| 94 | + let metadata_key = &*AHNLICH_AI_RESERVED_META_KEY; |
| 95 | + if store_value.contains_key(metadata_key) { |
| 96 | + return Err(AIProxyError::ReservedError(metadata_key.to_string())); |
| 97 | + } |
| 98 | + let store = self.get(store_name)?; |
| 99 | + let input_type = store_input.clone().into(); |
| 100 | + |
| 101 | + if store.r#type != input_type { |
| 102 | + return Err(AIProxyError::StoreTypeMismatch { |
| 103 | + store_type: store.r#type.clone(), |
| 104 | + input_type, |
| 105 | + }); |
| 106 | + } |
| 107 | + let store_key = store.model.model_ndarray(&store_input); |
| 108 | + let metadata_value: MetadataValue = store_input.into(); |
| 109 | + let mut final_store_value: StdHashMap<MetadataKey, MetadataValue> = |
| 110 | + store_value.clone().into_iter().collect(); |
| 111 | + final_store_value.insert(metadata_key.clone(), metadata_value); |
| 112 | + return Ok((store_key, final_store_value)); |
| 113 | + } |
| 114 | + |
| 115 | + /// Converts (storekey, storevalue) into (storeinput, storevalue) |
| 116 | + /// by removing the reserved_key from storevalue |
| 117 | + #[tracing::instrument(skip(self))] |
| 118 | + pub(crate) fn store_key_val_to_store_input_val( |
| 119 | + &self, |
| 120 | + output: Vec<(StoreKey, StoreValue)>, |
| 121 | + ) -> Vec<(StoreInput, StoreValue)> { |
| 122 | + let metadata_key = &*AHNLICH_AI_RESERVED_META_KEY; |
| 123 | + |
| 124 | + // TODO: Will parallelized |
| 125 | + output |
| 126 | + .into_iter() |
| 127 | + .filter_map(|(_, mut store_value)| { |
| 128 | + store_value |
| 129 | + .remove(metadata_key) |
| 130 | + .map(|val| (val, store_value)) |
| 131 | + }) |
| 132 | + .map(|(metadata_value, store_value)| { |
| 133 | + let store_input: StoreInput = metadata_value.into(); |
| 134 | + (store_input, store_value) |
| 135 | + }) |
| 136 | + .collect() |
| 137 | + } |
| 138 | + |
| 139 | + #[tracing::instrument(skip(self))] |
| 140 | + pub(crate) fn get_ndarray_repr_for_store( |
| 141 | + &self, |
| 142 | + store_name: &StoreName, |
| 143 | + store_input: &StoreInput, |
| 144 | + ) -> Result<StoreKey, AIProxyError> { |
| 145 | + let store = self.get(store_name)?; |
| 146 | + Ok(store.model.model_ndarray(store_input)) |
| 147 | + } |
| 148 | + |
| 149 | + /// Matches DROPSTORE - Drops a store if exist, else returns an error |
| 150 | + #[tracing::instrument(skip(self))] |
| 151 | + pub(crate) fn drop_store( |
| 152 | + &self, |
| 153 | + store_name: StoreName, |
| 154 | + error_if_not_exists: bool, |
| 155 | + ) -> Result<usize, AIProxyError> { |
| 156 | + let pinned = self.stores.pin(); |
| 157 | + let removed = pinned.remove(&store_name).is_some(); |
| 158 | + if !removed && error_if_not_exists { |
| 159 | + return Err(AIProxyError::StoreNotFound(store_name)); |
| 160 | + } |
| 161 | + let removed = if !removed { |
| 162 | + 0 |
| 163 | + } else { |
| 164 | + //self.set_write_flag(); |
| 165 | + 1 |
| 166 | + }; |
| 167 | + Ok(removed) |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +#[derive(Debug, Serialize, Deserialize)] |
| 172 | +pub struct AIStore { |
| 173 | + name: StoreName, |
| 174 | + /// Making use of a concurrent hashmap, we should be able to create an engine that manages stores |
| 175 | + r#type: AIStoreType, |
| 176 | + model: AIModel, |
| 177 | +} |
| 178 | + |
| 179 | +impl AIStore { |
| 180 | + pub(super) fn create(r#type: AIStoreType, store_name: StoreName, model: AIModel) -> Self { |
| 181 | + Self { |
| 182 | + r#type, |
| 183 | + name: store_name, |
| 184 | + model, |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments