@@ -129,6 +129,43 @@ pub(crate) struct TsdbDelta {
129129 pub ( crate ) samples : HashMap < SeriesId , Vec < Sample > > ,
130130}
131131
132+ impl TsdbDelta {
133+ /// Create an empty delta for a bucket
134+ pub ( crate ) fn empty ( bucket : TimeBucket ) -> Self {
135+ Self {
136+ bucket,
137+ forward_index : ForwardIndex :: default ( ) ,
138+ inverted_index : InvertedIndex :: default ( ) ,
139+ series_dict : HashMap :: new ( ) ,
140+ samples : HashMap :: new ( ) ,
141+ }
142+ }
143+
144+ /// Check if delta has any data
145+ pub ( crate ) fn is_empty ( & self ) -> bool {
146+ self . samples . is_empty ( ) && self . series_dict . is_empty ( )
147+ }
148+
149+ /// Merge another delta into this one, accumulating all data
150+ pub ( crate ) fn merge ( & mut self , other : TsdbDelta ) {
151+ // Merge forward index
152+ self . forward_index . merge ( & other. forward_index ) ;
153+
154+ // Merge inverted index
155+ self . inverted_index . merge ( other. inverted_index ) ;
156+
157+ // Merge series dict
158+ for ( fingerprint, series_id) in other. series_dict {
159+ self . series_dict . insert ( fingerprint, series_id) ;
160+ }
161+
162+ // Merge samples
163+ for ( series_id, samples) in other. samples {
164+ self . samples . entry ( series_id) . or_default ( ) . extend ( samples) ;
165+ }
166+ }
167+ }
168+
132169#[ cfg( test) ]
133170mod tests {
134171 use super :: * ;
0 commit comments