-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.go
245 lines (220 loc) · 7.27 KB
/
db.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
235
236
237
238
239
240
241
242
243
244
245
// Copyright 2015 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package client
import (
"bytes"
"fmt"
"golang.org/x/net/context"
"github.com/journeymidnight/nentropy/helper"
"github.com/journeymidnight/nentropy/osd/multiraftbase"
)
// KeyValue represents a single key/value pair. This is similar to
// roachpb.KeyValue except that the value may be nil.
type KeyValue struct {
Key multiraftbase.Key
Value *multiraftbase.Value // Timestamp will always be zero
}
func (kv *KeyValue) String() string {
return kv.Key.String() + "=" + kv.PrettyValue()
}
// Exists returns true iff the value exists.
func (kv *KeyValue) Exists() bool {
return kv.Value != nil
}
// PrettyValue returns a human-readable version of the value as a string.
func (kv *KeyValue) PrettyValue() string {
if kv.Value == nil {
return "nil"
}
return fmt.Sprintf("%v", kv.Value)
}
// ValueInt returns the value decoded as an int64. This method will panic if
// the value cannot be decoded as an int64.
//func (kv *KeyValue) ValueInt() int64 {
// if kv.Value == nil {
// return 0
// }
// i, err := kv.Value.GetInt()
// if err != nil {
// panic(err)
// }
// return i
//}
// Result holds the result for a single DB or Txn operation (e.g. Get, Put,
// etc).
type Result struct {
calls int
// Err contains any error encountered when performing the operation.
Err error
// Rows contains the key/value pairs for the operation. The number of rows
// returned varies by operation. For Get, Put, CPut, Inc and Del the number
// of rows returned is the number of keys operated on. For Scan the number of
// rows returned is the number or rows matching the scan capped by the
// maxRows parameter. For DelRange Rows is nil.
Rows []KeyValue
// Keys is set by some operations instead of returning the rows themselves.
Keys []multiraftbase.Key
PgInfos []multiraftbase.PgInfo
}
func (r Result) String() string {
if r.Err != nil {
return r.Err.Error()
}
var buf bytes.Buffer
for i, row := range r.Rows {
if i > 0 {
buf.WriteString("\n")
}
fmt.Fprintf(&buf, "%d: %s", i, &row)
}
return buf.String()
}
// DB is a database handle to a single cockroach cluster. A DB is safe for
// concurrent use by multiple goroutines.
type DB struct {
sender Sender
}
// GetSender returns the underlying Sender. Only exported for tests.
func (db *DB) GetSender() Sender {
return db.sender
}
// NewDB returns a new DB.
func NewDB(sender Sender) *DB {
return &DB{
sender: sender,
}
}
// Get retrieves the value for a key, returning the retrieved key/value or an
// error. It is not considered an error for the key not to exist.
//
// r, err := db.Get("a")
// // string(r.Key) == "a"
//
// key can be either a byte slice or a string.
func (db *DB) Get(ctx context.Context, key multiraftbase.Key) (KeyValue, error) {
b := &Batch{}
b.Get(key)
return getOneRow(db.Run(ctx, b), b)
}
// Put sets the value for a key.
//
// key can be either a byte slice or a string. value can be any key type, a
// proto.Message or any Go primitive type (bool, int, etc).
func (db *DB) Put(ctx context.Context, key multiraftbase.Key, value multiraftbase.Value) error {
b := &Batch{}
b.Put(key, value)
return getOneErr(db.Run(ctx, b), b)
}
// Del deletes one or more keys.
//
// key can be either a byte slice or a string.
func (db *DB) Del(ctx context.Context, key multiraftbase.Key) error {
b := &Batch{}
//b.Del(keys...)
return getOneErr(db.Run(ctx, b), b)
}
// sendAndFill is a helper which sends the given batch and fills its results,
// returning the appropriate error which is either from the first failing call,
// or an "internal" error.
func sendAndFill(ctx context.Context, send SenderFunc, b *Batch) error {
// Errors here will be attached to the results, so we will get them from
// the call to fillResults in the regular case in which an individual call
// fails. But send() also returns its own errors, so there's some dancing
// here to do because we want to run fillResults() so that the individual
// result gets initialized with an error from the corresponding call.
var ba multiraftbase.BatchRequest
ba.Request = b.req
ba.Header = b.Header
b.response, b.pErr = send(ctx, ba)
if b.pErr != nil {
_ = b.fillResults()
return b.pErr.GoError()
}
if err := b.fillResults(); err != nil {
b.pErr = multiraftbase.NewError(err)
return err
}
return nil
}
// Run executes the operations queued up within a batch. Before executing any
// of the operations the batch is first checked to see if there were any errors
// during its construction (e.g. failure to marshal a proto message).
//
// The operations within a batch are run in parallel and the order is
// non-deterministic. It is an unspecified behavior to modify and retrieve the
// same key within a batch.
//
// Upon completion, Batch.Results will contain the results for each
// operation. The order of the results matches the order the operations were
// added to the batch.
func (db *DB) Run(ctx context.Context, b *Batch) error {
if err := b.prepare(); err != nil {
return err
}
return sendAndFill(ctx, db.send, b)
}
// send runs the specified calls synchronously in a single batch and returns
// any errors. Returns (nil, nil) for an empty batch.
func (db *DB) send(
ctx context.Context, ba multiraftbase.BatchRequest,
) (*multiraftbase.BatchResponse, *multiraftbase.Error) {
m := ba.Request.GetValue().(multiraftbase.Request)
switch m.Method() {
case multiraftbase.Get:
case multiraftbase.Put:
case multiraftbase.Delete:
case multiraftbase.TruncateLog:
case multiraftbase.ChangeConf:
default:
return nil, multiraftbase.NewErrorf("method %s not allowed with INCONSISTENT batch", m)
}
br, pErr := db.sender.Send(ctx, ba)
if pErr != nil {
helper.Printf(5, "failed batch: %s", pErr)
return nil, pErr
}
return br, nil
}
// getOneErr returns the error for a single-request Batch that was run.
// runErr is the error returned by Run, b is the Batch that was passed to Run.
func getOneErr(runErr error, b *Batch) error {
if runErr != nil && len(b.Results) > 0 {
return b.Results[0].Err
}
return runErr
}
// getOneResult returns the result for a single-request Batch that was run.
// runErr is the error returned by Run, b is the Batch that was passed to Run.
func getOneResult(runErr error, b *Batch) (Result, error) {
if runErr != nil {
if len(b.Results) > 0 {
return b.Results[0], b.Results[0].Err
}
return Result{Err: runErr}, runErr
}
res := b.Results[0]
if res.Err != nil {
panic("run succeeded even through the result has an error")
}
return res, nil
}
// getOneRow returns the first row for a single-request Batch that was run.
// runErr is the error returned by Run, b is the Batch that was passed to Run.
func getOneRow(runErr error, b *Batch) (KeyValue, error) {
res, err := getOneResult(runErr, b)
if err != nil {
return KeyValue{}, err
}
return res.Rows[0], nil
}