1+ use std:: str:: FromStr ;
2+
13use super :: { Metadata , MetadataValueConversionError } ;
24use crate :: {
35 chroma_proto, test_segment, CollectionConfiguration , InternalCollectionConfiguration , Segment ,
@@ -30,6 +32,29 @@ use pyo3::types::PyAnyMethods;
3032) ]
3133pub struct CollectionUuid ( pub Uuid ) ;
3234
35+ /// DatabaseUuid is a wrapper around Uuid to provide a type for the database id.
36+ #[ derive(
37+ Copy ,
38+ Clone ,
39+ Debug ,
40+ Default ,
41+ Deserialize ,
42+ Eq ,
43+ PartialEq ,
44+ Ord ,
45+ PartialOrd ,
46+ Hash ,
47+ Serialize ,
48+ ToSchema ,
49+ ) ]
50+ pub struct DatabaseUuid ( pub Uuid ) ;
51+
52+ impl DatabaseUuid {
53+ pub fn new ( ) -> Self {
54+ DatabaseUuid ( Uuid :: new_v4 ( ) )
55+ }
56+ }
57+
3358impl CollectionUuid {
3459 pub fn new ( ) -> Self {
3560 CollectionUuid ( Uuid :: new_v4 ( ) )
@@ -51,6 +76,17 @@ impl std::str::FromStr for CollectionUuid {
5176 }
5277}
5378
79+ impl std:: str:: FromStr for DatabaseUuid {
80+ type Err = uuid:: Error ;
81+
82+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
83+ match Uuid :: parse_str ( s) {
84+ Ok ( uuid) => Ok ( DatabaseUuid ( uuid) ) ,
85+ Err ( err) => Err ( err) ,
86+ }
87+ }
88+ }
89+
5490impl std:: fmt:: Display for CollectionUuid {
5591 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
5692 write ! ( f, "{}" , self . 0 )
@@ -107,6 +143,8 @@ pub struct Collection {
107143 pub lineage_file_path : Option < String > ,
108144 #[ serde( skip, default = "SystemTime::now" ) ]
109145 pub updated_at : SystemTime ,
146+ #[ serde( skip) ]
147+ pub database_id : DatabaseUuid ,
110148}
111149
112150impl Default for Collection {
@@ -128,6 +166,7 @@ impl Default for Collection {
128166 root_collection_id : None ,
129167 lineage_file_path : None ,
130168 updated_at : SystemTime :: now ( ) ,
169+ database_id : DatabaseUuid :: new ( ) ,
131170 }
132171 }
133172}
@@ -218,11 +257,8 @@ impl TryFrom<chroma_proto::Collection> for Collection {
218257 type Error = CollectionConversionError ;
219258
220259 fn try_from ( proto_collection : chroma_proto:: Collection ) -> Result < Self , Self :: Error > {
221- let collection_uuid = match Uuid :: try_parse ( & proto_collection. id ) {
222- Ok ( uuid) => uuid,
223- Err ( _) => return Err ( CollectionConversionError :: InvalidUuid ) ,
224- } ;
225- let collection_id = CollectionUuid ( collection_uuid) ;
260+ let collection_id = CollectionUuid :: from_str ( & proto_collection. id )
261+ . map_err ( |_| CollectionConversionError :: InvalidUuid ) ?;
226262 let collection_metadata: Option < Metadata > = match proto_collection. metadata {
227263 Some ( proto_metadata) => match proto_metadata. try_into ( ) {
228264 Ok ( metadata) => Some ( metadata) ,
@@ -238,6 +274,12 @@ impl TryFrom<chroma_proto::Collection> for Collection {
238274 }
239275 None => SystemTime :: now ( ) ,
240276 } ;
277+ // TOOD(Sanket): this should be updated to error with "missing field" once all SysDb deployments are up-to-date
278+ let database_id = match proto_collection. database_id {
279+ Some ( db_id) => DatabaseUuid :: from_str ( & db_id)
280+ . map_err ( |_| CollectionConversionError :: InvalidUuid ) ?,
281+ None => DatabaseUuid :: new ( ) ,
282+ } ;
241283 Ok ( Collection {
242284 collection_id,
243285 name : proto_collection. name ,
@@ -257,6 +299,7 @@ impl TryFrom<chroma_proto::Collection> for Collection {
257299 . map ( |uuid| CollectionUuid ( Uuid :: try_parse ( & uuid) . unwrap ( ) ) ) ,
258300 lineage_file_path : proto_collection. lineage_file_path ,
259301 updated_at,
302+ database_id,
260303 } )
261304 }
262305}
@@ -296,6 +339,7 @@ impl TryFrom<Collection> for chroma_proto::Collection {
296339 root_collection_id : value. root_collection_id . map ( |uuid| uuid. 0 . to_string ( ) ) ,
297340 lineage_file_path : value. lineage_file_path ,
298341 updated_at : Some ( value. updated_at . into ( ) ) ,
342+ database_id : Some ( value. database_id . 0 . to_string ( ) ) ,
299343 } )
300344 }
301345}
@@ -348,6 +392,7 @@ mod test {
348392 seconds : 1 ,
349393 nanos : 1 ,
350394 } ) ,
395+ database_id : Some ( "00000000-0000-0000-0000-000000000000" . to_string ( ) ) ,
351396 } ;
352397 let converted_collection: Collection = proto_collection. try_into ( ) . unwrap ( ) ;
353398 assert_eq ! (
@@ -378,6 +423,7 @@ mod test {
378423 converted_collection. updated_at,
379424 SystemTime :: UNIX_EPOCH + Duration :: new( 1 , 1 )
380425 ) ;
426+ assert_eq ! ( converted_collection. database_id, DatabaseUuid ( Uuid :: nil( ) ) ) ;
381427 }
382428
383429 #[ test]
0 commit comments