Skip to content

Commit bcd715d

Browse files
committed
cleanup
1 parent 63499a9 commit bcd715d

File tree

15 files changed

+91
-273
lines changed

15 files changed

+91
-273
lines changed

.github/workflows/redbit.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ jobs:
6161
- name: Run Criterion Benchmarks
6262
run: |
6363
cargo bench --package utxo --bench utxo
64-
echo "function,ops/s" > benchmarks.csv
6564
find target/criterion -path "*/base/estimates.json" -print0 | while IFS= read -r -d '' file; do
6665
FUNCTION=$(basename $(dirname $(dirname "$file")))
6766
if [[ "$FUNCTION" == "criterion" ]]; then
@@ -86,7 +85,7 @@ jobs:
8685
- name: Update README
8786
run: |
8887
LIB_CONTENT=$(cat examples/utxo/src/lib.rs | sed 's/^/ /')
89-
MAIN_CONTENT=$(cat examples/utxo/src/db_demo.rs | sed 's/^/ /')
88+
MAIN_CONTENT=$(cat examples/utxo/src/demo.rs | sed 's/^/ /')
9089
9190
# Update BEGIN_LIB and BEGIN_MAIN sections
9291
awk -v lib_content="$LIB_CONTENT" -v main_content="$MAIN_CONTENT" '
@@ -101,7 +100,7 @@ jobs:
101100
# Prepare formatted benchmark lines
102101
HEADER=$(printf "%-45s %10s\n" "function" "ops/s")
103102
BODY=$(tail -n +2 benchmarks.csv | awk -F, '{ printf "%-45s %10s\n", $1, $2 }')
104-
FORMATTED_BENCH="$HEADER\n$BODY"
103+
FORMATTED_BENCH="$HEADER\n-------------------------------------------------------------\n$BODY"
105104
106105
# Escape double quotes and backslashes
107106
ESCAPED_BENCH=$(echo -e "$FORMATTED_BENCH" | sed 's/\\/\\\\/g; s/"/\\"/g')

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/.idea
2-
/target
2+
/target/
33
todo
4-
Cargo.lock
4+
Cargo.lock

README.md

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ through auto-generated REST API.
77
- ✅ Rust type and macro system and db engines at the byte level
88
- ✅ decentralized persistence options to maximize indexing speed and minimize data size
99
- ✅ meta space : self-tested and self-documented db & http layers of code derived from annotated structs
10+
- ✅ maximizing R/W speed while minimizing data size using hierarchical data structures of smart pointers
1011

1112
### Major Out-of-the-Box Features
1213

@@ -170,7 +171,7 @@ Let's say we want to persist Utxo into Redb using Redbit, declare annotated Stru
170171
```
171172
<!-- END_LIB -->
172173

173-
And R/W entire instances efficiently using indexes and dictionaries `examples/utxo/src/main.rs`:
174+
And R/W entire instances efficiently using indexes and dictionaries `examples/utxo/src/demo.rs`:
174175

175176
<!-- BEGIN_MAIN -->
176177
```rust
@@ -184,7 +185,7 @@ And R/W entire instances efficiently using indexes and dictionaries `examples/ut
184185

185186
println!("Persisting blocks:");
186187
for block in blocks.iter() {
187-
Block::store_unsafe_and_commit(&db, block)?
188+
Block::store_and_commit(&db, block)?
188189
}
189190

190191
let read_tx = db.begin_read()?;
@@ -306,63 +307,65 @@ BlockHeader__get 280582
306307

307308
### Http Endpoints generated
308309
```
309-
Endpoint /block/id/{value}
310-
Endpoint /block?take=
311-
Endpoint /block?first=
312-
Endpoint /block?last=
313-
Endpoint /block/id/{value}
314-
Endpoint /block/id?from=&until=
315-
Endpoint /block/{value}/header
316-
Endpoint /block/{value}/transactions
310+
endpoint description
311+
-------------------------------------------------------------
312+
GET:/block/id/{value} block_get
313+
GET:/block?take= block_take
314+
GET:/block?first= block_first
315+
GET:/block?last= block_last
316+
HEAD:/block/id/{value} block_exists
317+
GET:/block/id?from=&until= block_range
318+
GET:/block/{value}/header block_get_header
319+
GET:/block/{value}/transactions block_get_transactions
317320
318-
Endpoint /blockheader/id/{value}
319-
Endpoint /blockheader?take=
320-
Endpoint /blockheader?first=
321-
Endpoint /blockheader?last=
322-
Endpoint /blockheader/id/{value}
323-
Endpoint /blockheader/id?from=&until=
324-
Endpoint /blockheader/hash/{value}
325-
Endpoint /blockheader/timestamp/{value}
326-
Endpoint /blockheader/timestamp?from=&until=
327-
Endpoint /blockheader/merkle_root/{value}
321+
GET:/blockheader/id/{value} blockheader_get
322+
GET:/blockheader?take= blockheader_take
323+
GET:/blockheader?first= blockheader_first
324+
GET:/blockheader?last= blockheader_last
325+
HEAD:/blockheader/id/{value} blockheader_exists
326+
GET:/blockheader/id?from=&until= blockheader_range
327+
GET:/blockheader/hash/{value} blockheader_get_by_hash
328+
GET:/blockheader/timestamp/{value} blockheader_get_by_timestamp
329+
GET:/blockheader/timestamp?from=&until= blockheader_range_by_timestamp
330+
GET:/blockheader/merkle_root/{value} blockheader_get_by_merkle_root
328331
329-
Endpoint /transaction/id/{value}
330-
Endpoint /transaction?take=
331-
Endpoint /transaction?first=
332-
Endpoint /transaction?last=
333-
Endpoint /transaction/id/{value}
334-
Endpoint /transaction/id/{value}/parent_pk
335-
Endpoint /transaction/id?from=&until=
336-
Endpoint /transaction/hash/{value}
337-
Endpoint /transaction/{value}/utxos
338-
Endpoint /transaction/{value}/inputs
332+
GET:/transaction/id/{value} transaction_get
333+
GET:/transaction?take= transaction_take
334+
GET:/transaction?first= transaction_first
335+
GET:/transaction?last= transaction_last
336+
HEAD:/transaction/id/{value} transaction_exists
337+
GET:/transaction/id/{value}/parent_pk transaction_parent_pk
338+
GET:/transaction/id?from=&until= transaction_range
339+
GET:/transaction/hash/{value} transaction_get_by_hash
340+
GET:/transaction/{value}/utxos transaction_get_utxos
341+
GET:/transaction/{value}/inputs transaction_get_inputs
339342
340-
Endpoint /utxo/id/{value}
341-
Endpoint /utxo?take=
342-
Endpoint /utxo?first=
343-
Endpoint /utxo?last=
344-
Endpoint /utxo/id/{value}
345-
Endpoint /utxo/id/{value}/parent_pk
346-
Endpoint /utxo/id?from=&until=
347-
Endpoint /utxo/datum/{value}
348-
Endpoint /utxo/address/{value}
349-
Endpoint /utxo/{value}/assets
343+
GET:/utxo/id/{value} utxo_get
344+
GET:/utxo?take= utxo_take
345+
GET:/utxo?first= utxo_first
346+
GET:/utxo?last= utxo_last
347+
HEAD:/utxo/id/{value} utxo_exists
348+
GET:/utxo/id/{value}/parent_pk utxo_parent_pk
349+
GET:/utxo/id?from=&until= utxo_range
350+
GET:/utxo/datum/{value} utxo_get_by_datum
351+
GET:/utxo/address/{value} utxo_get_by_address
352+
GET:/utxo/{value}/assets utxo_get_assets
350353
351-
Endpoint /inputref/id/{value}
352-
Endpoint /inputref?take=
353-
Endpoint /inputref?first=
354-
Endpoint /inputref?last=
355-
Endpoint /inputref/id/{value}
356-
Endpoint /inputref/id/{value}/parent_pk
357-
Endpoint /inputref/id?from=&until=
354+
GET:/inputref/id/{value} inputref_get
355+
GET:/inputref?take= inputref_take
356+
GET:/inputref?first= inputref_first
357+
GET:/inputref?last= inputref_last
358+
HEAD:/inputref/id/{value} inputref_exists
359+
GET:/inputref/id/{value}/parent_pk inputref_parent_pk
360+
GET:/inputref/id?from=&until= inputref_range
358361
359-
Endpoint /asset/id/{value}
360-
Endpoint /asset?take=
361-
Endpoint /asset?first=
362-
Endpoint /asset?last=
363-
Endpoint /asset/id/{value}
364-
Endpoint /asset/id/{value}/parent_pk
365-
Endpoint /asset/id?from=&until=
366-
Endpoint /asset/name/{value}
367-
Endpoint /asset/policy_id/{value}
362+
GET:/asset/id/{value} asset_get
363+
GET:/asset?take= asset_take
364+
GET:/asset?first= asset_first
365+
GET:/asset?last= asset_last
366+
HEAD:/asset/id/{value} asset_exists
367+
GET:/asset/id/{value}/parent_pk asset_parent_pk
368+
GET:/asset/id?from=&until= asset_range
369+
GET:/asset/name/{value} asset_get_by_name
370+
GET:/asset/policy_id/{value} asset_get_by_policy_id
368371
```

examples/utxo/benches/utxo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn benchmark_persistence(c: &mut Criterion) {
1919
group.bench_function("Block::store_and_commit", |b| {
2020
b.iter(|| {
2121
for block in blocks.iter() {
22-
Block::store_unsafe_and_commit(&db, block).unwrap();
22+
Block::store_and_commit(&db, block).unwrap();
2323
}
2424
})
2525
});

examples/utxo/src/data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub fn empty_temp_db(name: &str) -> Database {
2424
pub fn init_temp_db(name: &str) -> (Vec<Block>, Database) {
2525
let db = empty_temp_db(name);
2626
let blocks = get_blocks(Height(4), 4, 4, 4);
27-
blocks.iter().for_each(|block| Block::store_unsafe_and_commit(&db, &block).expect("Failed to persist blocks"));
27+
blocks.iter().for_each(|block| Block::store_and_commit(&db, &block).expect("Failed to persist blocks"));
2828
(blocks, db)
2929
}
3030

examples/utxo/src/demo.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub fn run(db: Arc<Database>) -> Result<(), AppError> {
88

99
println!("Persisting blocks:");
1010
for block in blocks.iter() {
11-
Block::store_unsafe_and_commit(&db, block)?
11+
Block::store_and_commit(&db, block)?
1212
}
1313

1414
let read_tx = db.begin_read()?;
@@ -22,6 +22,9 @@ pub fn run(db: Arc<Database>) -> Result<(), AppError> {
2222
Block::range(&read_tx, &first_block.id, &last_block.id)?;
2323
Block::get_transactions(&read_tx, &first_block.id)?;
2424
Block::get_header(&read_tx, &first_block.id)?;
25+
Block::exists(&read_tx, &first_block.id)?;
26+
Block::first(&read_tx)?;
27+
Block::last(&read_tx)?;
2528

2629
println!("Querying block headers:");
2730
let first_block_header = BlockHeader::first(&read_tx)?.unwrap();
@@ -45,6 +48,7 @@ pub fn run(db: Arc<Database>) -> Result<(), AppError> {
4548
Transaction::range(&read_tx, &first_transaction.id, &last_transaction.id)?;
4649
Transaction::get_utxos(&read_tx, &first_transaction.id)?;
4750
Transaction::get_inputs(&read_tx, &first_transaction.id)?;
51+
Transaction::parent_pk(&read_tx, &first_transaction.id)?;
4852

4953
println!("Querying utxos:");
5054
let first_utxo = Utxo::first(&read_tx)?.unwrap();
@@ -56,6 +60,18 @@ pub fn run(db: Arc<Database>) -> Result<(), AppError> {
5660
Utxo::get_by_datum(&read_tx, &first_utxo.datum)?;
5761
Utxo::range(&read_tx, &first_utxo.id, &last_utxo.id)?;
5862
Utxo::get_assets(&read_tx, &first_utxo.id)?;
63+
Utxo::parent_pk(&read_tx, &first_utxo.id)?;
64+
65+
println!("Querying input refs:");
66+
let first_input_ref = InputRef::first(&read_tx)?.unwrap();
67+
let last_input_ref = InputRef::last(&read_tx)?.unwrap();
68+
69+
InputRef::take(&read_tx, 1000)?;
70+
InputRef::exists(&read_tx, &first_input_ref.id)?;
71+
InputRef::get(&read_tx, &first_input_ref.id)?;
72+
InputRef::range(&read_tx, &first_input_ref.id, &last_input_ref.id)?;
73+
InputRef::parent_pk(&read_tx, &first_input_ref.id)?;
74+
5975

6076
println!("Querying assets:");
6177
let first_asset = Asset::first(&read_tx)?.unwrap();
@@ -66,6 +82,7 @@ pub fn run(db: Arc<Database>) -> Result<(), AppError> {
6682
Asset::get_by_name(&read_tx, &first_asset.name)?;
6783
Asset::get_by_policy_id(&read_tx, &first_asset.policy_id)?;
6884
Asset::range(&read_tx, &first_asset.id, &last_asset.id)?;
85+
Asset::parent_pk(&read_tx, &first_asset.id)?;
6986

7087
println!("Deleting blocks:");
7188
for block in blocks.iter() {

examples/utxo/target/macros/Asset

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -367,61 +367,6 @@ impl Asset {
367367
},
368368
})
369369
}
370-
pub fn store(
371-
write_tx: &::redb::WriteTransaction,
372-
instance: &Asset,
373-
) -> Result<(), AppError> {
374-
if is_one_to_many_child && instance.exists(id.parent()) {
375-
let mut table_pk_1 = write_tx.open_table(ASSET_ID)?;
376-
table_pk_1.insert(&instance.id, ())?;
377-
let mut table_col_1 = write_tx.open_table(ASSET_AMOUNT_BY_ID)?;
378-
table_col_1.insert(&instance.id, &instance.amount)?;
379-
let mut dict_pk_by_pk = write_tx.open_table(ASSET_NAME_DICT_PK_BY_ID)?;
380-
let mut value_to_dict_pk = write_tx.open_table(ASSET_NAME_TO_DICT_PK)?;
381-
let mut value_by_dict_pk = write_tx.open_table(ASSET_NAME_BY_DICT_PK)?;
382-
let mut dict_index = write_tx.open_multimap_table(ASSET_NAME_DICT_INDEX)?;
383-
let (birth_id, newly_created) = {
384-
if let Some(guard) = value_to_dict_pk.get(&instance.name)? {
385-
(guard.value().clone(), false)
386-
} else {
387-
let new_birth = instance.id.clone();
388-
(new_birth, true)
389-
}
390-
};
391-
if newly_created {
392-
value_to_dict_pk.insert(&instance.name, &birth_id)?;
393-
value_by_dict_pk.insert(&birth_id, &instance.name)?;
394-
}
395-
dict_pk_by_pk.insert(&instance.id, &birth_id)?;
396-
dict_index.insert(&birth_id, &instance.id)?;
397-
let mut dict_pk_by_pk = write_tx.open_table(ASSET_POLICY_ID_DICT_PK_BY_ID)?;
398-
let mut value_to_dict_pk = write_tx.open_table(ASSET_POLICY_ID_TO_DICT_PK)?;
399-
let mut value_by_dict_pk = write_tx.open_table(ASSET_POLICY_ID_BY_DICT_PK)?;
400-
let mut dict_index = write_tx
401-
.open_multimap_table(ASSET_POLICY_ID_DICT_INDEX)?;
402-
let (birth_id, newly_created) = {
403-
if let Some(guard) = value_to_dict_pk.get(&instance.policy_id)? {
404-
(guard.value().clone(), false)
405-
} else {
406-
let new_birth = instance.id.clone();
407-
(new_birth, true)
408-
}
409-
};
410-
if newly_created {
411-
value_to_dict_pk.insert(&instance.policy_id, &birth_id)?;
412-
value_by_dict_pk.insert(&birth_id, &instance.policy_id)?;
413-
}
414-
dict_pk_by_pk.insert(&instance.id, &birth_id)?;
415-
dict_index.insert(&birth_id, &instance.id)?;
416-
Ok(())
417-
} else {
418-
Err(
419-
AppError::InvalidPrimaryKey(
420-
format!("Cannot store {} with no primary key", stringify!(Asset)),
421-
),
422-
)
423-
}
424-
}
425370
pub fn store(
426371
write_tx: &::redb::WriteTransaction,
427372
instance: &Asset,
@@ -522,7 +467,7 @@ impl Asset {
522467
}
523468
Ok(())
524469
}
525-
pub fn store_unsafe_and_commit(
470+
pub fn store_and_commit(
526471
db: &::redb::Database,
527472
instance: &Asset,
528473
) -> Result<(), AppError> {

examples/utxo/target/macros/Block

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -227,25 +227,6 @@ impl Block {
227227
},
228228
})
229229
}
230-
pub fn store(
231-
write_tx: &::redb::WriteTransaction,
232-
instance: &Block,
233-
) -> Result<(), AppError> {
234-
if is_one_to_many_child && instance.exists(id.parent()) {
235-
let mut table_pk_1 = write_tx.open_table(BLOCK_ID)?;
236-
table_pk_1.insert(&instance.id, ())?;
237-
let child = &instance.header;
238-
BlockHeader::store(&write_tx, child)?;
239-
Transaction::store_many(&write_tx, &instance.transactions)?;
240-
Ok(())
241-
} else {
242-
Err(
243-
AppError::InvalidPrimaryKey(
244-
format!("Cannot store {} with no primary key", stringify!(Block)),
245-
),
246-
)
247-
}
248-
}
249230
pub fn store(
250231
write_tx: &::redb::WriteTransaction,
251232
instance: &Block,
@@ -277,7 +258,7 @@ impl Block {
277258
Transaction::store_many(&write_tx, &children)?;
278259
Ok(())
279260
}
280-
pub fn store_unsafe_and_commit(
261+
pub fn store_and_commit(
281262
db: &::redb::Database,
282263
instance: &Block,
283264
) -> Result<(), AppError> {

0 commit comments

Comments
 (0)