From b83222709a01a0209043f60e71cc5ceb77f465db Mon Sep 17 00:00:00 2001 From: Khosrow Afroozeh Date: Mon, 30 Nov 2015 17:50:15 +0100 Subject: [PATCH] make Generation and Expiration values consistent for WritePolicy and Record --- batch_command_get.go | 6 +++--- client_test.go | 12 ++++++------ command.go | 6 +++--- query_record_command.go | 4 ++-- read_command.go | 12 ++++++------ read_header_command.go | 4 ++-- record.go | 6 +++--- scan_command.go | 4 ++-- types/epoc.go | 4 ++-- utils/buffer/buffer.go | 11 +++++++++++ write_policy.go | 8 ++++---- 11 files changed, 44 insertions(+), 33 deletions(-) diff --git a/batch_command_get.go b/batch_command_get.go index fca51b00..94dd16af 100644 --- a/batch_command_get.go +++ b/batch_command_get.go @@ -87,8 +87,8 @@ func (cmd *batchCommandGet) parseRecordResults(ifc command, receiveSize int) (bo return false, nil } - generation := int(Buffer.BytesToUint32(cmd.dataBuffer, 6)) - expiration := TTL(int(Buffer.BytesToUint32(cmd.dataBuffer, 10))) + generation := Buffer.BytesToUint32(cmd.dataBuffer, 6) + expiration := TTL(Buffer.BytesToUint32(cmd.dataBuffer, 10)) fieldCount := int(Buffer.BytesToUint16(cmd.dataBuffer, 18)) opCount := int(Buffer.BytesToUint16(cmd.dataBuffer, 20)) key, err := cmd.parseKey(fieldCount) @@ -114,7 +114,7 @@ func (cmd *batchCommandGet) parseRecordResults(ifc command, receiveSize int) (bo // Parses the given byte buffer and populate the result object. // Returns the number of bytes that were parsed from the given buffer. -func (cmd *batchCommandGet) parseRecord(key *Key, opCount int, generation int, expiration int) (*Record, error) { +func (cmd *batchCommandGet) parseRecord(key *Key, opCount int, generation, expiration uint32) (*Record, error) { bins := make(map[string]interface{}, opCount) for i := 0; i < opCount; i++ { diff --git a/client_test.go b/client_test.go index d4adb836..3c5cc8f1 100644 --- a/client_test.go +++ b/client_test.go @@ -828,7 +828,7 @@ var _ = Describe("Aerospike", func() { for idx, rec := range records { if exList[idx].shouldExist { Expect(rec.Bins[bin.Name]).To(BeNil()) - Expect(rec.Generation).To(Equal(2)) + Expect(rec.Generation).To(Equal(uint32(2))) } else { Expect(rec).To(BeNil()) } @@ -935,7 +935,7 @@ var _ = Describe("Aerospike", func() { Expect(rec.Bins[bin1.Name]).To(Equal(bin1.Value.GetObject().(int))) Expect(rec.Bins[bin2.Name]).To(Equal(bin2.Value.GetObject().(string))) - Expect(rec.Generation).To(Equal(1)) + Expect(rec.Generation).To(Equal(uint32(1))) ops2 := []*Operation{ AddOp(bin1), // double the value of the bin @@ -948,7 +948,7 @@ var _ = Describe("Aerospike", func() { Expect(rec.Bins[bin1.Name]).To(Equal(bin1.Value.GetObject().(int) * 2)) Expect(rec.Bins[bin2.Name]).To(Equal(strings.Repeat(bin2.Value.GetObject().(string), 2))) - Expect(rec.Generation).To(Equal(2)) + Expect(rec.Generation).To(Equal(uint32(2))) ops3 := []*Operation{ AddOp(bin1), @@ -962,7 +962,7 @@ var _ = Describe("Aerospike", func() { Expect(rec.Bins[bin1.Name]).To(Equal(bin1.Value.GetObject().(int) * 3)) Expect(rec.Bins[bin2.Name]).To(Equal(strings.Repeat(bin2.Value.GetObject().(string), 3))) - Expect(rec.Generation).To(Equal(3)) + Expect(rec.Generation).To(Equal(uint32(3))) ops4 := []*Operation{ TouchOp(), @@ -972,7 +972,7 @@ var _ = Describe("Aerospike", func() { rec, err = client.Operate(nil, key, ops4...) Expect(err).ToNot(HaveOccurred()) - Expect(rec.Generation).To(Equal(4)) + Expect(rec.Generation).To(Equal(uint32(4))) Expect(len(rec.Bins)).To(Equal(0)) // GetOp should override GetHEaderOp @@ -984,7 +984,7 @@ var _ = Describe("Aerospike", func() { rec, err = client.Operate(nil, key, ops5...) Expect(err).ToNot(HaveOccurred()) - Expect(rec.Generation).To(Equal(4)) + Expect(rec.Generation).To(Equal(uint32(4))) Expect(len(rec.Bins)).To(Equal(2)) }) diff --git a/command.go b/command.go index 1bb61590..4848eeed 100644 --- a/command.go +++ b/command.go @@ -714,7 +714,7 @@ func (cmd *baseCommand) writeHeader(policy *BasePolicy, readAttr int, writeAttr // Header write for write operations. func (cmd *baseCommand) writeHeaderWithPolicy(policy *WritePolicy, readAttr int, writeAttr int, fieldCount int, operationCount int) { // Set flags. - generation := int32(0) + generation := uint32(0) infoAttr := 0 switch policy.RecordExistsAction { @@ -754,8 +754,8 @@ func (cmd *baseCommand) writeHeaderWithPolicy(policy *WritePolicy, readAttr int, cmd.dataBuffer[11] = byte(infoAttr) cmd.dataBuffer[12] = 0 // unused cmd.dataBuffer[13] = 0 // clear the result code - Buffer.Int32ToBytes(generation, cmd.dataBuffer, 14) - Buffer.Int32ToBytes(policy.Expiration, cmd.dataBuffer, 18) + Buffer.Uint32ToBytes(generation, cmd.dataBuffer, 14) + Buffer.Uint32ToBytes(policy.Expiration, cmd.dataBuffer, 18) // Initialize timeout. It will be written later. cmd.dataBuffer[22] = 0 diff --git a/query_record_command.go b/query_record_command.go index 940f1104..64af2d2d 100644 --- a/query_record_command.go +++ b/query_record_command.go @@ -64,8 +64,8 @@ func (cmd *queryRecordCommand) parseRecordResults(ifc command, receiveSize int) return false, nil } - generation := int(Buffer.BytesToUint32(cmd.dataBuffer, 6)) - expiration := TTL(int(Buffer.BytesToUint32(cmd.dataBuffer, 10))) + generation := Buffer.BytesToUint32(cmd.dataBuffer, 6) + expiration := TTL(Buffer.BytesToUint32(cmd.dataBuffer, 10)) fieldCount := int(Buffer.BytesToUint16(cmd.dataBuffer, 18)) opCount := int(Buffer.BytesToUint16(cmd.dataBuffer, 20)) diff --git a/read_command.go b/read_command.go index b3d23379..894152eb 100644 --- a/read_command.go +++ b/read_command.go @@ -67,8 +67,8 @@ func (cmd *readCommand) parseResult(ifc command, conn *Connection) error { sz := Buffer.BytesToInt64(cmd.dataBuffer, 0) headerLength := int(cmd.dataBuffer[8]) resultCode := ResultCode(cmd.dataBuffer[13] & 0xFF) - generation := int(Buffer.BytesToUint32(cmd.dataBuffer, 14)) - expiration := TTL(int(Buffer.BytesToUint32(cmd.dataBuffer, 18))) + generation := Buffer.BytesToUint32(cmd.dataBuffer, 14) + expiration := TTL(Buffer.BytesToUint32(cmd.dataBuffer, 18)) fieldCount := int(Buffer.BytesToUint16(cmd.dataBuffer, 26)) // almost certainly 0 opCount := int(Buffer.BytesToUint16(cmd.dataBuffer, 28)) receiveSize := int((sz & 0xFFFFFFFFFFFF) - int64(headerLength)) @@ -129,8 +129,8 @@ func (cmd *readCommand) handleUdfError(resultCode ResultCode) error { func (cmd *readCommand) parseRecord( opCount int, fieldCount int, - generation int, - expiration int, + generation uint32, + expiration uint32, ) (*Record, error) { var bins BinMap receiveOffset := 0 @@ -170,8 +170,8 @@ func (cmd *readCommand) parseRecord( func (cmd *readCommand) parseObject( opCount int, fieldCount int, - generation int, - expiration int, + generation uint32, + expiration uint32, ) error { receiveOffset := 0 diff --git a/read_header_command.go b/read_header_command.go index 1f792480..783b7748 100644 --- a/read_header_command.go +++ b/read_header_command.go @@ -52,8 +52,8 @@ func (cmd *readHeaderCommand) parseResult(ifc command, conn *Connection) error { resultCode := cmd.dataBuffer[13] & 0xFF if resultCode == 0 { - generation := int(Buffer.BytesToUint32(cmd.dataBuffer, 14)) - expiration := TTL(int(Buffer.BytesToUint32(cmd.dataBuffer, 18))) + generation := Buffer.BytesToUint32(cmd.dataBuffer, 14) + expiration := TTL(Buffer.BytesToUint32(cmd.dataBuffer, 18)) cmd.record = newRecord(cmd.node, cmd.key, nil, generation, expiration) } else { if ResultCode(resultCode) == KEY_NOT_FOUND_ERROR { diff --git a/record.go b/record.go index 473413a3..8ab758c1 100644 --- a/record.go +++ b/record.go @@ -32,14 +32,14 @@ type Record struct { Bins BinMap // Generation shows record modification count. - Generation int + Generation uint32 // Expiration is TTL (Time-To-Live). // Number of seconds until record expires. - Expiration int + Expiration uint32 } -func newRecord(node *Node, key *Key, bins BinMap, generation int, expiration int) *Record { +func newRecord(node *Node, key *Key, bins BinMap, generation, expiration uint32) *Record { r := &Record{ Node: node, Key: key, diff --git a/scan_command.go b/scan_command.go index 0a07ac6f..fcfaf207 100644 --- a/scan_command.go +++ b/scan_command.go @@ -86,8 +86,8 @@ func (cmd *scanCommand) parseRecordResults(ifc command, receiveSize int) (bool, return false, nil } - generation := int(Buffer.BytesToUint32(cmd.dataBuffer, 6)) - expiration := TTL(int(Buffer.BytesToUint32(cmd.dataBuffer, 10))) + generation := Buffer.BytesToUint32(cmd.dataBuffer, 6) + expiration := TTL(Buffer.BytesToUint32(cmd.dataBuffer, 10)) fieldCount := int(Buffer.BytesToUint16(cmd.dataBuffer, 18)) opCount := int(Buffer.BytesToUint16(cmd.dataBuffer, 20)) diff --git a/types/epoc.go b/types/epoc.go index 08af57d4..a262c0d6 100644 --- a/types/epoc.go +++ b/types/epoc.go @@ -10,6 +10,6 @@ const ( ) // TTL converts an Expiration time from citrusleaf epoc to TTL in seconds. -func TTL(secsFromCitrusLeafEpoc int) int { - return int(int64(CITRUSLEAF_EPOCH+secsFromCitrusLeafEpoc) - time.Now().Unix()) +func TTL(secsFromCitrusLeafEpoc uint32) uint32 { + return uint32(int64(CITRUSLEAF_EPOCH+secsFromCitrusLeafEpoc) - time.Now().Unix()) } diff --git a/utils/buffer/buffer.go b/utils/buffer/buffer.go index 57e0b3fd..ba729505 100644 --- a/utils/buffer/buffer.go +++ b/utils/buffer/buffer.go @@ -145,6 +145,17 @@ func Int32ToBytes(num int32, buffer []byte, offset int) []byte { return b } +// Converts an int32 to a byte slice of size 4 +func Uint32ToBytes(num uint32, buffer []byte, offset int) []byte { + if buffer != nil { + binary.BigEndian.PutUint32(buffer[offset:], num) + return nil + } + b := make([]byte, uint32sz) + binary.BigEndian.PutUint32(b, num) + return b +} + // Converts func BytesToInt16(buf []byte, offset int) int16 { return int16(binary.BigEndian.Uint16(buf[offset : offset+uint16sz])) diff --git a/write_policy.go b/write_policy.go index 50879d80..9620a462 100644 --- a/write_policy.go +++ b/write_policy.go @@ -35,16 +35,16 @@ type WritePolicy struct { // Generation is the number of times a record has been // modified (including creation) on the server. // If a write operation is creating a record, the expected generation would be 0. - Generation int32 + Generation uint32 // Expiration determimes record expiration in seconds. Also known as TTL (Time-To-Live). // Seconds record will live before being removed by the server. // Expiration values: - // -1: Never expire for Aerospike 2 server versions >= 2.7.2 and Aerospike 3 server + // MaxUint32: Never expire for Aerospike 2 server versions >= 2.7.2 and Aerospike 3 server // versions >= 3.1.4. Do not use -1 for older servers. // 0: Default to namespace configuration variable "default-ttl" on the server. // > 0: Actual expiration in seconds. - Expiration int32 + Expiration uint32 // Send user defined key in addition to hash digest on a record put. // The default is to not send the user defined key. @@ -52,7 +52,7 @@ type WritePolicy struct { } // NewWritePolicy initializes a new WritePolicy instance with default parameters. -func NewWritePolicy(generation, expiration int32) *WritePolicy { +func NewWritePolicy(generation, expiration uint32) *WritePolicy { return &WritePolicy{ BasePolicy: *NewPolicy(), RecordExistsAction: UPDATE,