Add info boxes contents
This commit is contained in:
@@ -11,3 +11,5 @@ color-eyre.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
directories.workspace = true
|
||||
|
||||
models = { path = "../models" }
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
pub mod types;
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use color_eyre::eyre::eyre;
|
||||
use rocksdb::{ColumnFamilyDescriptor, IteratorMode, OptimisticTransactionDB, Options};
|
||||
use serde::{Serialize};
|
||||
use serde::de::DeserializeOwned;
|
||||
use crate::types::{RocksColumn, RocksReference, RocksReferences};
|
||||
use color_eyre::Result;
|
||||
use directories::BaseDirs;
|
||||
use lazy_static::lazy_static;
|
||||
use models::db::{RocksColumn, RocksReference, RocksReferences};
|
||||
|
||||
const APP_DIR_NAME: &str = "sus_manager";
|
||||
lazy_static! {
|
||||
@@ -37,39 +36,36 @@ pub struct RocksDBFactory {
|
||||
cfs: Vec<String>,
|
||||
path: PathBuf,
|
||||
db_opts: Options,
|
||||
cf_opts: Options,
|
||||
context: Option<RocksDB>
|
||||
cf_opts: Options
|
||||
}
|
||||
|
||||
impl RocksDBFactory {
|
||||
pub fn new(path: PathBuf, db_opts: Options, cf_opts: Options) -> Result<Self> {
|
||||
let instance = Self {
|
||||
let mut instance = Self {
|
||||
cfs: vec![],
|
||||
path,
|
||||
db_opts,
|
||||
cf_opts,
|
||||
context: None
|
||||
cf_opts
|
||||
};
|
||||
instance.register::<models::dlsite::DLSiteManiax>();
|
||||
instance.register::<models::dlsite::DLSiteCategory>();
|
||||
instance.register::<models::dlsite::DLSiteGenre>();
|
||||
if !instance.path.exists() {
|
||||
std::fs::create_dir_all(instance.path.as_path())?;
|
||||
}
|
||||
Ok(instance)
|
||||
}
|
||||
|
||||
pub fn register<T>(&mut self) where T: RocksColumn {
|
||||
fn register<T>(&mut self) where T: RocksColumn {
|
||||
self.cfs.push(T::get_column_name());
|
||||
}
|
||||
|
||||
pub fn get_current_context(&mut self) -> Result<RocksDB> {
|
||||
if let Some(context) = &self.context {
|
||||
return Ok(context.clone());
|
||||
}
|
||||
let cfs = self.cfs
|
||||
.iter()
|
||||
.map(|cf| ColumnFamilyDescriptor::new(cf, self.cf_opts.clone()))
|
||||
.collect::<Vec<_>>();
|
||||
let context = RocksDB::new(cfs, self.path.clone(), self.db_opts.clone())?;
|
||||
self.context = Some(context.clone());
|
||||
Ok(context)
|
||||
}
|
||||
}
|
||||
@@ -80,11 +76,17 @@ impl Default for RocksDBFactory {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RocksDB {
|
||||
db: Arc<OptimisticTransactionDB>,
|
||||
}
|
||||
|
||||
impl Drop for RocksDB {
|
||||
fn drop(&mut self) {
|
||||
std::mem::drop(self.db.clone());
|
||||
}
|
||||
}
|
||||
|
||||
impl RocksDB {
|
||||
pub fn new(cfs: Vec<ColumnFamilyDescriptor>, path: PathBuf, db_opts: Options) -> Result<Self> {
|
||||
let db = OptimisticTransactionDB::open_cf_descriptors(
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
|
||||
pub trait RocksColumn {
|
||||
type Id: Serialize + DeserializeOwned + Clone;
|
||||
fn get_id(&self) -> Self::Id;
|
||||
fn set_id(&mut self, id: Self::Id);
|
||||
fn get_column_name() -> String;
|
||||
}
|
||||
|
||||
pub trait RocksReference<T> where T: RocksColumn {
|
||||
fn get_reference_id(&self) -> T::Id;
|
||||
}
|
||||
|
||||
pub trait RocksReferences<T> where T: RocksColumn {
|
||||
fn get_reference_ids(&self) -> Vec<T::Id>;
|
||||
}
|
||||
Reference in New Issue
Block a user