Method: Aerospike::Client#truncate

Defined in:
lib/aerospike/client.rb

#truncate(namespace, set_name = nil, before_last_update = nil, options = {}) ⇒ Object

Removes records in the specified namespace/set efficiently.

This method is orders of magnitude faster than deleting records one at a time. It requires Aerospike Server version 3.12 or later. See www.aerospike.com/docs/reference/info#truncate for further information.

This asynchronous server call may return before the truncate is complete. The user can still write new records after the server call returns because new records will have last update times greater than the truncate cut-off (set at the time of the truncate call.)

If no policy options are provided, @default_info_policy will be used.



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/aerospike/client.rb', line 216

def truncate(namespace, set_name = nil, before_last_update = nil, options = {})
  policy = create_policy(options, Policy, default_info_policy)

  node = @cluster.random_node

  if set_name && !set_name.to_s.strip.empty?
    str_cmd = "truncate:namespace=#{namespace}"
    str_cmd << ";set=#{set_name}" unless set_name.to_s.strip.empty?
  else
    str_cmd = if node.supports_feature?(Aerospike::Features::TRUNCATE_NAMESPACE)
      "truncate-namespace:namespace=#{namespace}"
              else
      "truncate:namespace=#{namespace}"
              end
  end

  if before_last_update
    lut_nanos = (before_last_update.to_f * 1_000_000_000.0).round
    str_cmd << ";lut=#{lut_nanos}"
  elsif supports_feature?(Aerospike::Features::LUT_NOW)
    # Servers >= 4.3.1.4 require lut argument
    str_cmd << ";lut=now"
  end

  response = send_info_command(policy, str_cmd, node).upcase
  return if response == "OK"
  raise Aerospike::Exceptions::Aerospike.new(Aerospike::ResultCode::SERVER_ERROR, "Truncate failed: #{response}", [node])
end