Merge hash pool in from master
Conflicts: graph/leveldb/triplestore.go graph/mongo/triplestore.go
This commit is contained in:
commit
8720e17d87
4 changed files with 76 additions and 68 deletions
|
|
@ -177,13 +177,13 @@ func (it *Iterator) Contains(v graph.Value) bool {
|
|||
case quad.Subject:
|
||||
offset = 0
|
||||
case quad.Predicate:
|
||||
offset = (it.qs.hasherSize * 2)
|
||||
offset = (hashSize * 2)
|
||||
case quad.Object:
|
||||
offset = (it.qs.hasherSize * 2) * 2
|
||||
offset = (hashSize * 2) * 2
|
||||
case quad.Label:
|
||||
offset = (it.qs.hasherSize * 2) * 3
|
||||
offset = (hashSize * 2) * 3
|
||||
}
|
||||
val := v.(string)[offset : it.qs.hasherSize*2+offset]
|
||||
val := v.(string)[offset : hashSize*2+offset]
|
||||
if val == it.hash {
|
||||
it.result = v
|
||||
return graph.ContainsLogOut(it, v, true)
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ import (
|
|||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"hash"
|
||||
"sync"
|
||||
|
||||
"gopkg.in/mgo.v2"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
|
|
@ -34,12 +35,17 @@ func init() {
|
|||
|
||||
const DefaultDBName = "cayley"
|
||||
|
||||
var (
|
||||
hashPool = sync.Pool{
|
||||
New: func() interface{} { return sha1.New() },
|
||||
}
|
||||
hashSize = sha1.Size
|
||||
)
|
||||
|
||||
type TripleStore struct {
|
||||
session *mgo.Session
|
||||
db *mgo.Database
|
||||
hasherSize int
|
||||
makeHasher func() hash.Hash
|
||||
idCache *IDLru
|
||||
session *mgo.Session
|
||||
db *mgo.Database
|
||||
idCache *IDLru
|
||||
}
|
||||
|
||||
func createNewMongoGraph(addr string, options graph.Options) error {
|
||||
|
|
@ -91,26 +97,26 @@ func newTripleStore(addr string, options graph.Options) (graph.TripleStore, erro
|
|||
}
|
||||
qs.db = conn.DB(dbName)
|
||||
qs.session = conn
|
||||
qs.hasherSize = sha1.Size
|
||||
qs.makeHasher = sha1.New
|
||||
qs.idCache = NewIDLru(1 << 16)
|
||||
return &qs, nil
|
||||
}
|
||||
|
||||
func (qs *TripleStore) getIdForQuad(t quad.Quad) string {
|
||||
hasher := qs.makeHasher()
|
||||
id := qs.convertStringToByteHash(t.Subject, hasher)
|
||||
id += qs.convertStringToByteHash(t.Predicate, hasher)
|
||||
id += qs.convertStringToByteHash(t.Object, hasher)
|
||||
id += qs.convertStringToByteHash(t.Label, hasher)
|
||||
id := qs.convertStringToByteHash(t.Subject)
|
||||
id += qs.convertStringToByteHash(t.Predicate)
|
||||
id += qs.convertStringToByteHash(t.Object)
|
||||
id += qs.convertStringToByteHash(t.Label)
|
||||
return id
|
||||
}
|
||||
|
||||
func (qs *TripleStore) convertStringToByteHash(s string, hasher hash.Hash) string {
|
||||
hasher.Reset()
|
||||
key := make([]byte, 0, qs.hasherSize)
|
||||
hasher.Write([]byte(s))
|
||||
key = hasher.Sum(key)
|
||||
func (qs *TripleStore) convertStringToByteHash(s string) string {
|
||||
h := hashPool.Get().(hash.Hash)
|
||||
h.Reset()
|
||||
defer hashPool.Put(h)
|
||||
|
||||
key := make([]byte, 0, hashSize)
|
||||
h.Write([]byte(s))
|
||||
key = h.Sum(key)
|
||||
return hex.EncodeToString(key)
|
||||
}
|
||||
|
||||
|
|
@ -282,8 +288,7 @@ func (qs *TripleStore) TriplesAllIterator() graph.Iterator {
|
|||
}
|
||||
|
||||
func (qs *TripleStore) ValueOf(s string) graph.Value {
|
||||
h := qs.makeHasher()
|
||||
return qs.convertStringToByteHash(s, h)
|
||||
return qs.convertStringToByteHash(s)
|
||||
}
|
||||
|
||||
func (qs *TripleStore) NameOf(v graph.Value) string {
|
||||
|
|
@ -341,13 +346,13 @@ func (qs *TripleStore) TripleDirection(in graph.Value, d quad.Direction) graph.V
|
|||
case quad.Subject:
|
||||
offset = 0
|
||||
case quad.Predicate:
|
||||
offset = (qs.hasherSize * 2)
|
||||
offset = (hashSize * 2)
|
||||
case quad.Object:
|
||||
offset = (qs.hasherSize * 2) * 2
|
||||
offset = (hashSize * 2) * 2
|
||||
case quad.Label:
|
||||
offset = (qs.hasherSize * 2) * 3
|
||||
offset = (hashSize * 2) * 3
|
||||
}
|
||||
val := in.(string)[offset : qs.hasherSize*2+offset]
|
||||
val := in.(string)[offset : hashSize*2+offset]
|
||||
return val
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue