feat: Prefer doing DatabaseView updates atomically

This commit is contained in:
Clément Renault 2018-12-29 20:52:00 +01:00
parent cf5d56e63a
commit f4b04dfb72
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE
2 changed files with 10 additions and 13 deletions

View File

@ -7,6 +7,7 @@ authors = ["Kerollmops <renault.cle@gmail.com>"]
[dependencies] [dependencies]
bincode = "1.0" bincode = "1.0"
byteorder = "1.2" byteorder = "1.2"
crossbeam = "0.6"
fst = "0.3" fst = "0.3"
hashbrown = "0.1" hashbrown = "0.1"
lazy_static = "1.1" lazy_static = "1.1"

View File

@ -1,10 +1,11 @@
use std::sync::{Arc, Mutex, RwLock, RwLockReadGuard}; use std::sync::{Arc, Mutex};
use std::error::Error; use std::error::Error;
use std::path::Path; use std::path::Path;
use rocksdb::rocksdb_options::{DBOptions, IngestExternalFileOptions, ColumnFamilyOptions}; use rocksdb::rocksdb_options::{DBOptions, IngestExternalFileOptions, ColumnFamilyOptions};
use rocksdb::rocksdb::{Writable, Snapshot}; use rocksdb::rocksdb::{Writable, Snapshot};
use rocksdb::{DB, DBVector, MergeOperands}; use rocksdb::{DB, DBVector, MergeOperands};
use crossbeam::atomic::ArcCell;
use crate::database::{DatabaseView, Update, Schema}; use crate::database::{DatabaseView, Update, Schema};
use crate::database::{DATA_INDEX, DATA_SCHEMA}; use crate::database::{DATA_INDEX, DATA_SCHEMA};
@ -17,7 +18,7 @@ pub struct Database {
db: Mutex<Arc<DB>>, db: Mutex<Arc<DB>>,
// This view is updated each time the DB ingests an update // This view is updated each time the DB ingests an update
view: RwLock<DatabaseView<Arc<DB>>>, view: ArcCell<DatabaseView<Arc<DB>>>,
} }
impl Database { impl Database {
@ -44,7 +45,7 @@ impl Database {
let db = Arc::new(db); let db = Arc::new(db);
let snapshot = Snapshot::new(db.clone()); let snapshot = Snapshot::new(db.clone());
let view = RwLock::new(DatabaseView::new(snapshot)?); let view = ArcCell::new(Arc::new(DatabaseView::new(snapshot)?));
Ok(Database { db: Mutex::new(db), view }) Ok(Database { db: Mutex::new(db), view })
} }
@ -68,7 +69,7 @@ impl Database {
let db = Arc::new(db); let db = Arc::new(db);
let snapshot = Snapshot::new(db.clone()); let snapshot = Snapshot::new(db.clone());
let view = RwLock::new(DatabaseView::new(snapshot)?); let view = ArcCell::new(Arc::new(DatabaseView::new(snapshot)?));
Ok(Database { db: Mutex::new(db), view }) Ok(Database { db: Mutex::new(db), view })
} }
@ -101,13 +102,8 @@ impl Database {
Snapshot::new(db.clone()) Snapshot::new(db.clone())
}; };
// Here we will block the view creation for the minimum amount of time: let view = Arc::new(DatabaseView::new(snapshot)?);
// updating the DatabaseView itself with the new database snapshot self.view.set(view);
let view = DatabaseView::new(snapshot)?;
match self.view.write() {
Ok(mut lock) => *lock = view,
Err(e) => return Err(e.to_string().into()),
}
Ok(()) Ok(())
} }
@ -123,8 +119,8 @@ impl Database {
} }
} }
pub fn view(&self) -> RwLockReadGuard<DatabaseView<Arc<DB>>> { pub fn view(&self) -> Arc<DatabaseView<Arc<DB>>> {
self.view.read().unwrap() self.view.get()
} }
} }