func runIteratorWithCallback(it graph.Iterator, ses *GremlinSession, 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 *GremlinSession, 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 runIteratorToArrayNoTags(it graph.Iterator, ses *GremlinSession, 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 *GremlinSession) { if ses.lookingForQueryShape { graph.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 TestAllIterator(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() }) }) }) }