Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
e4a6ee3d
Commit
e4a6ee3d
authored
Feb 16, 2014
by
obscuren
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added dirty tracking on the cache
parent
72640441
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
2 deletions
+35
-2
trie.go
ethutil/trie.go
+15
-2
trie_test.go
ethutil/trie_test.go
+20
-0
No files found.
ethutil/trie.go
View file @
e4a6ee3d
...
@@ -20,8 +20,9 @@ func (n *Node) Copy() *Node {
...
@@ -20,8 +20,9 @@ func (n *Node) Copy() *Node {
}
}
type
Cache
struct
{
type
Cache
struct
{
nodes
map
[
string
]
*
Node
nodes
map
[
string
]
*
Node
db
Database
db
Database
IsDirty
bool
}
}
func
NewCache
(
db
Database
)
*
Cache
{
func
NewCache
(
db
Database
)
*
Cache
{
...
@@ -36,6 +37,7 @@ func (cache *Cache) Put(v interface{}) interface{} {
...
@@ -36,6 +37,7 @@ func (cache *Cache) Put(v interface{}) interface{} {
sha
:=
Sha3Bin
(
enc
)
sha
:=
Sha3Bin
(
enc
)
cache
.
nodes
[
string
(
sha
)]
=
NewNode
(
sha
,
value
,
true
)
cache
.
nodes
[
string
(
sha
)]
=
NewNode
(
sha
,
value
,
true
)
cache
.
IsDirty
=
true
return
sha
return
sha
}
}
...
@@ -60,12 +62,18 @@ func (cache *Cache) Get(key []byte) *Value {
...
@@ -60,12 +62,18 @@ func (cache *Cache) Get(key []byte) *Value {
}
}
func
(
cache
*
Cache
)
Commit
()
{
func
(
cache
*
Cache
)
Commit
()
{
// Don't try to commit if it isn't dirty
if
!
cache
.
IsDirty
{
return
}
for
key
,
node
:=
range
cache
.
nodes
{
for
key
,
node
:=
range
cache
.
nodes
{
if
node
.
Dirty
{
if
node
.
Dirty
{
cache
.
db
.
Put
([]
byte
(
key
),
node
.
Value
.
Encode
())
cache
.
db
.
Put
([]
byte
(
key
),
node
.
Value
.
Encode
())
node
.
Dirty
=
false
node
.
Dirty
=
false
}
}
}
}
cache
.
IsDirty
=
false
// If the nodes grows beyond the 200 entries we simple empty it
// If the nodes grows beyond the 200 entries we simple empty it
// FIXME come up with something better
// FIXME come up with something better
...
@@ -80,6 +88,7 @@ func (cache *Cache) Undo() {
...
@@ -80,6 +88,7 @@ func (cache *Cache) Undo() {
delete
(
cache
.
nodes
,
key
)
delete
(
cache
.
nodes
,
key
)
}
}
}
}
cache
.
IsDirty
=
false
}
}
// A (modified) Radix Trie implementation. The Trie implements
// A (modified) Radix Trie implementation. The Trie implements
...
@@ -103,6 +112,10 @@ func (t *Trie) Sync() {
...
@@ -103,6 +112,10 @@ func (t *Trie) Sync() {
t
.
cache
.
Commit
()
t
.
cache
.
Commit
()
}
}
func
(
t
*
Trie
)
Undo
()
{
t
.
cache
.
Undo
()
}
/*
/*
* Public (query) interface functions
* Public (query) interface functions
*/
*/
...
...
ethutil/trie_test.go
View file @
e4a6ee3d
...
@@ -45,6 +45,26 @@ func TestTrieSync(t *testing.T) {
...
@@ -45,6 +45,26 @@ func TestTrieSync(t *testing.T) {
}
}
}
}
func
TestTrieDirtyTracking
(
t
*
testing
.
T
)
{
_
,
trie
:=
New
()
trie
.
Update
(
"dog"
,
LONG_WORD
)
if
!
trie
.
cache
.
IsDirty
{
t
.
Error
(
"Expected trie to be dirty"
)
}
trie
.
Sync
()
if
trie
.
cache
.
IsDirty
{
t
.
Error
(
"Expected trie not to be dirty"
)
}
trie
.
Update
(
"test"
,
LONG_WORD
)
trie
.
cache
.
Undo
()
if
trie
.
cache
.
IsDirty
{
t
.
Error
(
"Expected trie not to be dirty"
)
}
}
func
TestTrieReset
(
t
*
testing
.
T
)
{
func
TestTrieReset
(
t
*
testing
.
T
)
{
_
,
trie
:=
New
()
_
,
trie
:=
New
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment