good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
bor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
bor
Commits
30f3b4d4
Commit
30f3b4d4
authored
Jan 1, 2014
by
Jeffrey Wilcke
Browse files
Options
Downloads
Patches
Plain Diff
Added a few more comments and cleaned up code
parent
df6f7e8a
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
trie.go
+57
-54
57 additions, 54 deletions
trie.go
with
57 additions
and
54 deletions
trie.go
+
57
−
54
View file @
30f3b4d4
...
@@ -10,6 +10,38 @@ type Database interface {
...
@@ -10,6 +10,38 @@ type Database interface {
Get
(
key
[]
byte
)
([]
byte
,
error
)
Get
(
key
[]
byte
)
([]
byte
,
error
)
}
}
/*
* Trie helper functions
*/
// Helper function for printing out the raw contents of a slice
func
PrintSlice
(
slice
[]
string
)
{
fmt
.
Printf
(
"["
)
for
i
,
val
:=
range
slice
{
fmt
.
Printf
(
"%q"
,
val
)
if
i
!=
len
(
slice
)
-
1
{
fmt
.
Printf
(
","
)
}
}
fmt
.
Printf
(
"]
\n
"
)
}
// RLP Decodes a node in to a [2] or [17] string slice
func
DecodeNode
(
data
[]
byte
)
[]
string
{
dec
,
_
:=
Decode
(
data
,
0
)
if
slice
,
ok
:=
dec
.
([]
interface
{});
ok
{
strSlice
:=
make
([]
string
,
len
(
slice
))
for
i
,
s
:=
range
slice
{
if
str
,
ok
:=
s
.
([]
byte
);
ok
{
strSlice
[
i
]
=
string
(
str
)
}
}
return
strSlice
}
return
nil
}
// A (modified) Radix Trie implementation
type
Trie
struct
{
type
Trie
struct
{
root
string
root
string
db
Database
db
Database
...
@@ -19,18 +51,9 @@ func NewTrie(db Database, root string) *Trie {
...
@@ -19,18 +51,9 @@ func NewTrie(db Database, root string) *Trie {
return
&
Trie
{
db
:
db
,
root
:
""
}
return
&
Trie
{
db
:
db
,
root
:
""
}
}
}
func
(
t
*
Trie
)
Put
(
node
interface
{})
[]
byte
{
/*
//if s, ok := node.([]string); ok {
* Public (query) interface functions
// PrintSlice(s)
*/
//}
enc
:=
Encode
(
node
)
sha
:=
Sha256Bin
(
enc
)
t
.
db
.
Put
([]
byte
(
sha
),
enc
)
return
sha
}
func
(
t
*
Trie
)
Update
(
key
string
,
value
string
)
{
func
(
t
*
Trie
)
Update
(
key
string
,
value
string
)
{
k
:=
CompactHexDecode
(
key
)
k
:=
CompactHexDecode
(
key
)
...
@@ -43,10 +66,29 @@ func (t *Trie) Get(key string) string {
...
@@ -43,10 +66,29 @@ func (t *Trie) Get(key string) string {
return
t
.
GetState
(
t
.
root
,
k
)
return
t
.
GetState
(
t
.
root
,
k
)
}
}
/*
* State functions (shouldn't be needed directly).
*/
// Wrapper around the regular db "Put" which generates a key and value
func
(
t
*
Trie
)
Put
(
node
interface
{})
[]
byte
{
enc
:=
Encode
(
node
)
sha
:=
Sha256Bin
(
enc
)
t
.
db
.
Put
([]
byte
(
sha
),
enc
)
return
sha
}
// Helper function for printing a node (using fetch, decode and slice printing)
func
(
t
*
Trie
)
PrintNode
(
n
string
)
{
data
,
_
:=
t
.
db
.
Get
([]
byte
(
n
))
d
:=
DecodeNode
(
data
)
PrintSlice
(
d
)
}
// Returns the state of an object
// Returns the state of an object
func
(
t
*
Trie
)
GetState
(
node
string
,
key
[]
int
)
string
{
func
(
t
*
Trie
)
GetState
(
node
string
,
key
[]
int
)
string
{
//if Debug { fmt.Println("get =", key) }
// Return the node if key is empty (= found)
// Return the node if key is empty (= found)
if
len
(
key
)
==
0
||
node
==
""
{
if
len
(
key
)
==
0
||
node
==
""
{
return
node
return
node
...
@@ -66,10 +108,6 @@ func (t *Trie) GetState(node string, key []int) string {
...
@@ -66,10 +108,6 @@ func (t *Trie) GetState(node string, key []int) string {
k
:=
CompactDecode
(
currentNode
[
0
])
k
:=
CompactDecode
(
currentNode
[
0
])
v
:=
currentNode
[
1
]
v
:=
currentNode
[
1
]
//fmt.Println(k, key)
//fmt.Printf("k1:%v\nk2:%v\n", k, key[:len(k)-1])
//fmt.Println(len(key), ">=", len(k)-1, "&&", k, key[:len(k)])
if
len
(
key
)
>=
len
(
k
)
&&
CompareIntSlice
(
k
,
key
[
:
len
(
k
)])
{
if
len
(
key
)
>=
len
(
k
)
&&
CompareIntSlice
(
k
,
key
[
:
len
(
k
)])
{
return
t
.
GetState
(
v
,
key
[
len
(
k
)
:
])
return
t
.
GetState
(
v
,
key
[
len
(
k
)
:
])
}
else
{
}
else
{
...
@@ -95,47 +133,13 @@ func (t *Trie) UpdateState(node string, key []int, value string) string {
...
@@ -95,47 +133,13 @@ func (t *Trie) UpdateState(node string, key []int, value string) string {
return
""
return
""
}
}
func
DecodeNode
(
data
[]
byte
)
[]
string
{
dec
,
_
:=
Decode
(
data
,
0
)
if
slice
,
ok
:=
dec
.
([]
interface
{});
ok
{
strSlice
:=
make
([]
string
,
len
(
slice
))
for
i
,
s
:=
range
slice
{
if
str
,
ok
:=
s
.
([]
byte
);
ok
{
strSlice
[
i
]
=
string
(
str
)
}
}
return
strSlice
}
return
nil
}
func
(
t
*
Trie
)
PrintNode
(
n
string
)
{
data
,
_
:=
t
.
db
.
Get
([]
byte
(
n
))
d
:=
DecodeNode
(
data
)
PrintSlice
(
d
)
}
func
PrintSlice
(
slice
[]
string
)
{
fmt
.
Printf
(
"["
)
for
i
,
val
:=
range
slice
{
fmt
.
Printf
(
"%q"
,
val
)
if
i
!=
len
(
slice
)
-
1
{
fmt
.
Printf
(
","
)
}
}
fmt
.
Printf
(
"]
\n
"
)
}
func
(
t
*
Trie
)
InsertState
(
node
string
,
key
[]
int
,
value
string
)
string
{
func
(
t
*
Trie
)
InsertState
(
node
string
,
key
[]
int
,
value
string
)
string
{
//if Debug { fmt.Println("insrt", key, value, "node:", node) }
if
len
(
key
)
==
0
{
if
len
(
key
)
==
0
{
return
value
return
value
}
}
//fmt.Println(node)
// New node
// Root node!
if
node
==
""
{
if
node
==
""
{
newNode
:=
[]
string
{
CompactEncode
(
key
),
value
}
newNode
:=
[]
string
{
CompactEncode
(
key
),
value
}
...
@@ -195,4 +199,3 @@ func (t *Trie) InsertState(node string, key []int, value string) string {
...
@@ -195,4 +199,3 @@ func (t *Trie) InsertState(node string, key []int, value string) string {
return
""
return
""
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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