Skip to content
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

Provide indexer store base on SQLite #1088

Merged
merged 8 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 206 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 22 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ members = [
"crates/rooch-open-rpc-spec",
"crates/rooch-open-rpc-spec-builder",
"crates/rooch-open-rpc-macros",
"crates/rooch-store"
"crates/rooch-store",
"crates/rooch-indexer"
]

default-members = [
Expand Down Expand Up @@ -97,6 +98,7 @@ rooch-open-rpc-spec = { path = "crates/rooch-open-rpc-spec" }
rooch-open-rpc-spec-builder = { path = "crates/rooch-open-rpc-spec-builder" }
rooch-open-rpc-macros = { path = "crates/rooch-open-rpc-macros" }
rooch-store = { path = "crates/rooch-store" }
rooch-indexer = { path = "crates/rooch-indexer" }

# External crate dependencies.
# Please do not add any test features here: they should be declared by the individual crate.
Expand Down Expand Up @@ -211,6 +213,25 @@ uint = "0.9.5"
open-fastrlp = "0.1.4"
const-hex = "1.6.2"

cached = "0.43.0"
diesel = { version = "2.1.0", features = [
"chrono",
"sqlite",
"r2d2",
"serde_json",
"64-column-tables",
] }
diesel-derive-enum = { version = "2.0.1", features = ["sqlite"] }
diesel_migrations = { version = "2.0.0" }
tap = "1.0.1"
backoff = { version = "0.4.0", features = [
"futures",
"futures-core",
"pin-project-lite",
"tokio",
"tokio_1",
] }

# Note: the BEGIN and END comments below are required for external tooling. Do not remove.
# BEGIN MOVE DEPENDENCIES
move-abigen = { git = "https://github.com/rooch-network/move", rev = "f7e7cc8e0521e5325caaae90c9de5de409d291a9" }
Expand Down
85 changes: 85 additions & 0 deletions crates/rooch-config/src/indexer_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::store_config::R_DEFAULT_DB_DIR;
use crate::{BaseConfig, ConfigModule, RoochOpt};
use anyhow::Result;
use clap::Parser;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::Arc;

pub const ROOCH_INDEXER_DB_FILENAME: &str = "indexer.sqlite";

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, Parser)]
#[clap(name = "Rooch indexer")]
pub struct IndexerConfig {
#[clap(skip)]
pub db_url: Option<String>,

#[serde(skip)]
#[clap(skip)]
base: Option<Arc<BaseConfig>>,
}

impl IndexerConfig {
pub fn merge_with_opt_and_init(&mut self, opt: &RoochOpt, base: Arc<BaseConfig>) -> Result<()> {
self.merge_with_opt(opt, base)?;
self.init()?;
Ok(())
}

pub fn init(&self) -> Result<()> {
let indexer_db = self.clone().get_indexer_db();
if !indexer_db.exists() {
std::fs::create_dir_all(indexer_db.clone())?;
}
println!("IndexerConfig init store dir {:?}", indexer_db);
Ok(())
}

fn base(&self) -> &BaseConfig {
self.base.as_ref().expect("Config should init.")
}

pub fn data_dir(&self) -> &Path {
self.base().data_dir()
}

pub fn get_indexer_db(&self) -> PathBuf {
self.data_dir()
.join(R_DEFAULT_DB_DIR.as_path())
.join(ROOCH_INDEXER_DB_FILENAME)
}
}

impl ConfigModule for IndexerConfig {
fn merge_with_opt(&mut self, _opt: &RoochOpt, base: Arc<BaseConfig>) -> Result<()> {
self.base = Some(base);

Ok(())
}
}

impl std::fmt::Display for IndexerConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
serde_json::to_string(self).map_err(|_e| std::fmt::Error)?
)
}
}

impl FromStr for IndexerConfig {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let deserialized: IndexerConfig = serde_json::from_str(s)?;
Ok(deserialized)
}
}
Loading