-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.go
43 lines (33 loc) · 1.38 KB
/
transaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package framework
import (
"fmt"
"github.com/flier/curator.go"
)
func CreateTransaction(client curator.CuratorFramework) error {
// this example shows how to use ZooKeeper's new transactions
results, err := client.InTransaction().Create().ForPathWithData("/a/path", []byte("some data")).
And().SetData().ForPathWithData("/another/path", []byte("other data")).
And().Delete().ForPath("/yet/another/path").
Commit() // IMPORTANT! The transaction is not submitted until commit() is called
for _, result := range results {
fmt.Printf("%s - %v", result.ForPath, result.Type)
}
return err
}
// These next four methods show how to use Curator's transaction APIs in a more traditional - one-at-a-time - manner
func StartTransaction(client curator.CuratorFramework) curator.Transaction {
// start the transaction builder
return client.InTransaction()
}
func AddCreateToTransaction(transaction curator.Transaction) curator.TransactionFinal {
// add a create operation
return transaction.Create().ForPathWithData("/a/path", []byte("some data")).And()
}
func AddDeleteToTransaction(transaction curator.Transaction) curator.TransactionFinal {
// add a create operation
return transaction.Delete().ForPath("/yet/another/path").And()
}
func CommitTransaction(transaction curator.TransactionFinal) ([]curator.TransactionResult, error) {
// commit the transaction
return transaction.Commit()
}