Destutter graph/...
This commit is contained in:
parent
913d567ae1
commit
40f3363cde
20 changed files with 188 additions and 189 deletions
|
|
@ -29,7 +29,7 @@ import (
|
|||
func init() {
|
||||
glog.SetToStderr(true)
|
||||
cfg := config.ParseConfigFromFile("cayley_appengine.cfg")
|
||||
ts := memstore.NewMemTripleStore()
|
||||
ts := memstore.NewTripleStore()
|
||||
glog.Errorln(cfg)
|
||||
LoadTriplesFromFileInto(ts, cfg.DatabasePath, cfg.LoadSize)
|
||||
http.SetupRoutes(ts, cfg)
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ func Load(ts graph.TripleStore, cfg *config.Config, triplePath string, firstTime
|
|||
switch cfg.DatabaseType {
|
||||
case "mongo", "mongodb":
|
||||
if firstTime {
|
||||
loadMongo(ts.(*mongo.MongoTripleStore), triplePath)
|
||||
loadMongo(ts.(*mongo.TripleStore), triplePath)
|
||||
} else {
|
||||
LoadTriplesFromFileInto(ts, triplePath, cfg.LoadSize)
|
||||
}
|
||||
|
|
@ -43,7 +43,7 @@ func Load(ts graph.TripleStore, cfg *config.Config, triplePath string, firstTime
|
|||
|
||||
}
|
||||
|
||||
func loadMongo(ts *mongo.MongoTripleStore, path string) {
|
||||
func loadMongo(ts *mongo.TripleStore, path string) {
|
||||
tChan := make(chan *graph.Triple)
|
||||
go ReadTriplesFromFile(tChan, path)
|
||||
ts.BulkLoad(tChan)
|
||||
|
|
|
|||
|
|
@ -28,11 +28,11 @@ func OpenTSFrom(cfg *config.Config) graph.TripleStore {
|
|||
glog.Infof("Opening database \"%s\" at %s", cfg.DatabaseType, cfg.DatabasePath)
|
||||
switch cfg.DatabaseType {
|
||||
case "mongo", "mongodb":
|
||||
return mongo.NewMongoTripleStore(cfg.DatabasePath, cfg.DatabaseOptions)
|
||||
return mongo.NewTripleStore(cfg.DatabasePath, cfg.DatabaseOptions)
|
||||
case "leveldb":
|
||||
return leveldb.NewDefaultLevelDBTripleStore(cfg.DatabasePath, cfg.DatabaseOptions)
|
||||
return leveldb.NewTripleStore(cfg.DatabasePath, cfg.DatabaseOptions)
|
||||
case "mem":
|
||||
ts := memstore.NewMemTripleStore()
|
||||
ts := memstore.NewTripleStore()
|
||||
Load(ts, cfg, cfg.DatabasePath, true)
|
||||
return ts
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ func Repl(ts graph.TripleStore, queryLanguage string, cfg *config.Config) {
|
|||
var ses graph.Session
|
||||
switch queryLanguage {
|
||||
case "sexp":
|
||||
ses = sexp.NewSexpSession(ts)
|
||||
ses = sexp.NewSession(ts)
|
||||
case "mql":
|
||||
ses = mql.NewSession(ts)
|
||||
case "gremlin":
|
||||
|
|
|
|||
|
|
@ -19,26 +19,26 @@ import (
|
|||
"fmt"
|
||||
"strings"
|
||||
|
||||
leveldb_it "github.com/syndtr/goleveldb/leveldb/iterator"
|
||||
leveldb_opt "github.com/syndtr/goleveldb/leveldb/opt"
|
||||
"github.com/syndtr/goleveldb/leveldb/iterator"
|
||||
"github.com/syndtr/goleveldb/leveldb/opt"
|
||||
|
||||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
type LevelDBAllIterator struct {
|
||||
type AllIterator struct {
|
||||
graph.BaseIterator
|
||||
prefix []byte
|
||||
dir string
|
||||
open bool
|
||||
it leveldb_it.Iterator
|
||||
ts *LevelDBTripleStore
|
||||
ro *leveldb_opt.ReadOptions
|
||||
it iterator.Iterator
|
||||
ts *TripleStore
|
||||
ro *opt.ReadOptions
|
||||
}
|
||||
|
||||
func NewLevelDBAllIterator(prefix, dir string, ts *LevelDBTripleStore) *LevelDBAllIterator {
|
||||
var it LevelDBAllIterator
|
||||
func NewLevelDBAllIterator(prefix, dir string, ts *TripleStore) *AllIterator {
|
||||
var it AllIterator
|
||||
graph.BaseIteratorInit(&it.BaseIterator)
|
||||
it.ro = &leveldb_opt.ReadOptions{}
|
||||
it.ro = &opt.ReadOptions{}
|
||||
it.ro.DontFillCache = true
|
||||
it.it = ts.db.NewIterator(nil, it.ro)
|
||||
it.prefix = []byte(prefix)
|
||||
|
|
@ -53,7 +53,7 @@ func NewLevelDBAllIterator(prefix, dir string, ts *LevelDBTripleStore) *LevelDBA
|
|||
return &it
|
||||
}
|
||||
|
||||
func (a *LevelDBAllIterator) Reset() {
|
||||
func (a *AllIterator) Reset() {
|
||||
if !a.open {
|
||||
a.it = a.ts.db.NewIterator(nil, a.ro)
|
||||
a.open = true
|
||||
|
|
@ -65,13 +65,13 @@ func (a *LevelDBAllIterator) Reset() {
|
|||
}
|
||||
}
|
||||
|
||||
func (a *LevelDBAllIterator) Clone() graph.Iterator {
|
||||
func (a *AllIterator) Clone() graph.Iterator {
|
||||
out := NewLevelDBAllIterator(string(a.prefix), a.dir, a.ts)
|
||||
out.CopyTagsFrom(a)
|
||||
return out
|
||||
}
|
||||
|
||||
func (a *LevelDBAllIterator) Next() (graph.TSVal, bool) {
|
||||
func (a *AllIterator) Next() (graph.TSVal, bool) {
|
||||
if !a.open {
|
||||
a.Last = nil
|
||||
return nil, false
|
||||
|
|
@ -91,19 +91,19 @@ func (a *LevelDBAllIterator) Next() (graph.TSVal, bool) {
|
|||
return out, true
|
||||
}
|
||||
|
||||
func (a *LevelDBAllIterator) Check(v graph.TSVal) bool {
|
||||
func (a *AllIterator) Check(v graph.TSVal) bool {
|
||||
a.Last = v
|
||||
return true
|
||||
}
|
||||
|
||||
func (lit *LevelDBAllIterator) Close() {
|
||||
func (lit *AllIterator) Close() {
|
||||
if lit.open {
|
||||
lit.it.Release()
|
||||
lit.open = false
|
||||
}
|
||||
}
|
||||
|
||||
func (a *LevelDBAllIterator) Size() (int64, bool) {
|
||||
func (a *AllIterator) Size() (int64, bool) {
|
||||
size, err := a.ts.GetApproximateSizeForPrefix(a.prefix)
|
||||
if err == nil {
|
||||
return size, false
|
||||
|
|
@ -112,19 +112,19 @@ func (a *LevelDBAllIterator) Size() (int64, bool) {
|
|||
return int64(^uint64(0) >> 1), false
|
||||
}
|
||||
|
||||
func (lit *LevelDBAllIterator) DebugString(indent int) string {
|
||||
func (lit *AllIterator) DebugString(indent int) string {
|
||||
size, _ := lit.Size()
|
||||
return fmt.Sprintf("%s(%s tags: %v leveldb size:%d %s %p)", strings.Repeat(" ", indent), lit.Type(), lit.Tags(), size, lit.dir, lit)
|
||||
}
|
||||
|
||||
func (lit *LevelDBAllIterator) Type() string { return "all" }
|
||||
func (lit *LevelDBAllIterator) Sorted() bool { return false }
|
||||
func (lit *AllIterator) Type() string { return "all" }
|
||||
func (lit *AllIterator) Sorted() bool { return false }
|
||||
|
||||
func (lit *LevelDBAllIterator) Optimize() (graph.Iterator, bool) {
|
||||
func (lit *AllIterator) Optimize() (graph.Iterator, bool) {
|
||||
return lit, false
|
||||
}
|
||||
|
||||
func (lit *LevelDBAllIterator) GetStats() *graph.IteratorStats {
|
||||
func (lit *AllIterator) GetStats() *graph.IteratorStats {
|
||||
s, _ := lit.Size()
|
||||
return &graph.IteratorStats{
|
||||
CheckCost: 1,
|
||||
|
|
|
|||
|
|
@ -16,30 +16,29 @@ package leveldb
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
_ "encoding/binary"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
leveldb_it "github.com/syndtr/goleveldb/leveldb/iterator"
|
||||
leveldb_opt "github.com/syndtr/goleveldb/leveldb/opt"
|
||||
"github.com/syndtr/goleveldb/leveldb/iterator"
|
||||
"github.com/syndtr/goleveldb/leveldb/opt"
|
||||
|
||||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
type LevelDBIterator struct {
|
||||
type Iterator struct {
|
||||
graph.BaseIterator
|
||||
nextPrefix []byte
|
||||
checkId []byte
|
||||
dir string
|
||||
open bool
|
||||
it leveldb_it.Iterator
|
||||
ts *LevelDBTripleStore
|
||||
ro *leveldb_opt.ReadOptions
|
||||
it iterator.Iterator
|
||||
ts *TripleStore
|
||||
ro *opt.ReadOptions
|
||||
originalPrefix string
|
||||
}
|
||||
|
||||
func NewLevelDBIterator(prefix, dir string, value graph.TSVal, ts *LevelDBTripleStore) *LevelDBIterator {
|
||||
var it LevelDBIterator
|
||||
func NewIterator(prefix, dir string, value graph.TSVal, ts *TripleStore) *Iterator {
|
||||
var it Iterator
|
||||
graph.BaseIteratorInit(&it.BaseIterator)
|
||||
it.checkId = value.([]byte)
|
||||
it.dir = dir
|
||||
|
|
@ -47,7 +46,7 @@ func NewLevelDBIterator(prefix, dir string, value graph.TSVal, ts *LevelDBTriple
|
|||
it.nextPrefix = make([]byte, 0, 2+ts.hasher.Size())
|
||||
it.nextPrefix = append(it.nextPrefix, []byte(prefix)...)
|
||||
it.nextPrefix = append(it.nextPrefix, []byte(it.checkId[1:])...)
|
||||
it.ro = &leveldb_opt.ReadOptions{}
|
||||
it.ro = &opt.ReadOptions{}
|
||||
it.ro.DontFillCache = true
|
||||
it.it = ts.db.NewIterator(nil, it.ro)
|
||||
it.open = true
|
||||
|
|
@ -60,7 +59,7 @@ func NewLevelDBIterator(prefix, dir string, value graph.TSVal, ts *LevelDBTriple
|
|||
return &it
|
||||
}
|
||||
|
||||
func (lit *LevelDBIterator) Reset() {
|
||||
func (lit *Iterator) Reset() {
|
||||
if !lit.open {
|
||||
lit.it = lit.ts.db.NewIterator(nil, lit.ro)
|
||||
lit.open = true
|
||||
|
|
@ -72,20 +71,20 @@ func (lit *LevelDBIterator) Reset() {
|
|||
}
|
||||
}
|
||||
|
||||
func (lit *LevelDBIterator) Clone() graph.Iterator {
|
||||
out := NewLevelDBIterator(lit.originalPrefix, lit.dir, lit.checkId, lit.ts)
|
||||
func (lit *Iterator) Clone() graph.Iterator {
|
||||
out := NewIterator(lit.originalPrefix, lit.dir, lit.checkId, lit.ts)
|
||||
out.CopyTagsFrom(lit)
|
||||
return out
|
||||
}
|
||||
|
||||
func (lit *LevelDBIterator) Close() {
|
||||
func (lit *Iterator) Close() {
|
||||
if lit.open {
|
||||
lit.it.Release()
|
||||
lit.open = false
|
||||
}
|
||||
}
|
||||
|
||||
func (lit *LevelDBIterator) Next() (graph.TSVal, bool) {
|
||||
func (lit *Iterator) Next() (graph.TSVal, bool) {
|
||||
if lit.it == nil {
|
||||
lit.Last = nil
|
||||
return nil, false
|
||||
|
|
@ -114,7 +113,7 @@ func (lit *LevelDBIterator) Next() (graph.TSVal, bool) {
|
|||
return nil, false
|
||||
}
|
||||
|
||||
func GetPositionFromPrefix(prefix []byte, dir string, ts *LevelDBTripleStore) int {
|
||||
func GetPositionFromPrefix(prefix []byte, dir string, ts *TripleStore) int {
|
||||
if bytes.Equal(prefix, []byte("sp")) {
|
||||
switch dir {
|
||||
case "s":
|
||||
|
|
@ -166,7 +165,7 @@ func GetPositionFromPrefix(prefix []byte, dir string, ts *LevelDBTripleStore) in
|
|||
panic("Notreached")
|
||||
}
|
||||
|
||||
func (lit *LevelDBIterator) Check(v graph.TSVal) bool {
|
||||
func (lit *Iterator) Check(v graph.TSVal) bool {
|
||||
val := v.([]byte)
|
||||
if val[0] == 'z' {
|
||||
return false
|
||||
|
|
@ -186,23 +185,23 @@ func (lit *LevelDBIterator) Check(v graph.TSVal) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func (lit *LevelDBIterator) Size() (int64, bool) {
|
||||
func (lit *Iterator) Size() (int64, bool) {
|
||||
return lit.ts.GetSizeFor(lit.checkId), true
|
||||
}
|
||||
|
||||
func (lit *LevelDBIterator) DebugString(indent int) string {
|
||||
func (lit *Iterator) DebugString(indent int) string {
|
||||
size, _ := lit.Size()
|
||||
return fmt.Sprintf("%s(%s %d tags: %v dir: %s size:%d %s)", strings.Repeat(" ", indent), lit.Type(), lit.GetUid(), lit.Tags(), lit.dir, size, lit.ts.GetNameFor(lit.checkId))
|
||||
}
|
||||
|
||||
func (lit *LevelDBIterator) Type() string { return "leveldb" }
|
||||
func (lit *LevelDBIterator) Sorted() bool { return false }
|
||||
func (lit *Iterator) Type() string { return "leveldb" }
|
||||
func (lit *Iterator) Sorted() bool { return false }
|
||||
|
||||
func (lit *LevelDBIterator) Optimize() (graph.Iterator, bool) {
|
||||
func (lit *Iterator) Optimize() (graph.Iterator, bool) {
|
||||
return lit, false
|
||||
}
|
||||
|
||||
func (lit *LevelDBIterator) GetStats() *graph.IteratorStats {
|
||||
func (lit *Iterator) GetStats() *graph.IteratorStats {
|
||||
s, _ := lit.Size()
|
||||
return &graph.IteratorStats{
|
||||
CheckCost: 1,
|
||||
|
|
|
|||
|
|
@ -79,7 +79,7 @@ func TestCreateDatabase(t *testing.T) {
|
|||
ok := CreateNewLevelDB(tmpDir)
|
||||
So(ok, ShouldBeTrue)
|
||||
Convey("And has good defaults for a new database", func() {
|
||||
ts := NewDefaultLevelDBTripleStore(tmpDir, nil)
|
||||
ts := NewTripleStore(tmpDir, nil)
|
||||
So(ts, ShouldNotBeNil)
|
||||
So(ts.Size(), ShouldEqual, 0)
|
||||
ts.Close()
|
||||
|
|
@ -89,7 +89,7 @@ func TestCreateDatabase(t *testing.T) {
|
|||
Convey("Fails if it cannot create the database", func() {
|
||||
ok := CreateNewLevelDB("/dev/null/some terrible path")
|
||||
So(ok, ShouldBeFalse)
|
||||
So(func() { NewDefaultLevelDBTripleStore("/dev/null/some terrible path", nil) }, ShouldPanic)
|
||||
So(func() { NewTripleStore("/dev/null/some terrible path", nil) }, ShouldPanic)
|
||||
})
|
||||
|
||||
Reset(func() {
|
||||
|
|
@ -101,14 +101,14 @@ func TestCreateDatabase(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestLoadDatabase(t *testing.T) {
|
||||
var ts *LevelDBTripleStore
|
||||
var ts *TripleStore
|
||||
|
||||
Convey("Given a created database path", t, func() {
|
||||
tmpDir, _ := ioutil.TempDir(os.TempDir(), "cayley_test")
|
||||
t.Log(tmpDir)
|
||||
ok := CreateNewLevelDB(tmpDir)
|
||||
So(ok, ShouldBeTrue)
|
||||
ts = NewDefaultLevelDBTripleStore(tmpDir, nil)
|
||||
ts = NewTripleStore(tmpDir, nil)
|
||||
|
||||
Convey("Can load a single triple", func() {
|
||||
ts.AddTriple(graph.MakeTriple("Something", "points_to", "Something Else", "context"))
|
||||
|
|
@ -139,7 +139,7 @@ func TestLoadDatabase(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestIterator(t *testing.T) {
|
||||
var ts *LevelDBTripleStore
|
||||
var ts *TripleStore
|
||||
|
||||
Convey("Given a prepared database", t, func() {
|
||||
tmpDir, _ := ioutil.TempDir(os.TempDir(), "cayley_test")
|
||||
|
|
@ -147,7 +147,7 @@ func TestIterator(t *testing.T) {
|
|||
defer os.RemoveAll(tmpDir)
|
||||
ok := CreateNewLevelDB(tmpDir)
|
||||
So(ok, ShouldBeTrue)
|
||||
ts = NewDefaultLevelDBTripleStore(tmpDir, nil)
|
||||
ts = NewTripleStore(tmpDir, nil)
|
||||
ts.AddTripleSet(makeTripleSet())
|
||||
var it graph.Iterator
|
||||
|
||||
|
|
@ -234,7 +234,7 @@ func TestIterator(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestSetIterator(t *testing.T) {
|
||||
var ts *LevelDBTripleStore
|
||||
var ts *TripleStore
|
||||
var tmpDir string
|
||||
|
||||
Convey("Given a prepared database", t, func() {
|
||||
|
|
@ -243,7 +243,7 @@ func TestSetIterator(t *testing.T) {
|
|||
defer os.RemoveAll(tmpDir)
|
||||
ok := CreateNewLevelDB(tmpDir)
|
||||
So(ok, ShouldBeTrue)
|
||||
ts = NewDefaultLevelDBTripleStore(tmpDir, nil)
|
||||
ts = NewTripleStore(tmpDir, nil)
|
||||
ts.AddTripleSet(makeTripleSet())
|
||||
var it graph.Iterator
|
||||
|
||||
|
|
@ -383,7 +383,7 @@ func TestSetIterator(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestOptimize(t *testing.T) {
|
||||
var ts *LevelDBTripleStore
|
||||
var ts *TripleStore
|
||||
var lto graph.Iterator
|
||||
var tmpDir string
|
||||
|
||||
|
|
@ -393,7 +393,7 @@ func TestOptimize(t *testing.T) {
|
|||
defer os.RemoveAll(tmpDir)
|
||||
ok := CreateNewLevelDB(tmpDir)
|
||||
So(ok, ShouldBeTrue)
|
||||
ts = NewDefaultLevelDBTripleStore(tmpDir, nil)
|
||||
ts = NewTripleStore(tmpDir, nil)
|
||||
ts.AddTripleSet(makeTripleSet())
|
||||
|
||||
Convey("With an linksto-fixed pair", func() {
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ import (
|
|||
const DefaultCacheSize = 2
|
||||
const DefaultWriteBufferSize = 20
|
||||
|
||||
type LevelDBTripleStore struct {
|
||||
type TripleStore struct {
|
||||
dbOpts *leveldb_opt.Options
|
||||
db *leveldb.DB
|
||||
path string
|
||||
|
|
@ -53,7 +53,7 @@ func CreateNewLevelDB(path string) bool {
|
|||
return false
|
||||
}
|
||||
defer db.Close()
|
||||
ts := &LevelDBTripleStore{}
|
||||
ts := &TripleStore{}
|
||||
ts.db = db
|
||||
ts.writeopts = &leveldb_opt.WriteOptions{
|
||||
Sync: true,
|
||||
|
|
@ -62,8 +62,8 @@ func CreateNewLevelDB(path string) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
func NewDefaultLevelDBTripleStore(path string, options graph.OptionsDict) *LevelDBTripleStore {
|
||||
var ts LevelDBTripleStore
|
||||
func NewTripleStore(path string, options graph.OptionsDict) *TripleStore {
|
||||
var ts TripleStore
|
||||
ts.path = path
|
||||
cache_size := DefaultCacheSize
|
||||
if val, ok := options.GetIntKey("cache_size_mb"); ok {
|
||||
|
|
@ -94,7 +94,7 @@ func NewDefaultLevelDBTripleStore(path string, options graph.OptionsDict) *Level
|
|||
return &ts
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) GetStats() string {
|
||||
func (ts *TripleStore) GetStats() string {
|
||||
out := ""
|
||||
stats, err := ts.db.GetProperty("leveldb.stats")
|
||||
if err == nil {
|
||||
|
|
@ -104,11 +104,11 @@ func (ts *LevelDBTripleStore) GetStats() string {
|
|||
return out
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) Size() int64 {
|
||||
func (ts *TripleStore) Size() int64 {
|
||||
return ts.size
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) createKeyFor(dir1, dir2, dir3 string, triple *graph.Triple) []byte {
|
||||
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))...)
|
||||
|
|
@ -117,7 +117,7 @@ func (ts *LevelDBTripleStore) createKeyFor(dir1, dir2, dir3 string, triple *grap
|
|||
return key
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) createProvKeyFor(dir1, dir2, dir3 string, triple *graph.Triple) []byte {
|
||||
func (ts *TripleStore) createProvKeyFor(dir1, dir2, dir3 string, triple *graph.Triple) []byte {
|
||||
key := make([]byte, 0, 2+(ts.hasher.Size()*4))
|
||||
key = append(key, []byte("c"+dir1)...)
|
||||
key = append(key, ts.convertStringToByteHash(triple.Get("c"))...)
|
||||
|
|
@ -127,14 +127,14 @@ func (ts *LevelDBTripleStore) createProvKeyFor(dir1, dir2, dir3 string, triple *
|
|||
return key
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) createValueKeyFor(s string) []byte {
|
||||
func (ts *TripleStore) createValueKeyFor(s string) []byte {
|
||||
key := make([]byte, 0, 1+ts.hasher.Size())
|
||||
key = append(key, []byte("z")...)
|
||||
key = append(key, ts.convertStringToByteHash(s)...)
|
||||
return key
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) AddTriple(t *graph.Triple) {
|
||||
func (ts *TripleStore) AddTriple(t *graph.Triple) {
|
||||
batch := &leveldb.Batch{}
|
||||
ts.buildWrite(batch, t)
|
||||
err := ts.db.Write(batch, ts.writeopts)
|
||||
|
|
@ -145,7 +145,7 @@ func (ts *LevelDBTripleStore) AddTriple(t *graph.Triple) {
|
|||
ts.size++
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) RemoveTriple(t *graph.Triple) {
|
||||
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")
|
||||
|
|
@ -174,7 +174,7 @@ func (ts *LevelDBTripleStore) RemoveTriple(t *graph.Triple) {
|
|||
ts.size--
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) buildTripleWrite(batch *leveldb.Batch, t *graph.Triple) {
|
||||
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)
|
||||
|
|
@ -188,7 +188,7 @@ func (ts *LevelDBTripleStore) buildTripleWrite(batch *leveldb.Batch, t *graph.Tr
|
|||
}
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) buildWrite(batch *leveldb.Batch, t *graph.Triple) {
|
||||
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)
|
||||
|
|
@ -203,7 +203,7 @@ type ValueData struct {
|
|||
Size int64
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) UpdateValueKeyBy(name string, amount int, batch *leveldb.Batch) {
|
||||
func (ts *TripleStore) UpdateValueKeyBy(name string, amount int, batch *leveldb.Batch) {
|
||||
value := &ValueData{name, int64(amount)}
|
||||
key := ts.createValueKeyFor(name)
|
||||
b, err := ts.db.Get(key, ts.readopts)
|
||||
|
|
@ -249,7 +249,7 @@ func (ts *LevelDBTripleStore) UpdateValueKeyBy(name string, amount int, batch *l
|
|||
}
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) AddTripleSet(t_s []*graph.Triple) {
|
||||
func (ts *TripleStore) AddTripleSet(t_s []*graph.Triple) {
|
||||
batch := &leveldb.Batch{}
|
||||
newTs := len(t_s)
|
||||
resizeMap := make(map[string]int)
|
||||
|
|
@ -273,7 +273,7 @@ func (ts *LevelDBTripleStore) AddTripleSet(t_s []*graph.Triple) {
|
|||
ts.size += int64(newTs)
|
||||
}
|
||||
|
||||
func (ldbts *LevelDBTripleStore) Close() {
|
||||
func (ldbts *TripleStore) Close() {
|
||||
buf := new(bytes.Buffer)
|
||||
err := binary.Write(buf, binary.LittleEndian, ldbts.size)
|
||||
if err == nil {
|
||||
|
|
@ -288,7 +288,7 @@ func (ldbts *LevelDBTripleStore) Close() {
|
|||
ldbts.open = false
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) GetTriple(k graph.TSVal) *graph.Triple {
|
||||
func (ts *TripleStore) GetTriple(k graph.TSVal) *graph.Triple {
|
||||
var triple graph.Triple
|
||||
b, err := ts.db.Get(k.([]byte), ts.readopts)
|
||||
if err != nil && err != leveldb.ErrNotFound {
|
||||
|
|
@ -307,7 +307,7 @@ func (ts *LevelDBTripleStore) GetTriple(k graph.TSVal) *graph.Triple {
|
|||
return &triple
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) convertStringToByteHash(s string) []byte {
|
||||
func (ts *TripleStore) convertStringToByteHash(s string) []byte {
|
||||
ts.hasher.Reset()
|
||||
key := make([]byte, 0, ts.hasher.Size())
|
||||
ts.hasher.Write([]byte(s))
|
||||
|
|
@ -315,11 +315,11 @@ func (ts *LevelDBTripleStore) convertStringToByteHash(s string) []byte {
|
|||
return key
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) GetIdFor(s string) graph.TSVal {
|
||||
func (ts *TripleStore) GetIdFor(s string) graph.TSVal {
|
||||
return ts.createValueKeyFor(s)
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) getValueData(value_key []byte) ValueData {
|
||||
func (ts *TripleStore) getValueData(value_key []byte) ValueData {
|
||||
var out ValueData
|
||||
if glog.V(3) {
|
||||
glog.V(3).Infof("%s %v\n", string(value_key[0]), value_key)
|
||||
|
|
@ -339,7 +339,7 @@ func (ts *LevelDBTripleStore) getValueData(value_key []byte) ValueData {
|
|||
return out
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) GetNameFor(k graph.TSVal) string {
|
||||
func (ts *TripleStore) GetNameFor(k graph.TSVal) string {
|
||||
if k == nil {
|
||||
glog.V(2).Infoln("k was nil")
|
||||
return ""
|
||||
|
|
@ -347,14 +347,14 @@ func (ts *LevelDBTripleStore) GetNameFor(k graph.TSVal) string {
|
|||
return ts.getValueData(k.([]byte)).Name
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) GetSizeFor(k graph.TSVal) int64 {
|
||||
func (ts *TripleStore) GetSizeFor(k graph.TSVal) int64 {
|
||||
if k == nil {
|
||||
return 0
|
||||
}
|
||||
return int64(ts.getValueData(k.([]byte)).Size)
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) getSize() {
|
||||
func (ts *TripleStore) getSize() {
|
||||
var size int64
|
||||
b, err := ts.db.Get([]byte("__size"), ts.readopts)
|
||||
if err != nil && err != leveldb.ErrNotFound {
|
||||
|
|
@ -373,7 +373,7 @@ func (ts *LevelDBTripleStore) getSize() {
|
|||
ts.size = size
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) GetApproximateSizeForPrefix(pre []byte) (int64, error) {
|
||||
func (ts *TripleStore) GetApproximateSizeForPrefix(pre []byte) (int64, error) {
|
||||
limit := make([]byte, len(pre))
|
||||
copy(limit, pre)
|
||||
end := len(limit) - 1
|
||||
|
|
@ -388,29 +388,29 @@ func (ts *LevelDBTripleStore) GetApproximateSizeForPrefix(pre []byte) (int64, er
|
|||
return 0, nil
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) GetTripleIterator(dir string, val graph.TSVal) graph.Iterator {
|
||||
func (ts *TripleStore) GetTripleIterator(dir string, val graph.TSVal) graph.Iterator {
|
||||
switch dir {
|
||||
case "s":
|
||||
return NewLevelDBIterator("sp", "s", val, ts)
|
||||
return NewIterator("sp", "s", val, ts)
|
||||
case "p":
|
||||
return NewLevelDBIterator("po", "p", val, ts)
|
||||
return NewIterator("po", "p", val, ts)
|
||||
case "o":
|
||||
return NewLevelDBIterator("os", "o", val, ts)
|
||||
return NewIterator("os", "o", val, ts)
|
||||
case "c":
|
||||
return NewLevelDBIterator("cp", "c", val, ts)
|
||||
return NewIterator("cp", "c", val, ts)
|
||||
}
|
||||
panic("Notreached " + dir)
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) GetNodesAllIterator() graph.Iterator {
|
||||
func (ts *TripleStore) GetNodesAllIterator() graph.Iterator {
|
||||
return NewLevelDBAllIterator("z", "v", ts)
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) GetTriplesAllIterator() graph.Iterator {
|
||||
func (ts *TripleStore) GetTriplesAllIterator() graph.Iterator {
|
||||
return NewLevelDBAllIterator("po", "p", ts)
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) GetTripleDirection(val graph.TSVal, direction string) graph.TSVal {
|
||||
func (ts *TripleStore) GetTripleDirection(val graph.TSVal, direction string) graph.TSVal {
|
||||
v := val.([]uint8)
|
||||
offset := GetPositionFromPrefix(v[0:2], direction, ts)
|
||||
if offset != -1 {
|
||||
|
|
@ -424,6 +424,6 @@ func compareBytes(a, b graph.TSVal) bool {
|
|||
return bytes.Equal(a.([]uint8), b.([]uint8))
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) MakeFixed() *graph.FixedIterator {
|
||||
func (ts *TripleStore) MakeFixed() *graph.FixedIterator {
|
||||
return graph.NewFixedIteratorWithCompare(compareBytes)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ import (
|
|||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
func (ts *LevelDBTripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
|
||||
func (ts *TripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
|
||||
switch it.Type() {
|
||||
case "linksto":
|
||||
return ts.optimizeLinksTo(it.(*graph.LinksToIterator))
|
||||
|
|
@ -27,7 +27,7 @@ func (ts *LevelDBTripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterato
|
|||
return it, false
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) optimizeLinksTo(it *graph.LinksToIterator) (graph.Iterator, bool) {
|
||||
func (ts *TripleStore) optimizeLinksTo(it *graph.LinksToIterator) (graph.Iterator, bool) {
|
||||
l := it.GetSubIterators()
|
||||
if l.Len() != 1 {
|
||||
return it, false
|
||||
|
|
|
|||
|
|
@ -18,19 +18,19 @@ import (
|
|||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
type MemstoreAllIterator struct {
|
||||
type AllIterator struct {
|
||||
graph.Int64AllIterator
|
||||
ts *MemTripleStore
|
||||
ts *TripleStore
|
||||
}
|
||||
|
||||
func NewMemstoreAllIterator(ts *MemTripleStore) *MemstoreAllIterator {
|
||||
var out MemstoreAllIterator
|
||||
func NewMemstoreAllIterator(ts *TripleStore) *AllIterator {
|
||||
var out AllIterator
|
||||
out.Int64AllIterator = *graph.NewInt64AllIterator(1, ts.idCounter-1)
|
||||
out.ts = ts
|
||||
return &out
|
||||
}
|
||||
|
||||
func (memall *MemstoreAllIterator) Next() (graph.TSVal, bool) {
|
||||
func (memall *AllIterator) Next() (graph.TSVal, bool) {
|
||||
next, out := memall.Int64AllIterator.Next()
|
||||
if !out {
|
||||
return next, out
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ import (
|
|||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
type LlrbIterator struct {
|
||||
type Iterator struct {
|
||||
graph.BaseIterator
|
||||
tree *llrb.LLRB
|
||||
data string
|
||||
|
|
@ -51,8 +51,8 @@ func IterateOne(tree *llrb.LLRB, last Int64) Int64 {
|
|||
return next
|
||||
}
|
||||
|
||||
func NewLlrbIterator(tree *llrb.LLRB, data string) *LlrbIterator {
|
||||
var it LlrbIterator
|
||||
func NewLlrbIterator(tree *llrb.LLRB, data string) *Iterator {
|
||||
var it Iterator
|
||||
graph.BaseIteratorInit(&it.BaseIterator)
|
||||
it.tree = tree
|
||||
it.iterLast = Int64(-1)
|
||||
|
|
@ -60,19 +60,19 @@ func NewLlrbIterator(tree *llrb.LLRB, data string) *LlrbIterator {
|
|||
return &it
|
||||
}
|
||||
|
||||
func (it *LlrbIterator) Reset() {
|
||||
func (it *Iterator) Reset() {
|
||||
it.iterLast = Int64(-1)
|
||||
}
|
||||
|
||||
func (it *LlrbIterator) Clone() graph.Iterator {
|
||||
func (it *Iterator) Clone() graph.Iterator {
|
||||
var new_it = NewLlrbIterator(it.tree, it.data)
|
||||
new_it.CopyTagsFrom(it)
|
||||
return new_it
|
||||
}
|
||||
|
||||
func (it *LlrbIterator) Close() {}
|
||||
func (it *Iterator) Close() {}
|
||||
|
||||
func (it *LlrbIterator) Next() (graph.TSVal, bool) {
|
||||
func (it *Iterator) Next() (graph.TSVal, bool) {
|
||||
graph.NextLogIn(it)
|
||||
if it.tree.Max() == nil || it.Last == int64(it.tree.Max().(Int64)) {
|
||||
return graph.NextLogOut(it, nil, false)
|
||||
|
|
@ -82,11 +82,11 @@ func (it *LlrbIterator) Next() (graph.TSVal, bool) {
|
|||
return graph.NextLogOut(it, it.Last, true)
|
||||
}
|
||||
|
||||
func (it *LlrbIterator) Size() (int64, bool) {
|
||||
func (it *Iterator) Size() (int64, bool) {
|
||||
return int64(it.tree.Len()), true
|
||||
}
|
||||
|
||||
func (it *LlrbIterator) Check(v graph.TSVal) bool {
|
||||
func (it *Iterator) Check(v graph.TSVal) bool {
|
||||
graph.CheckLogIn(it, v)
|
||||
if it.tree.Has(Int64(v.(int64))) {
|
||||
it.Last = v
|
||||
|
|
@ -95,22 +95,22 @@ func (it *LlrbIterator) Check(v graph.TSVal) bool {
|
|||
return graph.CheckLogOut(it, v, false)
|
||||
}
|
||||
|
||||
func (it *LlrbIterator) DebugString(indent int) string {
|
||||
func (it *Iterator) DebugString(indent int) string {
|
||||
size, _ := it.Size()
|
||||
return fmt.Sprintf("%s(%s tags:%s size:%d %s)", strings.Repeat(" ", indent), it.Type(), it.Tags(), size, it.data)
|
||||
}
|
||||
|
||||
func (it *LlrbIterator) Type() string {
|
||||
func (it *Iterator) Type() string {
|
||||
return "llrb"
|
||||
}
|
||||
func (it *LlrbIterator) Sorted() bool {
|
||||
func (it *Iterator) Sorted() bool {
|
||||
return true
|
||||
}
|
||||
func (it *LlrbIterator) Optimize() (graph.Iterator, bool) {
|
||||
func (it *Iterator) Optimize() (graph.Iterator, bool) {
|
||||
return it, false
|
||||
}
|
||||
|
||||
func (it *LlrbIterator) GetStats() *graph.IteratorStats {
|
||||
func (it *Iterator) GetStats() *graph.IteratorStats {
|
||||
return &graph.IteratorStats{
|
||||
CheckCost: int64(math.Log(float64(it.tree.Len()))) + 1,
|
||||
NextCost: 1,
|
||||
|
|
|
|||
|
|
@ -28,8 +28,8 @@ import "github.com/google/cayley/graph"
|
|||
// +---+
|
||||
//
|
||||
|
||||
func MakeTestingMemstore() *MemTripleStore {
|
||||
ts := NewMemTripleStore()
|
||||
func MakeTestingMemstore() *TripleStore {
|
||||
ts := NewTripleStore()
|
||||
ts.AddTriple(graph.MakeTriple("A", "follows", "B", ""))
|
||||
ts.AddTriple(graph.MakeTriple("C", "follows", "B", ""))
|
||||
ts.AddTriple(graph.MakeTriple("C", "follows", "D", ""))
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ func (tdi *TripleDirectionIndex) Get(dir string, id int64) (*llrb.LLRB, bool) {
|
|||
return tree, exists
|
||||
}
|
||||
|
||||
type MemTripleStore struct {
|
||||
type TripleStore struct {
|
||||
idCounter int64
|
||||
tripleIdCounter int64
|
||||
idMap map[string]int64
|
||||
|
|
@ -77,8 +77,8 @@ type MemTripleStore struct {
|
|||
// vip_index map[string]map[int64]map[string]map[int64]*llrb.Tree
|
||||
}
|
||||
|
||||
func NewMemTripleStore() *MemTripleStore {
|
||||
var ts MemTripleStore
|
||||
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)
|
||||
|
|
@ -92,13 +92,13 @@ func NewMemTripleStore() *MemTripleStore {
|
|||
return &ts
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) AddTripleSet(triples []*graph.Triple) {
|
||||
func (ts *TripleStore) AddTripleSet(triples []*graph.Triple) {
|
||||
for _, t := range triples {
|
||||
ts.AddTriple(t)
|
||||
}
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) tripleExists(t *graph.Triple) (bool, int64) {
|
||||
func (ts *TripleStore) tripleExists(t *graph.Triple) (bool, int64) {
|
||||
smallest := -1
|
||||
var smallest_tree *llrb.LLRB
|
||||
for _, dir := range graph.TripleDirections {
|
||||
|
|
@ -135,7 +135,7 @@ func (ts *MemTripleStore) tripleExists(t *graph.Triple) (bool, int64) {
|
|||
return false, 0
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) AddTriple(t *graph.Triple) {
|
||||
func (ts *TripleStore) AddTriple(t *graph.Triple) {
|
||||
if exists, _ := ts.tripleExists(t); exists {
|
||||
return
|
||||
}
|
||||
|
|
@ -169,7 +169,7 @@ func (ts *MemTripleStore) AddTriple(t *graph.Triple) {
|
|||
// TODO(barakmich): Add VIP indexing
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) RemoveTriple(t *graph.Triple) {
|
||||
func (ts *TripleStore) RemoveTriple(t *graph.Triple) {
|
||||
var tripleID int64
|
||||
var exists bool
|
||||
tripleID = 0
|
||||
|
|
@ -215,11 +215,11 @@ func (ts *MemTripleStore) RemoveTriple(t *graph.Triple) {
|
|||
}
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) GetTriple(index graph.TSVal) *graph.Triple {
|
||||
func (ts *TripleStore) GetTriple(index graph.TSVal) *graph.Triple {
|
||||
return &ts.triples[index.(int64)]
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) GetTripleIterator(direction string, value graph.TSVal) graph.Iterator {
|
||||
func (ts *TripleStore) GetTripleIterator(direction string, value graph.TSVal) graph.Iterator {
|
||||
index, ok := ts.index.Get(direction, value.(int64))
|
||||
data := fmt.Sprintf("dir:%s val:%d", direction, value.(int64))
|
||||
if ok {
|
||||
|
|
@ -228,11 +228,11 @@ func (ts *MemTripleStore) GetTripleIterator(direction string, value graph.TSVal)
|
|||
return &graph.NullIterator{}
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) Size() int64 {
|
||||
func (ts *TripleStore) Size() int64 {
|
||||
return ts.size - 1 // Don't count the sentinel
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) DebugPrint() {
|
||||
func (ts *TripleStore) DebugPrint() {
|
||||
for i, t := range ts.triples {
|
||||
if i == 0 {
|
||||
continue
|
||||
|
|
@ -241,28 +241,28 @@ func (ts *MemTripleStore) DebugPrint() {
|
|||
}
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) GetIdFor(name string) graph.TSVal {
|
||||
func (ts *TripleStore) GetIdFor(name string) graph.TSVal {
|
||||
return ts.idMap[name]
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) GetNameFor(id graph.TSVal) string {
|
||||
func (ts *TripleStore) GetNameFor(id graph.TSVal) string {
|
||||
return ts.revIdMap[id.(int64)]
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) GetTriplesAllIterator() graph.Iterator {
|
||||
func (ts *TripleStore) GetTriplesAllIterator() graph.Iterator {
|
||||
return graph.NewInt64AllIterator(0, ts.Size())
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) MakeFixed() *graph.FixedIterator {
|
||||
func (ts *TripleStore) MakeFixed() *graph.FixedIterator {
|
||||
return graph.NewFixedIteratorWithCompare(graph.BasicEquality)
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) GetTripleDirection(val graph.TSVal, direction string) graph.TSVal {
|
||||
func (ts *TripleStore) GetTripleDirection(val graph.TSVal, direction string) graph.TSVal {
|
||||
name := ts.GetTriple(val).Get(direction)
|
||||
return ts.GetIdFor(name)
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) GetNodesAllIterator() graph.Iterator {
|
||||
func (ts *TripleStore) GetNodesAllIterator() graph.Iterator {
|
||||
return NewMemstoreAllIterator(ts)
|
||||
}
|
||||
func (ts *MemTripleStore) Close() {}
|
||||
func (ts *TripleStore) Close() {}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ import (
|
|||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
func (ts *MemTripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
|
||||
func (ts *TripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
|
||||
switch it.Type() {
|
||||
case "linksto":
|
||||
return ts.optimizeLinksTo(it.(*graph.LinksToIterator))
|
||||
|
|
@ -27,7 +27,7 @@ func (ts *MemTripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, b
|
|||
return it, false
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) optimizeLinksTo(it *graph.LinksToIterator) (graph.Iterator, bool) {
|
||||
func (ts *TripleStore) optimizeLinksTo(it *graph.LinksToIterator) (graph.Iterator, bool) {
|
||||
l := it.GetSubIterators()
|
||||
if l.Len() != 1 {
|
||||
return it, false
|
||||
|
|
|
|||
|
|
@ -107,7 +107,7 @@ func TestLinksToOptimization(t *testing.T) {
|
|||
if newIt.Type() != "llrb" {
|
||||
t.Fatal("Didn't swap out to LLRB")
|
||||
}
|
||||
v := newIt.(*LlrbIterator)
|
||||
v := newIt.(*Iterator)
|
||||
v_clone := v.Clone()
|
||||
if v_clone.DebugString(0) != v.DebugString(0) {
|
||||
t.Fatal("Wrong iterator. Got ", v_clone.DebugString(0))
|
||||
|
|
|
|||
|
|
@ -25,9 +25,9 @@ import (
|
|||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
type MongoIterator struct {
|
||||
type Iterator struct {
|
||||
graph.BaseIterator
|
||||
ts *MongoTripleStore
|
||||
ts *TripleStore
|
||||
dir string
|
||||
iter *mgo.Iter
|
||||
hash string
|
||||
|
|
@ -38,8 +38,8 @@ type MongoIterator struct {
|
|||
collection string
|
||||
}
|
||||
|
||||
func NewMongoIterator(ts *MongoTripleStore, collection string, dir string, val graph.TSVal) *MongoIterator {
|
||||
var m MongoIterator
|
||||
func NewMongoIterator(ts *TripleStore, collection string, dir string, val graph.TSVal) *Iterator {
|
||||
var m Iterator
|
||||
graph.BaseIteratorInit(&m.BaseIterator)
|
||||
|
||||
m.name = ts.GetNameFor(val)
|
||||
|
|
@ -70,8 +70,8 @@ func NewMongoIterator(ts *MongoTripleStore, collection string, dir string, val g
|
|||
return &m
|
||||
}
|
||||
|
||||
func NewMongoAllIterator(ts *MongoTripleStore, collection string) *MongoIterator {
|
||||
var m MongoIterator
|
||||
func NewMongoAllIterator(ts *TripleStore, collection string) *Iterator {
|
||||
var m Iterator
|
||||
m.ts = ts
|
||||
m.dir = "all"
|
||||
m.constraint = nil
|
||||
|
|
@ -88,17 +88,17 @@ func NewMongoAllIterator(ts *MongoTripleStore, collection string) *MongoIterator
|
|||
return &m
|
||||
}
|
||||
|
||||
func (m *MongoIterator) Reset() {
|
||||
func (m *Iterator) Reset() {
|
||||
m.iter.Close()
|
||||
m.iter = m.ts.db.C(m.collection).Find(m.constraint).Iter()
|
||||
|
||||
}
|
||||
|
||||
func (m *MongoIterator) Close() {
|
||||
func (m *Iterator) Close() {
|
||||
m.iter.Close()
|
||||
}
|
||||
|
||||
func (m *MongoIterator) Clone() graph.Iterator {
|
||||
func (m *Iterator) Clone() graph.Iterator {
|
||||
var newM graph.Iterator
|
||||
if m.isAll {
|
||||
newM = NewMongoAllIterator(m.ts, m.collection)
|
||||
|
|
@ -109,7 +109,7 @@ func (m *MongoIterator) Clone() graph.Iterator {
|
|||
return newM
|
||||
}
|
||||
|
||||
func (m *MongoIterator) Next() (graph.TSVal, bool) {
|
||||
func (m *Iterator) Next() (graph.TSVal, bool) {
|
||||
var result struct {
|
||||
Id string "_id"
|
||||
//Sub string "Sub"
|
||||
|
|
@ -120,7 +120,7 @@ func (m *MongoIterator) Next() (graph.TSVal, bool) {
|
|||
if !found {
|
||||
err := m.iter.Err()
|
||||
if err != nil {
|
||||
glog.Errorln("Error Nexting MongoIterator: ", err)
|
||||
glog.Errorln("Error Nexting Iterator: ", err)
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
|
@ -128,7 +128,7 @@ func (m *MongoIterator) Next() (graph.TSVal, bool) {
|
|||
return result.Id, true
|
||||
}
|
||||
|
||||
func (m *MongoIterator) Check(v graph.TSVal) bool {
|
||||
func (m *Iterator) Check(v graph.TSVal) bool {
|
||||
graph.CheckLogIn(m, v)
|
||||
if m.isAll {
|
||||
m.Last = v
|
||||
|
|
@ -153,25 +153,25 @@ func (m *MongoIterator) Check(v graph.TSVal) bool {
|
|||
return graph.CheckLogOut(m, v, false)
|
||||
}
|
||||
|
||||
func (m *MongoIterator) Size() (int64, bool) {
|
||||
func (m *Iterator) Size() (int64, bool) {
|
||||
return m.size, true
|
||||
}
|
||||
|
||||
func (m *MongoIterator) Type() string {
|
||||
func (m *Iterator) Type() string {
|
||||
if m.isAll {
|
||||
return "all"
|
||||
}
|
||||
return "mongo"
|
||||
}
|
||||
func (m *MongoIterator) Sorted() bool { return true }
|
||||
func (m *MongoIterator) Optimize() (graph.Iterator, bool) { return m, false }
|
||||
func (m *Iterator) Sorted() bool { return true }
|
||||
func (m *Iterator) Optimize() (graph.Iterator, bool) { return m, false }
|
||||
|
||||
func (m *MongoIterator) DebugString(indent int) string {
|
||||
func (m *Iterator) DebugString(indent int) string {
|
||||
size, _ := m.Size()
|
||||
return fmt.Sprintf("%s(%s size:%d %s %s)", strings.Repeat(" ", indent), m.Type(), size, m.hash, m.name)
|
||||
}
|
||||
|
||||
func (m *MongoIterator) GetStats() *graph.IteratorStats {
|
||||
func (m *Iterator) GetStats() *graph.IteratorStats {
|
||||
size, _ := m.Size()
|
||||
return &graph.IteratorStats{
|
||||
CheckCost: 1,
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ import (
|
|||
|
||||
const DefaultDBName = "cayley"
|
||||
|
||||
type MongoTripleStore struct {
|
||||
type TripleStore struct {
|
||||
session *mgo.Session
|
||||
db *mgo.Database
|
||||
hasher hash.Hash
|
||||
|
|
@ -65,8 +65,8 @@ func CreateNewMongoGraph(addr string, options graph.OptionsDict) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
func NewMongoTripleStore(addr string, options graph.OptionsDict) *MongoTripleStore {
|
||||
var ts MongoTripleStore
|
||||
func NewTripleStore(addr string, options graph.OptionsDict) *TripleStore {
|
||||
var ts TripleStore
|
||||
conn, err := mgo.Dial(addr)
|
||||
if err != nil {
|
||||
glog.Fatal("Error connecting: ", err)
|
||||
|
|
@ -83,7 +83,7 @@ func NewMongoTripleStore(addr string, options graph.OptionsDict) *MongoTripleSto
|
|||
return &ts
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) getIdForTriple(t *graph.Triple) string {
|
||||
func (ts *TripleStore) getIdForTriple(t *graph.Triple) string {
|
||||
id := ts.ConvertStringToByteHash(t.Sub)
|
||||
id += ts.ConvertStringToByteHash(t.Pred)
|
||||
id += ts.ConvertStringToByteHash(t.Obj)
|
||||
|
|
@ -91,7 +91,7 @@ func (ts *MongoTripleStore) getIdForTriple(t *graph.Triple) string {
|
|||
return id
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) ConvertStringToByteHash(s string) string {
|
||||
func (ts *TripleStore) ConvertStringToByteHash(s string) string {
|
||||
ts.hasher.Reset()
|
||||
key := make([]byte, 0, ts.hasher.Size())
|
||||
ts.hasher.Write([]byte(s))
|
||||
|
|
@ -105,7 +105,7 @@ type MongoNode struct {
|
|||
Size int "Size"
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) updateNodeBy(node_name string, inc int) {
|
||||
func (ts *TripleStore) updateNodeBy(node_name string, inc int) {
|
||||
var size MongoNode
|
||||
node := ts.GetIdFor(node_name)
|
||||
err := ts.db.C("nodes").FindId(node).One(&size)
|
||||
|
|
@ -142,7 +142,7 @@ func (ts *MongoTripleStore) updateNodeBy(node_name string, inc int) {
|
|||
}
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) writeTriple(t *graph.Triple) bool {
|
||||
func (ts *TripleStore) writeTriple(t *graph.Triple) bool {
|
||||
tripledoc := bson.M{"_id": ts.getIdForTriple(t), "Sub": t.Sub, "Pred": t.Pred, "Obj": t.Obj, "Provenance": t.Provenance}
|
||||
err := ts.db.C("triples").Insert(tripledoc)
|
||||
if err != nil {
|
||||
|
|
@ -156,7 +156,7 @@ func (ts *MongoTripleStore) writeTriple(t *graph.Triple) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) AddTriple(t *graph.Triple) {
|
||||
func (ts *TripleStore) AddTriple(t *graph.Triple) {
|
||||
_ = ts.writeTriple(t)
|
||||
ts.updateNodeBy(t.Sub, 1)
|
||||
ts.updateNodeBy(t.Pred, 1)
|
||||
|
|
@ -166,7 +166,7 @@ func (ts *MongoTripleStore) AddTriple(t *graph.Triple) {
|
|||
}
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) AddTripleSet(in []*graph.Triple) {
|
||||
func (ts *TripleStore) AddTripleSet(in []*graph.Triple) {
|
||||
ts.session.SetSafe(nil)
|
||||
idMap := make(map[string]int)
|
||||
for _, t := range in {
|
||||
|
|
@ -186,7 +186,7 @@ func (ts *MongoTripleStore) AddTripleSet(in []*graph.Triple) {
|
|||
ts.session.SetSafe(&mgo.Safe{})
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) RemoveTriple(t *graph.Triple) {
|
||||
func (ts *TripleStore) RemoveTriple(t *graph.Triple) {
|
||||
err := ts.db.C("triples").RemoveId(ts.getIdForTriple(t))
|
||||
if err == mgo.ErrNotFound {
|
||||
return
|
||||
|
|
@ -202,7 +202,7 @@ func (ts *MongoTripleStore) RemoveTriple(t *graph.Triple) {
|
|||
}
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) GetTriple(val graph.TSVal) *graph.Triple {
|
||||
func (ts *TripleStore) GetTriple(val graph.TSVal) *graph.Triple {
|
||||
var bsonDoc bson.M
|
||||
err := ts.db.C("triples").FindId(val.(string)).One(&bsonDoc)
|
||||
if err != nil {
|
||||
|
|
@ -215,23 +215,23 @@ func (ts *MongoTripleStore) GetTriple(val graph.TSVal) *graph.Triple {
|
|||
bsonDoc["Provenance"].(string))
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) GetTripleIterator(dir string, val graph.TSVal) graph.Iterator {
|
||||
func (ts *TripleStore) GetTripleIterator(dir string, val graph.TSVal) graph.Iterator {
|
||||
return NewMongoIterator(ts, "triples", dir, val)
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) GetNodesAllIterator() graph.Iterator {
|
||||
func (ts *TripleStore) GetNodesAllIterator() graph.Iterator {
|
||||
return NewMongoAllIterator(ts, "nodes")
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) GetTriplesAllIterator() graph.Iterator {
|
||||
func (ts *TripleStore) GetTriplesAllIterator() graph.Iterator {
|
||||
return NewMongoAllIterator(ts, "triples")
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) GetIdFor(s string) graph.TSVal {
|
||||
func (ts *TripleStore) GetIdFor(s string) graph.TSVal {
|
||||
return ts.ConvertStringToByteHash(s)
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) GetNameFor(v graph.TSVal) string {
|
||||
func (ts *TripleStore) GetNameFor(v graph.TSVal) string {
|
||||
val, ok := ts.idCache.Get(v.(string))
|
||||
if ok {
|
||||
return val
|
||||
|
|
@ -245,7 +245,7 @@ func (ts *MongoTripleStore) GetNameFor(v graph.TSVal) string {
|
|||
return node.Name
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) Size() int64 {
|
||||
func (ts *TripleStore) Size() int64 {
|
||||
count, err := ts.db.C("triples").Count()
|
||||
if err != nil {
|
||||
glog.Error("Error: ", err)
|
||||
|
|
@ -258,15 +258,15 @@ func compareStrings(a, b graph.TSVal) bool {
|
|||
return a.(string) == b.(string)
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) MakeFixed() *graph.FixedIterator {
|
||||
func (ts *TripleStore) MakeFixed() *graph.FixedIterator {
|
||||
return graph.NewFixedIteratorWithCompare(compareStrings)
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) Close() {
|
||||
func (ts *TripleStore) Close() {
|
||||
ts.db.Session.Close()
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) GetTripleDirection(in graph.TSVal, dir string) graph.TSVal {
|
||||
func (ts *TripleStore) GetTripleDirection(in graph.TSVal, dir string) graph.TSVal {
|
||||
// Maybe do the trick here
|
||||
var offset int
|
||||
switch dir {
|
||||
|
|
@ -283,7 +283,7 @@ func (ts *MongoTripleStore) GetTripleDirection(in graph.TSVal, dir string) graph
|
|||
return val
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) BulkLoad(t_chan chan *graph.Triple) {
|
||||
func (ts *TripleStore) BulkLoad(t_chan chan *graph.Triple) {
|
||||
ts.session.SetSafe(nil)
|
||||
for triple := range t_chan {
|
||||
ts.writeTriple(triple)
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ import (
|
|||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
func (ts *MongoTripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
|
||||
func (ts *TripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
|
||||
switch it.Type() {
|
||||
case "linksto":
|
||||
return ts.optimizeLinksTo(it.(*graph.LinksToIterator))
|
||||
|
|
@ -27,7 +27,7 @@ func (ts *MongoTripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator,
|
|||
return it, false
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) optimizeLinksTo(it *graph.LinksToIterator) (graph.Iterator, bool) {
|
||||
func (ts *TripleStore) optimizeLinksTo(it *graph.LinksToIterator) (graph.Iterator, bool) {
|
||||
l := it.GetSubIterators()
|
||||
if l.Len() != 1 {
|
||||
return it, false
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ func TestBadParse(t *testing.T) {
|
|||
|
||||
func TestParseSexpWithMemstore(t *testing.T) {
|
||||
Convey("With a Memstore", t, func() {
|
||||
ts := memstore.NewMemTripleStore()
|
||||
ts := memstore.NewTripleStore()
|
||||
|
||||
Convey("It should parse an empty query", func() {
|
||||
it := BuildIteratorTreeForQuery(ts, "()")
|
||||
|
|
@ -64,7 +64,7 @@ func TestParseSexpWithMemstore(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestTreeConstraintParse(t *testing.T) {
|
||||
ts := memstore.NewMemTripleStore()
|
||||
ts := memstore.NewTripleStore()
|
||||
ts.AddTriple(graph.MakeTriple("i", "like", "food", ""))
|
||||
ts.AddTriple(graph.MakeTriple("food", "is", "good", ""))
|
||||
query := "(\"i\"\n" +
|
||||
|
|
@ -84,7 +84,7 @@ func TestTreeConstraintParse(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestTreeConstraintTagParse(t *testing.T) {
|
||||
ts := memstore.NewMemTripleStore()
|
||||
ts := memstore.NewTripleStore()
|
||||
ts.AddTriple(graph.MakeTriple("i", "like", "food", ""))
|
||||
ts.AddTriple(graph.MakeTriple("food", "is", "good", ""))
|
||||
query := "(\"i\"\n" +
|
||||
|
|
@ -104,7 +104,7 @@ func TestTreeConstraintTagParse(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestMultipleConstraintParse(t *testing.T) {
|
||||
ts := memstore.NewMemTripleStore()
|
||||
ts := memstore.NewTripleStore()
|
||||
ts.AddTriple(graph.MakeTriple("i", "like", "food", ""))
|
||||
ts.AddTriple(graph.MakeTriple("i", "like", "beer", ""))
|
||||
ts.AddTriple(graph.MakeTriple("you", "like", "beer", ""))
|
||||
|
|
|
|||
|
|
@ -24,22 +24,22 @@ import (
|
|||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
type SexpSession struct {
|
||||
type Session struct {
|
||||
ts graph.TripleStore
|
||||
debug bool
|
||||
}
|
||||
|
||||
func NewSexpSession(inputTripleStore graph.TripleStore) *SexpSession {
|
||||
var s SexpSession
|
||||
func NewSession(inputTripleStore graph.TripleStore) *Session {
|
||||
var s Session
|
||||
s.ts = inputTripleStore
|
||||
return &s
|
||||
}
|
||||
|
||||
func (s *SexpSession) ToggleDebug() {
|
||||
func (s *Session) ToggleDebug() {
|
||||
s.debug = !s.debug
|
||||
}
|
||||
|
||||
func (s *SexpSession) InputParses(input string) (graph.ParseResult, error) {
|
||||
func (s *Session) InputParses(input string) (graph.ParseResult, error) {
|
||||
var parenDepth int
|
||||
for i, x := range input {
|
||||
if x == '(' {
|
||||
|
|
@ -65,7 +65,7 @@ func (s *SexpSession) InputParses(input string) (graph.ParseResult, error) {
|
|||
return graph.ParseFail, errors.New("Invalid Syntax")
|
||||
}
|
||||
|
||||
func (s *SexpSession) ExecInput(input string, out chan interface{}, limit int) {
|
||||
func (s *Session) ExecInput(input string, out chan interface{}, limit int) {
|
||||
it := BuildIteratorTreeForQuery(s.ts, input)
|
||||
newIt, changed := it.Optimize()
|
||||
if changed {
|
||||
|
|
@ -101,7 +101,7 @@ func (s *SexpSession) ExecInput(input string, out chan interface{}, limit int) {
|
|||
close(out)
|
||||
}
|
||||
|
||||
func (s *SexpSession) ToText(result interface{}) string {
|
||||
func (s *Session) ToText(result interface{}) string {
|
||||
out := fmt.Sprintln("****")
|
||||
tags := result.(*map[string]graph.TSVal)
|
||||
tagKeys := make([]string, len(*tags))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue