Created
January 12, 2013 19:49
-
-
Save outoftime/4520182 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler' | |
Bundler.require | |
require 'benchmark' | |
database = CassandraCQL::Database.new('127.0.0.1:9160') | |
begin | |
database.execute("CREATE KEYSPACE bm_test WITH strategy_class='SimpleStrategy' AND strategy_options:replication_factor=1") | |
database.execute("USE bm_test") | |
database.execute("CREATE COLUMNFAMILY bm_test (KEY uuid PRIMARY KEY) WITH comparator = uuid AND default_validation = text") | |
uuid = CassandraCQL::UUID.new | |
bind = 10_000.times.flat_map { [CassandraCQL::UUID.new, ''] } | |
bind << uuid | |
statement = Array.new(10_000) { "? = ?" }.join(', ') | |
database.execute("UPDATE bm_test SET #{statement} WHERE KEY = ?", *bind) | |
Benchmark.bmbm(40) do |bm| | |
[1, 10, 100, 1000, 10_000].each do |columns| | |
bm.report("Array#index: #{columns} columns") do | |
$use_column_indices = false | |
database.execute("SELECT FIRST #{columns} * FROM bm_test WHERE KEY = ?", uuid).fetch_row.to_hash | |
end | |
bm.report("Hash lookup: #{columns} columns") do | |
$use_column_indices = true | |
database.execute("SELECT FIRST #{columns} * FROM bm_test WHERE KEY = ?", uuid).fetch_row.to_hash | |
end | |
end | |
end | |
ensure | |
database.execute("DROP KEYSPACE bm_test") | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
Copyright 2011 Inside Systems, Inc. | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
=end | |
module CassandraCQL | |
class Row | |
attr_reader :row | |
def initialize(row, schema) | |
@row, @schema = row, schema | |
@value_cache = Hash.new { |h, key| | |
# If it's a number and not one of our columns, assume it's an index | |
if key.kind_of?(Fixnum) and !column_names.include?(key) | |
column_name = column_names[key] | |
column_index = key | |
else | |
column_name = key | |
column_index = column_index(key) | |
end | |
if column_index.nil? | |
# Cache negative hits | |
h[column_name] = nil | |
else | |
h[column_name] = ColumnFamily.cast(@row.columns[column_index].value, @schema.values[@row.columns[column_index].name]) | |
end | |
} | |
end | |
def [](obj) | |
@value_cache[obj] | |
end | |
def column_names | |
@names ||= @row.columns.map do |column| | |
ColumnFamily.cast(column.name, @schema.names[column.name]) | |
end | |
end | |
def column_indices | |
@column_indices ||= Hash[column_names.each_with_index.to_a] | |
end | |
def column_index(column_name) | |
if defined?($use_column_indices) && $use_column_indices | |
column_indices[column_name] | |
else | |
column_names.index(column_name) | |
end | |
end | |
def column_values | |
column_names.map do |name| | |
@value_cache[name] | |
end | |
end | |
def columns | |
@row.columns.size | |
end | |
def to_a | |
column_values | |
end | |
# TODO: This should be an ordered hash | |
def to_hash | |
Hash[([column_names, column_values]).transpose] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment