added sync pools for hashing
This commit is contained in:
parent
9281fc2971
commit
643e721f82
3 changed files with 33 additions and 32 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
application: cayley-test
|
application: cayley-test
|
||||||
version: 1
|
version: 1
|
||||||
runtime: go
|
runtime: go
|
||||||
api_version: go1.4beta
|
api_version: go1.4.1
|
||||||
|
|
||||||
handlers:
|
handlers:
|
||||||
- url: /.*
|
- url: /.*
|
||||||
|
|
|
||||||
|
|
@ -162,13 +162,13 @@ func (it *Iterator) Contains(v graph.Value) bool {
|
||||||
case quad.Subject:
|
case quad.Subject:
|
||||||
offset = 0
|
offset = 0
|
||||||
case quad.Predicate:
|
case quad.Predicate:
|
||||||
offset = (it.qs.hashSize * 2)
|
offset = (hashSize * 2)
|
||||||
case quad.Object:
|
case quad.Object:
|
||||||
offset = (it.qs.hashSize * 2) * 2
|
offset = (hashSize * 2) * 2
|
||||||
case quad.Label:
|
case quad.Label:
|
||||||
offset = (it.qs.hashSize * 2) * 3
|
offset = (hashSize * 2) * 3
|
||||||
}
|
}
|
||||||
val := t.Hash[offset : offset+(it.qs.hashSize*2)]
|
val := t.Hash[offset : offset+(hashSize*2)]
|
||||||
if val == it.hash {
|
if val == it.hash {
|
||||||
return graph.ContainsLogOut(it, v, true)
|
return graph.ContainsLogOut(it, v, true)
|
||||||
}
|
}
|
||||||
|
|
@ -314,7 +314,7 @@ func (it *Iterator) Describe() graph.Description {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO (stefankoshiw) calculate costs
|
// TODO (panamafrancis) calculate costs
|
||||||
func (it *Iterator) Stats() graph.IteratorStats {
|
func (it *Iterator) Stats() graph.IteratorStats {
|
||||||
size, _ := it.Size()
|
size, _ := it.Size()
|
||||||
return graph.IteratorStats{
|
return graph.IteratorStats{
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"hash"
|
"hash"
|
||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"appengine"
|
"appengine"
|
||||||
"appengine/datastore"
|
"appengine/datastore"
|
||||||
|
|
@ -41,13 +42,15 @@ const (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// Order of quad fields
|
// Order of quad fields
|
||||||
spo = [4]quad.Direction{quad.Subject, quad.Predicate, quad.Object, quad.Label}
|
spo = [4]quad.Direction{quad.Subject, quad.Predicate, quad.Object, quad.Label}
|
||||||
|
hashPool = sync.Pool{
|
||||||
|
New: func() interface{} { return sha1.New() },
|
||||||
|
}
|
||||||
|
hashSize = sha1.Size
|
||||||
)
|
)
|
||||||
|
|
||||||
type QuadStore struct {
|
type QuadStore struct {
|
||||||
hashSize int
|
context appengine.Context
|
||||||
makeHasher func() hash.Hash
|
|
||||||
context appengine.Context
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type MetadataEntry struct {
|
type MetadataEntry struct {
|
||||||
|
|
@ -87,14 +90,12 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func initQuadStore(_ string, _ graph.Options) error {
|
func initQuadStore(_ string, _ graph.Options) error {
|
||||||
// TODO (stefankoshiw) check appengine datastore for consistency
|
// TODO (panamafrancis) check appengine datastore for consistency
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newQuadStore(_ string, options graph.Options) (graph.QuadStore, error) {
|
func newQuadStore(_ string, options graph.Options) (graph.QuadStore, error) {
|
||||||
var qs QuadStore
|
var qs QuadStore
|
||||||
qs.hashSize = sha1.Size
|
|
||||||
qs.makeHasher = sha1.New
|
|
||||||
return &qs, nil
|
return &qs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -109,17 +110,15 @@ func newQuadStoreForRequest(qs graph.QuadStore, options graph.Options) (graph.Qu
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qs *QuadStore) createKeyForQuad(q quad.Quad) *datastore.Key {
|
func (qs *QuadStore) createKeyForQuad(q quad.Quad) *datastore.Key {
|
||||||
hasher := qs.makeHasher()
|
id := hashOf(q.Subject)
|
||||||
id := qs.hashOf(q.Subject, hasher)
|
id += hashOf(q.Predicate)
|
||||||
id += qs.hashOf(q.Predicate, hasher)
|
id += hashOf(q.Object)
|
||||||
id += qs.hashOf(q.Object, hasher)
|
id += hashOf(q.Label)
|
||||||
id += qs.hashOf(q.Label, hasher)
|
|
||||||
return qs.createKeyFromToken(&Token{quadKind, id})
|
return qs.createKeyFromToken(&Token{quadKind, id})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qs *QuadStore) createKeyForNode(n string) *datastore.Key {
|
func (qs *QuadStore) createKeyForNode(n string) *datastore.Key {
|
||||||
hasher := qs.makeHasher()
|
id := hashOf(n)
|
||||||
id := qs.hashOf(n, hasher)
|
|
||||||
return qs.createKeyFromToken(&Token{nodeKind, id})
|
return qs.createKeyFromToken(&Token{nodeKind, id})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -401,8 +400,7 @@ func (qs *QuadStore) QuadsAllIterator() graph.Iterator {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qs *QuadStore) ValueOf(s string) graph.Value {
|
func (qs *QuadStore) ValueOf(s string) graph.Value {
|
||||||
hasher := qs.makeHasher()
|
id := hashOf(s)
|
||||||
id := qs.hashOf(s, hasher)
|
|
||||||
return &Token{Kind: nodeKind, Hash: id}
|
return &Token{Kind: nodeKind, Hash: id}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -419,7 +417,7 @@ func (qs *QuadStore) NameOf(val graph.Value) string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO (stefankoshiw) implement a cache
|
// TODO (panamafrancis) implement a cache
|
||||||
|
|
||||||
node := new(NodeEntry)
|
node := new(NodeEntry)
|
||||||
err := datastore.Get(qs.context, key, node)
|
err := datastore.Get(qs.context, key, node)
|
||||||
|
|
@ -537,21 +535,24 @@ func (qs *QuadStore) QuadDirection(val graph.Value, dir quad.Direction) graph.Va
|
||||||
case quad.Subject:
|
case quad.Subject:
|
||||||
offset = 0
|
offset = 0
|
||||||
case quad.Predicate:
|
case quad.Predicate:
|
||||||
offset = (qs.hashSize * 2)
|
offset = (hashSize * 2)
|
||||||
case quad.Object:
|
case quad.Object:
|
||||||
offset = (qs.hashSize * 2) * 2
|
offset = (hashSize * 2) * 2
|
||||||
case quad.Label:
|
case quad.Label:
|
||||||
offset = (qs.hashSize * 2) * 3
|
offset = (hashSize * 2) * 3
|
||||||
}
|
}
|
||||||
sub := t.Hash[offset : offset+(qs.hashSize*2)]
|
sub := t.Hash[offset : offset+(hashSize*2)]
|
||||||
return &Token{Kind: nodeKind, Hash: sub}
|
return &Token{Kind: nodeKind, Hash: sub}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qs *QuadStore) hashOf(s string, hasher hash.Hash) string {
|
func hashOf(s string) string {
|
||||||
hasher.Reset()
|
h := hashPool.Get().(hash.Hash)
|
||||||
key := make([]byte, 0, qs.hashSize)
|
h.Reset()
|
||||||
hasher.Write([]byte(s))
|
defer hashPool.Put(h)
|
||||||
key = hasher.Sum(key)
|
|
||||||
|
key := make([]byte, 0, hashSize)
|
||||||
|
h.Write([]byte(s))
|
||||||
|
key = h.Sum(key)
|
||||||
return hex.EncodeToString(key)
|
return hex.EncodeToString(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue