Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
parity-test-sync
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
parity
parity-test-sync
Commits
2db779a6
Commit
2db779a6
authored
9 years ago
by
Gav Wood
Browse files
Options
Downloads
Patches
Plain Diff
Memory DB.
parent
6477b2ad
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
src/bytes.rs
+2
-0
2 additions, 0 deletions
src/bytes.rs
src/hashdb.rs
+9
-0
9 additions, 0 deletions
src/hashdb.rs
src/lib.rs
+8
-6
8 additions, 6 deletions
src/lib.rs
src/memorydb.rs
+168
-0
168 additions, 0 deletions
src/memorydb.rs
src/sha3.rs
+1
-1
1 addition, 1 deletion
src/sha3.rs
with
188 additions
and
7 deletions
src/bytes.rs
+
2
−
0
View file @
2db779a6
...
...
@@ -10,6 +10,8 @@ use std::fmt;
use
std
::
error
::
Error
as
StdError
;
use
uint
::{
U128
,
U256
};
pub
type
Bytes
=
Vec
<
u8
>
;
pub
trait
BytesConvertable
{
fn
bytes
(
&
self
)
->
&
[
u8
];
}
...
...
This diff is collapsed.
Click to expand it.
src/hashdb.rs
0 → 100644
+
9
−
0
View file @
2db779a6
use
hash
::
*
;
use
bytes
::
Bytes
;
pub
trait
HashDB
{
fn
lookup
(
&
self
,
key
:
&
H256
)
->
Option
<&
Bytes
>
;
fn
exists
(
&
self
,
key
:
&
H256
)
->
bool
;
fn
insert
(
&
mut
self
,
value
:
&
[
u8
])
->
H256
;
fn
kill
(
&
mut
self
,
key
:
&
H256
);
}
This diff is collapsed.
Click to expand it.
src/lib.rs
+
8
−
6
View file @
2db779a6
...
...
@@ -6,8 +6,6 @@ extern crate tiny_keccak;
#[macro_use]
extern
crate
log
;
pub
use
std
::
str
::
FromStr
;
pub
mod
error
;
pub
mod
hash
;
pub
mod
uint
;
...
...
@@ -16,11 +14,15 @@ pub mod rlp;
pub
mod
vector
;
pub
mod
db
;
pub
mod
sha3
;
pub
mod
hashdb
;
pub
mod
memorydb
;
//pub mod network;
pub
type
Bytes
=
Vec
<
u8
>
;
pub
use
std
::
str
::
FromStr
;
pub
use
hash
::
*
;
pub
use
sha3
::
*
;
pub
use
bytes
::
*
;
pub
use
hashdb
::
*
;
pub
use
memorydb
::
*
;
#[test]
fn
it_works
()
{
}
This diff is collapsed.
Click to expand it.
src/memorydb.rs
0 → 100644
+
168
−
0
View file @
2db779a6
//! Reference-counted memory-based HashDB implementation.
//!
//! # Example
//! ```rust
//! extern crate ethcore_util;
//! use ethcore_util::hashdb::*;
//! use ethcore_util::memorydb::*;
//! fn main() {
//! let mut m = MemoryDB::new();
//! let d = "Hello world!".as_bytes();
//!
//! let k = m.insert(d);
//! assert!(m.exists(&k));
//! assert_eq!(m.lookup(&k).unwrap(), &d);
//!
//! m.insert(d);
//! assert!(m.exists(&k));
//!
//! m.kill(&k);
//! assert!(m.exists(&k));
//!
//! m.kill(&k);
//! assert!(!m.exists(&k));
//!
//! m.insert(d);
//! assert!(m.exists(&k));
//! assert_eq!(m.lookup(&k).unwrap(), &d);
//!
//! m.kill(&k);
//! assert!(!m.exists(&k));
//! }
//! ```
use
hash
::
*
;
use
bytes
::
*
;
use
sha3
::
*
;
use
hashdb
::
*
;
use
std
::
collections
::
HashMap
;
#[derive(Debug,Clone)]
pub
struct
MemoryDB
{
data
:
HashMap
<
H256
,
(
Bytes
,
i32
)
>
,
}
impl
MemoryDB
{
pub
fn
new
()
->
MemoryDB
{
MemoryDB
{
data
:
HashMap
::
new
()
}
}
pub
fn
clear
(
&
mut
self
)
{
self
.data
.clear
();
}
pub
fn
purge
(
&
mut
self
)
{
let
empties
:
Vec
<
_
>
=
self
.data
.iter
()
.filter
(|
&
(
_
,
&
(
_
,
rc
))|
rc
<=
0
)
.map
(|(
k
,
_
)|
k
.clone
())
.collect
();
for
empty
in
empties
{
self
.data
.remove
(
&
empty
);
}
}
}
impl
HashDB
for
MemoryDB
{
/// Do a hash dereference and look up a given hash into the bytes that make it up,
/// returning None if nothing is found (or if the only entries have 0 or fewer
/// references).
///
/// # Examples
/// ```rust
/// extern crate ethcore_util;
/// use ethcore_util::hashdb::*;
/// use ethcore_util::memorydb::*;
/// fn main() {
/// let mut m = MemoryDB::new();
/// let hello_bytes = "Hello world!".as_bytes();
/// let hash = m.insert(hello_bytes);
/// assert_eq!(m.lookup(&hash).unwrap(), &hello_bytes);
/// }
/// ```
fn
lookup
(
&
self
,
key
:
&
H256
)
->
Option
<&
Bytes
>
{
match
self
.data
.get
(
key
)
{
Some
(
&
(
ref
d
,
rc
))
if
rc
>
0
=>
Some
(
d
),
_
=>
None
}
}
/// Check for the existance of a hash-key.
///
/// # Examples
/// ```rust
/// extern crate ethcore_util;
/// use ethcore_util::hashdb::*;
/// use ethcore_util::memorydb::*;
/// use ethcore_util::sha3::*;
/// fn main() {
/// let mut m = MemoryDB::new();
/// let hello_bytes = "Hello world!".as_bytes();
/// assert!(!m.exists(&hello_bytes.sha3()));
/// let key = m.insert(hello_bytes);
/// assert!(m.exists(&key));
/// m.kill(&key);
/// assert!(!m.exists(&key));
/// }
/// ```
fn
exists
(
&
self
,
key
:
&
H256
)
->
bool
{
match
self
.data
.get
(
key
)
{
Some
(
&
(
_
,
x
))
if
x
>
0
=>
true
,
_
=>
false
}
}
/// Insert a datum item into the DB and return the datum's hash for a later lookup. Insertions
/// are counted and the equivalent number of `kill()`s must be performed before the data
/// is considered dead.
///
/// # Examples
/// ```rust
/// extern crate ethcore_util;
/// use ethcore_util::hashdb::*;
/// use ethcore_util::memorydb::*;
/// use ethcore_util::hash::*;
/// fn main() {
/// let mut m = MemoryDB::new();
/// let key = m.insert("Hello world!".as_bytes());
/// assert!(m.exists(&key));
/// }
/// ```
fn
insert
(
&
mut
self
,
value
:
&
[
u8
])
->
H256
{
let
key
=
value
.sha3
();
if
match
self
.data
.get_mut
(
&
key
)
{
Some
(
&
mut
(
ref
mut
old_value
,
ref
mut
rc
@
0
))
=>
{
*
old_value
=
From
::
from
(
value
.bytes
());
*
rc
=
1
;
false
},
Some
(
&
mut
(
_
,
ref
mut
x
))
=>
{
*
x
+=
1
;
false
}
,
None
=>
true
,
}{
// ... None falls through into...
self
.data
.insert
(
key
,
(
From
::
from
(
value
.bytes
()),
1
));
}
key
}
/// Remove a datum previously inserted. Insertions can be "owed" such that the same number of inserts may
/// happen without the data being eventually being inserted into the DB.
///
/// # Examples
/// ```rust
/// extern crate ethcore_util;
/// use ethcore_util::hashdb::*;
/// use ethcore_util::memorydb::*;
/// use ethcore_util::sha3::*;
/// fn main() {
/// let mut m = MemoryDB::new();
/// let d = "Hello world!".as_bytes();
/// let key = &d.sha3();
/// m.kill(key); // OK - we now owe an insertion.
/// assert!(!m.exists(key));
/// m.insert(d); // OK - now it's "empty" again.
/// assert!(!m.exists(key));
/// m.insert(d); // OK - now we've
/// assert_eq!(m.lookup(key).unwrap(), &d);
/// }
/// ```
fn
kill
(
&
mut
self
,
key
:
&
H256
)
{
if
match
self
.data
.get_mut
(
key
)
{
Some
(
&
mut
(
_
,
ref
mut
x
))
=>
{
*
x
-=
1
;
false
}
None
=>
true
}{
// ... None falls through into...
self
.data
.insert
(
*
key
,
(
Bytes
::
new
(),
-
1
));
}
}
}
This diff is collapsed.
Click to expand it.
src/sha3.rs
+
1
−
1
View file @
2db779a6
...
...
@@ -3,7 +3,7 @@ use tiny_keccak::keccak_256;
use
bytes
::
BytesConvertable
;
use
hash
::
H256
;
trait
Hashable
{
pub
trait
Hashable
{
fn
sha3
(
&
self
)
->
H256
;
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment