Skip to content

Commit

Permalink
Merge branch 'generation_ttl_consistent'
Browse files Browse the repository at this point in the history
  • Loading branch information
khaf committed Dec 1, 2015
2 parents f6db846 + b832227 commit 1192d9a
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 33 deletions.
8 changes: 4 additions & 4 deletions batch_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ func (cmd *baseMultiCommand) parseRecordResults(ifc command, receiveSize int) (b
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))

Expand Down Expand Up @@ -264,8 +264,8 @@ func (cmd *baseMultiCommand) parseObject(
obj reflect.Value,
opCount int,
fieldCount int,
generation int,
expiration int,
generation uint32,
expiration uint32,
) error {
for i := 0; i < opCount; i++ {
if err := cmd.readBytes(8); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions batch_command_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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++ {
Expand Down
12 changes: 6 additions & 6 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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
Expand All @@ -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),
Expand All @@ -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(),
Expand All @@ -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
Expand All @@ -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))
})

Expand Down
6 changes: 3 additions & 3 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,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 {
Expand Down Expand Up @@ -759,8 +759,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
Expand Down
12 changes: 6 additions & 6 deletions read_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions read_header_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions types/epoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
11 changes: 11 additions & 0 deletions utils/buffer/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down
8 changes: 4 additions & 4 deletions write_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,24 @@ 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.
SendKey bool
}

// 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,
Expand Down

0 comments on commit 1192d9a

Please sign in to comment.