-
Notifications
You must be signed in to change notification settings - Fork 10
[wip] dont recreate database #299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -69,19 +69,11 @@ impl LmdbBlockStorage { | |||||||||||||||||||||||
| info!(target: LOG_TARGET, "Using block storage at {:?}", path); | ||||||||||||||||||||||||
| if !fs::exists(path).expect("could not get file") { | ||||||||||||||||||||||||
| fs::create_dir_all(path).unwrap(); | ||||||||||||||||||||||||
| // fs::File::create(path).unwrap(); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| let mut manager = Manager::<LmdbEnvironment>::singleton().write().unwrap(); | ||||||||||||||||||||||||
| let file_handle = manager.get_or_create(path, Rkv::new::<Lmdb>).unwrap(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| let env = file_handle.read().expect("reader"); | ||||||||||||||||||||||||
| // let dbs = env.get_dbs().expect("No dbs"); | ||||||||||||||||||||||||
| // if !dbs.contains(&Some("migrations".to_string())) { | ||||||||||||||||||||||||
| // let store = env.open_integer("migrations", StoreOptions::create()).unwrap(); | ||||||||||||||||||||||||
| // let writer = env.write().expect("writer"); | ||||||||||||||||||||||||
| // store.put(&writer, 0, &rkv::Value::Str("init")).unwrap(); | ||||||||||||||||||||||||
| // writer.commit(); | ||||||||||||||||||||||||
| // } | ||||||||||||||||||||||||
| let mut migrations = HashMap::new(); | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| let store = env.open_single("migrations", StoreOptions::create()).unwrap(); | ||||||||||||||||||||||||
|
|
@@ -152,7 +144,18 @@ impl BlockCache for LmdbBlockStorage { | |||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| fn insert(&self, hash: BlockHash, block: Arc<P2Block>) { | ||||||||||||||||||||||||
| fn insert(&self, hash: BlockHash, block: Arc<P2Block>, force: bool) { | ||||||||||||||||||||||||
| //First we check if the block already exists, if it does we don't insert it, if force is set, we overwrite it | ||||||||||||||||||||||||
| if force { | ||||||||||||||||||||||||
| let env = self.file_handle.read().expect("reader"); | ||||||||||||||||||||||||
| let store = env.open_single("block_cache_v2", StoreOptions::create()).unwrap(); | ||||||||||||||||||||||||
| let reader = env.read().expect("reader"); | ||||||||||||||||||||||||
| let block = store.get(&reader, hash.as_bytes()).unwrap(); | ||||||||||||||||||||||||
| // we dont want to deserialise this block, so we just check if it exists | ||||||||||||||||||||||||
| if block.is_some() { | ||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| // Retry if the map is full | ||||||||||||||||||||||||
| // This weird pattern of setting a bool is so that the env is closed before resizing, otherwise | ||||||||||||||||||||||||
| // you can't resize with active transactions. | ||||||||||||||||||||||||
|
|
@@ -225,7 +228,7 @@ fn resize_db(env: &Rkv<LmdbEnvironment>) { | |||||||||||||||||||||||
| pub trait BlockCache { | ||||||||||||||||||||||||
| fn get(&self, hash: &BlockHash) -> Option<Arc<P2Block>>; | ||||||||||||||||||||||||
| fn delete(&self, hash: &BlockHash); | ||||||||||||||||||||||||
| fn insert(&self, hash: BlockHash, block: Arc<P2Block>); | ||||||||||||||||||||||||
| fn insert(&self, hash: BlockHash, block: Arc<P2Block>, force: bool); | ||||||||||||||||||||||||
| fn all_blocks(&self) -> Result<Vec<Arc<P2Block>>, Error>; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
@@ -256,7 +259,7 @@ pub mod test { | |||||||||||||||||||||||
| self.blocks.write().unwrap().remove(hash); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| fn insert(&self, hash: BlockHash, block: Arc<P2Block>) { | ||||||||||||||||||||||||
| fn insert(&self, hash: BlockHash, block: Arc<P2Block>, force: bool) { | ||||||||||||||||||||||||
| self.blocks.write().unwrap().insert(hash, block); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
Comment on lines
+262
to
264
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs implementation of force parameter The - fn insert(&self, hash: BlockHash, block: Arc<P2Block>, force: bool) {
- self.blocks.write().unwrap().insert(hash, block);
+ fn insert(&self, hash: BlockHash, block: Arc<P2Block>, force: bool) {
+ if !force {
+ if self.blocks.read().unwrap().contains_key(&hash) {
+ return;
+ }
+ }
+ self.blocks.write().unwrap().insert(hash, block);
}📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improved block insertion logic with force parameter
The updated
insertmethod now includes aforceparameter that determines whether to check for existing blocks before insertion. This is the key change that prevents recreating the database on start.However, the implementation logic seems inverted:
forceis true, it checks if the block exists and returns early if it doesforceis actually preventing insertion rather than forcing itThe parameter should be renamed for clarity or the logic should be inverted:
Alternatively, invert the logic if "force" should mean "overwrite existing":
📝 Committable suggestion