-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathmain.go
More file actions
83 lines (72 loc) · 1.79 KB
/
main.go
File metadata and controls
83 lines (72 loc) · 1.79 KB
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
package main
import (
"fmt"
"github.com/Trendyol/go-dcp"
"github.com/Trendyol/go-dcp/logger"
"github.com/Trendyol/go-dcp/models"
"github.com/couchbase/gocb/v2"
"log"
"time"
)
func listener(ctx *models.ListenerContext) {
switch event := ctx.Event.(type) {
case models.DcpMutation:
logger.Log.Info(
"mutated(vb=%v,eventTime=%v) | id: %v, value: %v | isCreated: %v",
event.VbID, event.EventTime, string(event.Key), string(event.Value), event.IsCreated(),
)
case models.DcpDeletion:
logger.Log.Info(
"deleted(vb=%v,eventTime=%v) | id: %v",
event.VbID, event.EventTime, string(event.Key),
)
case models.DcpExpiration:
logger.Log.Info(
"expired(vb=%v,eventTime=%v) | id: %v",
event.VbID, event.EventTime, string(event.Key),
)
}
ctx.Ack()
}
func main() {
time.Sleep(15 * time.Second) //wait for couchbase container initialize
go seedCouchbaseBucket()
connector, err := dcp.NewDcp("config.yml", listener)
if err != nil {
panic(err)
}
defer connector.Close()
connector.Start()
}
func seedCouchbaseBucket() {
cluster, err := gocb.Connect("couchbase://couchbase", gocb.ClusterOptions{
Username: "user",
Password: "password",
})
if err != nil {
log.Fatal(err)
}
bucket := cluster.Bucket("dcp-test")
err = bucket.WaitUntilReady(5*time.Second, nil)
if err != nil {
log.Fatal(err)
}
collection := bucket.DefaultCollection()
counter := 0
for {
counter++
documentID := fmt.Sprintf("doc-%d", counter)
document := map[string]interface{}{
"counter": counter,
"message": "Hello Couchbase",
"time": time.Now().Format(time.RFC3339),
}
_, err := collection.Upsert(documentID, document, nil)
if err != nil {
log.Println("Error inserting document:", err)
} else {
log.Println("Inserted document:", documentID)
}
time.Sleep(1 * time.Second)
}
}