Create quads hierarchy
* Move nquads into quad. * Create cquads simplified parser in quad. * Move Triple (renamed Quad) to quad. Also made sure mongo actually implements BulkLoader.
This commit is contained in:
parent
01bc63810b
commit
401c58426f
51 changed files with 13400 additions and 5495 deletions
|
|
@ -20,6 +20,7 @@ import (
|
|||
"github.com/barakmich/glog"
|
||||
"github.com/google/cayley/graph"
|
||||
"github.com/google/cayley/graph/iterator"
|
||||
"github.com/google/cayley/quad"
|
||||
|
||||
"github.com/petar/GoLLRB/llrb"
|
||||
)
|
||||
|
|
@ -40,21 +41,21 @@ func NewTripleDirectionIndex() *TripleDirectionIndex {
|
|||
return &tdi
|
||||
}
|
||||
|
||||
func (tdi *TripleDirectionIndex) GetForDir(d graph.Direction) map[int64]*llrb.LLRB {
|
||||
func (tdi *TripleDirectionIndex) GetForDir(d quad.Direction) map[int64]*llrb.LLRB {
|
||||
switch d {
|
||||
case graph.Subject:
|
||||
case quad.Subject:
|
||||
return tdi.subject
|
||||
case graph.Object:
|
||||
case quad.Object:
|
||||
return tdi.object
|
||||
case graph.Predicate:
|
||||
case quad.Predicate:
|
||||
return tdi.predicate
|
||||
case graph.Provenance:
|
||||
case quad.Provenance:
|
||||
return tdi.provenance
|
||||
}
|
||||
panic("illegal direction")
|
||||
}
|
||||
|
||||
func (tdi *TripleDirectionIndex) GetOrCreate(d graph.Direction, id int64) *llrb.LLRB {
|
||||
func (tdi *TripleDirectionIndex) GetOrCreate(d quad.Direction, id int64) *llrb.LLRB {
|
||||
directionIndex := tdi.GetForDir(d)
|
||||
if _, ok := directionIndex[id]; !ok {
|
||||
directionIndex[id] = llrb.New()
|
||||
|
|
@ -62,7 +63,7 @@ func (tdi *TripleDirectionIndex) GetOrCreate(d graph.Direction, id int64) *llrb.
|
|||
return directionIndex[id]
|
||||
}
|
||||
|
||||
func (tdi *TripleDirectionIndex) Get(d graph.Direction, id int64) (*llrb.LLRB, bool) {
|
||||
func (tdi *TripleDirectionIndex) Get(d quad.Direction, id int64) (*llrb.LLRB, bool) {
|
||||
directionIndex := tdi.GetForDir(d)
|
||||
tree, exists := directionIndex[id]
|
||||
return tree, exists
|
||||
|
|
@ -73,7 +74,7 @@ type TripleStore struct {
|
|||
tripleIdCounter int64
|
||||
idMap map[string]int64
|
||||
revIdMap map[int64]string
|
||||
triples []graph.Triple
|
||||
triples []quad.Quad
|
||||
size int64
|
||||
index TripleDirectionIndex
|
||||
// vip_index map[string]map[int64]map[string]map[int64]*llrb.Tree
|
||||
|
|
@ -83,10 +84,10 @@ 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)
|
||||
ts.triples = make([]quad.Quad, 1, 200)
|
||||
|
||||
// Sentinel null triple so triple indices start at 1
|
||||
ts.triples[0] = graph.Triple{}
|
||||
ts.triples[0] = quad.Quad{}
|
||||
ts.size = 1
|
||||
ts.index = *NewTripleDirectionIndex()
|
||||
ts.idCounter = 1
|
||||
|
|
@ -94,18 +95,18 @@ func newTripleStore() *TripleStore {
|
|||
return &ts
|
||||
}
|
||||
|
||||
func (ts *TripleStore) AddTripleSet(triples []*graph.Triple) {
|
||||
func (ts *TripleStore) AddTripleSet(triples []*quad.Quad) {
|
||||
for _, t := range triples {
|
||||
ts.AddTriple(t)
|
||||
}
|
||||
}
|
||||
|
||||
func (ts *TripleStore) tripleExists(t *graph.Triple) (bool, int64) {
|
||||
func (ts *TripleStore) tripleExists(t *quad.Quad) (bool, int64) {
|
||||
smallest := -1
|
||||
var smallest_tree *llrb.LLRB
|
||||
for d := graph.Subject; d <= graph.Provenance; d++ {
|
||||
for d := quad.Subject; d <= quad.Provenance; d++ {
|
||||
sid := t.Get(d)
|
||||
if d == graph.Provenance && sid == "" {
|
||||
if d == quad.Provenance && sid == "" {
|
||||
continue
|
||||
}
|
||||
id, ok := ts.idMap[sid]
|
||||
|
|
@ -137,7 +138,7 @@ func (ts *TripleStore) tripleExists(t *graph.Triple) (bool, int64) {
|
|||
return false, 0
|
||||
}
|
||||
|
||||
func (ts *TripleStore) AddTriple(t *graph.Triple) {
|
||||
func (ts *TripleStore) AddTriple(t *quad.Quad) {
|
||||
if exists, _ := ts.tripleExists(t); exists {
|
||||
return
|
||||
}
|
||||
|
|
@ -147,9 +148,9 @@ func (ts *TripleStore) AddTriple(t *graph.Triple) {
|
|||
ts.size++
|
||||
ts.tripleIdCounter++
|
||||
|
||||
for d := graph.Subject; d <= graph.Provenance; d++ {
|
||||
for d := quad.Subject; d <= quad.Provenance; d++ {
|
||||
sid := t.Get(d)
|
||||
if d == graph.Provenance && sid == "" {
|
||||
if d == quad.Provenance && sid == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := ts.idMap[sid]; !ok {
|
||||
|
|
@ -159,8 +160,8 @@ func (ts *TripleStore) AddTriple(t *graph.Triple) {
|
|||
}
|
||||
}
|
||||
|
||||
for d := graph.Subject; d <= graph.Provenance; d++ {
|
||||
if d == graph.Provenance && t.Get(d) == "" {
|
||||
for d := quad.Subject; d <= quad.Provenance; d++ {
|
||||
if d == quad.Provenance && t.Get(d) == "" {
|
||||
continue
|
||||
}
|
||||
id := ts.idMap[t.Get(d)]
|
||||
|
|
@ -171,7 +172,7 @@ func (ts *TripleStore) AddTriple(t *graph.Triple) {
|
|||
// TODO(barakmich): Add VIP indexing
|
||||
}
|
||||
|
||||
func (ts *TripleStore) RemoveTriple(t *graph.Triple) {
|
||||
func (ts *TripleStore) RemoveTriple(t *quad.Quad) {
|
||||
var tripleID int64
|
||||
var exists bool
|
||||
tripleID = 0
|
||||
|
|
@ -179,11 +180,11 @@ func (ts *TripleStore) RemoveTriple(t *graph.Triple) {
|
|||
return
|
||||
}
|
||||
|
||||
ts.triples[tripleID] = graph.Triple{}
|
||||
ts.triples[tripleID] = quad.Quad{}
|
||||
ts.size--
|
||||
|
||||
for d := graph.Subject; d <= graph.Provenance; d++ {
|
||||
if d == graph.Provenance && t.Get(d) == "" {
|
||||
for d := quad.Subject; d <= quad.Provenance; d++ {
|
||||
if d == quad.Provenance && t.Get(d) == "" {
|
||||
continue
|
||||
}
|
||||
id := ts.idMap[t.Get(d)]
|
||||
|
|
@ -191,8 +192,8 @@ func (ts *TripleStore) RemoveTriple(t *graph.Triple) {
|
|||
tree.Delete(Int64(tripleID))
|
||||
}
|
||||
|
||||
for d := graph.Subject; d <= graph.Provenance; d++ {
|
||||
if d == graph.Provenance && t.Get(d) == "" {
|
||||
for d := quad.Subject; d <= quad.Provenance; d++ {
|
||||
if d == quad.Provenance && t.Get(d) == "" {
|
||||
continue
|
||||
}
|
||||
id, ok := ts.idMap[t.Get(d)]
|
||||
|
|
@ -200,8 +201,8 @@ func (ts *TripleStore) RemoveTriple(t *graph.Triple) {
|
|||
continue
|
||||
}
|
||||
stillExists := false
|
||||
for d := graph.Subject; d <= graph.Provenance; d++ {
|
||||
if d == graph.Provenance && t.Get(d) == "" {
|
||||
for d := quad.Subject; d <= quad.Provenance; d++ {
|
||||
if d == quad.Provenance && t.Get(d) == "" {
|
||||
continue
|
||||
}
|
||||
nodeTree := ts.index.GetOrCreate(d, id)
|
||||
|
|
@ -217,11 +218,11 @@ func (ts *TripleStore) RemoveTriple(t *graph.Triple) {
|
|||
}
|
||||
}
|
||||
|
||||
func (ts *TripleStore) Triple(index graph.Value) *graph.Triple {
|
||||
func (ts *TripleStore) Quad(index graph.Value) *quad.Quad {
|
||||
return &ts.triples[index.(int64)]
|
||||
}
|
||||
|
||||
func (ts *TripleStore) TripleIterator(d graph.Direction, value graph.Value) graph.Iterator {
|
||||
func (ts *TripleStore) TripleIterator(d quad.Direction, value graph.Value) graph.Iterator {
|
||||
index, ok := ts.index.Get(d, value.(int64))
|
||||
data := fmt.Sprintf("dir:%s val:%d", d, value.(int64))
|
||||
if ok {
|
||||
|
|
@ -259,8 +260,8 @@ func (ts *TripleStore) FixedIterator() graph.FixedIterator {
|
|||
return iterator.NewFixedIteratorWithCompare(iterator.BasicEquality)
|
||||
}
|
||||
|
||||
func (ts *TripleStore) TripleDirection(val graph.Value, d graph.Direction) graph.Value {
|
||||
name := ts.Triple(val).Get(d)
|
||||
func (ts *TripleStore) TripleDirection(val graph.Value, d quad.Direction) graph.Value {
|
||||
name := ts.Quad(val).Get(d)
|
||||
return ts.ValueOf(name)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -19,8 +19,8 @@ import (
|
|||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/google/cayley/graph"
|
||||
"github.com/google/cayley/graph/iterator"
|
||||
"github.com/google/cayley/quad"
|
||||
)
|
||||
|
||||
// This is a simple test graph.
|
||||
|
|
@ -36,7 +36,7 @@ import (
|
|||
// \-->|#D#|------------->+---+
|
||||
// +---+
|
||||
//
|
||||
var simpleGraph = []*graph.Triple{
|
||||
var simpleGraph = []*quad.Quad{
|
||||
{"A", "follows", "B", ""},
|
||||
{"C", "follows", "B", ""},
|
||||
{"C", "follows", "D", ""},
|
||||
|
|
@ -50,7 +50,7 @@ var simpleGraph = []*graph.Triple{
|
|||
{"G", "status", "cool", "status_graph"},
|
||||
}
|
||||
|
||||
func makeTestStore(data []*graph.Triple) (*TripleStore, []pair) {
|
||||
func makeTestStore(data []*quad.Quad) (*TripleStore, []pair) {
|
||||
seen := make(map[string]struct{})
|
||||
ts := newTripleStore()
|
||||
var (
|
||||
|
|
@ -105,10 +105,10 @@ func TestIteratorsAndNextResultOrderA(t *testing.T) {
|
|||
all := ts.NodesAllIterator()
|
||||
|
||||
innerAnd := iterator.NewAnd()
|
||||
innerAnd.AddSubIterator(iterator.NewLinksTo(ts, fixed2, graph.Predicate))
|
||||
innerAnd.AddSubIterator(iterator.NewLinksTo(ts, all, graph.Object))
|
||||
innerAnd.AddSubIterator(iterator.NewLinksTo(ts, fixed2, quad.Predicate))
|
||||
innerAnd.AddSubIterator(iterator.NewLinksTo(ts, all, quad.Object))
|
||||
|
||||
hasa := iterator.NewHasA(ts, innerAnd, graph.Subject)
|
||||
hasa := iterator.NewHasA(ts, innerAnd, quad.Subject)
|
||||
outerAnd := iterator.NewAnd()
|
||||
outerAnd.AddSubIterator(fixed)
|
||||
outerAnd.AddSubIterator(hasa)
|
||||
|
|
@ -149,7 +149,7 @@ func TestLinksToOptimization(t *testing.T) {
|
|||
fixed := ts.FixedIterator()
|
||||
fixed.Add(ts.ValueOf("cool"))
|
||||
|
||||
lto := iterator.NewLinksTo(ts, fixed, graph.Object)
|
||||
lto := iterator.NewLinksTo(ts, fixed, quad.Object)
|
||||
lto.AddTag("foo")
|
||||
|
||||
newIt, changed := lto.Optimize()
|
||||
|
|
@ -173,7 +173,7 @@ func TestLinksToOptimization(t *testing.T) {
|
|||
func TestRemoveTriple(t *testing.T) {
|
||||
ts, _ := makeTestStore(simpleGraph)
|
||||
|
||||
ts.RemoveTriple(&graph.Triple{"E", "follows", "F", ""})
|
||||
ts.RemoveTriple(&quad.Quad{"E", "follows", "F", ""})
|
||||
|
||||
fixed := ts.FixedIterator()
|
||||
fixed.Add(ts.ValueOf("E"))
|
||||
|
|
@ -182,10 +182,10 @@ func TestRemoveTriple(t *testing.T) {
|
|||
fixed2.Add(ts.ValueOf("follows"))
|
||||
|
||||
innerAnd := iterator.NewAnd()
|
||||
innerAnd.AddSubIterator(iterator.NewLinksTo(ts, fixed, graph.Subject))
|
||||
innerAnd.AddSubIterator(iterator.NewLinksTo(ts, fixed2, graph.Predicate))
|
||||
innerAnd.AddSubIterator(iterator.NewLinksTo(ts, fixed, quad.Subject))
|
||||
innerAnd.AddSubIterator(iterator.NewLinksTo(ts, fixed2, quad.Predicate))
|
||||
|
||||
hasa := iterator.NewHasA(ts, innerAnd, graph.Object)
|
||||
hasa := iterator.NewHasA(ts, innerAnd, quad.Object)
|
||||
|
||||
newIt, _ := hasa.Optimize()
|
||||
_, ok := newIt.Next()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue