1// Copyright 2013-2020 Aerospike, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package aerospike
16
17import "math"
18
19const (
20	// TTLServerDefault will default to namespace configuration variable "default-ttl" on the server.
21	TTLServerDefault = 0
22	// TTLDontExpire will never expire for Aerospike 2 server versions >= 2.7.2 and Aerospike 3+ server.
23	TTLDontExpire = math.MaxUint32
24	// TTLDontUpdate will not change the record's ttl when record is written. Supported by Aerospike server versions >= 3.10.1
25	TTLDontUpdate = math.MaxUint32 - 1
26)
27
28// WritePolicy encapsulates parameters for policy attributes used in write operations.
29// This object is passed into methods where database writes can occur.
30type WritePolicy struct {
31	BasePolicy
32
33	// RecordExistsAction qualifies how to handle writes where the record already exists.
34	RecordExistsAction RecordExistsAction //= RecordExistsAction.UPDATE;
35
36	// GenerationPolicy qualifies how to handle record writes based on record generation. The default (NONE)
37	// indicates that the generation is not used to restrict writes.
38	GenerationPolicy GenerationPolicy //= GenerationPolicy.NONE;
39
40	// Desired consistency guarantee when committing a transaction on the server. The default
41	// (COMMIT_ALL) indicates that the server should wait for master and all replica commits to
42	// be successful before returning success to the client.
43	CommitLevel CommitLevel //= COMMIT_ALL
44
45	// Generation determines expected generation.
46	// Generation is the number of times a record has been
47	// modified (including creation) on the server.
48	// If a write operation is creating a record, the expected generation would be 0.
49	Generation uint32
50
51	// Expiration determines record expiration in seconds. Also known as TTL (Time-To-Live).
52	// Seconds record will live before being removed by the server.
53	// Expiration values:
54	// TTLServerDefault (0): Default to namespace configuration variable "default-ttl" on the server.
55	// TTLDontExpire (MaxUint32): Never expire for Aerospike 2 server versions >= 2.7.2 and Aerospike 3+ server
56	// TTLDontUpdate (MaxUint32 - 1): Do not change ttl when record is written. Supported by Aerospike server versions >= 3.10.1
57	// > 0: Actual expiration in seconds.
58	Expiration uint32
59
60	// RespondPerEachOp defines for client.Operate() method, return a result for every operation.
61	// Some list operations do not return results by default (ListClearOp() for example).
62	// This can sometimes make it difficult to determine the desired result offset in the returned
63	// bin's result list.
64	//
65	// Setting RespondPerEachOp to true makes it easier to identify the desired result offset
66	// (result offset equals bin's operate sequence). This only makes sense when multiple list
67	// operations are used in one operate call and some of those operations do not return results
68	// by default.
69	RespondPerEachOp bool
70
71	// DurableDelete leaves a tombstone for the record if the transaction results in a record deletion.
72	// This prevents deleted records from reappearing after node failures.
73	// Valid for Aerospike Server Enterprise Edition 3.10+ only.
74	DurableDelete bool
75}
76
77// NewWritePolicy initializes a new WritePolicy instance with default parameters.
78func NewWritePolicy(generation, expiration uint32) *WritePolicy {
79	res := &WritePolicy{
80		BasePolicy:         *NewPolicy(),
81		RecordExistsAction: UPDATE,
82		GenerationPolicy:   NONE,
83		CommitLevel:        COMMIT_ALL,
84		Generation:         generation,
85		Expiration:         expiration,
86	}
87
88	// Writes may not be idempotent.
89	// do not allow retries on writes by default.
90	res.MaxRetries = 0
91
92	return res
93}
94