fix mongo hasher
This commit is contained in:
parent
104e7d110d
commit
03798bc4fa
2 changed files with 27 additions and 23 deletions
|
|
@ -185,13 +185,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.hasher.Size() * 2)
|
offset = (it.qs.hasher_size * 2)
|
||||||
case quad.Object:
|
case quad.Object:
|
||||||
offset = (it.qs.hasher.Size() * 2) * 2
|
offset = (it.qs.hasher_size * 2) * 2
|
||||||
case quad.Label:
|
case quad.Label:
|
||||||
offset = (it.qs.hasher.Size() * 2) * 3
|
offset = (it.qs.hasher_size * 2) * 3
|
||||||
}
|
}
|
||||||
val := v.(string)[offset : it.qs.hasher.Size()*2+offset]
|
val := v.(string)[offset : it.qs.hasher_size*2+offset]
|
||||||
if val == it.hash {
|
if val == it.hash {
|
||||||
it.result = v
|
it.result = v
|
||||||
return graph.ContainsLogOut(it, v, true)
|
return graph.ContainsLogOut(it, v, true)
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,8 @@ const DefaultDBName = "cayley"
|
||||||
type TripleStore struct {
|
type TripleStore struct {
|
||||||
session *mgo.Session
|
session *mgo.Session
|
||||||
db *mgo.Database
|
db *mgo.Database
|
||||||
hasher hash.Hash
|
hasher_size int
|
||||||
|
make_hasher func() hash.Hash
|
||||||
idCache *IDLru
|
idCache *IDLru
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -86,24 +87,26 @@ func newTripleStore(addr string, options graph.Options) (graph.TripleStore, erro
|
||||||
}
|
}
|
||||||
qs.db = conn.DB(dbName)
|
qs.db = conn.DB(dbName)
|
||||||
qs.session = conn
|
qs.session = conn
|
||||||
qs.hasher = sha1.New()
|
qs.hasher_size = sha1.Size
|
||||||
|
qs.make_hasher = func() hash.Hash { return sha1.New() }
|
||||||
qs.idCache = NewIDLru(1 << 16)
|
qs.idCache = NewIDLru(1 << 16)
|
||||||
return &qs, nil
|
return &qs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qs *TripleStore) getIdForTriple(t quad.Quad) string {
|
func (qs *TripleStore) getIdForTriple(t quad.Quad) string {
|
||||||
id := qs.ConvertStringToByteHash(t.Subject)
|
hasher := qs.make_hasher()
|
||||||
id += qs.ConvertStringToByteHash(t.Predicate)
|
id := qs.convertStringToByteHash(t.Subject, hasher)
|
||||||
id += qs.ConvertStringToByteHash(t.Object)
|
id += qs.convertStringToByteHash(t.Predicate, hasher)
|
||||||
id += qs.ConvertStringToByteHash(t.Label)
|
id += qs.convertStringToByteHash(t.Object, hasher)
|
||||||
|
id += qs.convertStringToByteHash(t.Label, hasher)
|
||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qs *TripleStore) ConvertStringToByteHash(s string) string {
|
func (qs *TripleStore) convertStringToByteHash(s string, hasher hash.Hash) string {
|
||||||
qs.hasher.Reset()
|
hasher.Reset()
|
||||||
key := make([]byte, 0, qs.hasher.Size())
|
key := make([]byte, 0, qs.hasher_size)
|
||||||
qs.hasher.Write([]byte(s))
|
hasher.Write([]byte(s))
|
||||||
key = qs.hasher.Sum(key)
|
key = hasher.Sum(key)
|
||||||
return hex.EncodeToString(key)
|
return hex.EncodeToString(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -243,7 +246,8 @@ func (qs *TripleStore) TriplesAllIterator() graph.Iterator {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qs *TripleStore) ValueOf(s string) graph.Value {
|
func (qs *TripleStore) ValueOf(s string) graph.Value {
|
||||||
return qs.ConvertStringToByteHash(s)
|
h := qs.make_hasher()
|
||||||
|
return qs.convertStringToByteHash(s, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qs *TripleStore) NameOf(v graph.Value) string {
|
func (qs *TripleStore) NameOf(v graph.Value) string {
|
||||||
|
|
@ -288,13 +292,13 @@ func (qs *TripleStore) TripleDirection(in graph.Value, d quad.Direction) graph.V
|
||||||
case quad.Subject:
|
case quad.Subject:
|
||||||
offset = 0
|
offset = 0
|
||||||
case quad.Predicate:
|
case quad.Predicate:
|
||||||
offset = (qs.hasher.Size() * 2)
|
offset = (qs.hasher_size * 2)
|
||||||
case quad.Object:
|
case quad.Object:
|
||||||
offset = (qs.hasher.Size() * 2) * 2
|
offset = (qs.hasher_size * 2) * 2
|
||||||
case quad.Label:
|
case quad.Label:
|
||||||
offset = (qs.hasher.Size() * 2) * 3
|
offset = (qs.hasher_size * 2) * 3
|
||||||
}
|
}
|
||||||
val := in.(string)[offset : qs.hasher.Size()*2+offset]
|
val := in.(string)[offset : qs.hasher_size*2+offset]
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue