-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
161 lines (133 loc) · 3.56 KB
/
utils.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package internal
import (
"bytes"
"context"
"fmt"
"io"
"strings"
"time"
"unsafe"
"cloud.google.com/go/spanner"
"cloud.google.com/go/storage"
)
var (
SpannerString = func(s spanner.NullString) string {
switch {
case !s.IsNull():
return s.String()
default:
return ""
}
}
)
func StringToBytes(s string) []byte {
return *(*[]byte)(unsafe.Pointer(
&struct {
string
int
}{s, len(s)},
))
}
func GetSpannerCurrentTime(client *spanner.Client) time.Time {
in := &QuerySpannerSingleInput{
Client: client,
Query: "select current_timestamp() as now",
}
rows, err := QuerySpannerSingle(context.Background(), in)
if err != nil {
return time.Time{}
}
type nowT struct {
Now spanner.NullTime
}
for _, row := range rows {
var v nowT
err = row.ToStruct(&v)
if err != nil {
return time.Time{}
}
return v.Now.Time
}
return time.Time{}
}
// LockTimeDiff returns the difference between lock's start time + duration against
// Spanner's current time. Negative means already expired, else still active.
func LockTimeDiff(client *spanner.Client, start string, duration int64) (int64, error) {
var q strings.Builder
fmt.Fprintf(&q, "select timestamp_diff(timestamp_add(timestamp '%v', ", start)
fmt.Fprintf(&q, "interval %v second), current_timestamp, second) as diff", duration)
in := &QuerySpannerSingleInput{
Client: client,
Query: q.String(),
}
rows, err := QuerySpannerSingle(context.Background(), in)
if err != nil {
return 0, err
}
type diffT struct {
Diff spanner.NullInt64
}
for _, row := range rows {
var v diffT
err = row.ToStruct(&v)
if err != nil {
return 0, err
}
return v.Diff.Int64, nil
}
return 0, fmt.Errorf("Internal: something is wrong.")
}
func GetSnapshot(object string) ([]byte, error) {
bucket := "alphaus-tokyo"
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
return nil, fmt.Errorf("storage.NewClient: %w", err)
}
defer client.Close()
ctx, cancel := context.WithTimeout(ctx, time.Second*50)
defer cancel()
rc, err := client.Bucket(bucket).Object(object).NewReader(ctx)
if err != nil {
return nil, fmt.Errorf("Object(%q).NewReader: %w", object, err)
}
defer rc.Close()
data, err := io.ReadAll(rc)
if err != nil {
return nil, fmt.Errorf("io.ReadAll: %w", err)
}
return data, nil
}
func PutSnapshot(object string, data []byte) error {
bucket := "alphaus-tokyo"
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
return fmt.Errorf("storage.NewClient: %w", err)
}
rdr := bytes.NewReader(data)
ctx, cancel := context.WithTimeout(ctx, time.Second*50)
defer cancel()
o := client.Bucket(bucket).Object(object)
// Optional: set a generation-match precondition to avoid potential race
// conditions and data corruptions. The request to upload is aborted if the
// object's generation number does not match your precondition.
// For an object that does not yet exist, set the DoesNotExist precondition.
o = o.If(storage.Conditions{DoesNotExist: true})
// If the live object already exists in your bucket, set instead a
// generation-match precondition using the live object's generation number.
// attrs, err := o.Attrs(ctx)
// if err != nil {
// return fmt.Errorf("object.Attrs: %w", err)
// }
// o = o.If(storage.Conditions{GenerationMatch: attrs.Generation})
// Upload an object with storage.Writer.
wc := o.NewWriter(ctx)
if _, err = io.Copy(wc, rdr); err != nil {
return fmt.Errorf("io.Copy: %w", err)
}
if err := wc.Close(); err != nil {
return fmt.Errorf("Writer.Close: %w", err)
}
return nil
}