Skip to content

Commit 30c27b7

Browse files
committed
fix: Load existing collections on startup
This commit fixes a bug where existing collections were not loaded on server startup. The function has been modified to load the collections from the file system. A new test has been added to make sure that the collections are loaded on startup.
1 parent ee11929 commit 30c27b7

1 file changed

Lines changed: 57 additions & 3 deletions

File tree

src/db.rs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,46 @@ impl<'a> Iterator for MergedIterator<'a> {
245245
impl DB {
246246
pub fn new(root_dir: &str, memtable_threshold: usize, jstable_threshold: u64) -> Self {
247247
fs::create_dir_all(root_dir).unwrap();
248+
let mut collections = HashMap::new();
249+
250+
if let Ok(entries) = fs::read_dir(root_dir) {
251+
for entry in entries {
252+
if let Ok(entry) = entry {
253+
if entry.path().is_dir() {
254+
let dir_path = entry.path();
255+
256+
// Try to find collection name from JSTable-0
257+
let jstable_path = dir_path.join("jstable-0");
258+
let col_name = if jstable_path.exists() {
259+
if let Ok(iter) =
260+
jstable::JSTableIterator::new(jstable_path.to_str().unwrap())
261+
{
262+
Some(iter.collection)
263+
} else {
264+
None
265+
}
266+
} else {
267+
// Fallback to directory name (sanitized) if no jstable
268+
entry.file_name().to_str().map(|s| s.to_string())
269+
};
270+
271+
if let Some(name) = col_name {
272+
let collection = Collection::new(
273+
name.clone(),
274+
dir_path,
275+
memtable_threshold,
276+
jstable_threshold,
277+
);
278+
collections.insert(name, collection);
279+
}
280+
}
281+
}
282+
}
283+
}
284+
248285
DB {
249286
root_dir: PathBuf::from(root_dir),
250-
collections: HashMap::new(),
287+
collections,
251288
memtable_threshold,
252289
jstable_threshold,
253290
}
@@ -417,12 +454,11 @@ mod tests {
417454
db.delete("test", &id1).unwrap();
418455

419456
// Recover by creating new DB instance pointed to same dir
420-
let mut db2 = DB::new(
457+
let db2 = DB::new(
421458
dir.path().to_str().unwrap(),
422459
MEMTABLE_THRESHOLD,
423460
JSTABLE_THRESHOLD,
424461
);
425-
db2.create_collection("test").unwrap();
426462
// "test" should be loaded if it persisted JSTable or fallback to dir name
427463
let col = db2.collections.get("test").unwrap();
428464

@@ -604,4 +640,22 @@ mod tests {
604640
let res = db.insert("test", json!({ "a": 1 }));
605641
assert!(res.is_err());
606642
}
643+
644+
#[test]
645+
fn test_db_load_collections_on_startup() {
646+
let dir = tempdir().unwrap();
647+
let mut db = DB::new(
648+
dir.path().to_str().unwrap(),
649+
MEMTABLE_THRESHOLD,
650+
JSTABLE_THRESHOLD,
651+
);
652+
db.create_collection("test").unwrap();
653+
654+
let db2 = DB::new(
655+
dir.path().to_str().unwrap(),
656+
MEMTABLE_THRESHOLD,
657+
JSTABLE_THRESHOLD,
658+
);
659+
assert!(db2.collections.contains_key("test"));
660+
}
607661
}

0 commit comments

Comments
 (0)