aerospike 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +0 -0
  3. data/LICENSE +203 -0
  4. data/README.md +123 -0
  5. data/lib/aerospike.rb +69 -0
  6. data/lib/aerospike/aerospike_exception.rb +111 -0
  7. data/lib/aerospike/bin.rb +46 -0
  8. data/lib/aerospike/client.rb +649 -0
  9. data/lib/aerospike/cluster/cluster.rb +537 -0
  10. data/lib/aerospike/cluster/connection.rb +113 -0
  11. data/lib/aerospike/cluster/node.rb +248 -0
  12. data/lib/aerospike/cluster/node_validator.rb +85 -0
  13. data/lib/aerospike/cluster/partition.rb +54 -0
  14. data/lib/aerospike/cluster/partition_tokenizer_new.rb +128 -0
  15. data/lib/aerospike/cluster/partition_tokenizer_old.rb +135 -0
  16. data/lib/aerospike/command/batch_command.rb +120 -0
  17. data/lib/aerospike/command/batch_command_exists.rb +93 -0
  18. data/lib/aerospike/command/batch_command_get.rb +150 -0
  19. data/lib/aerospike/command/batch_item.rb +69 -0
  20. data/lib/aerospike/command/batch_node.rb +82 -0
  21. data/lib/aerospike/command/command.rb +680 -0
  22. data/lib/aerospike/command/delete_command.rb +57 -0
  23. data/lib/aerospike/command/execute_command.rb +42 -0
  24. data/lib/aerospike/command/exists_command.rb +57 -0
  25. data/lib/aerospike/command/field_type.rb +44 -0
  26. data/lib/aerospike/command/operate_command.rb +37 -0
  27. data/lib/aerospike/command/read_command.rb +174 -0
  28. data/lib/aerospike/command/read_header_command.rb +63 -0
  29. data/lib/aerospike/command/single_command.rb +60 -0
  30. data/lib/aerospike/command/touch_command.rb +50 -0
  31. data/lib/aerospike/command/write_command.rb +60 -0
  32. data/lib/aerospike/host.rb +43 -0
  33. data/lib/aerospike/info.rb +96 -0
  34. data/lib/aerospike/key.rb +99 -0
  35. data/lib/aerospike/language.rb +25 -0
  36. data/lib/aerospike/ldt/large.rb +69 -0
  37. data/lib/aerospike/ldt/large_list.rb +100 -0
  38. data/lib/aerospike/ldt/large_map.rb +82 -0
  39. data/lib/aerospike/ldt/large_set.rb +78 -0
  40. data/lib/aerospike/ldt/large_stack.rb +72 -0
  41. data/lib/aerospike/loggable.rb +55 -0
  42. data/lib/aerospike/operation.rb +70 -0
  43. data/lib/aerospike/policy/client_policy.rb +37 -0
  44. data/lib/aerospike/policy/generation_policy.rb +37 -0
  45. data/lib/aerospike/policy/policy.rb +54 -0
  46. data/lib/aerospike/policy/priority.rb +34 -0
  47. data/lib/aerospike/policy/record_exists_action.rb +45 -0
  48. data/lib/aerospike/policy/write_policy.rb +61 -0
  49. data/lib/aerospike/record.rb +42 -0
  50. data/lib/aerospike/result_code.rb +353 -0
  51. data/lib/aerospike/task/index_task.rb +59 -0
  52. data/lib/aerospike/task/task.rb +71 -0
  53. data/lib/aerospike/task/udf_register_task.rb +55 -0
  54. data/lib/aerospike/task/udf_remove_task.rb +55 -0
  55. data/lib/aerospike/udf.rb +24 -0
  56. data/lib/aerospike/utils/buffer.rb +139 -0
  57. data/lib/aerospike/utils/epoc.rb +28 -0
  58. data/lib/aerospike/utils/pool.rb +65 -0
  59. data/lib/aerospike/value/particle_type.rb +45 -0
  60. data/lib/aerospike/value/value.rb +380 -0
  61. data/lib/aerospike/version.rb +4 -0
  62. metadata +132 -0
@@ -0,0 +1,60 @@
1
+ # encoding: utf-8
2
+ # Copyright 2014 Aerospike, Inc.
3
+ #
4
+ # Portions may be licensed to Aerospike, Inc. under one or more contributor
5
+ # license agreements.
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
8
+ # use this file except in compliance with the License. You may obtain a copy of
9
+ # the License at http:#www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations under
15
+ # the License.
16
+
17
+ require 'aerospike/cluster/partition'
18
+ require 'aerospike/command/command'
19
+
20
+ module Aerospike
21
+
22
+ private
23
+
24
+ class SingleCommand < Command
25
+
26
+ def initialize(cluster, key)
27
+ @cluster = cluster
28
+ @key = key
29
+ @partition = Partition.new_by_key(key)
30
+
31
+ super(@cluster.get_node(@partition))
32
+
33
+ self
34
+ end
35
+
36
+
37
+ protected
38
+
39
+ def empty_socket
40
+ # There should not be any more bytes.
41
+ # Empty the socket to be safe.
42
+ sz = @data_buffer.read_int64( 0)
43
+ header_length = @data_buffer.read(8).ord
44
+ receive_size = Integer(sz&0xFFFFFFFFFFFF) - header_length
45
+
46
+ # Read remaining message bytes.
47
+ if receive_size > 0
48
+ size_buffer_sz(receive_size)
49
+ begin
50
+ @conn.read(@data_buffer, receive_size)
51
+ rescue => e
52
+ Aerospike.logger.error("#{e}")
53
+ raise e
54
+ end
55
+ end
56
+ end
57
+
58
+ end # class
59
+
60
+ end # module
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+ # Copyright 2014 Aerospike, Inc.
3
+ #
4
+ # Portions may be licensed to Aerospike, Inc. under one or more contributor
5
+ # license agreements.
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
8
+ # use this file except in compliance with the License. You may obtain a copy of
9
+ # the License at http:#www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations under
15
+ # the License.
16
+
17
+ require 'aerospike/command/single_command'
18
+
19
+ module Aerospike
20
+
21
+ private
22
+
23
+ class TouchCommand < SingleCommand
24
+
25
+ def initialize(cluster, policy, key)
26
+ super(cluster, key)
27
+
28
+ @policy = policy
29
+
30
+ self
31
+ end
32
+
33
+ def write_buffer
34
+ set_touch(@policy, @key)
35
+ end
36
+
37
+ def parse_result
38
+ # Read header.
39
+ @conn.read(@data_buffer, MSG_TOTAL_HEADER_SIZE)
40
+
41
+ result_code = @data_buffer.read(13).ord & 0xFF
42
+
43
+ raise Aerospike::Exceptions::Aerospike.new(result_code) if result_code != 0
44
+
45
+ empty_socket
46
+ end
47
+
48
+ end # class
49
+
50
+ end # module
@@ -0,0 +1,60 @@
1
+ # encoding: utf-8
2
+ # Copyright 2014 Aerospike, Inc.
3
+ #
4
+ # Portions may be licensed to Aerospike, Inc. under one or more contributor
5
+ # license agreements.
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
8
+ # use this file except in compliance with the License. You may obtain a copy of
9
+ # the License at http:#www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations under
15
+ # the License.
16
+
17
+ require 'aerospike/command/single_command'
18
+
19
+ module Aerospike
20
+
21
+ private
22
+
23
+ class WriteCommand < SingleCommand
24
+
25
+ def initialize(cluster, policy, key, bins, operation)
26
+
27
+ super(cluster, key)
28
+
29
+ @bins = bins
30
+ @operation = operation
31
+ @policy = policy
32
+
33
+ self
34
+ end
35
+
36
+ def write_buffer
37
+ set_write(@policy, @operation, @key, @bins)
38
+ end
39
+
40
+ def parse_result
41
+ # Read header.
42
+ begin
43
+ @conn.read(@data_buffer, MSG_TOTAL_HEADER_SIZE)
44
+ rescue => e
45
+ Aerospike.logger.error("#{e}")
46
+ raise e
47
+ end
48
+
49
+ result_code = @data_buffer.read(13).ord & 0xFF
50
+
51
+ if result_code != 0
52
+ raise Aerospike::Exceptions::Aerospike.new(result_code)
53
+ end
54
+
55
+ empty_socket
56
+ end
57
+
58
+ end # class
59
+
60
+ end # module
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+ # Copyright 2014 Aerospike, Inc.
3
+ #
4
+ # Portions may be licensed to Aerospike, Inc. under one or more contributor
5
+ # license agreements.
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
8
+ # use this file except in compliance with the License. You may obtain a copy of
9
+ # the License at http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations under
15
+ # the License.
16
+
17
+ module Aerospike
18
+
19
+ class Host
20
+
21
+ attr_accessor :name, :port
22
+
23
+ def initialize(host_name, host_port)
24
+ @name = host_name
25
+ @port = host_port
26
+ end
27
+
28
+ def to_s
29
+ "#{@name}:#{@port.to_s}"
30
+ end
31
+
32
+ def ==(other)
33
+ other && other.is_a?(Host) && other.name == @name && other.port == @port
34
+ end
35
+ alias eql? ==
36
+
37
+ def hash
38
+ to_s.hash
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,96 @@
1
+ # encoding: utf-8
2
+ # Copyright 2014 Aerospike, Inc.
3
+ #
4
+ # Portions may be licensed to Aerospike, Inc. under one or more contributor
5
+ # license agreements.
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License") you may not
8
+ # use this file except in compliance with the License. You may obtain a copy of
9
+ # the License at http:#www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations under
15
+ # the License.
16
+
17
+ require 'aerospike/utils/buffer'
18
+
19
+ module Aerospike
20
+
21
+ private
22
+
23
+ # Polymorphic value classes used to efficiently serialize objects into the wire protocol.
24
+ class Info
25
+
26
+ def self.request(conn, *commands)
27
+ buffer = Buffer.get
28
+
29
+ # If conservative estimate may be exceeded, get exact estimate
30
+ # to preserve memory and resize buffer.
31
+ offset = 8
32
+
33
+ commands.each do |command|
34
+ offset += command.bytesize + 1
35
+ end
36
+
37
+ buffer.resize(offset)
38
+
39
+ offset = 8 # Skip size field.
40
+
41
+ # The command format is: <name1>\n<name2>\n...
42
+ commands.each do |command|
43
+ buffer.write_binary(command, offset)
44
+ offset += command.bytesize
45
+ buffer.write_byte("\n", offset)
46
+ offset += 1
47
+ end
48
+
49
+ begin
50
+ buf_length = send_command(conn, offset, buffer)
51
+ parse_multiple_response(buf_length, buffer)
52
+ rescue => e
53
+ Aerospike.logger.error("#{e}")
54
+ ensure
55
+ Buffer.put(buffer)
56
+ end
57
+ end
58
+
59
+ def self.parse_multiple_response(buf_length, buffer)
60
+ res = {}
61
+ buffer.read(0, buf_length).split("\n").each do |vstr|
62
+ k, v = vstr.split("\t")
63
+ res[k] = v
64
+ end
65
+ res
66
+ end
67
+
68
+ private
69
+
70
+ def self.send_command(conn, offset, buffer)
71
+ begin
72
+ # Write size field.
73
+ size = (offset - 8) | (2 << 56) | (1 << 48)
74
+
75
+ buffer.write_int64(size, 0)
76
+ conn.write(buffer, offset)
77
+
78
+ # Read - reuse input buffer.
79
+ conn.read(buffer, 8)
80
+
81
+ size = buffer.read_int64(0)
82
+ length = size & 0xFFFFFFFFFFFF
83
+
84
+ buffer.resize(length)
85
+
86
+ conn.read(buffer, length)
87
+ return length
88
+ rescue => e
89
+ conn.close
90
+ raise e
91
+ end
92
+ end
93
+
94
+ end
95
+
96
+ end
@@ -0,0 +1,99 @@
1
+ # encoding: utf-8
2
+ # Copyright 2014 Aerospike, Inc.
3
+ #
4
+ # Portions may be licensed to Aerospike, Inc. under one or more contributor
5
+ # license agreements.
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
8
+ # use this file except in compliance with the License. You may obtain a copy of
9
+ # the License at http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations under
15
+ # the License.
16
+
17
+ require 'digest'
18
+
19
+ require 'aerospike/value/value'
20
+
21
+ module Aerospike
22
+
23
+ class Key
24
+
25
+ @@digest_pool = Pool.new
26
+ @@digest_pool.create_block = Proc.new do
27
+ unless RUBY_PLATFORM == 'java'
28
+ Digest::RMD160.new
29
+ else
30
+ h = OpenSSL::Digest::RIPEMD160.new
31
+ end
32
+ end
33
+
34
+
35
+ attr_reader :namespace, :set_name, :digest
36
+
37
+ def initialize(ns, set, val, digest=nil)
38
+ @namespace = ns
39
+ @set_name = set
40
+ @user_key = Value.of(val)
41
+
42
+ unless digest
43
+ compute_digest
44
+ else
45
+ @digest = digest
46
+ end
47
+
48
+ self
49
+ end
50
+
51
+ def to_s
52
+ "#{@namespace}:#{@set_name}:#{@user_key}:#{@digest.nil? ? '' : @digest.bytes}"
53
+ end
54
+
55
+ def user_key
56
+ @user_key.get if @user_key
57
+ end
58
+
59
+ def user_key_as_value
60
+ @user_key
61
+ end
62
+
63
+ def ==(other)
64
+ other && other.is_a?(Key) &&
65
+ other.digest == @digest &&
66
+ other.namespace == @namespace
67
+ end
68
+ alias eql? ==
69
+
70
+ def hash
71
+ @digest.hash
72
+ end
73
+
74
+ private
75
+
76
+ def compute_digest
77
+ key_type = @user_key.type
78
+
79
+ if key_type == Aerospike::ParticleType::NULL
80
+ raise Aerospike::Exceptions::Aerospike.new(Aerospike::ResultCode::PARAMETER_ERROR, "Invalid key: nil")
81
+ end
82
+
83
+ # get a hash from pool and make it ready for work
84
+ h = @@digest_pool.poll
85
+ h.reset
86
+
87
+ # Compute a complete digest
88
+ h.update(@set_name)
89
+ h.update(key_type.chr)
90
+ h.update(@user_key.to_bytes)
91
+ @digest = h.digest
92
+
93
+ # put the hash object back to the pool
94
+ @@digest_pool.offer(h)
95
+ end
96
+
97
+ end
98
+
99
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+ # Copyright 2014 Aerospike, Inc.
3
+ #
4
+ # Portions may be licensed to Aerospike, Inc. under one or more contributor
5
+ # license agreements.
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
8
+ # use this file except in compliance with the License. You may obtain a copy of
9
+ # the License at http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations under
15
+ # the License.
16
+
17
+ module Aerospike
18
+
19
+ module Language
20
+
21
+ LUA = 'LUA'
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,69 @@
1
+ # encoding: utf-8
2
+ # Copyright 2014 Aerospike, Inc.
3
+ #
4
+ # Portions may be licensed to Aerospike, Inc. under one or more contributor
5
+ # license agreements.
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the 'License'); you may not
8
+ # use this file except in compliance with the License. You may obtain a copy of
9
+ # the License at http:#www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations under
15
+ # the License.
16
+
17
+ require 'aerospike/value/particle_type'
18
+
19
+ module Aerospike
20
+
21
+ private
22
+
23
+ class Large
24
+
25
+ def initialize(client, policy, key, bin_name, user_module=nil)
26
+ @client = client
27
+ @policy = policy
28
+ @key = key
29
+ @bin_name = Aerospike::ParticleType::STRING.chr + bin_name
30
+ @user_module = Aerospike::ParticleType::STRING.chr + user_module unless user_module.nil?
31
+
32
+ self
33
+ end
34
+
35
+ # Delete bin containing the object.
36
+ def destroy
37
+ @client.execute_udf(@key, @PACKAGE_NAME, 'destroy', [@bin_name], @policy)
38
+ end
39
+
40
+ # Return size of object.
41
+ def size
42
+ @client.execute_udf(@key, @PACKAGE_NAME, 'size', [@bin_name], @policy)
43
+ end
44
+
45
+ # Return map of object configuration parameters.
46
+ def config
47
+ @client.execute_udf(@key, @PACKAGE_NAME, 'get_config', [@bin_name], @policy)
48
+ end
49
+
50
+ # Set maximum number of entries in the object.
51
+ #
52
+ # capacity max entries in set
53
+ def capacity=(capacity)
54
+ @client.execute_udf(@key, @PACKAGE_NAME, 'set_capacity', [@bin_name, capacity], @policy)
55
+ end
56
+
57
+ # Return maximum number of entries in the object.
58
+ def capacity
59
+ @client.execute_udf(@key, @PACKAGE_NAME, 'get_capacity', [@bin_name], @policy)
60
+ end
61
+
62
+ # Return list of all objects on the stack.
63
+ def scan
64
+ @client.execute_udf(@key, @PACKAGE_NAME, 'scan', [@bin_name], @policy)
65
+ end
66
+
67
+ end # class
68
+
69
+ end #class