Class: ActiveRecord::ConnectionAdapters::HypertableAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- ActiveRecord::ConnectionAdapters::HypertableAdapter
- Defined in:
- lib/active_record/connection_adapters/hypertable_adapter.rb
Constant Summary collapse
- @@read_latency =
Following cattr_accessors are used to record and access query performance statistics.
0.0
- @@write_latency =
0.0
- @@cells_read =
0
Instance Attribute Summary collapse
-
#retry_on_failure ⇒ Object
Returns the value of attribute retry_on_failure.
Class Method Summary collapse
-
.get_timing ⇒ Object
Return the current set of performance statistics.
-
.reset_timing ⇒ Object
Reset performance metrics.
Instance Method Summary collapse
- #adapter_name ⇒ Object
- #add_column(table_name, column_name, type = :string, options = {}) ⇒ Object
- #add_column_options!(hql, options) ⇒ Object
- #add_column_to_name_map(table_name, name) ⇒ Object
- #add_qualified_column(table_name, column_family, qualifiers = [], default = '', sql_type = nil, null = true) ⇒ Object
-
#cell_native_array(row_key, column_family, column_qualifier, value = nil, timestamp = nil) ⇒ Object
Create native array format for cell.
- #change_column(table_name, column_name, new_column_name) ⇒ Object
- #change_column_default(table_name, column_name, default) ⇒ Object
- #change_column_null(table_name, column_name, null, default = nil) ⇒ Object
-
#close_mutator(mutator, flush = 0) ⇒ Object
Flush is always called in a mutator’s destructor due to recent no_log_sync changes.
- #close_scanner(scanner) ⇒ Object
-
#columns(table_name, name = nil) ⇒ Object
Returns array of column objects for the table associated with this class.
-
#convert_options_to_scan_spec(options = {}) ⇒ Object
Convert an options hash to a scan spec.
- #create_table(table_name, options = {}, &block) ⇒ Object
-
#create_table_hql(table_name, options = {}) {|table_definition| ... } ⇒ Object
Translate “sexy” ActiveRecord::Migration syntax to an HQL CREATE TABLE statement.
-
#delete_cells(table_name, cells) ⇒ Object
Delete cells from a table.
-
#delete_rows(table_name, row_keys) ⇒ Object
Delete rows from a table.
-
#describe_table(table_name) ⇒ Object
Return an XML document describing the table named in the first argument.
- #drop_table(table_name, options = {}) ⇒ Object
-
#each_cell(scanner, &block) ⇒ Object
Iterator methods.
- #each_cell_as_arrays(scanner, &block) ⇒ Object
- #each_row(scanner, &block) ⇒ Object
- #each_row_as_arrays(scanner, &block) ⇒ Object
-
#execute(hql, name = nil) ⇒ Object
Execute an HQL query against Hypertable and return the native HqlResult object that comes back from the Thrift client API.
-
#execute_with_options(options) ⇒ Object
Execute a query against Hypertable and return the matching cells.
- #flush_mutator(mutator) ⇒ Object
-
#handle_thrift_exceptions_with_missing_message ⇒ Object
Exceptions generated by Thrift IDL do not set a message.
- #hypertable_column_name(name, table_name, declared_columns_only = false) ⇒ Object
-
#initialize(connection, logger, config) ⇒ HypertableAdapter
constructor
A new instance of HypertableAdapter.
-
#insert_fixture(fixture, table_name) ⇒ Object
Insert a test fixture into a table.
- #is_qualified_column_name?(column_name) ⇒ Boolean
-
#native_database_types ⇒ Object
Hypertable only supports string types at the moment, so treat all values as strings and leave it to the application to handle types.
- #new_column(column_name, default_value = '') ⇒ Object
-
#open_mutator(table_name, flags = 0, flush_interval = 0) ⇒ Object
Mutator methods.
-
#open_scanner(table_name, scan_spec) ⇒ Object
Scanner methods.
- #qualified_column_name(column_family, qualifier = nil) ⇒ Object
- #quote(value, column = nil) ⇒ Object
- #quote_column_name(name) ⇒ Object
- #quote_column_name_for_table(name, table_name) ⇒ Object
- #raw_thrift_client(&block) ⇒ Object
- #remove_column(table_name, *column_names) ⇒ Object (also: #remove_columns)
- #remove_column_from_name_map(table_name, name) ⇒ Object
-
#rename_column(table_name, column_name, new_column_name) ⇒ Object
Schema alterations.
- #rename_table(table_name, options = {}) ⇒ Object
- #requested_columns_from_string(s) ⇒ Object
-
#retry_on_connection_error ⇒ Object
Attempt to reconnect to the Thrift Broker once before aborting.
- #rubify_column_name(column_name) ⇒ Object
- #sanitize_conditions(options) ⇒ Object
- #supports_migrations? ⇒ Boolean
-
#tables(name = nil) ⇒ Object
Returns an array of tables available in the current Hypertable instance.
-
#thrift_cell_from_native_array(array) ⇒ Object
Return a Hypertable::ThriftGen::Cell object from a cell passed in as an array of format: [row_key, column_name, value] Hypertable::ThriftGen::Cell objects are required when setting a flag on write - used by special operations (e.g,. delete ).
- #with_scanner(table_name, scan_spec, &block) ⇒ Object
-
#write_cells(table_name, cells, options = {}) ⇒ Object
Write an array of cells to the named table.
Constructor Details
#initialize(connection, logger, config) ⇒ HypertableAdapter
Returns a new instance of HypertableAdapter.
78 79 80 81 82 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 78 def initialize(connection, logger, config) super(connection, logger) @config = config @hypertable_column_names = {} end |
Instance Attribute Details
#retry_on_failure ⇒ Object
Returns the value of attribute retry_on_failure.
76 77 78 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 76 def retry_on_failure @retry_on_failure end |
Class Method Details
.get_timing ⇒ Object
Return the current set of performance statistics. The application can retrieve (and reset) these statistics after every query or request for its own logging purposes.
95 96 97 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 95 def self.get_timing [@@read_latency, @@write_latency, @@cells_read] end |
.reset_timing ⇒ Object
Reset performance metrics.
100 101 102 103 104 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 100 def self.reset_timing @@read_latency = 0.0 @@write_latency = 0.0 @@cells_read = 0 end |
Instance Method Details
#adapter_name ⇒ Object
106 107 108 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 106 def adapter_name 'Hypertable' end |
#add_column(table_name, column_name, type = :string, options = {}) ⇒ Object
442 443 444 445 446 447 448 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 442 def add_column(table_name, column_name, type=:string, = {}) hql = [ "ALTER TABLE #{quote_table_name(table_name)} ADD (" ] hql << quote_column_name(column_name) hql << "MAX_VERSIONS=#{[:max_versions]}" if ![:max_versions].blank? hql << ")" execute(hql.join(' ')) end |
#add_column_options!(hql, options) ⇒ Object
450 451 452 453 454 455 456 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 450 def (hql, ) hql << " MAX_VERSIONS =1 #{quote([:default], [:column])}" if () # must explicitly check for :null to allow change_column to work on migrations if [:null] == false hql << " NOT NULL" end end |
#add_column_to_name_map(table_name, name) ⇒ Object
353 354 355 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 353 def add_column_to_name_map(table_name, name) @hypertable_column_names[table_name][rubify_column_name(name)] = name end |
#add_qualified_column(table_name, column_family, qualifiers = [], default = '', sql_type = nil, null = true) ⇒ Object
357 358 359 360 361 362 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 357 def add_qualified_column(table_name, column_family, qualifiers=[], default='', sql_type=nil, null=true) qc = QualifiedColumn.new(column_family, default, sql_type, null) qc.qualifiers = qualifiers qualifiers.each{|q| add_column_to_name_map(table_name, qualified_column_name(column_family, q))} qc end |
#cell_native_array(row_key, column_family, column_qualifier, value = nil, timestamp = nil) ⇒ Object
Create native array format for cell. Most HyperRecord operations deal with cells in native array format since operations on an array are much faster than operations on Hypertable::ThriftGen::Cell objects. [“row_key”, “column_family”, “column_qualifier”, “value”],
585 586 587 588 589 590 591 592 593 594 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 585 def cell_native_array(row_key, column_family, column_qualifier, value=nil, =nil) [ row_key.to_s, column_family.to_s, column_qualifier.to_s, value.to_s ].map do |s| s.respond_to?(:force_encoding) ? s.force_encoding('ascii-8bit') : s end end |
#change_column(table_name, column_name, new_column_name) ⇒ Object
391 392 393 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 391 def change_column(table_name, column_name, new_column_name) raise "change_column operation not supported by Hypertable." end |
#change_column_default(table_name, column_name, default) ⇒ Object
434 435 436 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 434 def change_column_default(table_name, column_name, default) raise "change_column_default operation not supported by Hypertable." end |
#change_column_null(table_name, column_name, null, default = nil) ⇒ Object
438 439 440 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 438 def change_column_null(table_name, column_name, null, default = nil) raise "change_column_null operation not supported by Hypertable." end |
#close_mutator(mutator, flush = 0) ⇒ Object
Flush is always called in a mutator’s destructor due to recent no_log_sync changes. Adding an explicit flush here just adds one round trip for an extra flush call, so change the default to flush=0. Consider removing this argument and always sending 0.
657 658 659 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 657 def close_mutator(mutator, flush=0) @connection.close_mutator(mutator, flush) end |
#close_scanner(scanner) ⇒ Object
671 672 673 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 671 def close_scanner(scanner) @connection.close_scanner(scanner) end |
#columns(table_name, name = nil) ⇒ Object
Returns array of column objects for the table associated with this class. Hypertable allows columns to include dashes in the name.
This doesn’t play well with Ruby (can’t have dashes in method names), so we maintain a mapping of original column names to Ruby-safe names.
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 324 def columns(table_name, name = nil) # Each table always has a row key called 'ROW' columns = [ Column.new('ROW', '') ] schema = describe_table(table_name) doc = REXML::Document.new(schema) column_families = doc.each_element('Schema/AccessGroup/ColumnFamily') { |cf| cf } @hypertable_column_names[table_name] ||= {} for cf in column_families # Columns are lazily-deleted in Hypertable so still may show up # in describe table output. Ignore. del = cf.elements['deleted'] deleted = del ? del.text : 'false' next if deleted == 'true' column_name = cf.elements['Name'].text rubified_name = rubify_column_name(column_name) @hypertable_column_names[table_name][rubified_name] = column_name columns << new_column(rubified_name, '') end columns end |
#convert_options_to_scan_spec(options = {}) ⇒ Object
Convert an options hash to a scan spec. A scan spec is native representation of the query parameters that must be sent to Hypertable. hypertable.org/thrift-api-ref/Client.html#Struct_ScanSpec
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 175 def (={}) sanitize_conditions() # Rows can be specified using a number of different options: # :row_keys => [row_key_1, row_key_2, ...] # :start_row and :end_row # :row_intervals => [[start_1, end_1], [start_2, end_2]] row_intervals = [] [:start_inclusive] = .has_key?(:start_inclusive) ? [:start_inclusive] : true [:end_inclusive] = .has_key?(:end_inclusive) ? [:end_inclusive] : true if [:row_keys] [:row_keys].flatten.each do |rk| row_intervals << [rk, rk] end elsif [:row_intervals] [:row_intervals].each do |ri| row_intervals << [ri.first, ri.last] end elsif [:start_row] raise "missing :end_row" if ![:end_row] row_intervals << [[:start_row], [:end_row]] end # Add each row interval to the scan spec [:row_intervals] = row_intervals.map do |row_interval| ri = Hypertable::ThriftGen::RowInterval.new ri.start_row = row_interval.first ri.start_inclusive = [:start_inclusive] ri.end_row = row_interval.last ri.end_inclusive = [:end_inclusive] ri end scan_spec = Hypertable::ThriftGen::ScanSpec.new # Hypertable can store multiple revisions for each cell but this # feature does not map into an ORM very well. By default, just # retrieve the latest revision of each cell. Since this is most # common config when using HyperRecord, tables should be defined # with MAX_VERSIONS=1 at creation time to save space and reduce # query time. [:revs] ||= 1 # Most of the time, we're not interested in cells that have been # marked deleted but have not actually been deleted yet. [:return_deletes] ||= false for key in .keys case key.to_sym when :row_intervals scan_spec.row_intervals = [key] when :cell_intervals scan_spec.cell_intervals = [key] when :start_time scan_spec.start_time = [key] when :end_time scan_spec.end_time = [key] when :limit scan_spec.row_limit = [key] when :revs scan_spec.revs = [key] when :return_deletes scan_spec.return_deletes = [key] when :select # Columns listed here can only be column families (not # column qualifiers) at this time. requested_columns = if [key].is_a?(String) requested_columns_from_string([key]) elsif [key].is_a?(Symbol) requested_columns_from_string([key].to_s) elsif [key].is_a?(Array) [key].map{|k| k.to_s} else [key] end scan_spec.columns = requested_columns.map do |column| status, family, qualifier = is_qualified_column_name?(column) family end.uniq when :table_name, :start_row, :end_row, :start_inclusive, :end_inclusive, :select, :columns, :row_keys, :conditions, :include, :readonly, :scan_spec, :instantiate_only_requested_columns # ignore else raise "Unrecognized scan spec option: #{key}" end end scan_spec end |
#create_table(table_name, options = {}, &block) ⇒ Object
420 421 422 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 420 def create_table(table_name, ={}, &block) execute(create_table_hql(table_name, , &block)) end |
#create_table_hql(table_name, options = {}) {|table_definition| ... } ⇒ Object
Translate “sexy” ActiveRecord::Migration syntax to an HQL CREATE TABLE statement.
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 397 def create_table_hql(table_name, ={}, &block) table_definition = HyperTableDefinition.new(self) yield table_definition if [:force] && table_exists?(table_name) drop_table(table_name, ) end create_sql = [ "CREATE TABLE #{quote_table_name(table_name)} (" ] column_sql = [] for col in table_definition.columns column_sql << [ quote_table_name(col.name), col.max_versions ? "MAX_VERSIONS=#{col.max_versions}" : '' ].join(' ') end create_sql << column_sql.join(', ') create_sql << ") #{[:options]}" create_sql.join(' ').strip end |
#delete_cells(table_name, cells) ⇒ Object
Delete cells from a table.
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 597 def delete_cells(table_name, cells) t1 = Time.now retry_on_connection_error { @connection.with_mutator(table_name) do |mutator| thrift_cells = cells.map{|c| cell = thrift_cell_from_native_array(c) cell.key.flag = Hypertable::ThriftGen::CellFlag::DELETE_CELL cell } @connection.set_cells(mutator, thrift_cells) end } @@write_latency += Time.now - t1 end |
#delete_rows(table_name, row_keys) ⇒ Object
Delete rows from a table.
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 615 def delete_rows(table_name, row_keys) t1 = Time.now cells = row_keys.map do |row_key| cell = Hypertable::ThriftGen::Cell.new cell.key = Hypertable::ThriftGen::Key.new cell.key.row = row_key cell.key.flag = Hypertable::ThriftGen::CellFlag::DELETE_ROW cell end retry_on_connection_error { @connection.with_mutator(table_name) do |mutator| @connection.set_cells(mutator, cells) end } @@write_latency += Time.now - t1 end |
#describe_table(table_name) ⇒ Object
Return an XML document describing the table named in the first argument. Output is equivalent to that returned by the DESCRIBE TABLE command available in the Hypertable CLI. <Schema generation=“2”>
<AccessGroup name="default">
<ColumnFamily id="1">
<Generation>1</Generation>
<Name>date</Name>
<deleted>false</deleted>
</ColumnFamily>
</AccessGroup>
</Schema>
510 511 512 513 514 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 510 def describe_table(table_name) retry_on_connection_error { @connection.get_schema(table_name) } end |
#drop_table(table_name, options = {}) ⇒ Object
424 425 426 427 428 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 424 def drop_table(table_name, = {}) retry_on_connection_error { @connection.drop_table(table_name, [:if_exists] || false) } end |
#each_cell(scanner, &block) ⇒ Object
Iterator methods
681 682 683 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 681 def each_cell(scanner, &block) @connection.each_cell(scanner, &block) end |
#each_cell_as_arrays(scanner, &block) ⇒ Object
685 686 687 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 685 def each_cell_as_arrays(scanner, &block) @connection.each_cell_as_arrays(scanner, &block) end |
#each_row(scanner, &block) ⇒ Object
689 690 691 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 689 def each_row(scanner, &block) @connection.each_row(scanner, &block) end |
#each_row_as_arrays(scanner, &block) ⇒ Object
693 694 695 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 693 def each_row_as_arrays(scanner, &block) @connection.each_row_as_arrays(scanner, &block) end |
#execute(hql, name = nil) ⇒ Object
Execute an HQL query against Hypertable and return the native HqlResult object that comes back from the Thrift client API.
137 138 139 140 141 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 137 def execute(hql, name=nil) log(hql, name) { retry_on_connection_error { @connection.hql_query(hql) } } end |
#execute_with_options(options) ⇒ Object
Execute a query against Hypertable and return the matching cells. The query parameters are denoted in an options hash, which is converted to a “scan spec” by convert_options_to_scan_spec. A “scan spec” is the mechanism used to specify query parameters (e.g., the columns to retrieve, the number of rows to retrieve, etc.) to Hypertable.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 149 def () scan_spec = () t1 = Time.now # Use native array method (get_cells_as_arrays) for cell retrieval - # much faster than get_cells that returns Hypertable::ThriftGen::Cell # objects. # [ # ["page_1", "name", "", "LOLcats and more", "1237331693147619001"], # ["page_1", "url", "", "http://...", "1237331693147619002"] # ] cells = retry_on_connection_error { @connection.get_cells_as_arrays([:table_name], scan_spec) } # Capture performance metrics @@read_latency += Time.now - t1 @@cells_read += cells.length cells end |
#flush_mutator(mutator) ⇒ Object
661 662 663 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 661 def flush_mutator(mutator) @connection.flush_mutator(mutator) end |
#handle_thrift_exceptions_with_missing_message ⇒ Object
Exceptions generated by Thrift IDL do not set a message. This causes a lot of problems for Rails which expects a String value and throws exception when it encounters NilClass. Unfortunately, you cannot assign a message to exceptions so define a singleton to accomplish same goal.
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 280 def begin yield rescue Exception => err if !err. if err.respond_to?("message=") err. = err.what || '' else def err. self.what || '' end end end raise err end end |
#hypertable_column_name(name, table_name, declared_columns_only = false) ⇒ Object
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 481 def hypertable_column_name(name, table_name, declared_columns_only=false) begin columns(table_name) if @hypertable_column_names[table_name].blank? n = @hypertable_column_names[table_name][name] n ||= name if !declared_columns_only n rescue Exception => err raise [ "hypertable_column_name exception", err., "table: #{table_name}", "column: #{name}", "@htcn: #{pp @hypertable_column_names}" ].join("\n") end end |
#insert_fixture(fixture, table_name) ⇒ Object
Insert a test fixture into a table.
635 636 637 638 639 640 641 642 643 644 645 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 635 def insert_fixture(fixture, table_name) fixture_hash = fixture.to_hash = fixture_hash.delete('timestamp') row_key = fixture_hash.delete('ROW') cells = [] fixture_hash.keys.each do |k| column_name, column_family = k.split(':', 2) cells << cell_native_array(row_key, column_name, column_family, fixture_hash[k], ) end write_cells(table_name, cells) end |
#is_qualified_column_name?(column_name) ⇒ Boolean
376 377 378 379 380 381 382 383 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 376 def is_qualified_column_name?(column_name) column_family, qualifier = column_name.split(':', 2) if qualifier [true, column_family, qualifier] else [false, column_name, nil] end end |
#native_database_types ⇒ Object
Hypertable only supports string types at the moment, so treat all values as strings and leave it to the application to handle types.
117 118 119 120 121 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 117 def native_database_types { :string => { :name => "varchar", :limit => 255 } } end |
#new_column(column_name, default_value = '') ⇒ Object
364 365 366 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 364 def new_column(column_name, default_value='') Column.new(rubify_column_name(column_name), default_value) end |
#open_mutator(table_name, flags = 0, flush_interval = 0) ⇒ Object
Mutator methods
649 650 651 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 649 def open_mutator(table_name, flags=0, flush_interval=0) @connection.open_mutator(table_name, flags, flush_interval) end |
#open_scanner(table_name, scan_spec) ⇒ Object
Scanner methods
667 668 669 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 667 def open_scanner(table_name, scan_spec) @connection.open_scanner(table_name, scan_spec, true) end |
#qualified_column_name(column_family, qualifier = nil) ⇒ Object
368 369 370 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 368 def qualified_column_name(column_family, qualifier=nil) [column_family, qualifier].compact.join(':') end |
#quote(value, column = nil) ⇒ Object
465 466 467 468 469 470 471 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 465 def quote(value, column = nil) case value when NilClass then '' when String then value else super(value, column) end end |
#quote_column_name(name) ⇒ Object
473 474 475 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 473 def quote_column_name(name) "'#{name}'" end |
#quote_column_name_for_table(name, table_name) ⇒ Object
477 478 479 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 477 def quote_column_name_for_table(name, table_name) quote_column_name(hypertable_column_name(name, table_name)) end |
#raw_thrift_client(&block) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 84 def raw_thrift_client(&block) t1 = Time.now results = Hypertable.with_thrift_client(@config[:host], @config[:port], @config[:timeout], &block) @@read_latency += Time.now - t1 results end |
#remove_column(table_name, *column_names) ⇒ Object Also known as: remove_columns
458 459 460 461 462 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 458 def remove_column(table_name, *column_names) column_names.flatten.each do |column_name| execute "ALTER TABLE #{quote_table_name(table_name)} DROP(#{quote_column_name(column_name)})" end end |
#remove_column_from_name_map(table_name, name) ⇒ Object
349 350 351 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 349 def remove_column_from_name_map(table_name, name) @hypertable_column_names[table_name].delete(rubify_column_name(name)) end |
#rename_column(table_name, column_name, new_column_name) ⇒ Object
Schema alterations
387 388 389 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 387 def rename_column(table_name, column_name, new_column_name) raise "rename_column operation not supported by Hypertable." end |
#rename_table(table_name, options = {}) ⇒ Object
430 431 432 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 430 def rename_table(table_name, = {}) raise "rename_table operation not supported by Hypertable." end |
#requested_columns_from_string(s) ⇒ Object
267 268 269 270 271 272 273 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 267 def requested_columns_from_string(s) if s == '*' [] else s.split(',').map{|s| s.strip} end end |
#retry_on_connection_error ⇒ Object
Attempt to reconnect to the Thrift Broker once before aborting. This ensures graceful recovery in the case that the Thrift Broker goes down and then comes back up.
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 301 def retry_on_connection_error @retry_on_failure = true begin { yield } rescue Thrift::TransportException, IOError, Thrift::ApplicationException, Thrift::ProtocolException => err if @retry_on_failure @retry_on_failure = false @connection.close @connection.open retry else raise err end end end |
#rubify_column_name(column_name) ⇒ Object
372 373 374 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 372 def rubify_column_name(column_name) column_name.to_s.gsub(/-+/, '_') end |
#sanitize_conditions(options) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 123 def sanitize_conditions() case [:conditions] when Hash # requires Hypertable API to support query by arbitrary cell value raise "HyperRecord does not support specifying conditions by Hash" when NilClass # do nothing else raise "Only hash conditions are supported" end end |
#supports_migrations? ⇒ Boolean
110 111 112 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 110 def supports_migrations? true end |
#tables(name = nil) ⇒ Object
Returns an array of tables available in the current Hypertable instance.
518 519 520 521 522 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 518 def tables(name=nil) retry_on_connection_error { @connection.get_tables } end |
#thrift_cell_from_native_array(array) ⇒ Object
Return a Hypertable::ThriftGen::Cell object from a cell passed in as an array of format: [row_key, column_name, value] Hypertable::ThriftGen::Cell objects are required when setting a flag on write - used by special operations (e.g,. delete )
569 570 571 572 573 574 575 576 577 578 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 569 def thrift_cell_from_native_array(array) cell = Hypertable::ThriftGen::Cell.new cell.key = Hypertable::ThriftGen::Key.new cell.key.row = array[0] cell.key.column_family = array[1] cell.key.column_qualifier = array[2] if !array[2].blank? cell.value = array[3] if array[3] cell.key. = array[4] if array[4] cell end |
#with_scanner(table_name, scan_spec, &block) ⇒ Object
675 676 677 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 675 def with_scanner(table_name, scan_spec, &block) @connection.with_scanner(table_name, scan_spec, &block) end |
#write_cells(table_name, cells, options = {}) ⇒ Object
Write an array of cells to the named table. By default, write_cells will open and close a mutator for this operation. Closing the mutator flushes the data, which guarantees is it is stored in Hypertable before the call returns. This also slows down the operation, so if you’re doing lots of writes and want to manage mutator flushes at the application layer then you can pass in a mutator as argument. Mutators can be created with the open_mutator method. In the near future (Summer 2009), Hypertable will provide a periodic mutator that automatically flushes at specific intervals.
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 |
# File 'lib/active_record/connection_adapters/hypertable_adapter.rb', line 533 def write_cells(table_name, cells, ={}) return if cells.blank? mutator = [:mutator] flags = [:flags] flush_interval = [:flush_interval] retry_on_connection_error { local_mutator_created = !mutator begin t1 = Time.now mutator ||= open_mutator(table_name, flags, flush_interval) if [:asynchronous_write] mutate_spec = Hypertable::ThriftGen::MutateSpec.new mutate_spec.appname = 'hyper_record' mutate_spec.flush_interval = 1000 mutate_spec.flags = 2 @connection.put_cells_as_arrays(table_name, mutate_spec, cells) else @connection.set_cells_as_arrays(mutator, cells) end ensure if local_mutator_created && mutator close_mutator(mutator) mutator = nil end @@write_latency += Time.now - t1 end } end |