func (ts *MongoTripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
	switch it.Type() {
	case "linksto":
		return ts.optimizeLinksTo(it.(*graph.LinksToIterator))

	}
	return it, false
}
예제 #2
0
func (ts *QuadStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
	switch it.Type() {
	case graph.LinksTo:
		return ts.optimizeLinksTo(it.(*iterator.LinksTo))

	}
	return it, false
}
예제 #3
0
파일: optimizers.go 프로젝트: e4x/cayley
func (qs *QuadStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
	switch it.Type() {
	case graph.LinksTo:
		return qs.optimizeLinksTo(it.(*iterator.LinksTo))
	case graph.HasA:
		return qs.optimizeHasA(it.(*iterator.HasA))
	case graph.And:
		return qs.optimizeAnd(it.(*iterator.And))
	}
	return it, false
}
예제 #4
0
func (qs *QuadStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
	switch it.Type() {
	case graph.LinksTo:
		return qs.optimizeLinksTo(it.(*iterator.LinksTo))
	case graph.And:
		return qs.optimizeAndIterator(it.(*iterator.And))
	case graph.Comparison:
		return qs.optimizeComparison(it.(*iterator.Comparison))
	}
	return it, false
}
예제 #5
0
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()
			})
		})
	})

}
예제 #6
0
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.")
	}

	qs, err := newTripleStore(tmpDir, nil)
	if qs == nil || err != nil {
		t.Error("Failed to create leveldb TripleStore.")
	}
	qs.AddTripleSet(makeTripleSet())
	var it graph.Iterator

	it = qs.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(qs, 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.Contains(qs.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.Contains(qs.ValueOf(pq)) {
				t.Errorf("Failed to check %q correctly", pq)
			}
		}
	*/
	it.Reset()

	it = qs.TriplesAllIterator()
	edge, _ := graph.Next(it)
	triple := qs.Quad(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)
	}

	qs.Close()
}
예제 #7
0
func (s *queryShape) MakeNode(it graph.Iterator) *Node {
	n := Node{Id: s.nodeId}
	for _, tag := range it.Tagger().Tags() {
		n.Tags = append(n.Tags, tag)
	}
	for k, _ := range it.Tagger().Fixed() {
		n.Tags = append(n.Tags, k)
	}

	switch it.Type() {
	case graph.And:
		for _, sub := range it.SubIterators() {
			s.nodeId++
			newNode := s.MakeNode(sub)
			if sub.Type() != graph.Or {
				s.StealNode(&n, newNode)
			} else {
				s.AddNode(newNode)
				s.AddLink(&Link{n.Id, newNode.Id, 0, 0})
			}
		}
	case graph.Fixed:
		n.IsFixed = true
		for graph.Next(it) {
			n.Values = append(n.Values, s.qs.NameOf(it.Result()))
		}
	case graph.HasA:
		hasa := it.(*HasA)
		s.PushHasa(n.Id, hasa.dir)
		s.nodeId++
		newNode := s.MakeNode(hasa.primaryIt)
		s.AddNode(newNode)
		s.RemoveHasa()
	case graph.Or:
		for _, sub := range it.SubIterators() {
			s.nodeId++
			newNode := s.MakeNode(sub)
			if sub.Type() == graph.Or {
				s.StealNode(&n, newNode)
			} else {
				s.AddNode(newNode)
				s.AddLink(&Link{n.Id, newNode.Id, 0, 0})
			}
		}
	case graph.LinksTo:
		n.IsLinkNode = true
		lto := it.(*LinksTo)
		s.nodeId++
		newNode := s.MakeNode(lto.primaryIt)
		hasaID, hasaDir := s.LastHasa()
		if (hasaDir == quad.Subject && lto.dir == quad.Object) ||
			(hasaDir == quad.Object && lto.dir == quad.Subject) {
			s.AddNode(newNode)
			if hasaDir == quad.Subject {
				s.AddLink(&Link{hasaID, newNode.Id, 0, n.Id})
			} else {
				s.AddLink(&Link{newNode.Id, hasaID, 0, n.Id})
			}
		} else if lto.primaryIt.Type() == graph.Fixed {
			s.StealNode(&n, newNode)
		} else {
			s.AddNode(newNode)
		}
	case graph.Optional:
		// Unsupported, for the moment
		fallthrough
	case graph.All:
	}
	return &n
}
예제 #8
0
파일: tests.go 프로젝트: bmatsuo/cayley
// TestQuadStoreNodesAllIterator iterates the nodes in a fixture and asserts
// the result.
func TestQuadStoreNodesAllIterator(t *testing.T, ctx context.Context) {
	qs := ContextQuadStore(ctx)

	_, err := WriteFixtureQuadStore(qs, "simple")
	if err != nil {
		t.Errorf("Unexpected error writing fixures: %v", err)
	}

	var it graph.Iterator
	it = qs.NodesAllIterator()
	if it == nil {
		t.Fatal("Got nil iterator.")
	}
	defer it.Reset()

	size, _ := it.Size()
	if size <= 0 || size >= 20 {
		t.Errorf("Unexpected size, got:%d expect:(0, 20)", size)
	}
	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 := IterateNames(qs, 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.Contains(qs.ValueOf(pq)) {
			t.Errorf("Failed to find and check %q correctly", pq)
		}
	}
	for _, pq := range []string{"baller"} {
		if it.Contains(qs.ValueOf(pq)) {
			t.Errorf("Failed to check %q correctly", pq)
		}
	}
}
예제 #9
0
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 "and":
		for _, sub := range it.GetSubIterators() {
			qs.nodeId++
			newNode := qs.MakeNode(sub)
			if sub.Type() != "or" {
				qs.StealNode(&n, newNode)
			} else {
				qs.AddNode(newNode)
				qs.AddLink(&Link{n.Id, newNode.Id, 0, 0})
			}
		}
	case "fixed":
		n.IsFixed = true
		for {
			val, more := it.Next()
			if !more {
				break
			}
			n.Values = append(n.Values, qs.ts.GetNameFor(val))
		}
	case "hasa":
		hasa := it.(*HasA)
		qs.PushHasa(n.Id, hasa.dir)
		qs.nodeId++
		newNode := qs.MakeNode(hasa.primaryIt)
		qs.AddNode(newNode)
		qs.RemoveHasa()
	case "or":
		for _, sub := range it.GetSubIterators() {
			qs.nodeId++
			newNode := qs.MakeNode(sub)
			if sub.Type() == "or" {
				qs.StealNode(&n, newNode)
			} else {
				qs.AddNode(newNode)
				qs.AddLink(&Link{n.Id, newNode.Id, 0, 0})
			}
		}
	case "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() == "fixed" {
			qs.StealNode(&n, newNode)
		} else {
			qs.AddNode(newNode)
		}
	case "optional":
		// Unsupported, for the moment
		fallthrough
	case "all":
	}
	return &n
}
예제 #10
0
파일: graphtest.go 프로젝트: dennwc/cayley
func TestIterator(t testing.TB, gen DatabaseFunc) {
	qs, opts, closer := gen(t)
	defer closer()

	MakeWriter(t, qs, opts, MakeQuadSet()...)

	var it graph.Iterator

	it = qs.NodesAllIterator()
	require.NotNil(t, it)

	size, _ := it.Size()
	require.True(t, size > 0 && size < 20, "Unexpected size")
	// TODO: leveldb had this test
	//if exact {
	//	t.Errorf("Got unexpected exact result.")
	//}
	require.Equal(t, graph.All, it.Type(), "Unexpected iterator type")

	optIt, changed := it.Optimize()
	require.True(t, !changed && optIt == it, "Optimize unexpectedly changed iterator: %v, %T", changed, optIt)

	expect := []string{
		"A",
		"B",
		"C",
		"D",
		"E",
		"F",
		"G",
		"follows",
		"status",
		"cool",
		"status_graph",
	}
	sort.Strings(expect)
	for i := 0; i < 2; i++ {
		got := IteratedRawStrings(t, qs, it)
		sort.Strings(got)
		require.Equal(t, expect, got, "Unexpected iterated result on repeat %d", i)
		it.Reset()
	}

	for _, pq := range expect {
		require.True(t, it.Contains(qs.ValueOf(quad.Raw(pq))), "Failed to find and check %q correctly", pq)

	}
	// FIXME(kortschak) Why does this fail?
	/*
		for _, pq := range []string{"baller"} {
			if it.Contains(qs.ValueOf(pq)) {
				t.Errorf("Failed to check %q correctly", pq)
			}
		}
	*/
	it.Reset()

	it = qs.QuadsAllIterator()
	optIt, changed = it.Optimize()
	require.True(t, !changed && optIt == it, "Optimize unexpectedly changed iterator: %v, %T", changed, optIt)

	require.True(t, graph.Next(it))

	q := qs.Quad(it.Result())
	require.Nil(t, it.Err())
	require.True(t, q.IsValid(), "Invalid quad returned: %q", q)
	set := MakeQuadSet()
	var ok bool
	for _, e := range set {
		if e.String() == q.String() {
			ok = true
			break
		}
	}
	require.True(t, ok, "Failed to find %q during iteration, got:%q", q, set)
}