generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
113 lines (94 loc) · 2.85 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
/*
* Copyright (c) 2023, 2024 Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
/*
Package main shows how to add indexes against a NamedMap or NamedCache to support queries and aggregations.
*/
package main
import (
"context"
"fmt"
"github.com/oracle/coherence-go-client/v2/coherence"
"github.com/oracle/coherence-go-client/v2/coherence/aggregators"
"github.com/oracle/coherence-go-client/v2/coherence/extractors"
"github.com/oracle/coherence-go-client/v2/coherence/filters"
"time"
)
type Person struct {
ID int `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
City string `json:"city"`
}
func (p Person) String() string {
return fmt.Sprintf("Person{id=%d, name=%s, age=%d, city=%s}", p.ID, p.Name, p.Age, p.City)
}
func main() {
var (
ctx = context.Background()
cities = []string{"Perth", "Adelaide", "Sydney", "Melbourne"}
insertCount = 50_000
count *int64
)
// create a new Session to the default gRPC port of 1408 using plain text
session, err := coherence.NewSession(ctx, coherence.WithPlainText())
if err != nil {
panic(err)
}
defer session.Close()
namedMap, err := coherence.GetNamedMap[int, Person](session, "people")
if err != nil {
panic(err)
}
if err = namedMap.Clear(ctx); err != nil {
panic(err)
}
fmt.Printf("Adding %d random people using PutAll()\n", insertCount)
batchSize := 5000
buffer := make(map[int]Person)
for i := 1; i <= insertCount; i++ {
p := Person{ID: i, Name: fmt.Sprintf("Person-%d", i), Age: 15 + i%40, City: cities[i%4]}
buffer[p.ID] = p
if i%batchSize == 0 {
if err = namedMap.PutAll(ctx, buffer); err != nil {
panic(err)
}
buffer = make(map[int]Person)
}
}
// write any left in the buffer
if len(buffer) > 0 {
if err = namedMap.PutAll(ctx, buffer); err != nil {
panic(err)
}
}
size, _ := namedMap.Size(ctx)
fmt.Println("Cache size is", size)
age := extractors.Extract[int]("age")
fmt.Println("Retrieve the count of people between the age of 20 and 40 without indexes")
start := time.Now()
count, err = coherence.AggregateFilter(ctx, namedMap, filters.Between(age, 20, 40), aggregators.Count())
duration := time.Since(start)
if err != nil {
panic(err)
}
fmt.Printf("Count = %v, duration without index: %v. Adding index and waiting 10 seconds...\n", *count, duration)
err = coherence.AddIndex(ctx, namedMap, age, true)
if err != nil {
panic(err)
}
time.Sleep(time.Duration(10) * time.Second)
start = time.Now()
count, err = coherence.AggregateFilter(ctx, namedMap, filters.Between(age, 20, 40), aggregators.Count())
duration = time.Since(start)
if err != nil {
panic(err)
}
fmt.Printf("Count = %v, duration WITH index: %v\n", *count, duration)
err = coherence.RemoveIndex(ctx, namedMap, age)
if err != nil {
panic(err)
}
}