Skip to content

Commit 4d605ac

Browse files
authored
feat: add ephemeral store (#1723)
1 parent c71b593 commit 4d605ac

File tree

5 files changed

+574
-0
lines changed

5 files changed

+574
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use std::path::PathBuf;
2+
3+
use ethportal_api::types::network::Subnetwork;
4+
use r2d2::Pool;
5+
use r2d2_sqlite::SqliteConnectionManager;
6+
7+
use crate::{versioned::ContentType, PortalStorageConfig};
8+
9+
/// The config for the EphemeralV1Store
10+
#[derive(Clone, Debug)]
11+
pub struct EphemeralV1StoreConfig {
12+
pub content_type: ContentType,
13+
pub subnetwork: Subnetwork,
14+
pub node_data_dir: PathBuf,
15+
pub sql_connection_pool: Pool<SqliteConnectionManager>,
16+
}
17+
18+
#[allow(unused)]
19+
impl EphemeralV1StoreConfig {
20+
pub fn new(
21+
content_type: ContentType,
22+
subnetwork: Subnetwork,
23+
config: PortalStorageConfig,
24+
) -> Self {
25+
Self {
26+
content_type,
27+
subnetwork,
28+
node_data_dir: config.node_data_dir,
29+
sql_connection_pool: config.sql_connection_pool,
30+
}
31+
}
32+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
mod config;
2+
pub(super) mod sql;
3+
mod store;
4+
5+
pub use config::EphemeralV1StoreConfig;
6+
#[allow(unused)]
7+
pub use store::EphemeralV1Store;
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use crate::versioned::ContentType;
2+
3+
/// The name of the sql table. The `eph1` stands for `ephemeral_v1`.
4+
pub fn table_name(content_type: &ContentType) -> String {
5+
format!("eph1_{content_type}")
6+
}
7+
8+
pub fn create_table(content_type: &ContentType) -> String {
9+
format!(
10+
"
11+
CREATE TABLE IF NOT EXISTS {0} (
12+
content_id BLOB PRIMARY KEY,
13+
content_key BLOB NOT NULL,
14+
content_value BLOB NOT NULL,
15+
type INTEGER NOT NULL,
16+
slot INTEGER NOT NULL,
17+
content_size INTEGER NOT NULL
18+
);
19+
CREATE INDEX IF NOT EXISTS {0}_type_idx ON {0} (type);
20+
CREATE INDEX IF NOT EXISTS {0}_slot_idx ON {0} (slot);
21+
CREATE INDEX IF NOT EXISTS {0}_content_size_idx ON {0} (content_size);
22+
",
23+
table_name(content_type)
24+
)
25+
}
26+
27+
pub fn insert(content_type: &ContentType) -> String {
28+
format!(
29+
"
30+
INSERT OR IGNORE INTO {} (
31+
content_id,
32+
content_key,
33+
content_value,
34+
type,
35+
slot,
36+
content_size
37+
)
38+
VALUES (
39+
:content_id,
40+
:content_key,
41+
:content_value,
42+
:type,
43+
:slot,
44+
:content_size
45+
)",
46+
table_name(content_type)
47+
)
48+
}
49+
50+
pub fn delete(content_type: &ContentType) -> String {
51+
format!(
52+
"DELETE FROM {}
53+
WHERE content_id = :content_id
54+
RETURNING content_size",
55+
table_name(content_type)
56+
)
57+
}
58+
59+
pub fn lookup_key(content_type: &ContentType) -> String {
60+
format!(
61+
"SELECT content_key FROM {} WHERE content_id = :content_id LIMIT 1",
62+
table_name(content_type)
63+
)
64+
}
65+
66+
pub fn lookup_value(content_type: &ContentType) -> String {
67+
format!(
68+
"SELECT content_value FROM {} WHERE content_id = :content_id LIMIT 1",
69+
table_name(content_type)
70+
)
71+
}
72+
73+
pub fn entry_count_and_size(content_type: &ContentType) -> String {
74+
format!(
75+
"SELECT COUNT(*) as count, TOTAL(content_size) as used_capacity FROM {}",
76+
table_name(content_type)
77+
)
78+
}

0 commit comments

Comments
 (0)