本文整理匯總了Golang中github.com/FoundationDB/fdb-go/fdb.Transactor類的典型用法代碼示例。如果您正苦於以下問題:Golang Transactor類的具體用法?Golang Transactor怎麽用?Golang Transactor使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Transactor類的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: setOne
func setOne(t fdb.Transactor, key []byte, value []byte) {
fmt.Printf("setOne got: %T\n", t)
t.Transact(func(tr fdb.Transaction) (interface{}, error) {
// We don't actually call tr.Set here to avoid mutating a real database.
// tr.Set(key, value)
return nil, nil
})
}
示例2: setMany
func setMany(t fdb.Transactor, value []byte, keys ...[]byte) {
fmt.Printf("setMany got: %T\n", t)
t.Transact(func(tr fdb.Transaction) (interface{}, error) {
for _, key := range keys {
setOne(tr, key, value)
}
return nil, nil
})
}
示例3: executeMutation
func (sm *StackMachine) executeMutation(t fdb.Transactor, f func(fdb.Transaction) (interface{}, error), isDB bool, idx int) {
_, e := t.Transact(f)
if e != nil {
panic(e)
}
if isDB {
sm.store(idx, []byte("RESULT_NOT_PRESENT"))
}
}
示例4: Move
func (dl directoryLayer) Move(t fdb.Transactor, oldPath []string, newPath []string) (DirectorySubspace, error) {
r, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) {
if e := dl.checkVersion(tr, &tr); e != nil {
return nil, e
}
sliceEnd := len(oldPath)
if sliceEnd > len(newPath) {
sliceEnd = len(newPath)
}
if stringsEqual(oldPath, newPath[:sliceEnd]) {
return nil, errors.New("the destination directory cannot be a subdirectory of the source directory")
}
oldNode := dl.find(tr, oldPath).prefetchMetadata(tr)
newNode := dl.find(tr, newPath).prefetchMetadata(tr)
if !oldNode.exists() {
return nil, errors.New("the source directory does not exist")
}
if oldNode.isInPartition(nil, false) || newNode.isInPartition(nil, false) {
if !oldNode.isInPartition(nil, false) || !newNode.isInPartition(nil, false) || !stringsEqual(oldNode.path, newNode.path) {
return nil, errors.New("cannot move between partitions")
}
nnc, e := newNode.getContents(dl, nil)
if e != nil {
return nil, e
}
return nnc.Move(tr, oldNode.getPartitionSubpath(), newNode.getPartitionSubpath())
}
if newNode.exists() {
return nil, errors.New("the destination directory already exists. Remove it first")
}
parentNode := dl.find(tr, newPath[:len(newPath)-1])
if !parentNode.exists() {
return nil, errors.New("the parent of the destination directory does not exist. Create it first")
}
p, e := dl.nodeSS.Unpack(oldNode.subspace)
if e != nil {
return nil, e
}
tr.Set(parentNode.subspace.Sub(_SUBDIRS, newPath[len(newPath)-1]), p[0].([]byte))
dl.removeFromParent(tr, oldPath)
return dl.contentsOfNode(oldNode.subspace, newPath, oldNode._layer.MustGet())
})
if e != nil {
return nil, e
}
return r.(DirectorySubspace), nil
}
示例5: Create
func (dl directoryLayer) Create(t fdb.Transactor, path []string, layer []byte) (DirectorySubspace, error) {
r, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) {
return dl.createOrOpen(tr, &tr, path, layer, nil, true, false)
})
if e != nil {
return nil, e
}
return r.(DirectorySubspace), nil
}
示例6: Remove
func (dl directoryLayer) Remove(t fdb.Transactor, path []string) (bool, error) {
r, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) {
if e := dl.checkVersion(tr, &tr); e != nil {
return false, e
}
if len(path) == 0 {
return false, errors.New("the root directory cannot be removed")
}
node := dl.find(tr, path).prefetchMetadata(tr)
if !node.exists() {
return false, nil
}
if node.isInPartition(nil, false) {
nc, e := node.getContents(dl, nil)
if e != nil {
return false, e
}
return nc.(directoryPartition).Remove(tr, node.getPartitionSubpath())
}
if e := dl.removeRecursive(tr, node.subspace); e != nil {
return false, e
}
dl.removeFromParent(tr, path)
return true, nil
})
if e != nil {
return false, e
}
return r.(bool), nil
}
示例7: processOp
func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, idx int, t fdb.Transactor, rt fdb.ReadTransactor) {
defer func() {
if r := recover(); r != nil {
sm.store(idx, []byte("DIRECTORY_ERROR"))
if createOps[op] {
de.store(nil)
}
}
}()
var e error
switch {
case op == "CREATE_SUBSPACE":
tuples := sm.popTuples(1)
rp := sm.waitAndPop().item.([]byte)
s := subspace.FromBytes(rp).Sub(tuples[0]...)
de.store(s)
case op == "CREATE_LAYER":
idx1 := sm.waitAndPop().item.(int64)
idx2 := sm.waitAndPop().item.(int64)
amp := sm.waitAndPop().item.(int64)
nodeSS := de.list[idx1]
contentSS := de.list[idx2]
if nodeSS == nil || contentSS == nil {
de.store(nil)
} else {
de.store(directory.NewDirectoryLayer(nodeSS.(subspace.Subspace), contentSS.(subspace.Subspace), (amp == int64(1))))
}
case op == "CREATE_OR_OPEN":
tuples := sm.popTuples(1)
l := sm.waitAndPop().item
var layer []byte
if l != nil {
layer = l.([]byte)
}
d, e := de.cwd().CreateOrOpen(t, tupleToPath(tuples[0]), layer)
if e != nil {
panic(e)
}
de.store(d)
case op == "CREATE":
tuples := sm.popTuples(1)
l := sm.waitAndPop().item
var layer []byte
if l != nil {
layer = l.([]byte)
}
p := sm.waitAndPop().item
var d directory.Directory
if p == nil {
d, e = de.cwd().Create(t, tupleToPath(tuples[0]), layer)
} else {
// p.([]byte) itself may be nil, but CreatePrefix handles that appropriately
d, e = de.cwd().CreatePrefix(t, tupleToPath(tuples[0]), layer, p.([]byte))
}
if e != nil {
panic(e)
}
de.store(d)
case op == "OPEN":
tuples := sm.popTuples(1)
l := sm.waitAndPop().item
var layer []byte
if l != nil {
layer = l.([]byte)
}
d, e := de.cwd().Open(rt, tupleToPath(tuples[0]), layer)
if e != nil {
panic(e)
}
de.store(d)
case op == "CHANGE":
i := sm.waitAndPop().item.(int64)
if de.list[i] == nil {
i = de.errorIndex
}
de.index = i
case op == "SET_ERROR_INDEX":
de.errorIndex = sm.waitAndPop().item.(int64)
case op == "MOVE":
tuples := sm.popTuples(2)
d, e := de.cwd().Move(t, tupleToPath(tuples[0]), tupleToPath(tuples[1]))
if e != nil {
panic(e)
}
de.store(d)
case op == "MOVE_TO":
tuples := sm.popTuples(1)
d, e := de.cwd().MoveTo(t, tupleToPath(tuples[0]))
if e != nil {
panic(e)
}
de.store(d)
case strings.HasPrefix(op, "REMOVE"):
path := sm.maybePath()
// This ***HAS*** to call Transact to ensure that any directory version
// key set in the process of trying to remove this potentially
// non-existent directory, in the REMOVE but not REMOVE_IF_EXISTS case,
//.........這裏部分代碼省略.........
示例8: processInst
func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) {
defer func() {
if r := recover(); r != nil {
switch r := r.(type) {
case fdb.Error:
sm.store(idx, []byte(tuple.Tuple{[]byte("ERROR"), []byte(fmt.Sprintf("%d", r.Code))}.Pack()))
default:
panic(r)
}
}
}()
var e error
op := inst[0].(string)
if sm.verbose {
fmt.Printf("%d. Instruction is %s (%v)\n", idx, op, sm.prefix)
fmt.Printf("Stack from [")
sm.dumpStack()
fmt.Printf(" ]\n")
}
var t fdb.Transactor
var rt fdb.ReadTransactor
var isDB bool
switch {
case strings.HasSuffix(op, "_SNAPSHOT"):
rt = sm.tr.Snapshot()
op = op[:len(op)-9]
case strings.HasSuffix(op, "_DATABASE"):
t = db
rt = db
op = op[:len(op)-9]
isDB = true
default:
t = sm.tr
rt = sm.tr
}
switch {
case op == "PUSH":
sm.store(idx, inst[1])
case op == "DUP":
entry := sm.stack[len(sm.stack)-1]
sm.store(entry.idx, entry.item)
case op == "EMPTY_STACK":
sm.stack = []stackEntry{}
sm.stack = make([]stackEntry, 0)
case op == "SWAP":
idx := sm.waitAndPop().item.(int64)
sm.stack[len(sm.stack)-1], sm.stack[len(sm.stack)-1-int(idx)] = sm.stack[len(sm.stack)-1-int(idx)], sm.stack[len(sm.stack)-1]
case op == "POP":
sm.stack = sm.stack[:len(sm.stack)-1]
case op == "SUB":
sm.store(idx, sm.waitAndPop().item.(int64)-sm.waitAndPop().item.(int64))
case op == "NEW_TRANSACTION":
sm.tr, e = db.CreateTransaction()
if e != nil {
panic(e)
}
case op == "ON_ERROR":
sm.store(idx, sm.tr.OnError(fdb.Error{int(sm.waitAndPop().item.(int64))}))
case op == "GET_READ_VERSION":
_, e = rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) {
sm.lastVersion = rtr.GetReadVersion().MustGet()
sm.store(idx, []byte("GOT_READ_VERSION"))
return nil, nil
})
if e != nil {
panic(e)
}
case op == "SET":
sm.executeMutation(t, func(tr fdb.Transaction) (interface{}, error) {
tr.Set(fdb.Key(sm.waitAndPop().item.([]byte)), sm.waitAndPop().item.([]byte))
return nil, nil
}, isDB, idx)
case op == "LOG_STACK":
prefix := sm.waitAndPop().item.([]byte)
for i := len(sm.stack) - 1; i >= 0; i-- {
if i%100 == 0 {
sm.tr.Commit().MustGet()
}
el := sm.waitAndPop()
var keyt tuple.Tuple
keyt = append(keyt, int64(i))
keyt = append(keyt, int64(el.idx))
pk := append(prefix, keyt.Pack()...)
var valt tuple.Tuple
valt = append(valt, el.item)
pv := valt.Pack()
vl := 40000
if len(pv) < vl {
vl = len(pv)
}
//.........這裏部分代碼省略.........