@@ -3,66 +3,20 @@ use serde::{Deserialize, Serialize};
33use std:: collections:: { BTreeMap , HashMap } ;
44use std:: fs;
55use std:: io:: { Read , Write } ;
6- use std:: path:: { Path , PathBuf } ;
6+ use std:: path:: PathBuf ;
77
88use crate :: bloom:: BloomFilter ;
99
1010use super :: Lsm ;
1111
1212#[ derive( Clone , Debug ) ]
13- pub struct Snapshots {
13+ pub struct SnapshotManager {
1414 path : PathBuf ,
1515}
1616
17- impl Snapshots {
18- pub fn new ( path : String ) -> Snapshots {
19- let path = Path :: new ( & path) ;
20-
21- if !path. exists ( ) {
22- std:: fs:: create_dir_all ( path) . unwrap ( ) ;
23- }
24-
25- Snapshots {
26- path : PathBuf :: from ( path) ,
27- }
28- }
29-
30- pub fn create_snapshot ( & self , lsm : & Lsm ) -> String {
31- let snapshot = Snapshot :: new (
32- lsm. memtable . lock ( ) . unwrap ( ) . clone ( ) ,
33- lsm. bloom_filter . lock ( ) . unwrap ( ) . clone ( ) ,
34- lsm. dense_index . lock ( ) . unwrap ( ) . clone ( ) ,
35- ) ;
36-
37- let timestamp = snapshot. timestamp ( ) ;
38- let path = Path :: new ( & self . path ) ;
39-
40- let snapshot_path = path. join ( timestamp. clone ( ) ) ;
41- let file = fs:: File :: create ( snapshot_path) . unwrap ( ) ;
42-
43- let snapshot = bson:: to_vec ( & snapshot) . unwrap ( ) ;
44-
45- let mut encoder = EncoderBuilder :: new ( )
46- . build ( file)
47- . expect ( "cannot create encoder" ) ;
48-
49- encoder. write_all ( & snapshot) . unwrap ( ) ;
50- encoder. flush ( ) . unwrap ( ) ;
51-
52- timestamp
53- }
54-
55- pub fn load_snapshot ( & self , timestamp : String ) -> Snapshot {
56- let path = self . path . join ( timestamp) ;
57-
58- let file = fs:: File :: open ( path) . unwrap ( ) ;
59-
60- let mut decoder = Decoder :: new ( file) . unwrap ( ) ;
61- let mut contents = Vec :: new ( ) ;
62- decoder. read_to_end ( & mut contents) . unwrap ( ) ;
63- let snapshot: Snapshot = bson:: from_slice ( & contents) . unwrap ( ) ;
64-
65- snapshot
17+ impl SnapshotManager {
18+ pub fn new ( path : PathBuf ) -> Self {
19+ SnapshotManager { path }
6620 }
6721
6822 pub fn load_last_snapshot ( & self ) -> Snapshot {
@@ -80,14 +34,17 @@ impl Snapshots {
8034 }
8135 }
8236
83- let snapshot: Snapshot = self . load_snapshot (
84- last_snapshot
85- . file_name ( )
86- . unwrap ( )
87- . to_str ( )
88- . unwrap ( )
89- . to_string ( ) ,
90- ) ;
37+ let snapshot: Snapshot = Snapshot :: load_snapshot ( last_snapshot) ;
38+
39+ snapshot
40+ }
41+
42+ pub fn load_snapshot_by_index ( & self , index : usize ) -> Snapshot {
43+ let mut paths = fs:: read_dir ( & self . path ) . unwrap ( ) ;
44+
45+ let snapshot = paths. nth ( index) . unwrap ( ) . unwrap ( ) . path ( ) ;
46+
47+ let snapshot: Snapshot = Snapshot :: load_snapshot ( snapshot) ;
9148
9249 snapshot
9350 }
@@ -113,8 +70,60 @@ impl Snapshot {
11370 }
11471 }
11572
73+ pub fn load_snapshot ( path : PathBuf ) -> Snapshot {
74+ let file = fs:: File :: open ( path) . unwrap ( ) ;
75+
76+ let mut decoder = Decoder :: new ( file) . unwrap ( ) ;
77+ let mut contents = Vec :: new ( ) ;
78+ decoder. read_to_end ( & mut contents) . unwrap ( ) ;
79+ let snapshot: Snapshot = bson:: from_slice ( & contents) . unwrap ( ) ;
80+
81+ snapshot
82+ }
83+
84+ pub fn create_snapshot ( lsm : & Lsm , path : PathBuf ) -> String {
85+ if !path. exists ( ) {
86+ std:: fs:: create_dir_all ( path. clone ( ) ) . unwrap ( ) ;
87+ }
88+
89+ let snapshot = Snapshot :: new (
90+ lsm. memtable . lock ( ) . unwrap ( ) . clone ( ) ,
91+ lsm. bloom_filter . lock ( ) . unwrap ( ) . clone ( ) ,
92+ lsm. dense_index . lock ( ) . unwrap ( ) . clone ( ) ,
93+ ) ;
94+
95+ let now = chrono:: Local :: now ( ) ;
96+ let timestamp = now. format ( "%Y-%m-%d-%H-%M-%S" ) . to_string ( ) ;
97+
98+ let snapshot_path = path. join ( timestamp. clone ( ) ) ;
99+ let file = fs:: File :: create ( snapshot_path) . unwrap ( ) ;
100+
101+ let snapshot = bson:: to_vec ( & snapshot) . unwrap ( ) ;
102+
103+ let mut encoder = EncoderBuilder :: new ( )
104+ . build ( file)
105+ . expect ( "cannot create encoder" ) ;
106+
107+ encoder. write_all ( & snapshot) . unwrap ( ) ;
108+ encoder. flush ( ) . unwrap ( ) ;
109+
110+ timestamp
111+ }
112+
113+ pub fn get_memtable ( & self ) -> & BTreeMap < String , bson:: Bson > {
114+ & self . memtable
115+ }
116+
117+ pub fn get_bloom_filter ( & self ) -> & BloomFilter {
118+ & self . bloom_filter
119+ }
120+
121+ pub fn get_dense_index ( & self ) -> & HashMap < String , String > {
122+ & self . dense_index
123+ }
124+
116125 pub fn timestamp ( & self ) -> String {
117126 let now = chrono:: Local :: now ( ) ;
118- now. format ( "%Y-%m-%d_ %H-%M-%S" ) . to_string ( )
127+ now. format ( "%Y-%m-%d- %H-%M-%S" ) . to_string ( )
119128 }
120129}
0 commit comments