Class: Aerospike::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/aerospike/client.rb

Overview

Examples:

# connect to the database client = Client.new(‘192.168.0.1’, 3000)

#=> raises Aerospike::Exceptions::Timeout if a :timeout is specified and :fail_if_not_connected set to true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, options = {}) ⇒ Client

Returns a new instance of Client.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/aerospike/client.rb', line 42

def initialize(host, port, options={})
  @default_policy = Policy.new
  @default_write_policy = WritePolicy.new
  @default_scan_policy = ScanPolicy.new
  @default_query_policy = QueryPolicy.new
  @default_admin_policy = QueryPolicy.new

  policy = opt_to_client_policy(options)

  @cluster = Cluster.new(policy, Host.new(host, port))
  @cluster.add_cluster_config_change_listener(self)
  @cluster.connect

  self
end

Instance Attribute Details

#default_admin_policyObject

Returns the value of attribute default_admin_policy.



39
40
41
# File 'lib/aerospike/client.rb', line 39

def default_admin_policy
  @default_admin_policy
end

#default_policyObject

Returns the value of attribute default_policy.



39
40
41
# File 'lib/aerospike/client.rb', line 39

def default_policy
  @default_policy
end

#default_query_policyObject

Returns the value of attribute default_query_policy.



39
40
41
# File 'lib/aerospike/client.rb', line 39

def default_query_policy
  @default_query_policy
end

#default_scan_policyObject

Returns the value of attribute default_scan_policy.



39
40
41
# File 'lib/aerospike/client.rb', line 39

def default_scan_policy
  @default_scan_policy
end

#default_write_policyObject

Returns the value of attribute default_write_policy.



39
40
41
# File 'lib/aerospike/client.rb', line 39

def default_write_policy
  @default_write_policy
end

Class Method Details

.new_many(hosts, options = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/aerospike/client.rb', line 58

def self.new_many(hosts, options={})
  # Don't initiualize, just instantiate
  client = Client.allocate

  client.default_policy = Policy.new
  client.default_write_policy = WritePolicy.new
  client.default_scan_policy = ScanPolicy.new
  client.default_query_policy = QueryPolicy.new
  client.default_admin_policy = QueryPolicy.new

  policy = client.send(:opt_to_client_policy , options)

  cluster = Cluster.new(policy, *hosts)
  cluster.add_cluster_config_change_listener(client)
  client.send(:cluster=, cluster)
  cluster.connect

  client
end

Instance Method Details

#add(key, bins, options = {}) ⇒ Object

Examples:

client.add key, {'bin', -1}, :timeout => 0.001


189
190
191
192
193
# File 'lib/aerospike/client.rb', line 189

def add(key, bins, options={})
  policy = opt_to_write_policy(options)
  command = WriteCommand.new(@cluster, policy, key, hash_to_bins(bins), Aerospike::Operation::ADD)
  execute_command(command)
end

#append(key, bins, options = {}) ⇒ Object

Examples:

client.append key, {'bin', 'value to append'}, :timeout => 0.001


147
148
149
150
151
# File 'lib/aerospike/client.rb', line 147

def append(key, bins, options={})
  policy = opt_to_write_policy(options)
  command = WriteCommand.new(@cluster, policy, key, hash_to_bins(bins), Aerospike::Operation::APPEND)
  execute_command(command)
end

#batch_exists(keys, options = {}) ⇒ Object

Check if multiple record keys exist in one batch call.

The returned array bool is in positional order with the original key array order.
The policy can be used to specify timeouts.


255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/aerospike/client.rb', line 255

def batch_exists(keys, options={})
  policy = opt_to_policy(options)

  # same array can be used without sychronization;
  # when a key exists, the corresponding index will be marked true
  exists_array = Array.new(keys.length)

  key_map = BatchItem.generate_map(keys)

  cmd_gen = Proc.new do |node, bns|
    BatchCommandExists.new(node, bns, policy, key_map, exists_array)
  end

  batch_execute(keys, &cmd_gen)
  exists_array
end

#batch_get(keys, bin_names = [], options = {}) ⇒ Object

Read multiple record headers and bins for specified keys in one batch call.

The returned records are in positional order with the original key array order.
If a key is not found, the positional record will be nil.
The policy can be used to specify timeouts.


303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/aerospike/client.rb', line 303

def batch_get(keys, bin_names=[], options={})
  policy = opt_to_policy(options)

  # wait until all migrations are finished
  # TODO: implement
  # @cluster.WaitUntillMigrationIsFinished(policy.timeout)

  # same array can be used without sychronization;
  # when a key exists, the corresponding index will be set to record
  records = Array.new(keys.length)

  key_map = BatchItem.generate_map(keys)

  cmd_gen = Proc.new do |node, bns|
    BatchCommandGet.new(node, bns, policy, key_map, bin_names.uniq, records, INFO1_READ)
  end

  batch_execute(keys, &cmd_gen)
  records
end

#batch_get_header(keys, options = {}) ⇒ Object

Read multiple record header data for specified keys in one batch call.

The returned records are in positional order with the original key array order.
If a key is not found, the positional record will be nil.
The policy can be used to specify timeouts.


328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/aerospike/client.rb', line 328

def batch_get_header(keys, options={})
  policy = opt_to_policy(options)

  # wait until all migrations are finished
  # TODO: Fix this and implement
  # @cluster.WaitUntillMigrationIsFinished(policy.timeout)

  # same array can be used without sychronization;
  # when a key exists, the corresponding index will be set to record
  records = Array.new(keys.length)

  key_map = BatchItem.generate_map(keys)

  cmd_gen = Proc.new do |node, bns|
    BatchCommandGet.new(node, bns, policy, key_map, nil, records, INFO1_READ | INFO1_NOBINDATA)
  end

  batch_execute(keys, &cmd_gen)
  records
end

#change_password(user, password, options = {}) ⇒ Object

Change user’s password. Clear-text password will be hashed using bcrypt before sending to server.



783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
# File 'lib/aerospike/client.rb', line 783

def change_password(user, password, options={})
  policy = opt_to_admin_policy(options)
  if @cluster.user == ''
    return NewAerospikeError(INVALID_USER)
  end

  hash = AdminCommand.hash_password(password)
  command = AdminCommand.new

  if user == @cluster.user
    # Change own password.
    command.change_password(@cluster, policy, user, hash)
  else
    # Change other user's password by user admin.
    command.set_password(@cluster, policy, user, hash)
  end

  @cluster.change_password(user, hash)
end

#closeObject

Closes all client connections to database server nodes.



81
82
83
# File 'lib/aerospike/client.rb', line 81

def close
  @cluster.close
end

#connected?Boolean

Determines if there are active connections to the database server cluster.

Returns +true+ if connections exist.

Returns:

  • (Boolean)


89
90
91
# File 'lib/aerospike/client.rb', line 89

def connected?
  @cluster.connected?
end

#create_index(namespace, set_name, index_name, bin_name, index_type, options = {}) ⇒ Object

Create secondary index.

This asynchronous server call will return before command is complete.
The user can optionally wait for command completion by using the returned
IndexTask instance.

This method is only supported by Aerospike 3 servers.
index_type should be :string, :numeric or :geo2dsphere (requires server version 3.7 or later)


580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/aerospike/client.rb', line 580

def create_index(namespace, set_name, index_name, bin_name, index_type, options={})
  policy = opt_to_write_policy(options)
  str_cmd = "sindex-create:ns=#{namespace}"
  str_cmd << ";set=#{set_name}" unless set_name.to_s.strip.empty?
  str_cmd << ";indexname=#{index_name};numbins=1;indexdata=#{bin_name},#{index_type.to_s.upcase}"
  str_cmd << ";priority=normal"

  # Send index command to one node. That node will distribute the command to other nodes.
  response_map = send_info_command(policy, str_cmd)
  _, response = response_map.first
  response = response.to_s.upcase

  if response == 'OK'
    # Return task that could optionally be polled for completion.
    return IndexTask.new(@cluster, namespace, index_name)
  end

  if response.start_with?('FAIL:200')
    # Index has already been created.  Do not need to poll for completion.
    return IndexTask.new(@cluster, namespace, index_name, true)
  end

  raise Aerospike::Exceptions::Aerospike.new(Aerospike::ResultCode::INDEX_GENERIC, "Create index failed: #{response}")
end

#create_user(user, password, roles, options = {}) ⇒ Object

Create user with password and roles. Clear-text password will be hashed using bcrypt before sending to server.



768
769
770
771
772
773
# File 'lib/aerospike/client.rb', line 768

def create_user(user, password, roles, options={})
  policy = opt_to_admin_policy(options)
  hash = AdminCommand.hash_password(password)
  command = AdminCommand.new
  command.create_user(@cluster, policy, user, hash, roles)
end

#delete(key, options = {}) ⇒ Object

Examples:

existed = client.delete key, :timeout => 0.001


211
212
213
214
215
216
# File 'lib/aerospike/client.rb', line 211

def delete(key, options={})
  policy = opt_to_write_policy(options)
  command = DeleteCommand.new(@cluster, policy, key)
  execute_command(command)
  command.existed
end

#drop_index(namespace, set_name, index_name, options = {}) ⇒ Object

Delete secondary index.

This method is only supported by Aerospike 3 servers.


607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
# File 'lib/aerospike/client.rb', line 607

def drop_index(namespace, set_name, index_name, options={})
  policy = opt_to_write_policy(options)
  str_cmd = "sindex-delete:ns=#{namespace}"
  str_cmd << ";set=#{set_name}" unless set_name.to_s.strip.empty?
  str_cmd << ";indexname=#{index_name}"

  # Send index command to one node. That node will distribute the command to other nodes.
  response_map = send_info_command(policy, str_cmd)
  _, response = response_map.first
  response = response.to_s.upcase

  return if response == 'OK'

  # Index did not previously exist. Return without error.
  return if response.start_with?('FAIL:201')

  raise Aerospike::Exceptions::Aerospike.new(Aerospike::ResultCode::INDEX_GENERICINDEX_GENERIC, "Drop index failed: #{response}")
end

#drop_user(user, options = {}) ⇒ Object

Remove user from cluster.



776
777
778
779
780
# File 'lib/aerospike/client.rb', line 776

def drop_user(user, options={})
  policy = opt_to_admin_policy(options)
  command = AdminCommand.new
  command.drop_user(@cluster, policy, user)
end

#execute_udf(key, package_name, function_name, args = [], options = {}) ⇒ Object

Execute user defined function on server and return results.

The function operates on a single record.
The package name is used to locate the udf file location:

udf file = <server udf dir>/<package name>.lua

This method is only supported by Aerospike 3 servers.


510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/aerospike/client.rb', line 510

def execute_udf(key, package_name, function_name, args=[], options={})
  policy = opt_to_write_policy(options)

  command = ExecuteCommand.new(@cluster, policy, key, package_name, function_name, args)
  execute_command(command)

  record = command.record

  return nil if !record || record.bins.length == 0

  result_map = record.bins

  # User defined functions don't have to return a value.
  key, obj = result_map.detect{|k, v| k.include?('SUCCESS')}
  if key
    return obj
  end

  key, obj = result_map.detect{|k, v| k.include?('FAILURE')}
  if key
    raise Aerospike::Exceptions::Aerospike.new(UDF_BAD_RESPONSE, "#{obj}")
  end

  raise Aerospike::Exceptions::Aerospike.new(UDF_BAD_RESPONSE, "Invalid UDF return value")
end

#execute_udf_on_query(statement, package_name, function_name, function_args = [], options = {}) ⇒ Object

execute_udf_on_query applies user defined function on records that match the statement filter. Records are not returned to the client. This asynchronous server call will return before command is complete. The user can optionally wait for command completion by using the returned ExecuteTask instance.

This method is only supported by Aerospike 3 servers. If the policy is nil, the default relevant policy will be used.



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/aerospike/client.rb', line 544

def execute_udf_on_query(statement, package_name, function_name, function_args=[], options={})
  policy = opt_to_query_policy(options)

  nodes = @cluster.nodes
  if nodes.length == 0
    raise Aerospike::Exceptions::Aerospike.new(Aerospike::ResultCode::SERVER_NOT_AVAILABLE, "Executing UDF failed because cluster is empty.")
  end

  # TODO: wait until all migrations are finished
  statement.set_aggregate_function(package_name, function_name, function_args, false)

  # Use a thread per node
  nodes.each do |node|
    Thread.new do
      abort_on_exception = true
      begin
        command = QueryCommand.new(node, policy, statement, nil)
        execute_command(command)
      rescue => e
        Aerospike.logger.error(e)
        raise e
      end
    end
  end

  ExecuteTask.new(@cluster, statement)
end

#exists(key, options = {}) ⇒ Object

Determines if a record key exists.

The policy can be used to specify timeouts.


245
246
247
248
249
250
# File 'lib/aerospike/client.rb', line 245

def exists(key, options={})
  policy = opt_to_policy(options)
  command = ExistsCommand.new(@cluster, policy, key)
  execute_command(command)
  command.exists
end

#get(key, bin_names = [], options = {}) ⇒ Object

Read record header and bins for specified key.

The policy can be used to specify timeouts.


278
279
280
281
282
283
284
# File 'lib/aerospike/client.rb', line 278

def get(key, bin_names=[], options={})
  policy = opt_to_policy(options)

  command = ReadCommand.new(@cluster, policy, key, bin_names)
  execute_command(command)
  command.record
end

#get_header(key, options = {}) ⇒ Object

Read record generation and expiration only for specified key. Bins are not read.

The policy can be used to specify timeouts.


288
289
290
291
292
293
# File 'lib/aerospike/client.rb', line 288

def get_header(key, options={})
  policy = opt_to_policy(options)
  command = ReadHeaderCommand.new(@cluster, policy, key)
  execute_command(command)
  command.record
end

#get_large_list(key, bin_name, user_module = nil, options = {}) ⇒ Object

Initialize large list operator. This operator can be used to create and manage a list

within a single bin.

This method is only supported by Aerospike 3 servers.


375
376
377
# File 'lib/aerospike/client.rb', line 375

def get_large_list(key, bin_name, user_module=nil, options={})
  LargeList.new(self, opt_to_write_policy(options), key, bin_name, user_module)
end

#get_large_map(key, bin_name, user_module = nil, options = {}) ⇒ Object

Initialize large map operator. This operator can be used to create and manage a map

within a single bin.

This method is only supported by Aerospike 3 servers.
DEPRECATED. This method will be removed from the client in the future.


384
385
386
# File 'lib/aerospike/client.rb', line 384

def get_large_map(key, bin_name, user_module=nil, options={})
  LargeMap.new(self, opt_to_write_policy(options), key, bin_name, user_module)
end

#get_large_set(key, bin_name, user_module = nil, options = {}) ⇒ Object

Initialize large set operator. This operator can be used to create and manage a set

within a single bin.

This method is only supported by Aerospike 3 servers.
DEPRECATED. This method will be removed from the client in the future.


393
394
395
# File 'lib/aerospike/client.rb', line 393

def get_large_set(key, bin_name, user_module=nil, options={})
  LargeSet.new(self, opt_to_write_policy(options), key, bin_name, user_module)
end

#get_large_stack(key, bin_name, user_module = nil, options = {}) ⇒ Object

Initialize large stack operator. This operator can be used to create and manage a stack

within a single bin.

This method is only supported by Aerospike 3 servers.
DEPRECATED. This method will be removed from the client in the future.


402
403
404
# File 'lib/aerospike/client.rb', line 402

def get_large_stack(key, bin_name, user_module=nil, options={})
  LargeStack.new(self, opt_to_write_policy(options), key, bin_name, user_module)
end

#grant_roles(user, roles, options = {}) ⇒ Object

Add roles to user’s list of roles.



804
805
806
807
808
# File 'lib/aerospike/client.rb', line 804

def grant_roles(user, roles, options={})
  policy = opt_to_admin_policy(options)
  command = AdminCommand.new
  command.grant_roles(@cluster, policy, user, roles)
end

#list_udf(options = {}) ⇒ Object

ListUDF lists all packages containing user defined functions in the server.

This method is only supported by Aerospike 3 servers.


474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/aerospike/client.rb', line 474

def list_udf(options={})
  str_cmd = 'udf-list'

  # Send command to one node. That node will distribute it to other nodes.
  response_map = @cluster.request_info(@default_policy, str_cmd)
  _, response = response_map.first

  vals = response.split(';')

  vals.map do |udf_info|
    next if udf_info.strip! == ''

    udf_parts = udf_info.split(',')
    udf = UDF.new
    udf_parts.each do |values|
      k, v = values.split('=', 2)
      case k
      when 'filename'
        udf.filename = v
      when 'hash'
        udf.hash = v
      when 'type'
        udf.language = v
      end
    end
    udf
  end
end

#node_namesObject

Returns list of active server node names in the cluster.



103
104
105
106
107
# File 'lib/aerospike/client.rb', line 103

def node_names
  @cluster.nodes.map do |node|
    node.get_name
  end
end

#nodesObject

Returns array of active server nodes in the cluster.



96
97
98
# File 'lib/aerospike/client.rb', line 96

def nodes
  @cluster.nodes
end

#operate(key, operations, options = {}) ⇒ Object

Perform multiple read/write operations on a single key in one batch call.

An example would be to add an integer value to an existing record and then
read the result, all in one database call.

Write operations are always performed first, regardless of operation order
relative to read operations.


359
360
361
362
363
364
365
# File 'lib/aerospike/client.rb', line 359

def operate(key, operations, options={})
  policy = opt_to_write_policy(options)

  command = OperateCommand.new(@cluster, policy, key, operations)
  execute_command(command)
  command.record
end

#prepend(key, bins, options = {}) ⇒ Object

Examples:

client.prepend key, {'bin', 'value to prepend'}, :timeout => 0.001


166
167
168
169
170
# File 'lib/aerospike/client.rb', line 166

def prepend(key, bins, options={})
  policy = opt_to_write_policy(options)
  command = WriteCommand.new(@cluster, policy, key, hash_to_bins(bins), Aerospike::Operation::PREPEND)
  execute_command(command)
end

#put(key, bins, options = {}) ⇒ Object

Examples:

client.put key, {'bin', 'value string'}, :timeout => 0.001


124
125
126
127
128
# File 'lib/aerospike/client.rb', line 124

def put(key, bins, options={})
  policy = opt_to_write_policy(options)
  command = WriteCommand.new(@cluster, policy, key, hash_to_bins(bins), Aerospike::Operation::WRITE)
  execute_command(command)
end

#query(statement, options = {}) ⇒ Object

Query executes a query and returns a recordset. The query executor puts records on a channel from separate goroutines. The caller can concurrently pops records off the channel through the record channel.

This method is only supported by Aerospike 3 servers. If the policy is nil, a default policy will be generated.



732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
# File 'lib/aerospike/client.rb', line 732

def query(statement, options={})
  policy = opt_to_query_policy(options)
  new_policy = policy.clone

  nodes = @cluster.nodes
  if nodes.length == 0
    raise Aerospike::Exceptions::Aerospike.new(Aerospike::ResultCode::SERVER_NOT_AVAILABLE, "Scan failed because cluster is empty.")
  end

  recordset = Recordset.new(policy.record_queue_size, nodes.length, :query)

  # Use a thread per node
  nodes.each do |node|
    Thread.new do
      abort_on_exception = true
      command = QueryCommand.new(node, new_policy, statement, recordset)
      begin
        execute_command(command)
      rescue => e
        Aerospike.logger.error(e.backtrace.join("\n")) unless e == QUERY_TERMINATED_EXCEPTION 
        recordset.cancel(e)
      ensure
        recordset.thread_finished
      end
    end
  end

  recordset
end

#query_user(user, options = {}) ⇒ Object

Retrieve roles for a given user.



818
819
820
821
822
# File 'lib/aerospike/client.rb', line 818

def query_user(user, options={})
  policy = opt_to_admin_policy(options)
  command = AdminCommand.new
  command.query_user(@cluster, policy, user)
end

#query_users(options = {}) ⇒ Object

Retrieve all users and their roles.



825
826
827
828
829
# File 'lib/aerospike/client.rb', line 825

def query_users(options={})
  policy = opt_to_admin_policy(options)
  command = AdminCommand.new
  command.query_users(@cluster, policy)
end

#register_udf(udf_body, server_path, language, options = {}) ⇒ Object

Register package containing user defined functions with server.

This asynchronous server call will return before command is complete.
The user can optionally wait for command completion by using the returned
RegisterTask instance.

This method is only supported by Aerospike 3 servers.


427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/aerospike/client.rb', line 427

def register_udf(udf_body, server_path, language, options={})
  content = Base64.strict_encode64(udf_body).force_encoding('binary')

  str_cmd = "udf-put:filename=#{server_path};content=#{content};"
  str_cmd << "content-len=#{content.length};udf-type=#{language};"
  # Send UDF to one node. That node will distribute the UDF to other nodes.
  response_map = @cluster.request_info(@default_policy, str_cmd)

  res = {}
  response_map.each do |k, response|
    vals = response.to_s.split(';')
    vals.each do |pair|
      k, v = pair.split("=", 2)
      res[k] = v
    end
  end

  if res['error']
    raise Aerospike::Exceptions::CommandRejected.new("Registration failed: #{res['error']}\nFile: #{res['file']}\nLine: #{res['line']}\nMessage: #{res['message']}")
  end

  UdfRegisterTask.new(@cluster, server_path)
end

#register_udf_from_file(client_path, server_path, language, options = {}) ⇒ Object

Register package containing user defined functions with server.

This asynchronous server call will return before command is complete.
The user can optionally wait for command completion by using the returned
RegisterTask instance.

This method is only supported by Aerospike 3 servers.


416
417
418
419
# File 'lib/aerospike/client.rb', line 416

def register_udf_from_file(client_path, server_path, language, options={})
  udf_body = File.read(client_path)
  register_udf(udf_body, server_path, language, options={})
end

#remove_udf(udf_name, options = {}) ⇒ Object

RemoveUDF removes a package containing user defined functions in the server.

This asynchronous server call will return before command is complete.
The user can optionally wait for command completion by using the returned
RemoveTask instance.

This method is only supported by Aerospike 3 servers.


457
458
459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/aerospike/client.rb', line 457

def remove_udf(udf_name, options={})
  str_cmd = "udf-remove:filename=#{udf_name};"

  # Send command to one node. That node will distribute it to other nodes.
  # Send UDF to one node. That node will distribute the UDF to other nodes.
  response_map = @cluster.request_info(@default_policy, str_cmd)
  _, response = response_map.first

  if response == 'ok'
    UdfRemoveTask.new(@cluster, udf_name)
  else
    raise Aerospike::Exceptions::Aerospike.new(Aerospike::ResultCode::SERVER_ERROR, response)
  end
end

#request_info(*commands) ⇒ Object



626
627
628
# File 'lib/aerospike/client.rb', line 626

def request_info(*commands)
  @cluster.request_info(@default_policy, *commands)
end

#revoke_roles(user, roles, options = {}) ⇒ Object

Remove roles from user’s list of roles.



811
812
813
814
815
# File 'lib/aerospike/client.rb', line 811

def revoke_roles(user, roles, options={})
  policy = opt_to_admin_policy(options)
  command = AdminCommand.new
  command.revoke_roles(@cluster, policy, user, roles)
end

#scan_all(namespace, set_name, bin_names = [], options = {}) ⇒ Object


Scan Operations




634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
# File 'lib/aerospike/client.rb', line 634

def scan_all(namespace, set_name, bin_names=[], options={})
  policy = opt_to_scan_policy(options)

  # wait until all migrations are finished
  # TODO: implement
  # @cluster.WaitUntillMigrationIsFinished(policy.timeout)

  # Retry policy must be one-shot for scans.
  # copy on write for policy
  new_policy = policy.clone

  nodes = @cluster.nodes
  if nodes.length == 0
    raise Aerospike::Exceptions::Aerospike.new(Aerospike::ResultCode::SERVER_NOT_AVAILABLE, "Scan failed because cluster is empty.")
  end

  recordset = Recordset.new(policy.record_queue_size, nodes.length, :scan)

  if policy.concurrent_nodes
    # Use a thread per node
    nodes.each do |node|
      Thread.new do
        abort_on_exception = true
        command = ScanCommand.new(node, new_policy, namespace, set_name, bin_names, recordset)
        begin
          execute_command(command)
        rescue => e
          Aerospike.logger.error(e.backtrace.join("\n")) unless e == SCAN_TERMINATED_EXCEPTION 
          recordset.cancel(e)
        ensure
          recordset.thread_finished
        end
      end
    end
  else
    Thread.new do
      abort_on_exception = true
      nodes.each do |node|
        command = ScanCommand.new(node, new_policy, namespace, set_name, bin_names, recordset)
        begin
          execute_command(command)
        rescue => e
          Aerospike.logger.error(e.backtrace.join("\n")) unless e == SCAN_TERMINATED_EXCEPTION 
          recordset.cancel(e)
        ensure
          recordset.thread_finished
        end
      end
    end
  end

  recordset
end

#scan_node(node, namespace, set_name, bin_names = [], options = {}) ⇒ Object

ScanNode reads all records in specified namespace and set, from one node only. The policy can be used to specify timeouts.



690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
# File 'lib/aerospike/client.rb', line 690

def scan_node(node, namespace, set_name, bin_names=[], options={})
  policy = opt_to_scan_policy(options)
  # wait until all migrations are finished
  # TODO: implement
  # @cluster.WaitUntillMigrationIsFinished(policy.timeout)

  # Retry policy must be one-shot for scans.
  # copy on write for policy
  new_policy = policy.clone
  new_policy.max_retries = 0

  node = @cluster.get_node_by_name(node) if !node.is_a?(Aerospike::Node)

  recordset = Recordset.new(policy.record_queue_size, 1, :scan)

  Thread.new do
    abort_on_exception = true
    command = ScanCommand.new(node, new_policy, namespace, set_name, bin_names, recordset)
    begin
      execute_command(command)
    rescue => e
      Aerospike.logger.error(e.backtrace.join("\n")) unless e == SCAN_TERMINATED_EXCEPTION 
      recordset.cancel(e)
    ensure
      recordset.thread_finished
    end
  end

  recordset
end

#touch(key, options = {}) ⇒ Object

Examples:

client.touch key, :timeout => 0.001


232
233
234
235
236
# File 'lib/aerospike/client.rb', line 232

def touch(key, options={})
  policy = opt_to_write_policy(options)
  command = TouchCommand.new(@cluster, policy, key)
  execute_command(command)
end