func checkIteratorContains(ts graph.TripleStore, it graph.Iterator, expected []string, t *testing.T) { var actual []string actual = nil for { val, ok := it.Next() if !ok { break } actual = append(actual, ts.GetNameFor(val)) } actualSet := actual[:] for _, a := range expected { found := false for j, b := range actualSet { if a == b { actualSet = append(actualSet[:j], actualSet[j+1:]...) found = true break } } if !found { t.Error("Couldn't find", a, "in actual output.\nActual:", actual, "\nExpected: ", expected, "\nRemainder: ", actualSet) return } } if len(actualSet) != 0 { t.Error("Actual output has more than expected.\nActual:", actual, "\nExpected: ", expected, "\nRemainder: ", actualSet) } }
func runIteratorWithCallback(it graph.Iterator, ses *Session, callback otto.Value, this otto.FunctionCall, limit int) { count := 0 it, _ = it.Optimize() for { if ses.doHalt { return } _, ok := it.Next() if !ok { break } tags := make(map[string]graph.TSVal) it.TagResults(&tags) val, _ := this.Otto.ToValue(tagsToValueMap(tags, ses)) val, _ = callback.Call(this.This, val) count++ if limit >= 0 && count >= limit { break } for it.NextResult() == true { if ses.doHalt { return } tags := make(map[string]graph.TSVal) it.TagResults(&tags) val, _ := this.Otto.ToValue(tagsToValueMap(tags, ses)) val, _ = callback.Call(this.This, val) count++ if limit >= 0 && count >= limit { break } } } it.Close() }
func runIteratorToArray(it graph.Iterator, ses *Session, limit int) []map[string]string { output := make([]map[string]string, 0) count := 0 it, _ = it.Optimize() for { if ses.doHalt { return nil } _, ok := it.Next() if !ok { break } tags := make(map[string]graph.TSVal) it.TagResults(&tags) output = append(output, tagsToValueMap(tags, ses)) count++ if limit >= 0 && count >= limit { break } for it.NextResult() == true { if ses.doHalt { return nil } tags := make(map[string]graph.TSVal) it.TagResults(&tags) output = append(output, tagsToValueMap(tags, ses)) count++ if limit >= 0 && count >= limit { break } } } it.Close() return output }
func extractValuesFromIterator(ts graph.TripleStore, it graph.Iterator) []string { var output []string for { val, ok := it.Next() if !ok { break } output = append(output, ts.GetNameFor(val)) } return output }
func extractTripleFromIterator(ts graph.TripleStore, it graph.Iterator) []string { var output []string for { val, ok := it.Next() if !ok { break } output = append(output, ts.GetTriple(val).ToString()) } return output }
func extractNumbersFromIterator(it graph.Iterator) []int { var outputNumbers []int for { val, ok := it.Next() if !ok { break } outputNumbers = append(outputNumbers, val.(int)) } return outputNumbers }
func iterated(it graph.Iterator) []int { var res []int for { val, ok := it.Next() if !ok { break } res = append(res, val.(int)) } return res }
func iteratedNames(ts graph.TripleStore, it graph.Iterator) []string { var res []string for { val, ok := it.Next() if !ok { break } res = append(res, ts.NameOf(val)) } sort.Strings(res) return res }
func iteratedTriples(ts graph.TripleStore, it graph.Iterator) []*graph.Triple { var res ordered for { val, ok := it.Next() if !ok { break } res = append(res, ts.Triple(val)) } sort.Sort(res) return res }
func runIteratorToArrayNoTags(it graph.Iterator, ses *Session, limit int) []string { output := make([]string, 0) count := 0 it, _ = it.Optimize() for { if ses.doHalt { return nil } val, ok := it.Next() if !ok { break } output = append(output, ses.ts.GetNameFor(val)) count++ if limit >= 0 && count >= limit { break } } it.Close() return output }
func runIteratorOnSession(it graph.Iterator, ses *Session) { if ses.lookingForQueryShape { iterator.OutputQueryShapeForIterator(it, ses.ts, &(ses.queryShape)) return } it, _ = it.Optimize() glog.V(2).Infoln(it.DebugString(0)) for { // TODO(barakmich): Better halting. if ses.doHalt { return } _, ok := it.Next() if !ok { break } tags := make(map[string]graph.TSVal) it.TagResults(&tags) cont := ses.SendResult(&GremlinResult{metaresult: false, err: "", val: nil, actualResults: &tags}) if !cont { break } for it.NextResult() == true { if ses.doHalt { return } tags := make(map[string]graph.TSVal) it.TagResults(&tags) cont := ses.SendResult(&GremlinResult{metaresult: false, err: "", val: nil, actualResults: &tags}) if !cont { break } } } it.Close() }
func TestIterator(t *testing.T) { var ts *LevelDBTripleStore Convey("Given a prepared database", t, func() { tmpDir, _ := ioutil.TempDir(os.TempDir(), "cayley_test") t.Log(tmpDir) defer os.RemoveAll(tmpDir) ok := CreateNewLevelDB(tmpDir) So(ok, ShouldBeTrue) ts = NewDefaultLevelDBTripleStore(tmpDir, nil) ts.AddTripleSet(makeTripleSet()) var it graph.Iterator Convey("Can create an all iterator for nodes", func() { it = ts.GetNodesAllIterator() So(it, ShouldNotBeNil) Convey("Has basics", func() { size, accurate := it.Size() So(size, ShouldBeBetween, 0, 20) So(accurate, ShouldBeFalse) So(it.Type(), ShouldEqual, "all") re_it, ok := it.Optimize() So(ok, ShouldBeFalse) So(re_it, ShouldPointTo, it) }) Convey("Iterates all nodes", func() { expected := []string{ "A", "B", "C", "D", "E", "F", "G", "follows", "status", "cool", "status_graph", } sort.Strings(expected) actual := extractValuesFromIterator(ts, it) sort.Strings(actual) So(actual, ShouldResemble, expected) it.Reset() actual = extractValuesFromIterator(ts, it) sort.Strings(actual) So(actual, ShouldResemble, expected) }) Convey("Contains a couple nodes", func() { So(it.Check(ts.GetIdFor("A")), ShouldBeTrue) So(it.Check(ts.GetIdFor("cool")), ShouldBeTrue) //So(it.Check(ts.GetIdFor("baller")), ShouldBeFalse) }) Reset(func() { it.Reset() }) }) Convey("Can create an all iterator for edges", func() { it := ts.GetTriplesAllIterator() So(it, ShouldNotBeNil) Convey("Has basics", func() { size, accurate := it.Size() So(size, ShouldBeBetween, 0, 20) So(accurate, ShouldBeFalse) So(it.Type(), ShouldEqual, "all") re_it, ok := it.Optimize() So(ok, ShouldBeFalse) So(re_it, ShouldPointTo, it) }) Convey("Iterates an edge", func() { edge_val, _ := it.Next() triple := ts.GetTriple(edge_val) set := makeTripleSet() var string_set []string for _, t := range set { string_set = append(string_set, t.ToString()) } So(triple.ToString(), ShouldBeIn, string_set) }) Reset(func() { ts.Close() }) }) }) }
func TestIterator(t *testing.T) { tmpDir, err := ioutil.TempDir(os.TempDir(), "cayley_test") if err != nil { t.Fatalf("Could not create working directory: %v", err) } defer os.RemoveAll(tmpDir) t.Log(tmpDir) err = createNewLevelDB(tmpDir, nil) if err != nil { t.Fatal("Failed to create LevelDB database.") } ts, err := newTripleStore(tmpDir, nil) if ts == nil || err != nil { t.Error("Failed to create leveldb TripleStore.") } ts.AddTripleSet(makeTripleSet()) var it graph.Iterator it = ts.NodesAllIterator() if it == nil { t.Fatal("Got nil iterator.") } size, exact := it.Size() if size <= 0 || size >= 20 { t.Errorf("Unexpected size, got:%d expect:(0, 20)", size) } if exact { t.Errorf("Got unexpected exact result.") } if typ := it.Type(); typ != graph.All { t.Errorf("Unexpected iterator type, got:%v expect:%v", typ, graph.All) } optIt, changed := it.Optimize() if changed || optIt != it { t.Errorf("Optimize unexpectedly changed iterator.") } expect := []string{ "A", "B", "C", "D", "E", "F", "G", "follows", "status", "cool", "status_graph", } sort.Strings(expect) for i := 0; i < 2; i++ { got := iteratedNames(ts, it) sort.Strings(got) if !reflect.DeepEqual(got, expect) { t.Errorf("Unexpected iterated result on repeat %d, got:%v expect:%v", i, got, expect) } it.Reset() } for _, pq := range expect { if !it.Check(ts.ValueOf(pq)) { t.Errorf("Failed to find and check %q correctly", pq) } } // FIXME(kortschak) Why does this fail? /* for _, pq := range []string{"baller"} { if it.Check(ts.ValueOf(pq)) { t.Errorf("Failed to check %q correctly", pq) } } */ it.Reset() it = ts.TriplesAllIterator() edge, _ := it.Next() triple := ts.Triple(edge) set := makeTripleSet() var ok bool for _, t := range set { if t.String() == triple.String() { ok = true break } } if !ok { t.Errorf("Failed to find %q during iteration, got:%q", triple, set) } ts.Close() }
func (qs *queryShape) MakeNode(it graph.Iterator) *Node { n := Node{Id: qs.nodeId} for _, tag := range it.Tags() { n.Tags = append(n.Tags, tag) } for k, _ := range it.FixedTags() { n.Tags = append(n.Tags, k) } switch it.Type() { case graph.And: for _, sub := range it.SubIterators() { qs.nodeId++ newNode := qs.MakeNode(sub) if sub.Type() != graph.Or { qs.StealNode(&n, newNode) } else { qs.AddNode(newNode) qs.AddLink(&Link{n.Id, newNode.Id, 0, 0}) } } case graph.Fixed: n.IsFixed = true for { val, more := it.Next() if !more { break } n.Values = append(n.Values, qs.ts.NameOf(val)) } case graph.HasA: hasa := it.(*HasA) qs.PushHasa(n.Id, hasa.dir) qs.nodeId++ newNode := qs.MakeNode(hasa.primaryIt) qs.AddNode(newNode) qs.RemoveHasa() case graph.Or: for _, sub := range it.SubIterators() { qs.nodeId++ newNode := qs.MakeNode(sub) if sub.Type() == graph.Or { qs.StealNode(&n, newNode) } else { qs.AddNode(newNode) qs.AddLink(&Link{n.Id, newNode.Id, 0, 0}) } } case graph.LinksTo: n.IsLinkNode = true lto := it.(*LinksTo) qs.nodeId++ newNode := qs.MakeNode(lto.primaryIt) hasaID, hasaDir := qs.LastHasa() if (hasaDir == graph.Subject && lto.dir == graph.Object) || (hasaDir == graph.Object && lto.dir == graph.Subject) { qs.AddNode(newNode) if hasaDir == graph.Subject { qs.AddLink(&Link{hasaID, newNode.Id, 0, n.Id}) } else { qs.AddLink(&Link{newNode.Id, hasaID, 0, n.Id}) } } else if lto.primaryIt.Type() == graph.Fixed { qs.StealNode(&n, newNode) } else { qs.AddNode(newNode) } case graph.Optional: // Unsupported, for the moment fallthrough case graph.All: } return &n }