Context API -- Performance of the simple API, ease of the easy API
It has been observed that programs using YDBGo tend to be verbose, to deal with errors, tptokens, allocating keys, buffers, etc. This could be remedied by introducing a new API that "hides" much of this logic, but still gives the users performance close to the simple API.
Below I outline an API proposal, but it is open to revision.
An example program:
import (
"lang.yottadb.com/go/yottadb"
)
func main() {
ctx = yottadb.Context()
key, err := ctx.MakeKey("^hello")
if err != nil {
// handle err
}
_, err = key.SetVal(ctx.MakeBuffer("Hello world!"))
if err != nil {
// handle err
}
for {
key, err = key.Clone().NextSub()
if err != nil {
// handle err
}
if key.NodeEnd() {
break
}
val, err := key.Val()
if err != nil {
// handle err
}
strval := val.Str()
fmt.Printf("Key is: {}", strval)
}
}
Proposed API:
type Context struct {
// Has unexported fields.
}
func New() *Context
func (ctx *Context) MakeBuffer(value string) (*BufferContext, error)
func (ctx *Context) MakeKey(elems ...string) (*KeyContext, error)
func (ctx *Context) TpRestart() bool
func (ctx *Context) Transaction(cb func() uint32, code string, vars ...string) error
func (ctx *Context) YdbExit() bool
type KeyContext struct {
// Has unexported fields.
}
func (k *KeyContext) Clone() *KeyContext
func (k *KeyContext) NextNode() (*KeyContext, error)
func (k *KeyContext) NextSub() (*KeyContext, error)
func (k *KeyContext) NodeEnd() bool
func (k *KeyContext) PrevNode() (*KeyContext, error)
func (k *KeyContext) PrevSub() (*KeyContext, error)
func (k *KeyContext) SetVal(val BufferT) (*BufferContext, error)
func (k *KeyContext) Val() (*BufferContext, error)
type BufferContext struct {
// Has unexported fields.
}
func (b *BufferContext) BAry() ([]byte, error
func (b *BufferContext) Clone() (*BufferContext, error)
func (b *BufferContext) Str() string
Edited by Charles Hathaway