-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathmain.go
109 lines (90 loc) · 3.21 KB
/
main.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"flag"
"k8s.io/klog"
"github.com/vmware/differential-datalog/go/pkg/ddlog"
)
// This example program assumes that the DDlog program being used is test/types_test/typesTest.dl.
const numDDlogWorkers = 1
func logger(msg string) {
klog.Errorf(msg)
}
// Example is the Go type corresponding to ExampleRelation in our DDlog program.
type Example struct {
Name string
Values []uint32
Labels map[string]string
}
var (
exampleRelationConstructor = ddlog.NewCString("ExampleRelation")
)
// NewRecordExample transforms a Go Example instance into a DDlog record for ExampleRelation.
func NewRecordExample(example *Example) ddlog.Record {
rName := ddlog.NewRecordString(example.Name)
rValues := ddlog.NewRecordVector()
for _, v := range example.Values {
rValues.Push(ddlog.NewRecordU32(v))
}
rLabels := ddlog.NewRecordMap()
for k, v := range example.Labels {
rLabels.Push(ddlog.NewRecordString(k), ddlog.NewRecordString(v))
}
return ddlog.NewRecordStructStatic(exampleRelationConstructor, rName, rValues, rLabels)
}
// NewRecordExampleKey returns a DDlog record for the primary key of the provided example. This can
// be used to efficiently delete a previously-inserted record (i.e. without having to provide the
// entire record).
func NewRecordExampleKey(example *Example) ddlog.Record {
return ddlog.NewRecordString(example.Name)
}
func main() {
klog.InitFlags(nil)
recordCommands := flag.String("record-commands", "", "Provide a file name where to record commands sent to DDlog")
dumpChanges := flag.String("dump-changes", "", "Provide a file name where to dump record changes")
flag.Parse()
// Ensures that DDlog will use our own logger (klog) to print error messages.
ddlog.SetErrMsgPrinter(logger)
var outRecordHandler ddlog.OutRecordHandler
if *dumpChanges == "" {
// ignore changes
outRecordHandler, _ = ddlog.NewOutRecordSink()
} else {
// write output changes to file
outRecordHandler, _ = ddlog.NewOutRecordDumper(*dumpChanges)
}
klog.Infof("Running new DDlog program")
ddlogProgram, err := ddlog.NewProgram(numDDlogWorkers, outRecordHandler)
if err != nil {
klog.Fatalf("Error when creating DDlog program: %v", err)
}
defer func() {
klog.Infof("Stopping DDlog program")
if err := ddlogProgram.Stop(); err != nil {
klog.Errorf("Error when stopping DDlog program: %v", err)
}
}()
if *recordCommands != "" {
ddlogProgram.StartRecordingCommands(*recordCommands)
}
e1 := &Example{
Name: "MyExample",
Values: []uint32{4, 1, 777},
Labels: map[string]string{
"owner": "X",
"priority": "Y",
},
}
r1 := NewRecordExample(e1)
klog.Infof("Inserting record %s", r1.Dump())
cmdInsert1 := ddlog.NewInsertCommand(ddlogProgram.GetTableID("ExampleRelation"), r1)
// In practice, each transction would likely include more than one command.
if err := ddlogProgram.ApplyUpdatesAsTransaction(cmdInsert1); err != nil {
klog.Errorf("Error during transaction: %v", err)
}
k1 := NewRecordExampleKey(e1)
klog.Infof("Deleting record with key %s", k1.Dump())
cmdDelete1 := ddlog.NewDeleteKeyCommand(ddlogProgram.GetTableID("ExampleRelation"), k1)
if err := ddlogProgram.ApplyUpdatesAsTransaction(cmdDelete1); err != nil {
klog.Errorf("Error during transaction: %v", err)
}
}