14
14
15
15
package aerospike
16
16
17
+ import pc "github.com/aerospike/aerospike-client-go/v8/internal/cache"
18
+
17
19
// BatchDeletePolicy is used in batch delete commands.
18
20
type BatchDeletePolicy struct {
19
21
// FilterExpression is optional expression filter. If FilterExpression exists and evaluates to false, the specific batch key
@@ -58,6 +60,17 @@ func NewBatchDeletePolicy() *BatchDeletePolicy {
58
60
}
59
61
}
60
62
63
+ func NewBatchDeletePolicyOrDefaultFromCache (dynConfig * DynConfig ) * BatchDeletePolicy {
64
+ if dynConfig == nil {
65
+ return NewBatchDeletePolicy ()
66
+ }
67
+
68
+ dynConfig .lock .RLock ()
69
+ defer dynConfig .lock .RUnlock ()
70
+
71
+ return dynConfig .mappedPolicies .Get (pc .BATCH_DELETE_POLICY ).(* BatchDeletePolicy )
72
+ }
73
+
61
74
func (bdp * BatchDeletePolicy ) toWritePolicy (bp * BatchPolicy ) * WritePolicy {
62
75
wp := bp .toWritePolicy ()
63
76
@@ -73,3 +86,115 @@ func (bdp *BatchDeletePolicy) toWritePolicy(bp *BatchPolicy) *WritePolicy {
73
86
}
74
87
return wp
75
88
}
89
+
90
+ func (bdp * BatchDeletePolicy ) toWritePolicyWithConfig (bp * BatchPolicy , dynConfig * DynConfig ) * WritePolicy {
91
+ wp := bp .toWritePolicy ()
92
+
93
+ if bdp != nil {
94
+ if bdp .FilterExpression != nil {
95
+ wp .FilterExpression = bdp .FilterExpression
96
+ }
97
+ wp .CommitLevel = bdp .CommitLevel
98
+ wp .GenerationPolicy = bdp .GenerationPolicy
99
+ wp .Generation = bdp .Generation
100
+ wp .DurableDelete = bdp .DurableDelete
101
+ wp .SendKey = bdp .SendKey
102
+ }
103
+
104
+ // In Case dynConfig is not initialized or running return the policy before
105
+ // merge
106
+ if dynConfig == nil {
107
+ return wp
108
+ }
109
+
110
+ dynConfig .lock .RLock ()
111
+ defer dynConfig .lock .RUnlock ()
112
+
113
+ config := dynConfig .config
114
+ if config != nil && config .Dynamic .BatchDelete != nil {
115
+ if config .Dynamic .BatchDelete .DurableDelete != nil {
116
+ wp .DurableDelete = * config .Dynamic .BatchDelete .DurableDelete
117
+ }
118
+ if config .Dynamic .BatchDelete .SendKey != nil {
119
+ wp .SendKey = * config .Dynamic .BatchDelete .SendKey
120
+ }
121
+ }
122
+
123
+ return wp
124
+ }
125
+
126
+ // copyBatchDeletePolicy creates a new BasePolicy instance and copies the values from the source BatchDeletePolicy.
127
+ func copyBatchDeletePolicy (src * BatchDeletePolicy ) * BatchDeletePolicy {
128
+ if src == nil {
129
+ return nil
130
+ }
131
+
132
+ response := NewBatchDeletePolicy ()
133
+
134
+ response .FilterExpression = src .FilterExpression
135
+ response .FilterExpression = src .FilterExpression
136
+ response .CommitLevel = src .CommitLevel
137
+ response .GenerationPolicy = src .GenerationPolicy
138
+ response .Generation = src .Generation
139
+ response .DurableDelete = src .DurableDelete
140
+ response .SendKey = src .SendKey
141
+
142
+ return response
143
+ }
144
+
145
+ // applyConfigToBatchDeletePolicy applies the dynamic configuration and generates a new policy
146
+ func applyConfigToBatchDeletePolicy (policy * BatchDeletePolicy , dynConfig * DynConfig ) * BatchDeletePolicy {
147
+ if dynConfig == nil {
148
+ return policy
149
+ }
150
+
151
+ config := dynConfig .config
152
+
153
+ if config == nil && ! dynConfig .configInitialized .Load () {
154
+ // On initial load it is possible that the config is not yet loaded. This will kick things off to make sure
155
+ // config is loaded.
156
+ dynConfig .loadConfig ()
157
+ config = dynConfig .config
158
+ }
159
+
160
+ dynConfig .lock .RLock ()
161
+ defer dynConfig .lock .RUnlock ()
162
+
163
+ if config != nil && config .Dynamic != nil && config .Dynamic .BatchDelete != nil {
164
+ // Dynamic configuration is exists for policy in question.
165
+ var responsePolicy * BatchDeletePolicy
166
+ // User has provided a custom policy. We need to apply the dynamic configuration.
167
+ if policy != nil {
168
+ // Copy the existing write policy to preserve any custom settings.
169
+ responsePolicy = copyBatchDeletePolicy (policy )
170
+ responsePolicy = mapDynamicBatchDeletePolicy (responsePolicy , dynConfig )
171
+
172
+ return responsePolicy
173
+ } else {
174
+ // Passed in policy is nil, fetch mapped default policy from cache.
175
+ responsePolicy = dynConfig .mappedPolicies .Get (pc .BATCH_DELETE_POLICY ).(* BatchDeletePolicy )
176
+
177
+ // If we have found entry in cache no need to map again return it.
178
+ return responsePolicy
179
+ }
180
+ } else {
181
+ return policy
182
+ }
183
+ }
184
+
185
+ func mapDynamicBatchDeletePolicy (policy * BatchDeletePolicy , dynConfig * DynConfig ) * BatchDeletePolicy {
186
+ if dynConfig .config == nil && dynConfig .config .Dynamic == nil {
187
+ return policy
188
+ }
189
+
190
+ if dynConfig .config .Dynamic .BatchDelete != nil {
191
+ if dynConfig .config .Dynamic .BatchDelete .DurableDelete != nil {
192
+ policy .DurableDelete = * dynConfig .config .Dynamic .BatchDelete .DurableDelete
193
+ }
194
+ if dynConfig .config .Dynamic .BatchDelete .SendKey != nil {
195
+ policy .SendKey = * dynConfig .config .Dynamic .BatchDelete .SendKey
196
+ }
197
+ }
198
+
199
+ return policy
200
+ }
0 commit comments