@@ -4,7 +4,6 @@ use std::path::PathBuf;
44use std:: sync:: Arc ;
55
66use chrono:: DateTime ;
7- use chrono:: Utc ;
87use fst:: map:: OpBuilder ;
98use fst:: Streamer ;
109use libsql_sys:: name:: NamespaceName ;
@@ -58,7 +57,7 @@ impl<B> Compactor<B> {
5857 "CREATE TABLE IF NOT EXISTS segments (
5958 start_frame_no INTEGER,
6059 end_frame_no INTEGER,
61- created_at DATE,
60+ timestamp DATE,
6261 size INTEGER,
6362 namespace_id INTEGER,
6463 PRIMARY KEY (start_frame_no, end_frame_no),
@@ -93,7 +92,7 @@ impl<B> Compactor<B> {
9392 pub fn analyze ( & self , namespace : & NamespaceName ) -> Result < AnalyzedSegments > {
9493 let mut stmt = self . meta . prepare_cached (
9594 r#"
96- SELECT start_frame_no, end_frame_no
95+ SELECT start_frame_no, end_frame_no, timestamp
9796 FROM segments as s
9897 JOIN monitored_namespaces as m
9998 ON m.id = s.namespace_id
@@ -105,11 +104,12 @@ impl<B> Compactor<B> {
105104 while let Some ( row) = rows. next ( ) ? {
106105 let start_frame_no: u64 = row. get ( 0 ) ?;
107106 let end_frame_no: u64 = row. get ( 1 ) ?;
107+ let timestamp: u64 = row. get ( 2 ) ?;
108108 // it's free to go from one end of a segment to the next
109109 graph. add_edge ( start_frame_no, end_frame_no, 0 ) ;
110110 if start_frame_no != 1 {
111111 // going from a segment to the next costs us
112- graph. add_edge ( start_frame_no - 1 , start_frame_no, 1 ) ;
112+ graph. add_edge ( start_frame_no - 1 , start_frame_no, timestamp ) ;
113113 }
114114 last_frame_no = last_frame_no. max ( end_frame_no) ;
115115 }
@@ -304,7 +304,7 @@ impl<B> Compactor<B> {
304304 segment_id : Uuid :: new_v4 ( ) ,
305305 start_frame_no : start,
306306 end_frame_no : end,
307- segment_timestamp : Utc :: now ( ) ,
307+ segment_timestamp : DateTime :: from_timestamp_millis ( set . last ( ) . unwrap ( ) . timestamp as _ ) . unwrap ( ) . to_utc ( ) ,
308308 } ,
309309 out_file,
310310 out_index. into_inner ( ) . unwrap ( ) ,
@@ -375,9 +375,13 @@ impl AnalyzedSegments {
375375 match path {
376376 Some ( ( _len, nodes) ) => {
377377 for chunk in nodes. chunks ( 2 ) {
378+ let start_frame_no = chunk[ 0 ] ;
379+ let end_frame_no = chunk[ 1 ] ;
380+ let timestamp = * self . graph . edges ( start_frame_no) . find_map ( |( _, to, ts) | ( to == end_frame_no) . then_some ( ts) ) . unwrap ( ) ;
378381 let key = SegmentKey {
379- start_frame_no : chunk[ 0 ] ,
380- end_frame_no : chunk[ 1 ] ,
382+ start_frame_no,
383+ end_frame_no,
384+ timestamp,
381385 } ;
382386 segments. push ( key) ;
383387 }
@@ -448,7 +452,7 @@ fn list_segments<'a>(
448452) -> Result < ( ) > {
449453 let mut stmt = conn. prepare_cached (
450454 r#"
451- SELECT created_at , size, start_frame_no, end_frame_no
455+ SELECT timestamp , size, start_frame_no, end_frame_no
452456 FROM segments as s
453457 JOIN monitored_namespaces as m
454458 ON m.id == s.namespace_id
@@ -462,9 +466,9 @@ fn list_segments<'a>(
462466 key : SegmentKey {
463467 start_frame_no : r. get ( 2 ) ?,
464468 end_frame_no : r. get ( 3 ) ?,
469+ timestamp : r. get ( 0 ) ?,
465470 } ,
466471 size : r. get ( 1 ) ?,
467- created_at : DateTime :: from_timestamp ( r. get ( 0 ) ?, 0 ) . unwrap ( ) ,
468472 } )
469473 } ) ?;
470474
@@ -486,7 +490,7 @@ fn register_segment_info(
486490 INSERT OR IGNORE INTO segments (
487491 start_frame_no,
488492 end_frame_no,
489- created_at ,
493+ timestamp ,
490494 size,
491495 namespace_id
492496 )
@@ -495,7 +499,7 @@ fn register_segment_info(
495499 stmt. execute ( (
496500 info. key . start_frame_no ,
497501 info. key . end_frame_no ,
498- info. created_at . timestamp ( ) ,
502+ info. key . timestamp ,
499503 info. size ,
500504 namespace_id,
501505 ) ) ?;
@@ -508,7 +512,7 @@ fn segments_range(
508512) -> Result < Option < ( SegmentInfo , SegmentInfo ) > > {
509513 let mut stmt = conn. prepare_cached (
510514 r#"
511- SELECT min(created_at ), size, start_frame_no, end_frame_no
515+ SELECT min(timestamp ), size, start_frame_no, end_frame_no
512516 FROM segments as s
513517 JOIN monitored_namespaces as m
514518 ON m.id == s.namespace_id
@@ -522,16 +526,16 @@ fn segments_range(
522526 key : SegmentKey {
523527 start_frame_no : r. get ( 2 ) ?,
524528 end_frame_no : r. get ( 3 ) ?,
529+ timestamp : r. get ( 0 ) ?,
525530 } ,
526531 size : r. get ( 1 ) ?,
527- created_at : DateTime :: from_timestamp ( r. get ( 0 ) ?, 0 ) . unwrap ( ) ,
528532 } )
529533 } )
530534 . optional ( ) ?;
531535
532536 let mut stmt = conn. prepare_cached (
533537 r#"
534- SELECT max(created_at ), size, start_frame_no, end_frame_no
538+ SELECT max(timestamp ), size, start_frame_no, end_frame_no
535539 FROM segments as s
536540 JOIN monitored_namespaces as m
537541 ON m.id == s.namespace_id
@@ -545,9 +549,9 @@ fn segments_range(
545549 key : SegmentKey {
546550 start_frame_no : r. get ( 2 ) ?,
547551 end_frame_no : r. get ( 3 ) ?,
552+ timestamp : r. get ( 0 ) ?,
548553 } ,
549554 size : r. get ( 1 ) ?,
550- created_at : DateTime :: from_timestamp ( r. get ( 0 ) ?, 0 ) . unwrap ( ) ,
551555 } )
552556 } )
553557 . optional ( ) ?;
0 commit comments