11//! Core types for the lance-context storage layer.
22
3+ use std:: collections:: HashMap ;
4+
35#[ derive( Debug , Clone ) ]
46pub struct Context {
57 uri : String ,
68 branch : String ,
7- entries : u64 ,
9+ next_id : u64 ,
10+ entries : Vec < ContextEntry > ,
11+ snapshots : HashMap < String , Snapshot > ,
812}
913
1014impl Context {
1115 pub fn new ( uri : impl Into < String > ) -> Self {
1216 Self {
1317 uri : uri. into ( ) ,
1418 branch : "main" . to_string ( ) ,
15- entries : 0 ,
19+ next_id : 1 ,
20+ entries : Vec :: new ( ) ,
21+ snapshots : HashMap :: new ( ) ,
1622 }
1723 }
1824
@@ -25,27 +31,66 @@ impl Context {
2531 }
2632
2733 pub fn entries ( & self ) -> u64 {
28- self . entries
34+ self . entries . len ( ) as u64
2935 }
3036
31- pub fn add ( & mut self , _role : & str , _content : & str , _data_type : Option < & str > ) {
32- self . entries += 1 ;
37+ pub fn add ( & mut self , role : & str , content : & str , data_type : Option < & str > ) -> u64 {
38+ let entry_id = self . next_id ;
39+ self . next_id += 1 ;
40+ self . entries . push ( ContextEntry {
41+ id : entry_id,
42+ role : role. to_string ( ) ,
43+ data_type : data_type. map ( |value| value. to_string ( ) ) ,
44+ content : content. to_string ( ) ,
45+ } ) ;
46+ entry_id
3347 }
3448
35- pub fn snapshot ( & self , label : Option < & str > ) -> String {
36- match label {
49+ pub fn snapshot ( & mut self , label : Option < & str > ) -> String {
50+ let id = match label {
3751 Some ( label) if !label. is_empty ( ) => label. to_string ( ) ,
38- _ => format ! ( "snapshot-{}" , self . entries) ,
39- }
52+ _ => format ! ( "snapshot-{}" , self . entries. len( ) ) ,
53+ } ;
54+ let snapshot = Snapshot {
55+ id : id. clone ( ) ,
56+ label : label. map ( |value| value. to_string ( ) ) ,
57+ entry_count : self . entries . len ( ) as u64 ,
58+ branch : self . branch . clone ( ) ,
59+ } ;
60+ self . snapshots . insert ( id. clone ( ) , snapshot) ;
61+ id
4062 }
4163
4264 pub fn fork ( & self , branch_name : impl Into < String > ) -> Self {
4365 Self {
4466 uri : self . uri . clone ( ) ,
4567 branch : branch_name. into ( ) ,
46- entries : self . entries ,
68+ next_id : self . next_id ,
69+ entries : self . entries . clone ( ) ,
70+ snapshots : self . snapshots . clone ( ) ,
71+ }
72+ }
73+
74+ pub fn checkout ( & mut self , snapshot_id : & str ) {
75+ if let Some ( snapshot) = self . snapshots . get ( snapshot_id) {
76+ self . entries . truncate ( snapshot. entry_count as usize ) ;
77+ self . next_id = self . entries . len ( ) as u64 + 1 ;
4778 }
4879 }
80+ }
4981
50- pub fn checkout ( & mut self , _snapshot_id : & str ) { }
82+ #[ derive( Debug , Clone ) ]
83+ pub struct ContextEntry {
84+ pub id : u64 ,
85+ pub role : String ,
86+ pub data_type : Option < String > ,
87+ pub content : String ,
88+ }
89+
90+ #[ derive( Debug , Clone ) ]
91+ pub struct Snapshot {
92+ pub id : String ,
93+ pub label : Option < String > ,
94+ pub entry_count : u64 ,
95+ pub branch : String ,
5196}
0 commit comments