Ejemplo n.º 1
0
func (ts *TripleStore) RemoveTriple(t *graph.Triple) {
	_, err := ts.db.Get(ts.createKeyFor(spo, t), ts.readopts)
	if err != nil && err != leveldb.ErrNotFound {
		glog.Errorf("Couldn't access DB to confirm deletion")
		return
	}
	if err == leveldb.ErrNotFound {
		// No such triple in the database, forget about it.
		return
	}
	batch := &leveldb.Batch{}
	batch.Delete(ts.createKeyFor(spo, t))
	batch.Delete(ts.createKeyFor(osp, t))
	batch.Delete(ts.createKeyFor(pos, t))
	ts.UpdateValueKeyBy(t.Get(graph.Subject), -1, batch)
	ts.UpdateValueKeyBy(t.Get(graph.Predicate), -1, batch)
	ts.UpdateValueKeyBy(t.Get(graph.Object), -1, batch)
	if t.Get(graph.Provenance) != "" {
		batch.Delete(ts.createProvKeyFor(pso, t))
		ts.UpdateValueKeyBy(t.Get(graph.Provenance), -1, batch)
	}
	err = ts.db.Write(batch, nil)
	if err != nil {
		glog.Errorf("Couldn't delete triple %s", t)
		return
	}
	ts.size--
}
Ejemplo n.º 2
0
func (ts *TripleStore) createKeyFor(dir1, dir2, dir3 string, triple *graph.Triple) []byte {
	key := make([]byte, 0, 2+(ts.hasher.Size()*3))
	key = append(key, []byte(dir1+dir2)...)
	key = append(key, ts.convertStringToByteHash(triple.Get(dir1))...)
	key = append(key, ts.convertStringToByteHash(triple.Get(dir2))...)
	key = append(key, ts.convertStringToByteHash(triple.Get(dir3))...)
	return key
}
Ejemplo n.º 3
0
func (ts *TripleStore) buildWrite(batch *leveldb.Batch, t *graph.Triple) {
	ts.buildTripleWrite(batch, t)
	ts.UpdateValueKeyBy(t.Get(graph.Subject), 1, nil)
	ts.UpdateValueKeyBy(t.Get(graph.Predicate), 1, nil)
	ts.UpdateValueKeyBy(t.Get(graph.Object), 1, nil)
	if t.Get(graph.Provenance) != "" {
		ts.UpdateValueKeyBy(t.Get(graph.Provenance), 1, nil)
	}
}
Ejemplo n.º 4
0
func (ts *TripleStore) createKeyFor(d [3]graph.Direction, triple *graph.Triple) []byte {
	key := make([]byte, 0, 2+(ts.hasher.Size()*3))
	// TODO(kortschak) Remove dependence on String() method.
	key = append(key, []byte{d[0].Prefix(), d[1].Prefix()}...)
	key = append(key, ts.convertStringToByteHash(triple.Get(d[0]))...)
	key = append(key, ts.convertStringToByteHash(triple.Get(d[1]))...)
	key = append(key, ts.convertStringToByteHash(triple.Get(d[2]))...)
	return key
}
Ejemplo n.º 5
0
func (ts *TripleStore) buildTripleWrite(batch *leveldb.Batch, t *graph.Triple) {
	bytes, err := json.Marshal(*t)
	if err != nil {
		glog.Errorf("Couldn't write to buffer for triple %s\n  %s\n", t, err)
		return
	}
	batch.Put(ts.createKeyFor(spo, t), bytes)
	batch.Put(ts.createKeyFor(osp, t), bytes)
	batch.Put(ts.createKeyFor(pos, t), bytes)
	if t.Get(graph.Provenance) != "" {
		batch.Put(ts.createProvKeyFor(pso, t), bytes)
	}
}
Ejemplo n.º 6
0
func (ts *TripleStore) buildTripleWrite(batch *leveldb.Batch, t *graph.Triple) {
	bytes, err := json.Marshal(*t)
	if err != nil {
		glog.Errorf("Couldn't write to buffer for triple %s\n  %s\n", t.ToString(), err)
		return
	}
	batch.Put(ts.createKeyFor("s", "p", "o", t), bytes)
	batch.Put(ts.createKeyFor("o", "s", "p", t), bytes)
	batch.Put(ts.createKeyFor("p", "o", "s", t), bytes)
	if t.Get("c") != "" {
		batch.Put(ts.createProvKeyFor("p", "s", "o", t), bytes)
	}
}
Ejemplo n.º 7
0
func (ts *TripleStore) RemoveTriple(t *graph.Triple) {
	_, err := ts.db.Get(ts.createKeyFor("s", "p", "o", t), ts.readopts)
	if err != nil && err != leveldb.ErrNotFound {
		glog.Errorf("Couldn't access DB to confirm deletion")
		return
	}
	if err == leveldb.ErrNotFound {
		// No such triple in the database, forget about it.
		return
	}
	batch := &leveldb.Batch{}
	batch.Delete(ts.createKeyFor("s", "p", "o", t))
	batch.Delete(ts.createKeyFor("o", "s", "p", t))
	batch.Delete(ts.createKeyFor("p", "o", "s", t))
	ts.UpdateValueKeyBy(t.Get("s"), -1, batch)
	ts.UpdateValueKeyBy(t.Get("p"), -1, batch)
	ts.UpdateValueKeyBy(t.Get("o"), -1, batch)
	if t.Get("c") != "" {
		batch.Delete(ts.createProvKeyFor("p", "s", "o", t))
		ts.UpdateValueKeyBy(t.Get("c"), -1, batch)
	}
	err = ts.db.Write(batch, nil)
	if err != nil {
		glog.Errorf("Couldn't delete triple %s", t.ToString())
		return
	}
	ts.size--
}
Ejemplo n.º 8
0
func (ts *TripleStore) buildWrite(batch *leveldb.Batch, t *graph.Triple) {
	ts.buildTripleWrite(batch, t)
	ts.UpdateValueKeyBy(t.Get("s"), 1, nil)
	ts.UpdateValueKeyBy(t.Get("p"), 1, nil)
	ts.UpdateValueKeyBy(t.Get("o"), 1, nil)
	if t.Get("c") != "" {
		ts.UpdateValueKeyBy(t.Get("c"), 1, nil)
	}
}
Ejemplo n.º 9
0
func (ts *TripleStore) AddTriple(t *graph.Triple) {
	if exists, _ := ts.tripleExists(t); exists {
		return
	}
	var tripleID int64
	ts.triples = append(ts.triples, *t)
	tripleID = ts.tripleIdCounter
	ts.size++
	ts.tripleIdCounter++

	for d := graph.Subject; d <= graph.Provenance; d++ {
		sid := t.Get(d)
		if d == graph.Provenance && sid == "" {
			continue
		}
		if _, ok := ts.idMap[sid]; !ok {
			ts.idMap[sid] = ts.idCounter
			ts.revIdMap[ts.idCounter] = sid
			ts.idCounter++
		}
	}

	for d := graph.Subject; d <= graph.Provenance; d++ {
		if d == graph.Provenance && t.Get(d) == "" {
			continue
		}
		id := ts.idMap[t.Get(d)]
		tree := ts.index.GetOrCreate(d, id)
		tree.ReplaceOrInsert(Int64(tripleID))
	}

	// TODO(barakmich): Add VIP indexing
}
Ejemplo n.º 10
0
func (ts *MemTripleStore) AddTriple(t *graph.Triple) {
	if exists, _ := ts.tripleExists(t); exists {
		return
	}
	var tripleID int64
	ts.triples = append(ts.triples, *t)
	tripleID = ts.tripleIdCounter
	ts.size++
	ts.tripleIdCounter++

	for _, dir := range graph.TripleDirections {
		sid := t.Get(dir)
		if dir == "c" && sid == "" {
			continue
		}
		if _, ok := ts.idMap[sid]; !ok {
			ts.idMap[sid] = ts.idCounter
			ts.revIdMap[ts.idCounter] = sid
			ts.idCounter++
		}
	}

	for _, dir := range graph.TripleDirections {
		if dir == "c" && t.Get(dir) == "" {
			continue
		}
		id := ts.idMap[t.Get(dir)]
		tree := ts.index.GetOrCreate(dir, id)
		tree.ReplaceOrInsert(Int64(tripleID))
	}

	// TODO(barakmich): Add VIP indexing
}
Ejemplo n.º 11
0
func (ts *TripleStore) tripleExists(t *graph.Triple) (bool, int64) {
	smallest := -1
	var smallest_tree *llrb.LLRB
	for d := graph.Subject; d <= graph.Provenance; d++ {
		sid := t.Get(d)
		if d == graph.Provenance && sid == "" {
			continue
		}
		id, ok := ts.idMap[sid]
		// If we've never heard about a node, it most not exist
		if !ok {
			return false, 0
		}
		index, exists := ts.index.Get(d, id)
		if !exists {
			// If it's never been indexed in this direction, it can't exist.
			return false, 0
		}
		if smallest == -1 || index.Len() < smallest {
			smallest = index.Len()
			smallest_tree = index
		}
	}
	it := NewLlrbIterator(smallest_tree, "")

	for {
		val, ok := it.Next()
		if !ok {
			break
		}
		if t.Equals(&ts.triples[val.(int64)]) {
			return true, val.(int64)
		}
	}
	return false, 0
}
Ejemplo n.º 12
0
func (ts *TripleStore) RemoveTriple(t *graph.Triple) {
	var tripleID int64
	var exists bool
	tripleID = 0
	if exists, tripleID = ts.tripleExists(t); !exists {
		return
	}

	ts.triples[tripleID] = graph.Triple{}
	ts.size--

	for d := graph.Subject; d <= graph.Provenance; d++ {
		if d == graph.Provenance && t.Get(d) == "" {
			continue
		}
		id := ts.idMap[t.Get(d)]
		tree := ts.index.GetOrCreate(d, id)
		tree.Delete(Int64(tripleID))
	}

	for d := graph.Subject; d <= graph.Provenance; d++ {
		if d == graph.Provenance && t.Get(d) == "" {
			continue
		}
		id, ok := ts.idMap[t.Get(d)]
		if !ok {
			continue
		}
		stillExists := false
		for d := graph.Subject; d <= graph.Provenance; d++ {
			if d == graph.Provenance && t.Get(d) == "" {
				continue
			}
			nodeTree := ts.index.GetOrCreate(d, id)
			if nodeTree.Len() != 0 {
				stillExists = true
				break
			}
		}
		if !stillExists {
			delete(ts.idMap, t.Get(d))
			delete(ts.revIdMap, id)
		}
	}
}
Ejemplo n.º 13
0
func (ts *MemTripleStore) RemoveTriple(t *graph.Triple) {
	var tripleID int64
	var exists bool
	tripleID = 0
	if exists, tripleID = ts.tripleExists(t); !exists {
		return
	}

	ts.triples[tripleID] = graph.Triple{}
	ts.size--

	for _, dir := range graph.TripleDirections {
		if dir == "c" && t.Get(dir) == "" {
			continue
		}
		id := ts.idMap[t.Get(dir)]
		tree := ts.index.GetOrCreate(dir, id)
		tree.Delete(Int64(tripleID))
	}

	for _, dir := range graph.TripleDirections {
		if dir == "c" && t.Get(dir) == "" {
			continue
		}
		id, ok := ts.idMap[t.Get(dir)]
		if !ok {
			continue
		}
		stillExists := false
		for _, dir := range graph.TripleDirections {
			if dir == "c" && t.Get(dir) == "" {
				continue
			}
			nodeTree := ts.index.GetOrCreate(dir, id)
			if nodeTree.Len() != 0 {
				stillExists = true
				break
			}
		}
		if !stillExists {
			delete(ts.idMap, t.Get(dir))
			delete(ts.revIdMap, id)
		}
	}
}