Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Transaction flag to return JSON objects. Simple go-service

  • Loading branch information...
commit b5a75a524a24ce792bb501c9e37c3191b8a5a426 1 parent 789b483
@brendonh authored
View
14 src/loge/database.go
@@ -67,9 +67,19 @@ func (db *LogeDB) newSnapshotID() uint64 {
}
func (db *LogeDB) Transact(actor Transactor, timeout time.Duration) bool {
+ var t = db.CreateTransaction()
+ return db.doTransact(t, actor, timeout)
+}
+
+func (db *LogeDB) TransactJSON(actor Transactor, timeout time.Duration) bool {
+ var t = db.CreateTransaction()
+ t.giveJSON = true
+ return db.doTransact(t, actor, timeout)
+}
+
+func (db *LogeDB) doTransact(t *Transaction, actor Transactor, timeout time.Duration) bool {
var start = time.Now()
for {
- var t = db.CreateTransaction()
actor(t)
if t.cancelled {
return false
@@ -131,7 +141,7 @@ func (db *LogeDB) Find(typeName string, linkName string, target LogeKey) (result
return
}
-func (db *LogeDB) FindFrom(typeName string, linkName string, target LogeKey, from LogeKey, limit int) (results []LogeKey) {
+func (db *LogeDB) FindSlice(typeName string, linkName string, target LogeKey, from LogeKey, limit int) (results []LogeKey) {
db.Transact(func (t *Transaction) {
results = t.FindSlice(typeName, linkName, target, from, limit).All()
}, 0)
View
2  src/loge/leveldb.go
@@ -326,7 +326,7 @@ func (store *levelDBStore) loadTypeMetadata() {
defer it.Close()
for it = it; it.Valid(); it.Next() {
- var typeInfo, _, err = typeType.DecodeObj(it.Value())
+ var typeInfo, _, err = typeType.DecodeObj(it.Value(), false)
if err != nil {
panic(fmt.Sprintf("Error loading type info: %v", err))
View
8 src/loge/objects.go
@@ -102,9 +102,9 @@ func (obj *logeObject) applyVersion(object interface{}, context transactionConte
}
}
-func (obj *logeObject) decode(blob []byte) (object interface{}, upgraded bool) {
+func (obj *logeObject) decode(blob []byte, toJSON bool) (object interface{}, upgraded bool) {
if obj.LinkName == "" {
- object, upgraded = obj.Type.Decode(blob)
+ object, upgraded = obj.Type.Decode(blob, toJSON)
} else {
var links linkList
spack.DecodeFromBytes(&links, obj.DB.linkTypeSpec, blob)
@@ -136,6 +136,6 @@ func (obj *logeObject) hasValue(object interface{}) bool {
}
-func (version *objectVersion) getObject() (interface{}, bool) {
- return version.LogeObj.decode(version.Blob)
+func (version *objectVersion) getObject(toJSON bool) (interface{}, bool) {
+ return version.LogeObj.decode(version.Blob, toJSON)
}
View
102 src/loge/service.go
@@ -0,0 +1,102 @@
+package loge
+
+import (
+ "fmt"
+
+ . "github.com/brendonh/go-service"
+)
+
+type LogeServiceContext struct {
+ Server
+ DB *LogeDB
+}
+
+func GetService() *Service {
+ service := NewService("loge")
+ service.AddMethod(
+ "info",
+ []APIArg {},
+ method_info)
+ service.AddMethod(
+ "find",
+ []APIArg {
+ APIArg{Name: "type", ArgType: StringArg},
+ APIArg{Name: "linkName", ArgType: StringArg},
+ APIArg{Name: "target", ArgType: StringArg},
+ APIArg{Name: "from", ArgType: StringArg, Default: ""},
+ APIArg{Name: "limit", ArgType: UIntArg, Default: -1},
+ },
+ method_find)
+ service.AddMethod(
+ "get",
+ []APIArg {
+ APIArg{Name: "type", ArgType: StringArg},
+ APIArg{Name: "key", ArgType: StringArg},
+ },
+ method_get)
+
+ return service
+}
+
+func method_info(args APIData, session Session, context ServerContext) (bool, APIData) {
+ var db = context.(*LogeServiceContext).DB
+
+ var dbInfo string
+ switch db.store.(type) {
+ case *memStore:
+ dbInfo = "Memory"
+ case *levelDBStore:
+ dbInfo = fmt.Sprintf("LevelDB: %s", db.store.(*levelDBStore).basePath)
+ }
+
+ var types []string
+ for typeName := range db.types {
+ types = append(types, typeName)
+ }
+
+ var response = make(APIData)
+ response["DB"] = dbInfo
+ response["Types"] = types
+ return true, response
+}
+
+func method_find(args APIData, session Session, context ServerContext) (bool, APIData) {
+ var db = context.(*LogeServiceContext).DB
+
+ var response = make(APIData)
+ response["keys"] = db.FindSlice(
+ args["type"].(string),
+ args["linkName"].(string),
+ LogeKey(args["target"].(string)),
+ LogeKey(args["from"].(string)),
+ args["limit"].(int))
+ return true, response
+}
+
+func method_get(args APIData, session Session, context ServerContext) (bool, APIData) {
+ var db = context.(*LogeServiceContext).DB
+ var response = make(APIData)
+
+ var typeName = args["type"].(string)
+ var key = LogeKey(args["key"].(string))
+
+ var obj interface{}
+ var links = make(map[string][]string)
+ db.TransactJSON(func (t *Transaction) {
+ obj = t.Read(typeName, key)
+ if obj != nil {
+ for linkName := range db.types[typeName].Links {
+ links[linkName] = t.ReadLinks(typeName, linkName, key)
+ }
+ }
+ }, 0)
+
+ if obj == nil {
+ response["found"] = false
+ } else {
+ response["found"] = true
+ response["obj"] = obj
+ response["links"] = links
+ }
+ return true, response
+}
View
3  src/loge/transactions.go
@@ -32,6 +32,7 @@ type Transaction struct {
state TransactionState
snapshotID uint64
cancelled bool
+ giveJSON bool
}
func NewTransaction(db *LogeDB, sID uint64) *Transaction {
@@ -142,7 +143,7 @@ func (t *Transaction) getVersion(ref objRef, forWrite bool, load bool) *liveVers
var version = t.db.acquireVersion(ref, t.context, load)
- object, upgraded := version.getObject()
+ object, upgraded := version.getObject(t.giveJSON)
lv = &liveVersion{
version: version,
View
10 src/loge/types.go
@@ -59,12 +59,16 @@ func (t *logeType) NilValue() interface{} {
return reflect.Zero(reflect.TypeOf(t.Exemplar)).Interface()
}
-func (t *logeType) Decode(enc []byte) (interface{}, bool) {
+func (t *logeType) Decode(enc []byte, toJSON bool) (interface{}, bool) {
if len(enc) == 0 {
- return t.NilValue(), false
+ if toJSON {
+ return nil, false
+ } else {
+ return t.NilValue(), false
+ }
}
- obj, upgraded, err := t.SpackType.DecodeObj(enc)
+ obj, upgraded, err := t.SpackType.DecodeObj(enc, toJSON)
if err != nil {
panic(fmt.Sprintf("Decode error: %v", err))
}
View
1  src/logetest/bench.go
@@ -49,7 +49,6 @@ func DoWrite(cores int, idx int) {
}
fmt.Printf("Done in %v\n", time.Since(startTime))
- db.FlushCache()
fmt.Printf("Flushed\n")
View
3  src/logetest/main.go
@@ -12,7 +12,8 @@ type Pet struct {
}
func main() {
- LinkBench()
+ StartService()
+ //LinkBench()
//LinkSandbox()
//WriteBench()
//Sandbox()
View
50 src/logetest/service.go
@@ -0,0 +1,50 @@
+package main
+
+import (
+ "fmt"
+ "os"
+ "os/signal"
+
+ "loge"
+
+ "github.com/brendonh/go-service"
+)
+
+
+func StartService() {
+ var db = loge.NewLogeDB(loge.NewLevelDBStore("data/links"))
+ defer db.Close()
+
+ db.CreateType(loge.NewTypeDef("person", 1, &Person{}))
+
+ var petDef = loge.NewTypeDef("pet", 1, &Pet{})
+ petDef.Links = loge.LinkSpec{ "owner": "person" }
+ db.CreateType(petDef)
+
+ var serviceCollection = goservice.NewServiceCollection()
+ serviceCollection.AddService(loge.GetService())
+
+ var server = &loge.LogeServiceContext{
+ *goservice.NewServer(
+ serviceCollection,
+ goservice.BasicSessionCreator),
+ db,
+ }
+
+ server.AddEndpoint(goservice.NewHttpRpcEndpoint(":6060", server, nil))
+ server.AddEndpoint(goservice.NewTelnetEndpoint(":6061", server))
+
+ server.Log("Server starting...")
+
+ var stopper = make(chan os.Signal, 1)
+ signal.Notify(stopper)
+
+ server.Start()
+ defer server.Stop()
+
+ <-stopper
+ close(stopper)
+
+ fmt.Printf("\n")
+ server.Log("Server stopping...")
+}

0 comments on commit b5a75a5

Please sign in to comment.
Something went wrong with that request. Please try again.