-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
127 lines (102 loc) · 4.26 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
)
func main() {
// InitializeDatabaseAndContainer() provides the convenience to quickly set up a cosmos DB and its container for our demonstration.
// Comment out InitializeDatabaseAndContainer() and only run demo() if the DB and container have been deployed on the cloud.
InitializeDatabaseAndContainer()
demoCrudAndEtag()
}
func demoCrudAndEtag() {
book := sampleBook
addBookInfo(book) // Add an item into the container
receivedBook1, etag1 := readBookInfo(book)
updateBookPrice(receivedBook1, 120.00, etag1) // This will succeed
updateBookPrice(receivedBook1, 150.00, etag1) // This is to mock a concurrent Write operation. It will be rejected, since a new Etag has been generated by the previous update operation.
receivedBook2, etag2 := readBookInfo(book)
updateBookPrice(receivedBook2, 150.00, etag2) // This will succeed since etag2 is the latest ETag so far.
demoSqlQuery(book)
deleteBookInfo(receivedBook2, etag2) // This will be rejected for outdated ETag as well.
receivedBook3, etag3 := readBookInfo(book) // receivedBook3 will contain the updated price 150.00
deleteBookInfo(receivedBook3, etag3) // This will succeed since etag3 is the latest ETag.
}
func addBookInfo(book Book) {
fmt.Printf("\r\nAdding sample book entry to DB...\r\n")
container := GetContainer()
pk := azcosmos.NewPartitionKeyString(book.Title)
marshalled, err := json.Marshal(book)
if err != nil {
panic(err)
}
itemResponse, err := container.CreateItem(context.Background(), pk, marshalled, nil)
if err != nil {
panic(err)
}
fmt.Printf("Sample book entry added. ActivityId %s consuming %v RU\r\n", itemResponse.ActivityID, itemResponse.RequestCharge)
}
func readBookInfo(book Book) (Book, azcore.ETag) {
fmt.Printf("\r\nReading book info...\r\n")
container := GetContainer()
pk := azcosmos.NewPartitionKeyString(book.Title)
itemResponse, err := container.ReadItem(context.Background(), pk, book.Id, nil)
var receivedBook Book
err = json.Unmarshal(itemResponse.Value, &receivedBook)
if err != nil {
panic(err)
}
jsonStr, err := json.Marshal(receivedBook)
if err != nil {
fmt.Printf("Error: %s", err.Error())
}
fmt.Printf("Book info read=%s\r\nEtag=%s\r\nActivityId %s consuming %v RU\r\n", string(jsonStr), itemResponse.ETag, itemResponse.ActivityID, itemResponse.RequestCharge)
return receivedBook, itemResponse.ETag
}
func updateBookPrice(book Book, newPrice float32, etag azcore.ETag) {
fmt.Printf("\r\nUpdating book price...\r\n")
container := GetContainer()
book.Price = newPrice
marshalledBook, err := json.Marshal(book)
if err != nil {
panic(err)
}
// Replace with Etag
pk := azcosmos.NewPartitionKeyString(book.Title)
itemResponse, err := container.ReplaceItem(context.Background(), pk, book.Id, marshalledBook, &azcosmos.ItemOptions{IfMatchEtag: &etag})
if err != nil {
fmt.Printf("Update rejected.\r\nError is\r\n%s\r\n", err)
} else {
fmt.Printf("Book price updated.\r\nEtag=%s\r\nActivityId %s consuming %v RU\r\n", itemResponse.ETag, itemResponse.ActivityID, itemResponse.RequestCharge)
}
}
func deleteBookInfo(book Book, etag azcore.ETag) {
fmt.Printf("\r\nDeleting book info...\r\n")
container := GetContainer()
// Replace with Etag
pk := azcosmos.NewPartitionKeyString(book.Title)
itemResponse, err := container.DeleteItem(context.Background(), pk, book.Id, &azcosmos.ItemOptions{IfMatchEtag: &etag})
if err != nil {
fmt.Printf("Delete operation rejected.\r\nError is\r\n%s\r\n", err)
} else {
fmt.Printf("Book info deleted.\r\nActivityId %s consuming %v RU\r\n", itemResponse.ActivityID, itemResponse.RequestCharge)
}
}
func demoSqlQuery(book Book) {
fmt.Printf("\r\nQuerying book info...\r\n")
container := GetContainer()
pager := container.NewQueryItemsPager("select * from books b", azcosmos.NewPartitionKeyString(book.Title), nil)
for pager.More() {
queryResponse, err := pager.NextPage(context.Background())
fmt.Printf("Reponse = %s\r\n", queryResponse.RawResponse.Body)
if err != nil {
fmt.Printf("failed to get next page of query response. error = %s\r\n", err)
}
for _, item := range queryResponse.Items {
fmt.Printf("Book = %s\r\n", string(item))
}
}
}