Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

list API for adminning

  • Loading branch information...
commit 5674e3637c8357461832c6649a424fabe0903c27 1 parent d160c80
@brendonh authored
View
20 src/loge/database.go
@@ -1,6 +1,7 @@
package loge
import (
+ "fmt"
"time"
"sync/atomic"
"reflect"
@@ -148,16 +149,31 @@ func (db *LogeDB) FindSlice(typeName string, linkName string, target LogeKey, fr
return
}
+func (db *LogeDB) ListSlice(typeName string, from LogeKey, limit int) (results []LogeKey) {
+ db.Transact(func (t *Transaction) {
+ results = t.ListSlice(typeName, from, limit).All()
+ }, 0)
+ return
+}
+
// -----------------------------------------------
// Internals
// -----------------------------------------------
func (db *LogeDB) makeObjRef(typeName string, key LogeKey) objRef {
- return makeObjRef(db.types[typeName], key)
+ typ, ok := db.types[typeName]
+ if !ok {
+ panic(fmt.Sprintf("Type not registered: %s", typeName))
+ }
+ return makeObjRef(typ, key)
}
func (db *LogeDB) makeLinkRef(typeName string, linkName string, key LogeKey) objRef {
- return makeLinkRef(db.types[typeName], linkName, key)
+ typ, ok := db.types[typeName]
+ if !ok {
+ panic(fmt.Sprintf("Type not registered: %s", typeName))
+ }
+ return makeLinkRef(typ, linkName, key)
}
View
28 src/loge/leveldb.go
@@ -301,6 +301,34 @@ func (context *levelDBContext) findSlice(ref objRef, from LogeKey, limit int) Re
}
+func (context *levelDBContext) listSlice(prefix []byte, from LogeKey, limit int) ResultSet {
+ if limit == 0 {
+ return &levelDBResultSet {
+ closed: true,
+ }
+ }
+
+ var it = context.ldbStore.iteratePrefix(prefix, []byte(from), context.readOptions)
+ if !it.Valid() {
+ it.Close()
+ return &levelDBResultSet {
+ closed: true,
+ }
+ }
+
+ var prefixLen = len(prefix)
+ var next = string(it.Key()[prefixLen:])
+
+ return &levelDBResultSet{
+ it: it,
+ prefixLen: prefixLen,
+ next: next,
+ closed: false,
+ limit: limit,
+ count: 0,
+ }
+}
+
// -----------------------------------------------
// Helpers
// -----------------------------------------------
View
14 src/loge/objref.go
@@ -12,6 +12,16 @@ type objRef struct {
CacheKey string
}
+func encodeTypeTag(typ *logeType) uint32 {
+ return uint32(typ.SpackType.Tag) << 16
+}
+
+func typePrefix(typ *logeType) []byte {
+ var buf = bytes.NewBuffer(make([]byte, 0, 4))
+ binary.Write(buf, binary.BigEndian, encodeTypeTag(typ))
+ return buf.Bytes()
+}
+
func encodeKey(tag uint32, key LogeKey) string {
var keyBytes = []byte(key)
var buf = bytes.NewBuffer(make([]byte, 0, len(keyBytes) + 4))
@@ -21,14 +31,14 @@ func encodeKey(tag uint32, key LogeKey) string {
}
func makeObjRef(typ *logeType, key LogeKey) objRef {
- var tag = uint32(typ.SpackType.Tag) << 16
+ var tag = encodeTypeTag(typ)
var cacheKey = encodeKey(tag, key)
var ref = objRef{ typ, key, "", cacheKey }
return ref
}
func makeLinkRef(typ *logeType, linkName string, key LogeKey) objRef {
- var tag = (uint32(typ.SpackType.Tag) << 16) | uint32(typ.Links[linkName].Tag)
+ var tag = encodeTypeTag(typ) | uint32(typ.Links[linkName].Tag)
var cacheKey = encodeKey(tag, key)
var ref = objRef{ typ, key, linkName, cacheKey }
return ref
View
32 src/loge/service.go
@@ -6,9 +6,9 @@ import (
. "github.com/brendonh/go-service"
)
-type LogeServiceContext struct {
- Server
- DB *LogeDB
+type LogeServiceContext interface {
+ ServerContext
+ DB() *LogeDB
}
func GetService() *Service {
@@ -18,6 +18,14 @@ func GetService() *Service {
[]APIArg {},
method_info)
service.AddMethod(
+ "list",
+ []APIArg {
+ APIArg{Name: "type", ArgType: StringArg},
+ APIArg{Name: "from", ArgType: StringArg, Default: ""},
+ APIArg{Name: "limit", ArgType: UIntArg, Default: -1},
+ },
+ method_list)
+ service.AddMethod(
"find",
[]APIArg {
APIArg{Name: "type", ArgType: StringArg},
@@ -39,7 +47,7 @@ func GetService() *Service {
}
func method_info(args APIData, session Session, context ServerContext) (bool, APIData) {
- var db = context.(*LogeServiceContext).DB
+ var db = context.(LogeServiceContext).DB()
var dbInfo string
switch db.store.(type) {
@@ -50,7 +58,6 @@ func method_info(args APIData, session Session, context ServerContext) (bool, AP
}
var types []string
- fmt.Print(db.types)
for typeName := range db.types {
types = append(types, typeName)
}
@@ -62,7 +69,7 @@ func method_info(args APIData, session Session, context ServerContext) (bool, AP
}
func method_find(args APIData, session Session, context ServerContext) (bool, APIData) {
- var db = context.(*LogeServiceContext).DB
+ var db = context.(LogeServiceContext).DB()
var response = make(APIData)
response["keys"] = db.FindSlice(
@@ -74,8 +81,19 @@ func method_find(args APIData, session Session, context ServerContext) (bool, AP
return true, response
}
+func method_list(args APIData, session Session, context ServerContext) (bool, APIData) {
+ var db = context.(LogeServiceContext).DB()
+
+ var response = make(APIData)
+ response["keys"] = db.ListSlice(
+ args["type"].(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 db = context.(LogeServiceContext).DB()
var response = make(APIData)
var typeName = args["type"].(string)
View
7 src/loge/storage.go
@@ -30,6 +30,8 @@ type transactionContext interface {
find(objRef) ResultSet
findSlice(objRef, LogeKey, int) ResultSet
+ listSlice([]byte, LogeKey, int) ResultSet
+
commit(uint64) error
rollback()
}
@@ -134,6 +136,11 @@ func (context *memContext) findSlice(ref objRef, from LogeKey, limit int) Result
panic("Find not implemented on memstore")
}
+func (context *memContext) listSlice(prefix []byte, from LogeKey, limit int) ResultSet {
+ // Until I can be bothered
+ panic("List not implemented on memstore")
+}
+
func (context *memContext) commit(sID uint64) error {
var store = context.mstore
store.lock.SpinLock()
View
9 src/loge/transactions.go
@@ -115,6 +115,15 @@ func (t *Transaction) FindSlice(typeName string, linkName string, target LogeKey
return t.context.findSlice(t.db.makeLinkRef(typeName, linkName, target), from, limit)
}
+func (t *Transaction) ListSlice(typeName string, from LogeKey, limit int) ResultSet {
+ typ, ok := t.db.types[typeName]
+ if !ok {
+ panic(fmt.Sprintf("No such type %s\n", typeName))
+ }
+ var prefix = typePrefix(typ)
+ return t.context.listSlice(prefix, from, limit)
+}
+
// -----------------------------------------------
// Internals
// -----------------------------------------------
View
10 src/logetest/service.go
@@ -10,6 +10,14 @@ import (
"github.com/brendonh/go-service"
)
+type context struct {
+ goservice.Server
+ db *loge.LogeDB
+}
+
+func (c *context) DB() *loge.LogeDB {
+ return c.db
+}
func StartService() {
var db = loge.NewLogeDB(loge.NewLevelDBStore("data/links"))
@@ -24,7 +32,7 @@ func StartService() {
var serviceCollection = goservice.NewServiceCollection()
serviceCollection.AddService(loge.GetService())
- var server = &loge.LogeServiceContext{
+ var server = &context{
*goservice.NewServer(
serviceCollection,
goservice.BasicSessionCreator),
Please sign in to comment.
Something went wrong with that request. Please try again.