@@ -6,6 +6,7 @@ use crate::btree::{BTree, ValueTrait};
66use crate :: dustdata_config;
77use crate :: error:: { Error , Result } ;
88use crate :: page:: io:: BlockIO ;
9+ use std:: fs;
910use std:: sync:: Mutex ;
1011use std:: { fmt:: Debug , sync:: Arc } ;
1112use xact:: TransactionBuilder ;
@@ -21,6 +22,8 @@ pub enum TransactionStatus {
2122 Aborted ,
2223}
2324
25+ pub const COLLECTION_LOCK_FILE : & str = ".lock" ;
26+
2427#[ derive( Clone ) ]
2528pub struct Collection < T : ValueTrait > {
2629 btree : Arc < Mutex < BTree < String , T > > > ,
@@ -31,9 +34,14 @@ pub struct Collection<T: ValueTrait> {
3134impl < T : ValueTrait > Collection < T > {
3235 pub fn new ( name : & str ) -> Result < Self > {
3336 let dustdata_config = dustdata_config ( ) ;
34-
3537 let base_path = dustdata_config. data_path . join ( name) ;
3638
39+ fs:: create_dir_all ( & base_path) . ok ( ) ;
40+
41+ // Create lock file
42+ fs:: File :: create_new ( base_path. join ( COLLECTION_LOCK_FILE ) )
43+ . map_err ( |_| Error :: DatabaseLocked ) ?;
44+
3745 let xlog = Arc :: new ( Mutex :: new ( xlog:: XLog :: new ( & base_path) ?) ) ;
3846
3947 let btree_block = BlockIO :: new ( base_path. join ( "Data.db" ) ) . map_err ( Error :: IoError ) ?;
@@ -99,3 +107,12 @@ impl<T: ValueTrait> Collection<T> {
99107 self . btree . lock ( ) . unwrap ( ) . get ( & key. to_string ( ) )
100108 }
101109}
110+
111+ impl < T : ValueTrait > Drop for Collection < T > {
112+ fn drop ( & mut self ) {
113+ let dustdata_config = dustdata_config ( ) ;
114+ let base_path = dustdata_config. data_path . join ( & self . name ) ;
115+
116+ fs:: remove_file ( base_path. join ( COLLECTION_LOCK_FILE ) ) . unwrap ( ) ;
117+ }
118+ }
0 commit comments