priam 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem "thrift", "~> 0.7.0"
7
+ gem "thrift_client", "~> 0.7.1"
8
+ gem "simple_uuid", "~> 0.2.0"
9
+ gem "cassandra", "~> 0.12.1"
10
+
11
+ # Add dependencies to develop your gem here.
12
+ # Include everything needed to run rake, tests, features, etc.
13
+ group :development do
14
+ gem "rspec", "~> 2.8.0"
15
+ gem "rdoc", "~> 3.12"
16
+ gem "bundler", ">= 1.0.0"
17
+ gem "jeweler", "~> 1.8.4"
18
+ gem "rcov", ">= 0"
19
+ end
@@ -0,0 +1,46 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ cassandra (0.12.1)
5
+ json
6
+ rake
7
+ simple_uuid (>= 0.2.0)
8
+ thrift_client (>= 0.7.0)
9
+ diff-lcs (1.1.3)
10
+ git (1.2.5)
11
+ jeweler (1.8.4)
12
+ bundler (~> 1.0)
13
+ git (>= 1.2.5)
14
+ rake
15
+ rdoc
16
+ json (1.7.5)
17
+ rake (10.0.2)
18
+ rcov (1.0.0)
19
+ rdoc (3.12)
20
+ json (~> 1.4)
21
+ rspec (2.8.0)
22
+ rspec-core (~> 2.8.0)
23
+ rspec-expectations (~> 2.8.0)
24
+ rspec-mocks (~> 2.8.0)
25
+ rspec-core (2.8.0)
26
+ rspec-expectations (2.8.0)
27
+ diff-lcs (~> 1.1.2)
28
+ rspec-mocks (2.8.0)
29
+ simple_uuid (0.2.0)
30
+ thrift (0.7.0)
31
+ thrift_client (0.7.1)
32
+ thrift (~> 0.7.0)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ bundler (>= 1.0.0)
39
+ cassandra (~> 0.12.1)
40
+ jeweler (~> 1.8.4)
41
+ rcov
42
+ rdoc (~> 3.12)
43
+ rspec (~> 2.8.0)
44
+ simple_uuid (~> 0.2.0)
45
+ thrift (~> 0.7.0)
46
+ thrift_client (~> 0.7.1)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Kenji Hara
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,61 @@
1
+ = priam
2
+
3
+ 'priam' is a command-base client for Cassandra.
4
+
5
+ == Supported Ruby versions and implementations
6
+ bunnish should work identically on:
7
+
8
+ * Ruby 1.9.3+
9
+ * Ruby 1.9.2+
10
+ * Ruby 1.8.7+
11
+
12
+ == Install
13
+
14
+ You can install priam by gem.
15
+ gem install priam
16
+
17
+ == Usage
18
+
19
+ insert value:
20
+ $ cat input.txt
21
+ key1 val2
22
+ key2 val2
23
+ $ cat input.txt | priam insert -h cassandra-server -p 9160 --keyspace TestKS --column-family TestCF --verbose
24
+ [2012-12-12 10:16:46](INFO) insert into cassandra://cassandra-server:9160/TestKS/TestCF
25
+ [2012-12-12 10:16:46](INFO) inserted 2 columns into cassandra://cassandra-server:9160/TestKS/TestCF
26
+
27
+ get record in json:
28
+ $ cat keys.txt
29
+ key1
30
+ key2
31
+ $ cat keys.txt | priam get -h cassandra-server -p 9160 --keyspace TestKS --column-family TestCF --verbose
32
+ {"d":"val1"}
33
+ {"d":"val2"}
34
+ [2012-12-12 10:17:54](INFO) GET [key1,key2]
35
+
36
+ get column value:
37
+ $ cat keys.txt | priam get -h cassandra-server -p 9160 --keyspace TestKS --column-family TestCF --name d --verbose
38
+ val1
39
+ val2
40
+ [2012-12-12 10:29:42](INFO) GET [key1,key2]
41
+
42
+ delete key&value:
43
+ $ cat keys.txt | priam delete -h cassandra-server -p 9160 --keyspace TestKS --column-family TestCF --verbose
44
+ [2012-12-12 10:31:02](INFO) removed column 'key1'
45
+ [2012-12-12 10:31:02](INFO) removed column 'key2'
46
+ [2012-12-12 10:31:02](INFO) removed 2 columns from cassandra://cassandra-server:9160/TestKS/TestCF
47
+
48
+ == Contributing to priam
49
+
50
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
51
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
52
+ * Fork the project.
53
+ * Start a feature/bugfix branch.
54
+ * Commit and push until you are happy with your contribution.
55
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
56
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
57
+
58
+ == Copyright
59
+
60
+ Copyright (c) 2012 Kenji Hara. See LICENSE.txt for
61
+ further details.
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "priam"
18
+ gem.homepage = "http://github.com/haracane/priam"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Command-base client for Cassandra.}
21
+ gem.description = %Q{Command-base client for Cassandra.}
22
+ gem.email = "haracane@gmail.com"
23
+ gem.authors = ["Kenji Hara"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "priam #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "priam"
4
+
5
+ command = ARGV.shift
6
+
7
+ exit_code = 0
8
+
9
+ case command
10
+ when "cql"
11
+ exit_code = Priam::Command::Cql.run(ARGV) || 0
12
+ when "delete"
13
+ exit_code = Priam::Command::Delete.run(ARGV) || 0
14
+ when "get"
15
+ exit_code = Priam::Command::Get.run(ARGV) || 0
16
+ when "put"
17
+ exit_code = Priam::Command::Put.run(ARGV) || 0
18
+ when "insert"
19
+ exit_code = Priam::Command::Insert.run(ARGV) || 0
20
+ else
21
+ STDERR.puts "Invalid command: '#{command}'"
22
+ exit_code = 1
23
+ end
24
+
25
+ exit exit_code
@@ -0,0 +1,33 @@
1
+ require "rubygems"
2
+ require "cassandra"
3
+
4
+ module Priam
5
+ def self.logger
6
+ if @logger.nil?
7
+ @logger = (rails_logger || default_logger)
8
+ @logger.formatter = proc { |severity, datetime, progname, msg|
9
+ datetime.strftime("[%Y-%m-%d %H:%M:%S](#{severity})#{msg}\n")
10
+ }
11
+ end
12
+ return @logger
13
+ end
14
+
15
+ def self.rails_logger
16
+ (defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger) ||
17
+ (defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER.respond_to?(:debug) && RAILS_DEFAULT_LOGGER)
18
+ end
19
+
20
+ def self.default_logger
21
+ require 'logger'
22
+ l = Logger.new(STDERR)
23
+ l.level = Logger::INFO
24
+ l
25
+ end
26
+
27
+ def self.logger=(logger)
28
+ @logger = logger
29
+ end
30
+ end
31
+
32
+ require "priam/command"
33
+ require "priam/core"
@@ -0,0 +1,9 @@
1
+ module Priam::Command
2
+ end
3
+
4
+ require "priam/command/cql"
5
+ require "priam/command/delete"
6
+ require "priam/command/get"
7
+ require "priam/command/insert"
8
+ require "priam/command/put"
9
+
@@ -0,0 +1,17 @@
1
+ require "priam/command/cql/create"
2
+
3
+ module Priam::Command
4
+ module Cql
5
+ def self.run(argv, input_stream=$stdin, output_stream=$stdout)
6
+ command = argv.shift
7
+ case command
8
+ when "create"
9
+ exit_code = Priam::Command::Cql::Create.run(argv) || 0
10
+ else
11
+ STDERR.puts "Invalid cql command: '#{command}'"
12
+ exit_code = 1
13
+ end
14
+ return exit_code
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,30 @@
1
+ module Priam::Command::Cql
2
+ module Create
3
+ def self.run(argv, input_stream=$stdin, output_stream=$stdout)
4
+ params = Priam::Core::Common.parse_opts(argv)
5
+ replication_factor = params[:replication_factor]
6
+ keyspace = params[:keyspace]
7
+ column_family = params[:column_family]
8
+ super_column = params[:super_column]
9
+
10
+ if keyspace
11
+ output_stream.puts <<-EOF
12
+ create keyspace #{keyspace}
13
+ with strategy_options={replication_factor:#{replication_factor}}
14
+ and placement_strategy = 'org.apache.cassandra.locator.SimpleStrategy';
15
+ EOF
16
+ if column_family
17
+ output_stream.puts <<-EOF
18
+ use #{keyspace};
19
+ create column family #{column_family}
20
+ with column_type = Standard -- or Super
21
+ and comparator = BytesType
22
+ and subcomparator = BytesType;
23
+ EOF
24
+ end
25
+ end
26
+
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,58 @@
1
+ module Priam::Command
2
+ module Delete
3
+ def self.run(argv, input_stream=$stdin, output_stream=$stdout)
4
+ params = Priam::Core::Common.parse_opts(argv)
5
+ host = params[:host]
6
+ port = params[:port]
7
+ keyspace = params[:keyspace]
8
+ column_family = params[:column_family]
9
+ super_column = params[:super_column]
10
+ raise_exception_flag = params[:raise_exception_flag]
11
+ check_exist_flag = params[:check_exist_flag]
12
+ output_keys_flag = params[:output_keys_flag]
13
+
14
+ client = Cassandra.new(keyspace, "#{host}:#{port}")
15
+
16
+ Priam.logger.debug "Cluster Name: #{client.cluster_name}"
17
+ Priam.logger.debug "Key Space : #{keyspace}"
18
+
19
+ count = 0
20
+
21
+ while line = input_stream.gets do
22
+ line.chomp!
23
+ key = line
24
+ begin
25
+ if super_column then
26
+ if check_exist_flag && !client.exists?(column_family, super_column, key) then
27
+ Priam.logger.info " column '#{key}' does not exist"
28
+ else
29
+ client.remove(column_family, super_column, key)
30
+ Priam.logger.info " removed column '#{key}'" if output_keys
31
+ count +=1
32
+ end
33
+ elsif column_family then
34
+ if check_exist_flag && !client.exists?(column_family, key) then
35
+ Priam.logger.info " column '#{key}' does not exist"
36
+ else
37
+ client.remove(column_family, key)
38
+ Priam.logger.info " removed column '#{key}'"
39
+ count +=1
40
+ end
41
+ end
42
+ rescue Exception=>e
43
+ if raise_exception_flag then
44
+ raise e
45
+ else
46
+ backtrace = e.backtrace.map{|s| " #{s}"}.join("\n")
47
+ Priam.logger.warn(" #{e.message}(#{e.class.name}): #{backtrace}")
48
+ next
49
+ end
50
+ end
51
+ end
52
+
53
+ uri = "cassandra://#{host}:#{port}/#{keyspace}/#{column_family}"
54
+ uri += "/#{super_column}" if super_column
55
+ Priam.logger.info " removed #{count} columns from #{uri}"
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,59 @@
1
+ module Priam::Command
2
+ module Get
3
+ def self.run(argv, input_stream=$stdin, output_stream=$stdout)
4
+ params = Priam::Core::Common.parse_opts(argv)
5
+ host = params[:host]
6
+ port = params[:port]
7
+ keyspace = params[:keyspace]
8
+ column_family = params[:column_family]
9
+ super_column = params[:super_column]
10
+ name = params[:name]
11
+
12
+ client = Cassandra.new(keyspace, "#{host}:#{port}")
13
+
14
+ Priam.logger.debug "Cluster Name: #{client.cluster_name}"
15
+ Priam.logger.debug "Key Space : #{keyspace}"
16
+
17
+ key_list = []
18
+ count = 0
19
+
20
+ while line = input_stream.gets do
21
+ key = line.chomp
22
+ key_list.push key
23
+ if key.nil? || key == '' then
24
+ output_stream.puts
25
+ next
26
+ end
27
+
28
+ begin
29
+ if super_column then
30
+ record = client.get(column_family, super_column, key)
31
+ else
32
+ record = client.get(column_family, key)
33
+ end
34
+
35
+ if name then
36
+ output_stream.puts "#{record[name]}"
37
+ else
38
+ output_stream.puts record.to_json
39
+ end
40
+ count += 1
41
+ if count % 10 == 0 then
42
+ Priam.logger.info " GET [#{key_list.join(',')}]"
43
+ key_list.clear
44
+ end
45
+ rescue Exception => e
46
+ raise e.class, "#{e.message}, key list = #{key_list.inspect}"
47
+ end
48
+ end
49
+
50
+ if key_list != [] then
51
+ Priam.logger.info " GET [#{key_list.join(',')}]"
52
+ end
53
+
54
+ uri = "cassandra://#{host}:#{port}/#{keyspace}/#{column_family}"
55
+ uri += "/#{super_column}" if super_column
56
+ Priam.logger.info " got #{count} columns from #{uri}"
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,114 @@
1
+ module Priam::Command
2
+ module Insert
3
+ def self.run(argv, input_stream=$stdin, output_stream=$stdout)
4
+ params = Priam::Core::Common.parse_opts(argv)
5
+ host = params[:host]
6
+ port = params[:port]
7
+ keyspace = params[:keyspace]
8
+ column_family = params[:column_family]
9
+ super_column = params[:super_column]
10
+ raise_exception_flag = params[:raise_exception_flag]
11
+ unit_size = params[:unit_size]
12
+ value_column = params[:value_column]
13
+ weight_second = params[:weight_second]
14
+ retry_max_count = params[:retry_max_count]
15
+ count_log_path = params[:count_log_path]
16
+
17
+ keyspace = argv.shift if 0 < argv.length
18
+ column_family = argv.shift if 0 < argv.length
19
+ super_column = argv.shift if 0 < argv.length
20
+
21
+ if keyspace.nil? then
22
+ Priam.logger.error " keyspace is not specified"
23
+ exit 1
24
+ end
25
+
26
+ if column_family.nil? then
27
+ Priam.logger.error " column_family is not specified"
28
+ exit 1
29
+ end
30
+
31
+ client = Cassandra.new(keyspace, "#{host}:#{port}")
32
+
33
+ exit 1 if client.nil?
34
+
35
+ Priam.logger.debug "Cluster Name: #{client.cluster_name}"
36
+ Priam.logger.debug "Key Space : #{keyspace}"
37
+
38
+ column_family = column_family.intern
39
+
40
+ # client.remove(column_family, target_date)
41
+
42
+ if !super_column.nil? then
43
+ exist_flag = false
44
+
45
+ begin
46
+ exist_flag = client.exists?(column_family, super_column)
47
+ rescue Exception=>e
48
+ exist_flag = false
49
+ end
50
+
51
+ if !exist_flag then
52
+ Priam.logger.info " create super column cassandra://#{host}:#{port}/#{keyspace}/#{column_family}/#{super_column}]"
53
+ client.insert(column_family, super_column, {})
54
+ end
55
+ Priam.logger.info " insert into cassandra://#{host}:#{port}/#{keyspace}/#{column_family}/#{super_column}"
56
+ else
57
+ Priam.logger.info " insert into cassandra://#{host}:#{port}/#{keyspace}/#{column_family}"
58
+ end
59
+
60
+
61
+ count = 0
62
+ while line = input_stream.gets do
63
+ line.chomp!
64
+ next if line == ''
65
+ record = line.chomp.split(/\t/, 2)
66
+ # record = record.map{|val| DbEscape.db_unescape(val)}
67
+ key = record.shift
68
+ value = record.shift || ""
69
+
70
+ # STDERR.puts "insert #{record.to_json}"
71
+ retry_count = 0
72
+ begin
73
+ # STDERR.puts "#{retry_count} #{retry_max_count}"
74
+ # raise "Test Error"
75
+ if super_column then
76
+ client.insert(column_family, super_column, {key=>{value_column=>value}})
77
+ else
78
+ client.insert(column_family, key, {value_column=>value})
79
+ end
80
+ count += 1
81
+ if count % unit_size == 0 then
82
+ Priam.logger.info " inserted #{count} columns"
83
+ end
84
+ rescue Exception=>e
85
+ if retry_max_count <= retry_count then
86
+ if raise_exception_flag then
87
+ raise e
88
+ else
89
+ Priam.logger.warn(" #{e.message}(#{e.class.name}): #{e.backtrace.map{|s| " #{s}"}.join("\n")}")
90
+ end
91
+ else
92
+ retry_count += 1
93
+ Priam.logger.warn(" #{e.message}(#{e.class.name})")
94
+ Priam.logger.info(" retry(#{retry_count})")
95
+ sleep(weight_second) if weight_second
96
+ retry
97
+ end
98
+ end
99
+ sleep(weight_second) if weight_second
100
+ end
101
+
102
+ if count_log_path
103
+ open(count_log_path, "w") do |f|
104
+ f.puts count
105
+ end
106
+ end
107
+
108
+ uri = "cassandra://#{host}:#{port}/#{keyspace}/#{column_family}"
109
+ uri += "/#{super_column}" if super_column
110
+ Priam.logger.info " inserted #{count} columns into #{uri}"
111
+
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,6 @@
1
+ module Priam::Command
2
+ module Put
3
+ def self.run(argv, input_stream=$stdin, output_stream=$stdout)
4
+ end
5
+ end
6
+ end
@@ -0,0 +1 @@
1
+ require "priam/core/common"
@@ -0,0 +1,88 @@
1
+ module Priam::Core
2
+ module Common
3
+ def self.parse_opts(argv)
4
+ host = 'localhost'
5
+ port = 9160
6
+ raise_exception_flag = false
7
+
8
+ value_column = "d"
9
+ unit_size = 10000
10
+ retry_max_count = 0
11
+
12
+ check_exist_flag = false
13
+ output_keys_flag = false
14
+
15
+ replication_factor = 1
16
+
17
+ verbose_flag = false
18
+
19
+ next_argv = []
20
+
21
+ while 0 < argv.size do
22
+ val = argv.shift
23
+ case val
24
+ when '-h'
25
+ host = argv.shift
26
+ when '-p'
27
+ port = argv.shift.to_i
28
+ when '--keyspace'
29
+ keyspace = argv.shift
30
+ when '--column-family'
31
+ column_family = argv.shift
32
+ when '--super-column'
33
+ super_column = argv.shift
34
+ when '--name'
35
+ name = argv.shift
36
+ when '--raise-exception'
37
+ raise_exception_flag = true
38
+ when '--unit-size'
39
+ unit_size = argv.shift.to_i
40
+ when '--value-column'
41
+ value_column = argv.shift
42
+ when '--weight'
43
+ weight_second = argv.shift.to_f / 1000
44
+ when '--retry'
45
+ retry_max_count = argv.shift.to_i
46
+ when '--count-log'
47
+ count_log_path = argv.shift
48
+ when '--check-exist'
49
+ check_exist_flag = true
50
+ when '--output-keys'
51
+ output_keys_flag = true
52
+ when '--replication-factor'
53
+ replication_factor = argv.shift.to_i
54
+ when '--verbose'
55
+ verbose_flag = true
56
+ else
57
+ next_argv.push val
58
+ end
59
+ end
60
+ argv.push(*next_argv)
61
+
62
+ if verbose_flag then
63
+ Priam.logger.level = Logger::INFO
64
+ else
65
+ Priam.logger.level = Logger::WARN
66
+ end
67
+
68
+ return {
69
+ :host=>host,
70
+ :port=>port,
71
+ :keyspace=>keyspace,
72
+ :column_family=>column_family,
73
+ :super_column=>super_column,
74
+ :name=>name,
75
+ :raise_exception_flag=>raise_exception_flag,
76
+ :unit_size=>unit_size,
77
+ :value_column=>value_column,
78
+ :weight_second=>weight_second,
79
+ :retry_max_count=>retry_max_count,
80
+ :count_log_path=>count_log_path,
81
+ :check_exist_flag=>check_exist_flag,
82
+ :output_keys_flag=>output_keys_flag,
83
+ :replication_factor=>replication_factor,
84
+ :verbose_flag=>verbose_flag
85
+ }
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,99 @@
1
+ require "spec_helper"
2
+
3
+ describe "bin/priam" do
4
+ before :all do
5
+ @stderr_dst = Priam::REDIRECT[:stderr]
6
+ PriamTest.create_test_schema()
7
+ @hostname = `hostname`.chomp
8
+ end
9
+
10
+ after :all do
11
+ PriamTest.drop_test_schema()
12
+ end
13
+
14
+ before :each do
15
+ input = [
16
+ ["key1", "val1"],
17
+ ]
18
+ tmpfile = Tempfile.new("bin_priam")
19
+ input.each do |record|
20
+ tmpfile.puts record.join("\t")
21
+ end
22
+ tmpfile.close
23
+ `cat #{tmpfile.path} | #{Priam::RUBY_CMD} -I #{Priam::LIB_DIR} #{Priam::BIN_DIR}/priam insert --keyspace PriamTest --column-family PriamCF -h #{@hostname} -p 9160 2> /dev/null`
24
+ tmpfile.unlink
25
+ end
26
+
27
+ context "when command = cql" do
28
+ context "when sub-command = create" do
29
+ it "should output create cql" do
30
+ result = `#{Priam::RUBY_CMD} -I #{Priam::LIB_DIR} #{Priam::BIN_DIR}/priam cql create --keyspace PriamTest --column-family PriamCF`
31
+ result.should =~ /create keyspace PriamTest/
32
+ result.should =~ /create column family PriamCF/
33
+ end
34
+ end
35
+ end
36
+
37
+ context "when command = insert" do
38
+ it "should insert values" do
39
+ input = [
40
+ ["key1", "val10"],
41
+ ["key2", "val2"]
42
+ ]
43
+ tmpfile = Tempfile.new("bin_priam")
44
+ input.each do |record|
45
+ tmpfile.puts record.join("\t")
46
+ end
47
+ tmpfile.close
48
+ `cat #{tmpfile.path} | #{Priam::RUBY_CMD} -I #{Priam::LIB_DIR} #{Priam::BIN_DIR}/priam insert --keyspace PriamTest --column-family PriamCF -h #{@hostname} -p 9160 --verbose #{@stderr_dst}`
49
+ tmpfile.unlink
50
+
51
+ result = `echo key1 | #{Priam::RUBY_CMD} -I #{Priam::LIB_DIR} #{Priam::BIN_DIR}/priam get --keyspace PriamTest --column-family PriamCF -h #{@hostname} -p 9160 --name d 2> /dev/null`
52
+ result.chomp!
53
+ result.should == "val10"
54
+
55
+ result = `echo key2 | #{Priam::RUBY_CMD} -I #{Priam::LIB_DIR} #{Priam::BIN_DIR}/priam get --keyspace PriamTest --column-family PriamCF -h #{@hostname} -p 9160 --name d 2> /dev/null`
56
+ result.chomp!
57
+ result.should == "val2"
58
+ end
59
+ end
60
+
61
+ context "when command = get" do
62
+ context "when key&value exists" do
63
+ it "should output value" do
64
+
65
+ result = `echo key1 | #{Priam::RUBY_CMD} -I #{Priam::LIB_DIR} #{Priam::BIN_DIR}/priam get --keyspace PriamTest --column-family PriamCF -h #{@hostname} -p 9160 --name d --verbose #{@stderr_dst}`
66
+ result.chomp!
67
+ result.should == "val1"
68
+ end
69
+ end
70
+ context "when key&value does not exist" do
71
+ it "should not output value" do
72
+ result = `echo nokey | #{Priam::RUBY_CMD} -I #{Priam::LIB_DIR} #{Priam::BIN_DIR}/priam get --keyspace PriamTest --column-family PriamCF -h #{@hostname} -p 9160 --name d --verbose #{@stderr_dst}`
73
+ result.chomp!
74
+ result.should == ""
75
+ end
76
+ end
77
+ end
78
+
79
+ context "when command = delete" do
80
+ context "when key&value exists" do
81
+ it "should delete key&value" do
82
+ `echo key1 | #{Priam::RUBY_CMD} -I #{Priam::LIB_DIR} #{Priam::BIN_DIR}/priam delete --keyspace PriamTest --column-family PriamCF -h #{@hostname} -p 9160 --verbose #{@stderr_dst}`
83
+ result = `echo key1 | #{Priam::RUBY_CMD} -I #{Priam::LIB_DIR} #{Priam::BIN_DIR}/priam get --keyspace PriamTest --column-family PriamCF -h #{@hostname} -p 9160 --name d 2>/dev/null`
84
+ result.chomp!
85
+ result.should == ""
86
+ end
87
+ end
88
+
89
+ context "when key&value does not exist" do
90
+ it "should do nothing" do
91
+ `echo nokey | #{Priam::RUBY_CMD} -I #{Priam::LIB_DIR} #{Priam::BIN_DIR}/priam delete --keyspace PriamTest --column-family PriamCF -h #{@hostname} -p 9160 --verbose #{@stderr_dst}`
92
+ result = `echo nokey | #{Priam::RUBY_CMD} -I #{Priam::LIB_DIR} #{Priam::BIN_DIR}/priam get --keyspace PriamTest --column-family PriamCF -h #{@hostname} -p 9160 --name d 2>/dev/null`
93
+ result.chomp!
94
+ result.should == ""
95
+ end
96
+ end
97
+ end
98
+
99
+ end
@@ -0,0 +1,6 @@
1
+ require "spec_helper"
2
+
3
+ describe Priam::Command::Delete do
4
+ it "should success" do
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ require "spec_helper"
2
+
3
+ describe Priam::Command::Get do
4
+ it "should success" do
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ require "spec_helper"
2
+
3
+ describe Priam::Command::Insert do
4
+ it "should success" do
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ require "spec_helper"
2
+
3
+ describe Priam::Command::Put do
4
+ it "should success" do
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ require "spec_helper"
2
+
3
+ describe Priam::Core::Common do
4
+ it "should success" do
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ require "spec_helper"
2
+
3
+ describe Priam do
4
+ it "should success" do
5
+ end
6
+ end
@@ -0,0 +1,61 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'priam'
5
+ require "tempfile"
6
+
7
+ # Requires supporting files with custom matchers and macros, etc,
8
+ # in ./support/ and its subdirectories.
9
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
10
+
11
+ RSpec.configure do |config|
12
+
13
+ end
14
+
15
+ module Priam
16
+ PRIAM_HOME = File.expand_path(File.dirname(__FILE__) + "/..")
17
+ BIN_DIR = "#{PRIAM_HOME}/bin"
18
+ LIB_DIR = "#{PRIAM_HOME}/lib"
19
+ RUBY_CMD = "/usr/bin/env ruby -I #{LIB_DIR}"
20
+ REDIRECT = {:stderr=>"2> /dev/null"}
21
+ end
22
+
23
+ Priam.logger = Logger.new(STDERR)
24
+ if File.exist?('/tmp/priam.debug') then
25
+ Priam.logger.level = Logger::DEBUG
26
+ Priam::REDIRECT[:stderr] = nil
27
+ else
28
+ Priam.logger.level = Logger::ERROR
29
+ Priam::REDIRECT[:stderr] = "2> /dev/null"
30
+ end
31
+
32
+ module PriamTest
33
+ def self.create_test_schema
34
+ cql = <<-EOF
35
+ create keyspace PriamTest
36
+ with strategy_options={replication_factor:1}
37
+ and placement_strategy = 'org.apache.cassandra.locator.SimpleStrategy';
38
+ use PriamTest;
39
+ create column family PriamCF
40
+ with column_type = Standard -- or Super
41
+ and comparator = BytesType
42
+ and subcomparator = BytesType;
43
+ EOF
44
+ tmpfile = Tempfile.new("spec_helper")
45
+ tmpfile.puts cql
46
+ tmpfile.close
47
+ hostname = `hostname`.chomp
48
+ `echo "#{cql}" /usr/lib/cassandra/bin/cassandra-cli -h #{hostname} -p 9160 -f #{tmpfile.path}`
49
+ tmpfile.unlink
50
+ end
51
+
52
+ def self.drop_test_schema
53
+ cql = "drop keyspace PriamTest"
54
+ tmpfile = Tempfile.new("spec_helper")
55
+ tmpfile.puts cql
56
+ tmpfile.close
57
+ hostname = `hostname`.chomp
58
+ `/usr/lib/cassandra/bin/cassandra-cli -h #{hostname} -p 9160 -f #{tmpfile.path}`
59
+ tmpfile.unlink
60
+ end
61
+ end
metadata ADDED
@@ -0,0 +1,232 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: priam
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Kenji Hara
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-12-12 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ type: :runtime
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ - 7
31
+ - 0
32
+ version: 0.7.0
33
+ version_requirements: *id001
34
+ name: thrift
35
+ prerelease: false
36
+ - !ruby/object:Gem::Dependency
37
+ type: :runtime
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 1
44
+ segments:
45
+ - 0
46
+ - 7
47
+ - 1
48
+ version: 0.7.1
49
+ version_requirements: *id002
50
+ name: thrift_client
51
+ prerelease: false
52
+ - !ruby/object:Gem::Dependency
53
+ type: :runtime
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 23
60
+ segments:
61
+ - 0
62
+ - 2
63
+ - 0
64
+ version: 0.2.0
65
+ version_requirements: *id003
66
+ name: simple_uuid
67
+ prerelease: false
68
+ - !ruby/object:Gem::Dependency
69
+ type: :runtime
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ hash: 45
76
+ segments:
77
+ - 0
78
+ - 12
79
+ - 1
80
+ version: 0.12.1
81
+ version_requirements: *id004
82
+ name: cassandra
83
+ prerelease: false
84
+ - !ruby/object:Gem::Dependency
85
+ type: :development
86
+ requirement: &id005 !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ~>
90
+ - !ruby/object:Gem::Version
91
+ hash: 47
92
+ segments:
93
+ - 2
94
+ - 8
95
+ - 0
96
+ version: 2.8.0
97
+ version_requirements: *id005
98
+ name: rspec
99
+ prerelease: false
100
+ - !ruby/object:Gem::Dependency
101
+ type: :development
102
+ requirement: &id006 !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ~>
106
+ - !ruby/object:Gem::Version
107
+ hash: 31
108
+ segments:
109
+ - 3
110
+ - 12
111
+ version: "3.12"
112
+ version_requirements: *id006
113
+ name: rdoc
114
+ prerelease: false
115
+ - !ruby/object:Gem::Dependency
116
+ type: :development
117
+ requirement: &id007 !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ hash: 23
123
+ segments:
124
+ - 1
125
+ - 0
126
+ - 0
127
+ version: 1.0.0
128
+ version_requirements: *id007
129
+ name: bundler
130
+ prerelease: false
131
+ - !ruby/object:Gem::Dependency
132
+ type: :development
133
+ requirement: &id008 !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ~>
137
+ - !ruby/object:Gem::Version
138
+ hash: 63
139
+ segments:
140
+ - 1
141
+ - 8
142
+ - 4
143
+ version: 1.8.4
144
+ version_requirements: *id008
145
+ name: jeweler
146
+ prerelease: false
147
+ - !ruby/object:Gem::Dependency
148
+ type: :development
149
+ requirement: &id009 !ruby/object:Gem::Requirement
150
+ none: false
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ hash: 3
155
+ segments:
156
+ - 0
157
+ version: "0"
158
+ version_requirements: *id009
159
+ name: rcov
160
+ prerelease: false
161
+ description: Command-base client for Cassandra.
162
+ email: haracane@gmail.com
163
+ executables:
164
+ - priam
165
+ extensions: []
166
+
167
+ extra_rdoc_files:
168
+ - LICENSE.txt
169
+ - README.rdoc
170
+ files:
171
+ - .document
172
+ - .rspec
173
+ - Gemfile
174
+ - Gemfile.lock
175
+ - LICENSE.txt
176
+ - README.rdoc
177
+ - Rakefile
178
+ - VERSION
179
+ - bin/priam
180
+ - lib/priam.rb
181
+ - lib/priam/command.rb
182
+ - lib/priam/command/cql.rb
183
+ - lib/priam/command/cql/create.rb
184
+ - lib/priam/command/delete.rb
185
+ - lib/priam/command/get.rb
186
+ - lib/priam/command/insert.rb
187
+ - lib/priam/command/put.rb
188
+ - lib/priam/core.rb
189
+ - lib/priam/core/common.rb
190
+ - spec/bin/priam_spec.rb
191
+ - spec/lib/priam/command/delete_spec.rb
192
+ - spec/lib/priam/command/get_spec.rb
193
+ - spec/lib/priam/command/insert_spec.rb
194
+ - spec/lib/priam/command/put_spec.rb
195
+ - spec/lib/priam/core/common_spec.rb
196
+ - spec/lib/priam_spec.rb
197
+ - spec/spec_helper.rb
198
+ homepage: http://github.com/haracane/priam
199
+ licenses:
200
+ - MIT
201
+ post_install_message:
202
+ rdoc_options: []
203
+
204
+ require_paths:
205
+ - lib
206
+ required_ruby_version: !ruby/object:Gem::Requirement
207
+ none: false
208
+ requirements:
209
+ - - ">="
210
+ - !ruby/object:Gem::Version
211
+ hash: 3
212
+ segments:
213
+ - 0
214
+ version: "0"
215
+ required_rubygems_version: !ruby/object:Gem::Requirement
216
+ none: false
217
+ requirements:
218
+ - - ">="
219
+ - !ruby/object:Gem::Version
220
+ hash: 3
221
+ segments:
222
+ - 0
223
+ version: "0"
224
+ requirements: []
225
+
226
+ rubyforge_project:
227
+ rubygems_version: 1.8.21
228
+ signing_key:
229
+ specification_version: 3
230
+ summary: Command-base client for Cassandra.
231
+ test_files: []
232
+