Destutter graph/...

This commit is contained in:
kortschak 2014-06-28 13:29:16 +09:30
parent 913d567ae1
commit 40f3363cde
20 changed files with 188 additions and 189 deletions

View file

@ -18,19 +18,19 @@ import (
"github.com/google/cayley/graph"
)
type MemstoreAllIterator struct {
type AllIterator struct {
graph.Int64AllIterator
ts *MemTripleStore
ts *TripleStore
}
func NewMemstoreAllIterator(ts *MemTripleStore) *MemstoreAllIterator {
var out MemstoreAllIterator
func NewMemstoreAllIterator(ts *TripleStore) *AllIterator {
var out AllIterator
out.Int64AllIterator = *graph.NewInt64AllIterator(1, ts.idCounter-1)
out.ts = ts
return &out
}
func (memall *MemstoreAllIterator) Next() (graph.TSVal, bool) {
func (memall *AllIterator) Next() (graph.TSVal, bool) {
next, out := memall.Int64AllIterator.Next()
if !out {
return next, out

View file

@ -24,7 +24,7 @@ import (
"github.com/google/cayley/graph"
)
type LlrbIterator struct {
type Iterator struct {
graph.BaseIterator
tree *llrb.LLRB
data string
@ -51,8 +51,8 @@ func IterateOne(tree *llrb.LLRB, last Int64) Int64 {
return next
}
func NewLlrbIterator(tree *llrb.LLRB, data string) *LlrbIterator {
var it LlrbIterator
func NewLlrbIterator(tree *llrb.LLRB, data string) *Iterator {
var it Iterator
graph.BaseIteratorInit(&it.BaseIterator)
it.tree = tree
it.iterLast = Int64(-1)
@ -60,19 +60,19 @@ func NewLlrbIterator(tree *llrb.LLRB, data string) *LlrbIterator {
return &it
}
func (it *LlrbIterator) Reset() {
func (it *Iterator) Reset() {
it.iterLast = Int64(-1)
}
func (it *LlrbIterator) Clone() graph.Iterator {
func (it *Iterator) Clone() graph.Iterator {
var new_it = NewLlrbIterator(it.tree, it.data)
new_it.CopyTagsFrom(it)
return new_it
}
func (it *LlrbIterator) Close() {}
func (it *Iterator) Close() {}
func (it *LlrbIterator) Next() (graph.TSVal, bool) {
func (it *Iterator) Next() (graph.TSVal, bool) {
graph.NextLogIn(it)
if it.tree.Max() == nil || it.Last == int64(it.tree.Max().(Int64)) {
return graph.NextLogOut(it, nil, false)
@ -82,11 +82,11 @@ func (it *LlrbIterator) Next() (graph.TSVal, bool) {
return graph.NextLogOut(it, it.Last, true)
}
func (it *LlrbIterator) Size() (int64, bool) {
func (it *Iterator) Size() (int64, bool) {
return int64(it.tree.Len()), true
}
func (it *LlrbIterator) Check(v graph.TSVal) bool {
func (it *Iterator) Check(v graph.TSVal) bool {
graph.CheckLogIn(it, v)
if it.tree.Has(Int64(v.(int64))) {
it.Last = v
@ -95,22 +95,22 @@ func (it *LlrbIterator) Check(v graph.TSVal) bool {
return graph.CheckLogOut(it, v, false)
}
func (it *LlrbIterator) DebugString(indent int) string {
func (it *Iterator) DebugString(indent int) string {
size, _ := it.Size()
return fmt.Sprintf("%s(%s tags:%s size:%d %s)", strings.Repeat(" ", indent), it.Type(), it.Tags(), size, it.data)
}
func (it *LlrbIterator) Type() string {
func (it *Iterator) Type() string {
return "llrb"
}
func (it *LlrbIterator) Sorted() bool {
func (it *Iterator) Sorted() bool {
return true
}
func (it *LlrbIterator) Optimize() (graph.Iterator, bool) {
func (it *Iterator) Optimize() (graph.Iterator, bool) {
return it, false
}
func (it *LlrbIterator) GetStats() *graph.IteratorStats {
func (it *Iterator) GetStats() *graph.IteratorStats {
return &graph.IteratorStats{
CheckCost: int64(math.Log(float64(it.tree.Len()))) + 1,
NextCost: 1,

View file

@ -28,8 +28,8 @@ import "github.com/google/cayley/graph"
// +---+
//
func MakeTestingMemstore() *MemTripleStore {
ts := NewMemTripleStore()
func MakeTestingMemstore() *TripleStore {
ts := NewTripleStore()
ts.AddTriple(graph.MakeTriple("A", "follows", "B", ""))
ts.AddTriple(graph.MakeTriple("C", "follows", "B", ""))
ts.AddTriple(graph.MakeTriple("C", "follows", "D", ""))

View file

@ -66,7 +66,7 @@ func (tdi *TripleDirectionIndex) Get(dir string, id int64) (*llrb.LLRB, bool) {
return tree, exists
}
type MemTripleStore struct {
type TripleStore struct {
idCounter int64
tripleIdCounter int64
idMap map[string]int64
@ -77,8 +77,8 @@ type MemTripleStore struct {
// vip_index map[string]map[int64]map[string]map[int64]*llrb.Tree
}
func NewMemTripleStore() *MemTripleStore {
var ts MemTripleStore
func NewTripleStore() *TripleStore {
var ts TripleStore
ts.idMap = make(map[string]int64)
ts.revIdMap = make(map[int64]string)
ts.triples = make([]graph.Triple, 1, 200)
@ -92,13 +92,13 @@ func NewMemTripleStore() *MemTripleStore {
return &ts
}
func (ts *MemTripleStore) AddTripleSet(triples []*graph.Triple) {
func (ts *TripleStore) AddTripleSet(triples []*graph.Triple) {
for _, t := range triples {
ts.AddTriple(t)
}
}
func (ts *MemTripleStore) tripleExists(t *graph.Triple) (bool, int64) {
func (ts *TripleStore) tripleExists(t *graph.Triple) (bool, int64) {
smallest := -1
var smallest_tree *llrb.LLRB
for _, dir := range graph.TripleDirections {
@ -135,7 +135,7 @@ func (ts *MemTripleStore) tripleExists(t *graph.Triple) (bool, int64) {
return false, 0
}
func (ts *MemTripleStore) AddTriple(t *graph.Triple) {
func (ts *TripleStore) AddTriple(t *graph.Triple) {
if exists, _ := ts.tripleExists(t); exists {
return
}
@ -169,7 +169,7 @@ func (ts *MemTripleStore) AddTriple(t *graph.Triple) {
// TODO(barakmich): Add VIP indexing
}
func (ts *MemTripleStore) RemoveTriple(t *graph.Triple) {
func (ts *TripleStore) RemoveTriple(t *graph.Triple) {
var tripleID int64
var exists bool
tripleID = 0
@ -215,11 +215,11 @@ func (ts *MemTripleStore) RemoveTriple(t *graph.Triple) {
}
}
func (ts *MemTripleStore) GetTriple(index graph.TSVal) *graph.Triple {
func (ts *TripleStore) GetTriple(index graph.TSVal) *graph.Triple {
return &ts.triples[index.(int64)]
}
func (ts *MemTripleStore) GetTripleIterator(direction string, value graph.TSVal) graph.Iterator {
func (ts *TripleStore) GetTripleIterator(direction string, value graph.TSVal) graph.Iterator {
index, ok := ts.index.Get(direction, value.(int64))
data := fmt.Sprintf("dir:%s val:%d", direction, value.(int64))
if ok {
@ -228,11 +228,11 @@ func (ts *MemTripleStore) GetTripleIterator(direction string, value graph.TSVal)
return &graph.NullIterator{}
}
func (ts *MemTripleStore) Size() int64 {
func (ts *TripleStore) Size() int64 {
return ts.size - 1 // Don't count the sentinel
}
func (ts *MemTripleStore) DebugPrint() {
func (ts *TripleStore) DebugPrint() {
for i, t := range ts.triples {
if i == 0 {
continue
@ -241,28 +241,28 @@ func (ts *MemTripleStore) DebugPrint() {
}
}
func (ts *MemTripleStore) GetIdFor(name string) graph.TSVal {
func (ts *TripleStore) GetIdFor(name string) graph.TSVal {
return ts.idMap[name]
}
func (ts *MemTripleStore) GetNameFor(id graph.TSVal) string {
func (ts *TripleStore) GetNameFor(id graph.TSVal) string {
return ts.revIdMap[id.(int64)]
}
func (ts *MemTripleStore) GetTriplesAllIterator() graph.Iterator {
func (ts *TripleStore) GetTriplesAllIterator() graph.Iterator {
return graph.NewInt64AllIterator(0, ts.Size())
}
func (ts *MemTripleStore) MakeFixed() *graph.FixedIterator {
func (ts *TripleStore) MakeFixed() *graph.FixedIterator {
return graph.NewFixedIteratorWithCompare(graph.BasicEquality)
}
func (ts *MemTripleStore) GetTripleDirection(val graph.TSVal, direction string) graph.TSVal {
func (ts *TripleStore) GetTripleDirection(val graph.TSVal, direction string) graph.TSVal {
name := ts.GetTriple(val).Get(direction)
return ts.GetIdFor(name)
}
func (ts *MemTripleStore) GetNodesAllIterator() graph.Iterator {
func (ts *TripleStore) GetNodesAllIterator() graph.Iterator {
return NewMemstoreAllIterator(ts)
}
func (ts *MemTripleStore) Close() {}
func (ts *TripleStore) Close() {}

View file

@ -18,7 +18,7 @@ import (
"github.com/google/cayley/graph"
)
func (ts *MemTripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
func (ts *TripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
switch it.Type() {
case "linksto":
return ts.optimizeLinksTo(it.(*graph.LinksToIterator))
@ -27,7 +27,7 @@ func (ts *MemTripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, b
return it, false
}
func (ts *MemTripleStore) optimizeLinksTo(it *graph.LinksToIterator) (graph.Iterator, bool) {
func (ts *TripleStore) optimizeLinksTo(it *graph.LinksToIterator) (graph.Iterator, bool) {
l := it.GetSubIterators()
if l.Len() != 1 {
return it, false

View file

@ -107,7 +107,7 @@ func TestLinksToOptimization(t *testing.T) {
if newIt.Type() != "llrb" {
t.Fatal("Didn't swap out to LLRB")
}
v := newIt.(*LlrbIterator)
v := newIt.(*Iterator)
v_clone := v.Clone()
if v_clone.DebugString(0) != v.DebugString(0) {
t.Fatal("Wrong iterator. Got ", v_clone.DebugString(0))