Canonicalise leveldb receiver names

Also remove redundant LevelDB infix missed previously.
This commit is contained in:
kortschak 2014-06-28 21:43:59 +09:30
parent 60d5c60817
commit b1ad887c78
3 changed files with 90 additions and 90 deletions

View file

@ -35,7 +35,7 @@ type AllIterator struct {
ro *opt.ReadOptions ro *opt.ReadOptions
} }
func NewLevelDBAllIterator(prefix, dir string, ts *TripleStore) *AllIterator { func NewAllIterator(prefix, dir string, ts *TripleStore) *AllIterator {
var it AllIterator var it AllIterator
graph.BaseIteratorInit(&it.BaseIterator) graph.BaseIteratorInit(&it.BaseIterator)
it.ro = &opt.ReadOptions{} it.ro = &opt.ReadOptions{}
@ -53,46 +53,46 @@ func NewLevelDBAllIterator(prefix, dir string, ts *TripleStore) *AllIterator {
return &it return &it
} }
func (a *AllIterator) Reset() { func (it *AllIterator) Reset() {
if !a.open { if !it.open {
a.it = a.ts.db.NewIterator(nil, a.ro) it.it = it.ts.db.NewIterator(nil, it.ro)
a.open = true it.open = true
} }
a.it.Seek(a.prefix) it.it.Seek(it.prefix)
if !a.it.Valid() { if !it.it.Valid() {
a.open = false it.open = false
a.it.Release() it.it.Release()
} }
} }
func (a *AllIterator) Clone() graph.Iterator { func (it *AllIterator) Clone() graph.Iterator {
out := NewLevelDBAllIterator(string(a.prefix), a.dir, a.ts) out := NewAllIterator(string(it.prefix), it.dir, it.ts)
out.CopyTagsFrom(a) out.CopyTagsFrom(it)
return out return out
} }
func (a *AllIterator) Next() (graph.TSVal, bool) { func (it *AllIterator) Next() (graph.TSVal, bool) {
if !a.open { if !it.open {
a.Last = nil it.Last = nil
return nil, false return nil, false
} }
var out []byte var out []byte
out = make([]byte, len(a.it.Key())) out = make([]byte, len(it.it.Key()))
copy(out, a.it.Key()) copy(out, it.it.Key())
a.it.Next() it.it.Next()
if !a.it.Valid() { if !it.it.Valid() {
a.Close() it.Close()
} }
if !bytes.HasPrefix(out, a.prefix) { if !bytes.HasPrefix(out, it.prefix) {
a.Close() it.Close()
return nil, false return nil, false
} }
a.Last = out it.Last = out
return out, true return out, true
} }
func (a *AllIterator) Check(v graph.TSVal) bool { func (it *AllIterator) Check(v graph.TSVal) bool {
a.Last = v it.Last = v
return true return true
} }
@ -103,8 +103,8 @@ func (lit *AllIterator) Close() {
} }
} }
func (a *AllIterator) Size() (int64, bool) { func (it *AllIterator) Size() (int64, bool) {
size, err := a.ts.GetApproximateSizeForPrefix(a.prefix) size, err := it.ts.GetApproximateSizeForPrefix(it.prefix)
if err == nil { if err == nil {
return size, false return size, false
} }
@ -112,20 +112,20 @@ func (a *AllIterator) Size() (int64, bool) {
return int64(^uint64(0) >> 1), false return int64(^uint64(0) >> 1), false
} }
func (lit *AllIterator) DebugString(indent int) string { func (it *AllIterator) DebugString(indent int) string {
size, _ := lit.Size() size, _ := it.Size()
return fmt.Sprintf("%s(%s tags: %v leveldb size:%d %s %p)", strings.Repeat(" ", indent), lit.Type(), lit.Tags(), size, lit.dir, lit) return fmt.Sprintf("%s(%s tags: %v leveldb size:%d %s %p)", strings.Repeat(" ", indent), it.Type(), it.Tags(), size, it.dir, it)
} }
func (lit *AllIterator) Type() string { return "all" } func (it *AllIterator) Type() string { return "all" }
func (lit *AllIterator) Sorted() bool { return false } func (it *AllIterator) Sorted() bool { return false }
func (lit *AllIterator) Optimize() (graph.Iterator, bool) { func (it *AllIterator) Optimize() (graph.Iterator, bool) {
return lit, false return it, false
} }
func (lit *AllIterator) GetStats() *graph.IteratorStats { func (it *AllIterator) GetStats() *graph.IteratorStats {
s, _ := lit.Size() s, _ := it.Size()
return &graph.IteratorStats{ return &graph.IteratorStats{
CheckCost: 1, CheckCost: 1,
NextCost: 2, NextCost: 2,

View file

@ -59,57 +59,57 @@ func NewIterator(prefix, dir string, value graph.TSVal, ts *TripleStore) *Iterat
return &it return &it
} }
func (lit *Iterator) Reset() { func (it *Iterator) Reset() {
if !lit.open { if !it.open {
lit.it = lit.ts.db.NewIterator(nil, lit.ro) it.it = it.ts.db.NewIterator(nil, it.ro)
lit.open = true it.open = true
} }
ok := lit.it.Seek(lit.nextPrefix) ok := it.it.Seek(it.nextPrefix)
if !ok { if !ok {
lit.open = false it.open = false
lit.it.Release() it.it.Release()
} }
} }
func (lit *Iterator) Clone() graph.Iterator { func (it *Iterator) Clone() graph.Iterator {
out := NewIterator(lit.originalPrefix, lit.dir, lit.checkId, lit.ts) out := NewIterator(it.originalPrefix, it.dir, it.checkId, it.ts)
out.CopyTagsFrom(lit) out.CopyTagsFrom(it)
return out return out
} }
func (lit *Iterator) Close() { func (it *Iterator) Close() {
if lit.open { if it.open {
lit.it.Release() it.it.Release()
lit.open = false it.open = false
} }
} }
func (lit *Iterator) Next() (graph.TSVal, bool) { func (it *Iterator) Next() (graph.TSVal, bool) {
if lit.it == nil { if it.it == nil {
lit.Last = nil it.Last = nil
return nil, false return nil, false
} }
if !lit.open { if !it.open {
lit.Last = nil it.Last = nil
return nil, false return nil, false
} }
if !lit.it.Valid() { if !it.it.Valid() {
lit.Last = nil it.Last = nil
lit.Close() it.Close()
return nil, false return nil, false
} }
if bytes.HasPrefix(lit.it.Key(), lit.nextPrefix) { if bytes.HasPrefix(it.it.Key(), it.nextPrefix) {
out := make([]byte, len(lit.it.Key())) out := make([]byte, len(it.it.Key()))
copy(out, lit.it.Key()) copy(out, it.it.Key())
lit.Last = out it.Last = out
ok := lit.it.Next() ok := it.it.Next()
if !ok { if !ok {
lit.Close() it.Close()
} }
return out, true return out, true
} }
lit.Close() it.Close()
lit.Last = nil it.Last = nil
return nil, false return nil, false
} }
@ -165,44 +165,44 @@ func GetPositionFromPrefix(prefix []byte, dir string, ts *TripleStore) int {
panic("Notreached") panic("Notreached")
} }
func (lit *Iterator) Check(v graph.TSVal) bool { func (it *Iterator) Check(v graph.TSVal) bool {
val := v.([]byte) val := v.([]byte)
if val[0] == 'z' { if val[0] == 'z' {
return false return false
} }
offset := GetPositionFromPrefix(val[0:2], lit.dir, lit.ts) offset := GetPositionFromPrefix(val[0:2], it.dir, it.ts)
if offset != -1 { if offset != -1 {
if bytes.HasPrefix(val[offset:], lit.checkId[1:]) { if bytes.HasPrefix(val[offset:], it.checkId[1:]) {
return true return true
} }
} else { } else {
nameForDir := lit.ts.GetTriple(v).Get(lit.dir) nameForDir := it.ts.GetTriple(v).Get(it.dir)
hashForDir := lit.ts.GetIdFor(nameForDir).([]byte) hashForDir := it.ts.GetIdFor(nameForDir).([]byte)
if bytes.Equal(hashForDir, lit.checkId) { if bytes.Equal(hashForDir, it.checkId) {
return true return true
} }
} }
return false return false
} }
func (lit *Iterator) Size() (int64, bool) { func (it *Iterator) Size() (int64, bool) {
return lit.ts.GetSizeFor(lit.checkId), true return it.ts.GetSizeFor(it.checkId), true
} }
func (lit *Iterator) DebugString(indent int) string { func (it *Iterator) DebugString(indent int) string {
size, _ := lit.Size() size, _ := it.Size()
return fmt.Sprintf("%s(%s %d tags: %v dir: %s size:%d %s)", strings.Repeat(" ", indent), lit.Type(), lit.GetUid(), lit.Tags(), lit.dir, size, lit.ts.GetNameFor(lit.checkId)) return fmt.Sprintf("%s(%s %d tags: %v dir: %s size:%d %s)", strings.Repeat(" ", indent), it.Type(), it.GetUid(), it.Tags(), it.dir, size, it.ts.GetNameFor(it.checkId))
} }
func (lit *Iterator) Type() string { return "leveldb" } func (it *Iterator) Type() string { return "leveldb" }
func (lit *Iterator) Sorted() bool { return false } func (it *Iterator) Sorted() bool { return false }
func (lit *Iterator) Optimize() (graph.Iterator, bool) { func (it *Iterator) Optimize() (graph.Iterator, bool) {
return lit, false return it, false
} }
func (lit *Iterator) GetStats() *graph.IteratorStats { func (it *Iterator) GetStats() *graph.IteratorStats {
s, _ := lit.Size() s, _ := it.Size()
return &graph.IteratorStats{ return &graph.IteratorStats{
CheckCost: 1, CheckCost: 1,
NextCost: 2, NextCost: 2,

View file

@ -273,19 +273,19 @@ func (ts *TripleStore) AddTripleSet(t_s []*graph.Triple) {
ts.size += int64(newTs) ts.size += int64(newTs)
} }
func (ldbts *TripleStore) Close() { func (ts *TripleStore) Close() {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
err := binary.Write(buf, binary.LittleEndian, ldbts.size) err := binary.Write(buf, binary.LittleEndian, ts.size)
if err == nil { if err == nil {
werr := ldbts.db.Put([]byte("__size"), buf.Bytes(), ldbts.writeopts) werr := ts.db.Put([]byte("__size"), buf.Bytes(), ts.writeopts)
if werr != nil { if werr != nil {
glog.Errorf("Couldn't write size before closing!") glog.Errorf("Couldn't write size before closing!")
} }
} else { } else {
glog.Errorf("Couldn't convert size before closing!") glog.Errorf("Couldn't convert size before closing!")
} }
ldbts.db.Close() ts.db.Close()
ldbts.open = false ts.open = false
} }
func (ts *TripleStore) GetTriple(k graph.TSVal) *graph.Triple { func (ts *TripleStore) GetTriple(k graph.TSVal) *graph.Triple {
@ -403,11 +403,11 @@ func (ts *TripleStore) GetTripleIterator(dir string, val graph.TSVal) graph.Iter
} }
func (ts *TripleStore) GetNodesAllIterator() graph.Iterator { func (ts *TripleStore) GetNodesAllIterator() graph.Iterator {
return NewLevelDBAllIterator("z", "v", ts) return NewAllIterator("z", "v", ts)
} }
func (ts *TripleStore) GetTriplesAllIterator() graph.Iterator { func (ts *TripleStore) GetTriplesAllIterator() graph.Iterator {
return NewLevelDBAllIterator("po", "p", ts) return NewAllIterator("po", "p", ts)
} }
func (ts *TripleStore) GetTripleDirection(val graph.TSVal, direction string) graph.TSVal { func (ts *TripleStore) GetTripleDirection(val graph.TSVal, direction string) graph.TSVal {