Exemplo n.º 1
0
func NewAllIterator(prefix string, d quad.Direction, ts *TripleStore) *AllIterator {
	opts := &opt.ReadOptions{
		DontFillCache: true,
	}

	it := AllIterator{
		uid:    iterator.NextUID(),
		ro:     opts,
		iter:   ts.db.NewIterator(nil, opts),
		prefix: []byte(prefix),
		dir:    d,
		open:   true,
		ts:     ts,
	}

	it.iter.Seek(it.prefix)
	if !it.iter.Valid() {
		// FIXME(kortschak) What are the semantics here? Is this iterator usable?
		// If not, we should return nil *Iterator and an error.
		it.open = false
		it.iter.Release()
	}

	return &it
}
Exemplo n.º 2
0
func (it *Iterator) Clone() graph.Iterator {
	var iter *b.Enumerator
	if it.result != nil {
		var ok bool
		iter, ok = it.tree.Seek(it.result.(int64))
		if !ok {
			panic("value unexpectedly missing")
		}
	} else {
		var err error
		iter, err = it.tree.SeekFirst()
		if err != nil {
			iter = nil
		}
	}

	m := &Iterator{
		uid:  iterator.NextUID(),
		ts:   it.ts,
		tree: it.tree,
		iter: iter,
		data: it.data,
	}
	m.tags.CopyFrom(it)

	return m
}
Exemplo n.º 3
0
func NewAllIterator(qs *QuadStore, kind string) *Iterator {
	if kind != nodeKind && kind != quadKind {
		glog.Error("Cannot create iterator for an unknown kind")
		return &Iterator{done: true}
	}
	if qs.context == nil {
		glog.Error("Cannot create iterator without a valid context")
		return &Iterator{done: true}
	}

	var size int64
	if kind == nodeKind {
		size = qs.NodeSize()
	} else {
		size = qs.Size()
	}

	return &Iterator{
		uid:   iterator.NextUID(),
		qs:    qs,
		size:  size,
		dir:   quad.Any,
		isAll: true,
		kind:  kind,
		done:  false,
	}
}
Exemplo n.º 4
0
func TestSQLNodeIteration(t *testing.T) {
	if *postgres_path == "" {
		t.SkipNow()
	}
	db, err := newQuadStore(*postgres_path, nil)
	if err != nil {
		t.Fatal(err)
	}
	link := NewSQLLinkIterator(db.(*QuadStore), quad.Object, "/en/humphrey_bogart")
	it := &SQLIterator{
		uid: iterator.NextUID(),
		qs:  db.(*QuadStore),
		sql: &SQLNodeIterator{
			tableName: newTableName(),
			linkIt: sqlItDir{
				it:  link.sql,
				dir: quad.Subject,
			},
		},
	}
	s, v := it.sql.buildSQL(true, nil)
	t.Log(s, v)
	c := 0
	for graph.Next(it) {
		t.Log(it.Result())
		c += 1
	}
	if c != 56 {
		t.Errorf("Not enough results, got %d expected 56", c)
	}

}
Exemplo n.º 5
0
func NewIterator(prefix string, d quad.Direction, value graph.Value, qs *QuadStore) *Iterator {
	vb := value.(Token)
	p := make([]byte, 0, 2+quad.HashSize)
	p = append(p, []byte(prefix)...)
	p = append(p, []byte(vb[1:])...)

	opts := &opt.ReadOptions{
		DontFillCache: true,
	}

	it := Iterator{
		uid:            iterator.NextUID(),
		nextPrefix:     p,
		checkID:        vb,
		dir:            d,
		originalPrefix: prefix,
		ro:             opts,
		iter:           qs.db.NewIterator(nil, opts),
		open:           true,
		qs:             qs,
	}

	ok := it.iter.Seek(it.nextPrefix)
	if !ok {
		it.open = false
		it.iter.Release()
		glog.Error("Opening LevelDB iterator couldn't seek to location ", it.nextPrefix)
	}

	return &it
}
Exemplo n.º 6
0
func NewIterator(qs *TripleStore, collection string, d quad.Direction, val graph.Value) *Iterator {
	name := qs.NameOf(val)

	constraint := bson.M{d.String(): name}

	size, err := qs.db.C(collection).Find(constraint).Count()
	if err != nil {
		// FIXME(kortschak) This should be passed back rather than just logging.
		glog.Errorln("Trouble getting size for iterator! ", err)
		return nil
	}

	return &Iterator{
		uid:        iterator.NextUID(),
		name:       name,
		constraint: constraint,
		collection: collection,
		qs:         qs,
		dir:        d,
		iter:       qs.db.C(collection).Find(constraint).Iter(),
		size:       int64(size),
		hash:       val.(string),
		isAll:      false,
	}
}
Exemplo n.º 7
0
func NewIterator(prefix string, d quad.Direction, value graph.Value, qs *TripleStore) *Iterator {
	vb := value.([]byte)
	p := make([]byte, 0, 2+qs.hasher.Size())
	p = append(p, []byte(prefix)...)
	p = append(p, []byte(vb[1:])...)

	opts := &opt.ReadOptions{
		DontFillCache: true,
	}

	it := Iterator{
		uid:            iterator.NextUID(),
		nextPrefix:     p,
		checkId:        vb,
		dir:            d,
		originalPrefix: prefix,
		ro:             opts,
		iter:           qs.db.NewIterator(nil, opts),
		open:           true,
		qs:             qs,
	}

	ok := it.iter.Seek(it.nextPrefix)
	if !ok {
		// FIXME(kortschak) What are the semantics here? Is this iterator usable?
		// If not, we should return nil *Iterator and an error.
		it.open = false
		it.iter.Release()
	}

	return &it
}
Exemplo n.º 8
0
func NewSQLIterator(qs *QuadStore, sql sqlIterator) *SQLIterator {
	l := &SQLIterator{
		uid: iterator.NextUID(),
		qs:  qs,
		sql: sql,
	}
	return l
}
Exemplo n.º 9
0
func NewAllIterator(qs *QuadStore, table string) *AllIterator {
	it := &AllIterator{
		uid:   iterator.NextUID(),
		qs:    qs,
		table: table,
	}
	return it
}
Exemplo n.º 10
0
func NewAllIterator(bucket []byte, d quad.Direction, qs *QuadStore) *AllIterator {
	return &AllIterator{
		uid:    iterator.NextUID(),
		bucket: bucket,
		dir:    d,
		qs:     qs,
	}
}
Exemplo n.º 11
0
func NewLlrbIterator(tree *llrb.LLRB, data string) *Iterator {
	return &Iterator{
		uid:      iterator.NextUID(),
		tree:     tree,
		iterLast: Int64(-1),
		data:     data,
	}
}
Exemplo n.º 12
0
// NewAllIterator allocates and initializes an AllIterator that is returned to
// the caller.
func NewAllIterator(db string, d quad.Direction, qs *QuadStore) *AllIterator {
	return &AllIterator{
		uid: iterator.NextUID(),
		db:  db,
		dir: d,
		qs:  qs,
	}
}
Exemplo n.º 13
0
func (it *SQLIterator) Clone() graph.Iterator {
	m := &SQLIterator{
		uid: iterator.NextUID(),
		qs:  it.qs,
		sql: it.sql.sqlClone(),
	}
	return m
}
Exemplo n.º 14
0
// NewLinksTo constructs a new indexed LinksTo iterator for Mongo around a direction
// and a subiterator of nodes.
func NewLinksTo(qs *QuadStore, it graph.Iterator, collection string, d quad.Direction, lset []graph.Linkage) *LinksTo {
	return &LinksTo{
		uid:        iterator.NextUID(),
		qs:         qs,
		primaryIt:  it,
		dir:        d,
		nextIt:     nil,
		lset:       lset,
		collection: collection,
	}
}
Exemplo n.º 15
0
func NewIterator(tree *Tree, qs *QuadStore, d quad.Direction, value graph.Value) *Iterator {
	iter := tree.SeekFirst()
	return &Iterator{
		nodes: d == 0,
		uid:   iterator.NextUID(),
		qs:    qs,
		tree:  tree,
		iter:  iter,
		d:     d,
		value: value,
	}
}
Exemplo n.º 16
0
func NewIteratorWithConstraints(qs *QuadStore, collection string, constraint bson.M) *Iterator {
	return &Iterator{
		uid:        iterator.NextUID(),
		qs:         qs,
		dir:        quad.Any,
		constraint: constraint,
		collection: collection,
		iter:       nil,
		size:       -1,
		hash:       "",
		isAll:      false,
	}
}
Exemplo n.º 17
0
func NewAllIterator(qs *QuadStore, collection string) *Iterator {
	return &Iterator{
		uid:        iterator.NextUID(),
		qs:         qs,
		dir:        quad.Any,
		constraint: nil,
		collection: collection,
		iter:       nil,
		size:       -1,
		hash:       "",
		isAll:      true,
	}
}
Exemplo n.º 18
0
func NewIterator(tree *b.Tree, data string, ts *TripleStore) *Iterator {
	iter, err := tree.SeekFirst()
	if err != nil {
		iter = nil
	}
	return &Iterator{
		uid:  iterator.NextUID(),
		ts:   ts,
		tree: tree,
		iter: iter,
		data: data,
	}
}
Exemplo n.º 19
0
func NewIterator(tree *b.Tree, qs *QuadStore, d quad.Direction, value graph.Value) *Iterator {
	iter, err := tree.SeekFirst()
	if err != nil {
		iter = nil
	}
	return &Iterator{
		uid:   iterator.NextUID(),
		qs:    qs,
		tree:  tree,
		iter:  iter,
		d:     d,
		value: value,
	}
}
Exemplo n.º 20
0
func NewIterator(qs *QuadStore, collection string, d quad.Direction, val graph.Value) *Iterator {
	h := val.(NodeHash)

	constraint := bson.M{d.String(): string(h)}

	return &Iterator{
		uid:        iterator.NextUID(),
		constraint: constraint,
		collection: collection,
		qs:         qs,
		dir:        d,
		iter:       nil,
		size:       -1,
		hash:       h,
		isAll:      false,
	}
}
Exemplo n.º 21
0
func NewSQLLinkIterator(qs *QuadStore, d quad.Direction, val string) *SQLIterator {
	l := &SQLIterator{
		uid: iterator.NextUID(),
		qs:  qs,
		sql: &SQLLinkIterator{
			constraints: []constraint{
				constraint{
					dir:  d,
					vals: []string{val},
				},
			},
			tableName: newTableName(),
			size:      0,
		},
	}
	return l
}
Exemplo n.º 22
0
func newSQLLinkIterator(qs *QuadStore, d quad.Direction, hash NodeHash) *SQLIterator {
	l := &SQLIterator{
		uid: iterator.NextUID(),
		qs:  qs,
		sql: &SQLLinkIterator{
			constraints: []constraint{
				constraint{
					dir:    d,
					hashes: []NodeHash{hash},
				},
			},
			tableName: newTableName(),
			size:      0,
		},
	}
	return l
}
Exemplo n.º 23
0
func NewIterator(qs *QuadStore, collection string, d quad.Direction, val graph.Value) *Iterator {
	name := qs.NameOf(val)

	constraint := bson.M{d.String(): name}

	return &Iterator{
		uid:        iterator.NextUID(),
		name:       name,
		constraint: constraint,
		collection: collection,
		qs:         qs,
		dir:        d,
		iter:       nil,
		size:       -1,
		hash:       val.(string),
		isAll:      false,
	}
}
Exemplo n.º 24
0
func NewAllIterator(qs *TripleStore, collection string) *Iterator {
	size, err := qs.db.C(collection).Count()
	if err != nil {
		// FIXME(kortschak) This should be passed back rather than just logging.
		glog.Errorln("Trouble getting size for iterator! ", err)
		return nil
	}

	return &Iterator{
		uid:        iterator.NextUID(),
		qs:         qs,
		dir:        quad.Any,
		constraint: nil,
		collection: collection,
		iter:       qs.db.C(collection).Find(nil).Iter(),
		size:       int64(size),
		hash:       "",
		isAll:      true,
	}
}
Exemplo n.º 25
0
func NewIterator(bucket []byte, d quad.Direction, value graph.Value, qs *QuadStore) *Iterator {
	tok := value.(*Token)
	if !bytes.Equal(tok.bucket, nodeBucket) {
		glog.Error("creating an iterator from a non-node value")
		return &Iterator{done: true}
	}

	it := Iterator{
		uid:    iterator.NextUID(),
		bucket: bucket,
		dir:    d,
		qs:     qs,
		size:   qs.SizeOf(value),
	}

	it.checkID = make([]byte, len(tok.key))
	copy(it.checkID, tok.key)

	return &it
}
Exemplo n.º 26
0
func NewIterator(qs *QuadStore, k string, d quad.Direction, val graph.Value) *Iterator {
	t := val.(*Token)
	if t == nil {
		glog.Error("Token == nil")
	}
	if t.Kind != nodeKind {
		glog.Error("Cannot create an iterator from a non-node value")
		return &Iterator{done: true}
	}
	if k != nodeKind && k != quadKind {
		glog.Error("Cannot create iterator for unknown kind")
		return &Iterator{done: true}
	}
	if qs.context == nil {
		glog.Error("Cannot create iterator without a valid context")
		return &Iterator{done: true}
	}
	name := qs.NameOf(t)

	// The number of references to this node is held in the nodes entity
	key := qs.createKeyFromToken(t)
	foundNode := new(NodeEntry)
	err := datastore.Get(qs.context, key, foundNode)
	if err != nil && err != datastore.ErrNoSuchEntity {
		glog.Errorf("Error: %v", err)
		return &Iterator{done: true}
	}
	size := foundNode.Size

	return &Iterator{
		uid:   iterator.NextUID(),
		name:  name,
		dir:   d,
		qs:    qs,
		size:  size,
		isAll: false,
		kind:  k,
		hash:  t.Hash,
		done:  false,
	}
}
Exemplo n.º 27
0
// NewIterator allocates and initializes a new Iterator that is returned to the
// caller.
func NewIterator(db string, d quad.Direction, value graph.Value, qs *QuadStore) *Iterator {
	tok := value.(*Token)
	if tok.db != nodeDB {
		glog.Error("creating an iterator from a non-node value")
		return &Iterator{done: true}
	}

	it := Iterator{
		uid:  iterator.NextUID(),
		dbi:  qs.nodeDBI,
		db:   db,
		dir:  d,
		qs:   qs,
		size: qs.SizeOf(value),
	}

	it.checkID = make([]byte, len(tok.key))
	copy(it.checkID, tok.key)

	return &it
}
Exemplo n.º 28
0
func (it *Iterator) Clone() graph.Iterator {
	var iter *Enumerator
	if it.result > 0 {
		var ok bool
		iter, ok = it.tree.Seek(it.result)
		if !ok {
			panic("value unexpectedly missing")
		}
	} else {
		iter = it.tree.SeekFirst()
	}

	m := &Iterator{
		uid:   iterator.NextUID(),
		qs:    it.qs,
		tree:  it.tree,
		iter:  iter,
		d:     it.d,
		value: it.value,
	}
	m.tags.CopyFrom(it)

	return m
}
Exemplo n.º 29
0
func (qs *QuadStore) optimizeLinksTo(it *iterator.LinksTo) (graph.Iterator, bool) {
	subs := it.SubIterators()
	if len(subs) != 1 {
		return it, false
	}
	primary := subs[0]
	switch primary.Type() {
	case graph.Fixed:
		size, _ := primary.Size()
		if size == 0 {
			return iterator.NewNull(), true
		}
		if size == 1 {
			if !graph.Next(primary) {
				panic("sql: unexpected size during optimize")
			}
			val := primary.Result()
			newIt := qs.QuadIterator(it.Direction(), val)
			nt := newIt.Tagger()
			nt.CopyFrom(it)
			for _, tag := range primary.Tagger().Tags() {
				nt.AddFixed(tag, val)
			}
			it.Close()
			return newIt, true
		} else if size > 1 {
			var vals []string
			for graph.Next(primary) {
				vals = append(vals, qs.NameOf(primary.Result()))
			}
			lsql := &SQLLinkIterator{
				constraints: []constraint{
					constraint{
						dir:  it.Direction(),
						vals: vals,
					},
				},
				tableName: newTableName(),
				size:      0,
			}
			l := &SQLIterator{
				uid: iterator.NextUID(),
				qs:  qs,
				sql: lsql,
			}
			nt := l.Tagger()
			nt.CopyFrom(it)
			for _, t := range primary.Tagger().Tags() {
				lsql.tagdirs = append(lsql.tagdirs, tagDir{
					dir: it.Direction(),
					tag: t,
				})
			}
			it.Close()
			return l, true
		}
	case sqlType:
		p := primary.(*SQLIterator)
		newit, err := linksto(p.sql, it.Direction(), qs)
		if err != nil {
			glog.Errorln(err)
			return it, false
		}
		newit.Tagger().CopyFrom(it)
		return newit, true
	case graph.All:
		linkit := &SQLLinkIterator{
			tableName: newTableName(),
			size:      qs.Size(),
		}
		for _, t := range primary.Tagger().Tags() {
			linkit.tagdirs = append(linkit.tagdirs, tagDir{
				dir: it.Direction(),
				tag: t,
			})
		}
		for k, v := range primary.Tagger().Fixed() {
			linkit.tagger.AddFixed(k, v)
		}
		linkit.tagger.CopyFrom(it)
		newit := NewSQLIterator(qs, linkit)
		return newit, true
	}
	return it, false
}