Skip to content

Commit 1507d1c

Browse files
authored
Merge pull request #19 from rustbase/bugfix/snapshots
fix: fixing snapshots interface
2 parents 3cecf30 + d5c6ffd commit 1507d1c

2 files changed

Lines changed: 76 additions & 75 deletions

File tree

src/storage/lsm/mod.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct Lsm {
2626
pub memtable: Arc<Mutex<BTreeMap<String, bson::Bson>>>,
2727
pub memtable_size: usize,
2828
pub lsm_config: LsmConfig,
29-
pub snapshots: snapshots::Snapshots,
29+
pub snapshots: snapshots::SnapshotManager,
3030
pub dense_index: Arc<Mutex<HashMap<String, String>>>,
3131
pub bloom_filter: Arc<Mutex<BloomFilter>>,
3232
}
@@ -51,12 +51,8 @@ impl Lsm {
5151
std::fs::create_dir_all(&config.sstable_path).unwrap();
5252
}
5353

54-
let snapshots = snapshots::Snapshots::new(
55-
std::path::Path::new(&config.sstable_path)
56-
.join("snapshots")
57-
.to_str()
58-
.unwrap()
59-
.to_string(),
54+
let snapshots = snapshots::SnapshotManager::new(
55+
std::path::Path::new(&config.sstable_path).join("snapshots"),
6056
);
6157

6258
Lsm {
@@ -233,14 +229,10 @@ impl Lsm {
233229
self.bloom_filter.lock().unwrap().clear();
234230
}
235231

236-
pub fn load_snapshot(&mut self, snapshot: Snapshot) {
237-
let mut memtable = self.memtable.lock().unwrap();
238-
let mut dense_index = self.dense_index.lock().unwrap();
239-
let mut bloom_filter = self.bloom_filter.lock().unwrap();
240-
241-
*memtable = snapshot.memtable;
242-
*dense_index = snapshot.dense_index;
243-
*bloom_filter = snapshot.bloom_filter;
232+
pub fn load_snapshot(path: path::PathBuf, snapshot: Snapshot) {
233+
sstable::Segment::from_tree(snapshot.get_memtable(), &path.display().to_string());
234+
index::write_index(&path.display().to_string(), snapshot.get_dense_index());
235+
filter::write_filter(&path.display().to_string(), snapshot.get_bloom_filter());
244236
}
245237
}
246238

src/storage/lsm/snapshots/mod.rs

Lines changed: 69 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,20 @@ use serde::{Deserialize, Serialize};
33
use std::collections::{BTreeMap, HashMap};
44
use std::fs;
55
use std::io::{Read, Write};
6-
use std::path::{Path, PathBuf};
6+
use std::path::PathBuf;
77

88
use crate::bloom::BloomFilter;
99

1010
use 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

Comments
 (0)