Permalink
Browse files

Dirty read operations

  • Loading branch information...
brendonh committed Feb 9, 2013
1 parent 08ca486 commit 35c230403c9aeaad94630b82eb663411d29851c6
Showing with 92 additions and 3 deletions.
  1. +21 −0 src/loge/database.go
  2. +59 −0 src/loge/dirty_test.go
  3. +12 −2 src/loge/storage.go
  4. +0 −1 src/loge/transactions.go
View
@@ -129,6 +129,27 @@ func (db *LogeDB) FlushCache() int {
return count
}
+// -----------------------------------------------
+// Dirty Operations
+// -----------------------------------------------
+
+func (db *LogeDB) DirtyExists(typeName string, key LogeKey) bool {
+ var obj = db.store.get(db.types[typeName], key)
+ return obj != nil
+}
+
+func (db *LogeDB) DirtyRead(typeName string, key LogeKey) interface{} {
+ var typ = db.types[typeName]
+ var obj = db.store.get(typ, key)
+ if obj == nil {
+ return typ.NilValue()
+ }
+ return obj
+}
+
+func (db *LogeDB) DirtyReadLinks(typeName string, linkName string, key LogeKey) []string {
+ return db.store.getLinks(db.types[typeName], linkName, key)
+}
// -----------------------------------------------
// Internals
View
@@ -0,0 +1,59 @@
+package loge
+
+import (
+ "testing"
+ "reflect"
+)
+
+func TestDirtyOps(test *testing.T) {
+ var db = NewLogeDB(NewMemStore())
+ db.CreateType("test", 1, &TestObj{}, LinkSpec{ "other": "test" })
+
+ db.Transact(func (t *Transaction) {
+ t.Set("test", "foo", &TestObj{ "foo" })
+ t.Set("test", "bar", &TestObj{ "bar" })
+
+ t.AddLink("test", "other", "foo", "bar")
+ t.AddLink("test", "other", "bar", "foo")
+ }, 0)
+
+ if !db.DirtyExists("test", "foo") {
+ test.Error("Dirty foo doesn't exist")
+ }
+
+ if !db.DirtyExists("test", "bar") {
+ test.Error("Dirty bar doesn't exist")
+ }
+
+ if db.DirtyExists("test", "wib") {
+ test.Error("Dirty wib exists")
+ }
+
+ if db.DirtyRead("test", "foo").(*TestObj).Name != "foo" {
+ test.Error("Dirty read has wrong name")
+ }
+
+ if db.DirtyRead("test", "bar").(*TestObj).Name != "bar" {
+ test.Error("Dirty read has wrong name")
+ }
+
+ var wib = db.DirtyRead("test", "wib").(*TestObj)
+ if wib != nil {
+ test.Errorf("Missing obj is not nil (%v)", wib)
+ }
+
+ var fooLinks = db.DirtyReadLinks("test", "other", "foo")
+ if !reflect.DeepEqual(fooLinks, []string{ "bar" }) {
+ test.Errorf("Wrong dirty links: %v", fooLinks)
+ }
+
+ var barLinks = db.DirtyReadLinks("test", "other", "bar")
+ if !reflect.DeepEqual(barLinks, []string{ "foo" }) {
+ test.Errorf("Wrong dirty links: %v", barLinks)
+ }
+
+ var wibLinks = db.DirtyReadLinks("test", "other", "wib")
+ if !reflect.DeepEqual(wibLinks, []string{}) {
+ test.Errorf("Wrong dirty links: %v", wibLinks)
+ }
+}
View
@@ -90,7 +90,11 @@ func (store *memStore) find(typ *logeType, linkName string, key LogeKey) ResultS
func (store *memStore) store(obj *logeObject) error {
obj.Lock.SpinLock()
defer obj.Lock.Unlock()
- store.objects[obj.Type.Name][obj.Key] = obj.Current.Object
+ if !obj.Current.hasValue() {
+ delete(store.objects[obj.Type.Name], obj.Key)
+ } else {
+ store.objects[obj.Type.Name][obj.Key] = obj.Current.Object
+ }
return nil
}
@@ -119,12 +123,18 @@ func (context *memContext) getLinks(t *logeType, linkName string, key LogeKey) [
}
func (context *memContext) store(obj *logeObject) error {
+ var val interface{}
+ if !obj.Current.hasValue() {
+ val = nil
+ } else {
+ val = obj.Current.Object
+ }
context.writes = append(
context.writes,
memWriteEntry{
TypeKey: obj.Type.Name,
ObjKey: obj.Key,
- Value: obj.Current.Object,
+ Value: val,
})
return nil
}
View
@@ -165,7 +165,6 @@ func (t *Transaction) Commit() bool {
}
func (t *Transaction) tryCommit() bool {
- //fmt.Printf("-------------\n")
for _, version := range t.versions {
var obj = version.LogeObj

0 comments on commit 35c2304

Please sign in to comment.