Skip to content

Commit 3486ecb

Browse files
committed
Added GetBinary() convenience function.
Added tests to verify Version returned from row_version query work properly with PutIfVersion.
1 parent 4ae9ee4 commit 3486ecb

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

nosqldb/data_ops_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,44 @@ func (suite *DataOpsTestSuite) TestPutGetDelete() {
303303
recordKB)
304304
}
305305

306+
// Get the version of the row using a query. Validate that the
307+
// version returned matches the version returned from the previous get.
308+
stmt = fmt.Sprintf("select row_version($t) as version from %s $t where id = 10", table)
309+
qReq := &nosqldb.QueryRequest{
310+
Statement: stmt,
311+
Consistency: types.Absolute,
312+
}
313+
qRes, err := suite.Client.Query(qReq)
314+
results, rerr := qRes.GetResults()
315+
ver, _ := results[0].GetBinary("version")
316+
if suite.NoError(err) && suite.NoError(rerr) {
317+
suite.checkGetResult(getReq, getRes,
318+
true, // rowPresent
319+
true, // checkFieldOrder
320+
nil, // expected value
321+
types.Version(ver), // expected version
322+
recordKB)
323+
}
324+
325+
// Update the row with a PutIfVersion using the version returned by the
326+
// row_version query. This should succeed.
327+
putReq = &nosqldb.PutRequest{
328+
TableName: table,
329+
Value: value,
330+
PutOption: types.PutIfVersion,
331+
MatchVersion: types.Version(ver),
332+
}
333+
putRes, err = suite.Client.Put(putReq)
334+
if suite.NoError(err) {
335+
suite.checkPutResult(putReq, putRes,
336+
true, // shouldSucceed
337+
false, // rowPresent
338+
nil, // expPrevValue
339+
nil, // expPrevVersion
340+
recordKB)
341+
}
342+
343+
306344
// Get non-existing row
307345
key.Put("id", 100)
308346
getReq = &nosqldb.GetRequest{

nosqldb/types/values.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,19 @@ func (m *MapValue) GetString(k string) (s string, ok bool) {
362362
return
363363
}
364364

365+
// GetBinary returns the binary value b associated with the specified key k.
366+
// If the value does not exist, or is not a binary value, this method returns
367+
// an empty byte array and sets ok to false.
368+
func (m *MapValue) GetBinary(k string) (b []byte, ok bool) {
369+
v, ok := m.Get(k)
370+
if !ok {
371+
return
372+
}
373+
374+
b, ok = v.([]byte)
375+
return
376+
}
377+
365378
// GetInt returns the int value i associated with the specified key k.
366379
// If the value does not exist, or is not an int value, this method returns 0
367380
// and sets ok to false.

0 commit comments

Comments
 (0)