-
Notifications
You must be signed in to change notification settings - Fork 774
/
namespace.go
234 lines (212 loc) · 6.58 KB
/
namespace.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package mysql
import (
"context"
"database/sql"
"errors"
"go.temporal.io/api/serviceerror"
"go.temporal.io/server/common/persistence/sql/sqlplugin"
)
const (
createNamespaceQuery = `INSERT INTO
namespaces (partition_id, id, name, is_global, data, data_encoding, notification_version)
VALUES(?, ?, ?, ?, ?, ?, ?)`
updateNamespaceQuery = `UPDATE namespaces
SET name = ?, data = ?, data_encoding = ?, is_global = ?, notification_version = ?
WHERE partition_id=54321 AND id = ?`
getNamespacePart = `SELECT id, name, is_global, data, data_encoding, notification_version FROM namespaces`
getNamespaceByIDQuery = getNamespacePart + ` WHERE partition_id=? AND id = ?`
getNamespaceByNameQuery = getNamespacePart + ` WHERE partition_id=? AND name = ?`
listNamespacesQuery = getNamespacePart + ` WHERE partition_id=? ORDER BY id LIMIT ?`
listNamespacesRangeQuery = getNamespacePart + ` WHERE partition_id=? AND id > ? ORDER BY id LIMIT ?`
deleteNamespaceByIDQuery = `DELETE FROM namespaces WHERE partition_id=? AND id = ?`
deleteNamespaceByNameQuery = `DELETE FROM namespaces WHERE partition_id=? AND name = ?`
getNamespaceMetadataQuery = `SELECT notification_version FROM namespace_metadata WHERE partition_id = 54321`
lockNamespaceMetadataQuery = `SELECT notification_version FROM namespace_metadata WHERE partition_id = 54321 FOR UPDATE`
updateNamespaceMetadataQuery = `UPDATE namespace_metadata SET notification_version = ? WHERE notification_version = ? AND partition_id = 54321`
)
const (
partitionID = 54321
)
var errMissingArgs = errors.New("missing one or more args for API")
// InsertIntoNamespace inserts a single row into namespaces table
func (mdb *db) InsertIntoNamespace(
ctx context.Context,
row *sqlplugin.NamespaceRow,
) (sql.Result, error) {
return mdb.conn.ExecContext(ctx,
createNamespaceQuery,
partitionID,
row.ID,
row.Name,
row.IsGlobal,
row.Data,
row.DataEncoding,
row.NotificationVersion,
)
}
// UpdateNamespace updates a single row in namespaces table
func (mdb *db) UpdateNamespace(
ctx context.Context,
row *sqlplugin.NamespaceRow,
) (sql.Result, error) {
return mdb.conn.ExecContext(ctx,
updateNamespaceQuery,
row.Name,
row.Data,
row.DataEncoding,
row.IsGlobal,
row.NotificationVersion,
row.ID,
)
}
// SelectFromNamespace reads one or more rows from namespaces table
func (mdb *db) SelectFromNamespace(
ctx context.Context,
filter sqlplugin.NamespaceFilter,
) ([]sqlplugin.NamespaceRow, error) {
switch {
case filter.ID != nil || filter.Name != nil:
if filter.ID != nil && filter.Name != nil {
return nil, serviceerror.NewInternal("only ID or name filter can be specified for selection")
}
return mdb.selectFromNamespace(ctx, filter)
case filter.PageSize != nil && *filter.PageSize > 0:
return mdb.selectAllFromNamespace(ctx, filter)
default:
return nil, errMissingArgs
}
}
func (mdb *db) selectFromNamespace(
ctx context.Context,
filter sqlplugin.NamespaceFilter,
) ([]sqlplugin.NamespaceRow, error) {
var err error
var row sqlplugin.NamespaceRow
switch {
case filter.ID != nil:
err = mdb.conn.GetContext(ctx,
&row,
getNamespaceByIDQuery,
partitionID,
*filter.ID,
)
case filter.Name != nil:
err = mdb.conn.GetContext(ctx,
&row,
getNamespaceByNameQuery,
partitionID,
*filter.Name,
)
}
if err != nil {
return nil, err
}
return []sqlplugin.NamespaceRow{row}, nil
}
func (mdb *db) selectAllFromNamespace(
ctx context.Context,
filter sqlplugin.NamespaceFilter,
) ([]sqlplugin.NamespaceRow, error) {
var err error
var rows []sqlplugin.NamespaceRow
switch {
case filter.GreaterThanID != nil:
err = mdb.conn.SelectContext(ctx,
&rows,
listNamespacesRangeQuery,
partitionID,
*filter.GreaterThanID,
*filter.PageSize,
)
default:
err = mdb.conn.SelectContext(ctx,
&rows,
listNamespacesQuery,
partitionID,
filter.PageSize,
)
}
return rows, err
}
// DeleteFromNamespace deletes a single row in namespaces table
func (mdb *db) DeleteFromNamespace(
ctx context.Context,
filter sqlplugin.NamespaceFilter,
) (sql.Result, error) {
var err error
var result sql.Result
switch {
case filter.ID != nil:
result, err = mdb.conn.ExecContext(ctx,
deleteNamespaceByIDQuery,
partitionID,
filter.ID,
)
default:
result, err = mdb.conn.ExecContext(ctx,
deleteNamespaceByNameQuery,
partitionID,
filter.Name,
)
}
return result, err
}
// LockNamespaceMetadata acquires a write lock on a single row in namespace_metadata table
func (mdb *db) LockNamespaceMetadata(
ctx context.Context,
) (*sqlplugin.NamespaceMetadataRow, error) {
var row sqlplugin.NamespaceMetadataRow
err := mdb.conn.GetContext(ctx,
&row.NotificationVersion,
lockNamespaceMetadataQuery,
)
if err != nil {
return nil, err
}
return &row, nil
}
// SelectFromNamespaceMetadata reads a single row in namespace_metadata table
func (mdb *db) SelectFromNamespaceMetadata(
ctx context.Context,
) (*sqlplugin.NamespaceMetadataRow, error) {
var row sqlplugin.NamespaceMetadataRow
err := mdb.conn.GetContext(ctx,
&row.NotificationVersion,
getNamespaceMetadataQuery,
)
return &row, err
}
// UpdateNamespaceMetadata updates a single row in namespace_metadata table
func (mdb *db) UpdateNamespaceMetadata(
ctx context.Context,
row *sqlplugin.NamespaceMetadataRow,
) (sql.Result, error) {
return mdb.conn.ExecContext(ctx,
updateNamespaceMetadataQuery,
row.NotificationVersion+1,
row.NotificationVersion,
)
}