Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Write link indexes

  • Loading branch information...
commit 239fcf51b61b8efe59d88259440dec7dc33f692f 1 parent e57c26a
@brendonh authored
View
1  src/loge/database.go
@@ -111,7 +111,6 @@ func (db *LogeDB) EnsureObj(objRef ObjRef, load bool) *LogeObject {
var key = objRef.Key
var objKey = objRef.String()
-
var typ = db.types[typeName]
db.lock.SpinLock()
View
35 src/loge/leveldb.go
@@ -15,6 +15,7 @@ const VERSION = 1
const LINK_TAG uint16 = 2
const LINK_INFO_TAG uint16 = 3
+const INDEX_TAG uint16 = 4
const START_TAG uint16 = 8
type LevelDBStore struct {
@@ -38,6 +39,7 @@ type LevelDBWriteBatch struct {
type LevelDBWriteEntry struct {
Key []byte
Val []byte
+ Delete bool
}
var writeOptions = levigo.NewWriteOptions()
@@ -199,7 +201,11 @@ func (store *LevelDBStore) Get(typ *LogeType, key LogeKey) interface{} {
func (store *LevelDBStore) GetLinks(typ *LogeType, linkName string, objKey LogeKey) Links {
var vt = store.types.Type(typ.Name)
- var linkInfo = typ.Links[linkName]
+ var linkInfo, ok = typ.Links[linkName]
+ if !ok {
+ panic(fmt.Sprintf("Link info missing for %s", linkName))
+ }
+
var key = encodeTaggedKey([]uint16{LINK_TAG, vt.Tag, linkInfo.Tag}, string(objKey))
val, err := store.db.Get(readOptions, key)
@@ -239,7 +245,6 @@ func (store *LevelDBStore) Writer() {
}
batch.result<- batch.Write()
}
- fmt.Printf("Writer closing\n")
store.flushed = true
}
@@ -272,11 +277,29 @@ func (batch *LevelDBWriteBatch) StoreLinks(linkObj *LogeObject) error {
batch.Append(key, val)
+ var prefix = append(
+ encodeTaggedKey([]uint16{INDEX_TAG, vt.Tag, linkInfo.Tag}, string(linkObj.Key)),
+ 0)
+
+ for _, target := range set.Added {
+ var key = append(prefix, []byte(target)...)
+ batch.Append(key, []byte{})
+ }
+
+ for _, target := range set.Removed {
+ var key = append(prefix, []byte(target)...)
+ batch.Delete(key)
+ }
+
return nil
}
func (batch *LevelDBWriteBatch) Append(key []byte, val []byte) {
- batch.batch = append(batch.batch, LevelDBWriteEntry{ key, val })
+ batch.batch = append(batch.batch, LevelDBWriteEntry{ key, val, false })
+}
+
+func (batch *LevelDBWriteBatch) Delete(key []byte) {
+ batch.batch = append(batch.batch, LevelDBWriteEntry{ key, nil, true })
}
func (batch *LevelDBWriteBatch) Commit() error {
@@ -288,7 +311,11 @@ func (batch *LevelDBWriteBatch) Write() error {
var wb = levigo.NewWriteBatch()
defer wb.Close()
for _, entry := range batch.batch {
- wb.Put(entry.Key, entry.Val)
+ if entry.Delete {
+ wb.Delete(entry.Key)
+ } else {
+ wb.Put(entry.Key, entry.Val)
+ }
}
return batch.store.db.Write(writeOptions, wb)
View
10 src/loge/links.go
@@ -39,7 +39,7 @@ func (links Links) Remove(key string) Links {
type LinkSet struct {
- Original Links
+ Original Links `loge:"keep"`
Added Links
Removed Links
}
@@ -67,7 +67,8 @@ func (ls *LinkSet) Freeze() {
func (ls *LinkSet) Set(keys []string) {
- sort.Strings(keys)
+ // XXX BGH TODO: Delta this
+ sort.Strings(keys)
ls.Removed = ls.Original
ls.Added = keys
}
@@ -81,6 +82,11 @@ func (ls *LinkSet) Add(key string) {
}
func (ls *LinkSet) Remove(key string) {
+ // XXX BGH Hrgh
+ if (!ls.Original.Has(key) && !ls.Added.Has(key)) || ls.Removed.Has(key) {
+ return
+ }
+
ls.Added = ls.Added.Remove(key)
ls.Removed = ls.Removed.Add(key)
}
View
5 src/loge/objects.go
@@ -43,10 +43,13 @@ func InitializeObject(db *LogeDB, t *LogeType, key LogeKey) *LogeObject {
func (obj *LogeObject) NewVersion() *LogeObjectVersion {
var current = obj.Current
+
+ var newObj = obj.Type.Copy(current.Object)
+
return &LogeObjectVersion{
LogeObj: obj,
Version: current.Version + 1,
- Object: obj.Type.Copy(current.Object),
+ Object: newObj,
Dirty: true,
}
}
View
14 src/loge/transactions.go
@@ -78,6 +78,20 @@ func (t *Transaction) AddLink(typeName string, linkName string, key LogeKey, tar
t.getLink(MakeLinkRef(typeName, linkName, key), true, true).Add(string(target))
}
+func (t *Transaction) RemoveLink(typeName string, linkName string, key LogeKey, target LogeKey) {
+ t.getLink(MakeLinkRef(typeName, linkName, key), true, true).Remove(string(target))
+}
+
+func (t *Transaction) SetLinks(typeName string, linkName string, key LogeKey, targets []LogeKey) {
+ // XXX BGH: Yargh
+ var stringTargets = make([]string, 0, len(targets))
+ for _, key := range targets {
+ stringTargets = append(stringTargets, string(key))
+ }
+ t.getLink(MakeLinkRef(typeName, linkName, key), true, true).Set(stringTargets)
+}
+
+
func (t *Transaction) getLink(objRef ObjRef, forWrite bool, load bool) *LinkSet {
var version = t.getObj(objRef, forWrite, load)
return version.Object.(*LinkSet)
View
11 src/logetest/bench.go
@@ -5,24 +5,13 @@ import (
"fmt"
"time"
"runtime"
- _ "runtime/pprof"
- _ "os"
)
const TOTAL = 1000000
const BATCH_SIZE = 10000
func WriteBench() {
- // f, err := os.Create("bench.prof")
- // if err != nil {
- // fmt.Printf("Oh no: %v\n", err)
- // return
- // }
- // pprof.StartCPUProfile(f)
- // defer pprof.StopCPUProfile()
-
var db = loge.NewLogeDB(loge.NewLevelDBStore("data/bench"))
- //var db = loge.NewLogeDB(loge.NewMemStore())
defer db.Close()
View
51 src/logetest/links.go
@@ -0,0 +1,51 @@
+package main
+
+import (
+ "loge"
+ "fmt"
+)
+
+func LinkSandbox() {
+ var db = loge.NewLogeDB(loge.NewLevelDBStore("data/links"))
+
+ defer db.Close()
+
+ db.CreateType("person", 1, &Person{}, nil)
+
+ db.CreateType("pet", 1, &Pet{}, loge.LinkSpec{
+ "owner": "person",
+ })
+
+ db.Transact(func (t *loge.Transaction) {
+ t.SetObj("person", "Brendon", &Person{ "Brendon", 31, []uint16{} })
+ t.SetObj("person", "Mike", &Person{ "Mike", 38, []uint16{} })
+ t.SetObj("pet", "Ted", &Pet { "Ted", "dog" })
+ t.SetObj("pet", "Bones", &Pet { "Bones", "dog" })
+ t.SetObj("pet", "BaoBao", &Pet { "BaoBao", "dog" })
+ t.SetObj("pet", "Ruby", &Pet { "Ruby", "dog" })
+ t.SetObj("pet", "HenYou", &Pet { "HenYou", "dog" })
+ t.SetObj("pet", "Garcon", &Pet { "Garcon", "dog" })
+ t.SetObj("pet", "Flower", &Pet { "Flower", "cat" })
+
+ t.SetLinks("pet", "owner", "Ted", []loge.LogeKey{"Brendon"})
+ t.SetLinks("pet", "owner", "Bones", []loge.LogeKey{"Brendon"})
+ t.SetLinks("pet", "owner", "BaoBao", []loge.LogeKey{"Brendon"})
+ t.SetLinks("pet", "owner", "Ruby", []loge.LogeKey{"Brendon"})
+ t.SetLinks("pet", "owner", "HenYou", []loge.LogeKey{"Mike"})
+ t.SetLinks("pet", "owner", "Garcon", []loge.LogeKey{"Mike"})
+ t.SetLinks("pet", "owner", "Flower", []loge.LogeKey{"Mike"})
+ }, 0)
+
+ db.Transact(func (t *loge.Transaction) {
+ t.RemoveLink("pet", "owner", "Ruby", "Brendon")
+ t.AddLink("pet", "owner", "Ruby", "Mike")
+ }, 0)
+
+ db.Transact(func (t *loge.Transaction) {
+ fmt.Printf("Ruby links: %v\n", t.ReadLinks("pet", "owner", "Ruby"))
+ }, 0)
+
+
+ fmt.Printf("Done\n")
+}
+
View
3  src/logetest/main.go
@@ -12,7 +12,8 @@ type Pet struct {
}
func main() {
- WriteBench()
+ LinkSandbox()
+ //WriteBench()
//Sandbox()
//Example()
}
Please sign in to comment.
Something went wrong with that request. Please try again.