-
Notifications
You must be signed in to change notification settings - Fork 20
/
caches.go
142 lines (129 loc) · 4.14 KB
/
caches.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
package datastore
import (
"bytes"
"context"
"fmt"
"strconv"
"strings"
)
// Middleware hooks to the Datastore's RPC and It can modify arguments and return values.
// see https://godoc.org/go.mercari.io/datastore/dsmiddleware
type Middleware interface {
// AllocateIDs intercepts AllocateIDs operation.
AllocateIDs(info *MiddlewareInfo, keys []Key) ([]Key, error)
// PutMultiWithoutTx intercepts PutMulti without Transaction operation.
PutMultiWithoutTx(info *MiddlewareInfo, keys []Key, psList []PropertyList) ([]Key, error)
// PutMultiWithTx intercepts PutMulti with Transaction operation.
PutMultiWithTx(info *MiddlewareInfo, keys []Key, psList []PropertyList) ([]PendingKey, error)
// GetMultiWithoutTx intercepts GetMulti without Transaction operation.
GetMultiWithoutTx(info *MiddlewareInfo, keys []Key, psList []PropertyList) error
// GetMultiWithTx intercepts GetMulti with Transaction operation.
GetMultiWithTx(info *MiddlewareInfo, keys []Key, psList []PropertyList) error
// DeleteMultiWithoutTx intercepts DeleteMulti without Transaction operation.
DeleteMultiWithoutTx(info *MiddlewareInfo, keys []Key) error
// DeleteMultiWithTx intercepts DeleteMulti with Transaction operation.
DeleteMultiWithTx(info *MiddlewareInfo, keys []Key) error
// PostCommit will kicked after Transaction commit.
PostCommit(info *MiddlewareInfo, tx Transaction, commit Commit) error
// PostRollback will kicked after Transaction rollback.
PostRollback(info *MiddlewareInfo, tx Transaction) error
// Run intercepts Run query operation.
Run(info *MiddlewareInfo, q Query, qDump *QueryDump) Iterator
// GetAll intercepts GetAll operation.
GetAll(info *MiddlewareInfo, q Query, qDump *QueryDump, psList *[]PropertyList) ([]Key, error)
// Next intercepts Next operation.
Next(info *MiddlewareInfo, q Query, qDump *QueryDump, iter Iterator, ps *PropertyList) (Key, error)
// Count intercepts Count operation.
Count(info *MiddlewareInfo, q Query, qDump *QueryDump) (int, error)
}
// MiddlewareInfo provides RPC's processing state.
type MiddlewareInfo struct {
Context context.Context
Client Client
Transaction Transaction
Next Middleware
}
// QueryDump provides information of executed query.
type QueryDump struct {
Kind string
Ancestor Key
EventualConsistency bool
Namespace string
Transaction Transaction
Filter []*QueryFilterCondition
Order []string
Project []string
DistinctOn []string
Distinct bool
KeysOnly bool
Limit int
Offset int
Start Cursor
End Cursor
}
func (dump *QueryDump) String() string {
// generate keys that are unique for queries
// TODO ProjectID...?
b := bytes.NewBufferString("v1:") // encoding format version
b.WriteString(dump.Kind)
if dump.Ancestor != nil {
b.WriteString("&a=")
b.WriteString(dump.Ancestor.String())
}
if dump.EventualConsistency {
b.WriteString("&e=t")
}
if dump.Namespace != "" {
b.WriteString("&n=")
b.WriteString(dump.Namespace)
}
if dump.Transaction != nil {
b.WriteString("&t=t")
}
if l := len(dump.Filter); l != 0 {
b.WriteString("&f=")
for idx, f := range dump.Filter {
b.WriteString(f.Filter)
b.WriteString(fmt.Sprintf("%+v", f.Value))
if (idx + 1) != l {
b.WriteString("|")
}
}
}
if l := len(dump.Order); l != 0 {
b.WriteString("&or=")
b.WriteString(strings.Join(dump.Order, "|"))
}
if l := len(dump.Project); l != 0 {
b.WriteString("&p=")
b.WriteString(strings.Join(dump.Project, "|"))
}
if dump.Distinct {
b.WriteString("&d=t")
}
if dump.KeysOnly {
b.WriteString("&k=t")
}
if dump.Limit != 0 {
b.WriteString("&l=")
b.WriteString(strconv.Itoa(dump.Limit))
}
if dump.Offset != 0 {
b.WriteString("&o=")
b.WriteString(strconv.Itoa(dump.Offset))
}
if dump.Start != nil {
b.WriteString("&s=")
b.WriteString(dump.Start.String())
}
if dump.End != nil {
b.WriteString("&e=")
b.WriteString(dump.End.String())
}
return b.String()
}
// QueryFilterCondition provides information of filter of query.
type QueryFilterCondition struct {
Filter string
Value interface{}
}