Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-datastore
Commits
14f793f3
Commit
14f793f3
authored
Jan 29, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
elastigo: use Blake2b hash for ids
parent
b0bb7a5c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
6 deletions
+25
-6
elastigo/datastore.go
elastigo/datastore.go
+25
-6
No files found.
elastigo/datastore.go
View file @
14f793f3
...
...
@@ -2,6 +2,7 @@ package elastigo
import
(
"fmt"
"github.com/codahale/blake2"
ds
"github.com/jbenet/datastore.go"
"github.com/mattbaird/elastigo/api"
"github.com/mattbaird/elastigo/core"
...
...
@@ -24,6 +25,10 @@ type Address struct {
type
Datastore
struct
{
addr
Address
index
string
// Elastic search does not allow slashes in their object ids,
// so we hash the key. By default, we use the provided BlakeKeyHash
KeyHash
func
(
ds
.
Key
)
string
}
func
NewDatastore
(
addr
Address
,
index
string
)
(
*
Datastore
,
error
)
{
...
...
@@ -38,8 +43,9 @@ func NewDatastore(addr Address, index string) (*Datastore, error) {
GlobalInstance
=
addr
return
&
Datastore
{
addr
:
addr
,
index
:
index
,
addr
:
addr
,
index
:
index
,
KeyHash
:
BlakeKeyHash
,
},
nil
}
...
...
@@ -54,7 +60,8 @@ func (d *Datastore) Index(key ds.Key) string {
// value should be JSON serializable.
func
(
d
*
Datastore
)
Put
(
key
ds
.
Key
,
value
interface
{})
(
err
error
)
{
res
,
err
:=
core
.
Index
(
false
,
d
.
Index
(
key
),
key
.
Type
(),
key
.
Name
(),
value
)
id
:=
d
.
KeyHash
(
key
)
res
,
err
:=
core
.
Index
(
false
,
d
.
Index
(
key
),
key
.
Type
(),
id
,
value
)
if
err
!=
nil
{
return
err
}
...
...
@@ -65,7 +72,8 @@ func (d *Datastore) Put(key ds.Key, value interface{}) (err error) {
}
func
(
d
*
Datastore
)
Get
(
key
ds
.
Key
)
(
value
interface
{},
err
error
)
{
res
,
err
:=
core
.
Get
(
false
,
d
.
Index
(
key
),
key
.
Type
(),
key
.
Name
())
id
:=
d
.
KeyHash
(
key
)
res
,
err
:=
core
.
Get
(
false
,
d
.
Index
(
key
),
key
.
Type
(),
id
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -76,11 +84,13 @@ func (d *Datastore) Get(key ds.Key) (value interface{}, err error) {
}
func
(
d
*
Datastore
)
Has
(
key
ds
.
Key
)
(
exists
bool
,
err
error
)
{
return
core
.
Exists
(
false
,
d
.
Index
(
key
),
key
.
Type
(),
key
.
Name
())
id
:=
d
.
KeyHash
(
key
)
return
core
.
Exists
(
false
,
d
.
Index
(
key
),
key
.
Type
(),
id
)
}
func
(
d
*
Datastore
)
Delete
(
key
ds
.
Key
)
(
err
error
)
{
res
,
err
:=
core
.
Delete
(
false
,
d
.
Index
(
key
),
key
.
Type
(),
key
.
Name
(),
0
,
""
)
id
:=
d
.
KeyHash
(
key
)
res
,
err
:=
core
.
Delete
(
false
,
d
.
Index
(
key
),
key
.
Type
(),
id
,
0
,
""
)
if
err
!=
nil
{
return
err
}
...
...
@@ -89,3 +99,12 @@ func (d *Datastore) Delete(key ds.Key) (err error) {
}
return
nil
}
// Hash a key and return the first 16 hex chars of its blake2b hash.
// basically: Blake2b(key).HexString[:16]
func
BlakeKeyHash
(
key
ds
.
Key
)
string
{
h
:=
blake2
.
NewBlake2B
()
h
.
Write
(
key
.
Bytes
())
d
:=
h
.
Sum
(
nil
)
return
fmt
.
Sprintf
(
"%x"
,
d
)[
:
16
]
}
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