Class: MassiveRecord::Adapters::Thrift::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/massive_record/adapters/thrift/table.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, table_name) ⇒ Table

Returns a new instance of Table.



29
30
31
32
33
# File 'lib/massive_record/adapters/thrift/table.rb', line 29

def initialize(connection, table_name)
  @connection = connection
  @name = table_name.to_s
  init_column_families
end

Instance Attribute Details

#column_familiesObject

Returns the value of attribute column_families.



6
7
8
# File 'lib/massive_record/adapters/thrift/table.rb', line 6

def column_families
  @column_families
end

#connectionObject

Returns the value of attribute connection.



6
7
8
# File 'lib/massive_record/adapters/thrift/table.rb', line 6

def connection
  @connection
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/massive_record/adapters/thrift/table.rb', line 6

def name
  @name
end

Class Method Details

.create(connection, table_name, column_families = []) ⇒ Object



40
41
42
43
44
# File 'lib/massive_record/adapters/thrift/table.rb', line 40

def self.create(connection, table_name, column_families = [])
  table = self.new(connection, table_name)
  table.column_families = column_families
  table.save
end

.warn_and_change_deprecated_finder_options(options) ⇒ Object

TODO Helper method to inform about changed options. Remove this in next version.. Also note that this method is used other places to wrap same functionality.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/massive_record/adapters/thrift/table.rb', line 13

def self.warn_and_change_deprecated_finder_options(options)
  deprecations = {
    :start => :starts_with
  }

  deprecations.each do |deprecated, current|
    if options.has_key? deprecated
      # TODO remove this for next version
      ActiveSupport::Deprecation.warn("finder option '#{deprecated}' is deprecated. Please use: '#{current}'")
      options[current] = options.delete deprecated
    end
  end
  
  options
end

Instance Method Details

#all(opts = {}) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/massive_record/adapters/thrift/table.rb', line 125

def all(opts = {})
  rows = []
  
  find_in_batches(opts) do |batch|
    rows |= batch
  end
  
  rows
end

#clientObject



56
57
58
# File 'lib/massive_record/adapters/thrift/table.rb', line 56

def client
  connection
end

#column_family_namesObject



82
83
84
# File 'lib/massive_record/adapters/thrift/table.rb', line 82

def column_family_names
  @column_families.collect{|column_family| column_family.name.to_s}
end

#column_namesObject



91
92
93
# File 'lib/massive_record/adapters/thrift/table.rb', line 91

def column_names
  first.column_names
end

#create_column_families(column_family_names) ⇒ Object



70
71
72
# File 'lib/massive_record/adapters/thrift/table.rb', line 70

def create_column_families(column_family_names)
  column_family_names.each{|name| @column_families.push(ColumnFamily.new(name))}
end

#destroyObject



64
65
66
67
68
# File 'lib/massive_record/adapters/thrift/table.rb', line 64

def destroy
  disable
  @table_exists = false
  client.deleteTable(name).nil?
end

#disableObject



60
61
62
# File 'lib/massive_record/adapters/thrift/table.rb', line 60

def disable
  client.disableTable(name).nil?
end

#exists?Boolean

Returns:

  • (Boolean)


201
202
203
# File 'lib/massive_record/adapters/thrift/table.rb', line 201

def exists?
  @table_exists ||= connection.tables.include?(name)
end

#fetch_column_familiesObject



74
75
76
77
78
79
80
# File 'lib/massive_record/adapters/thrift/table.rb', line 74

def fetch_column_families
  @column_families.clear
  client.getColumnDescriptors(name).each do |column_name, description| 
    @column_families.push(ColumnFamily.new(column_name.split(":").first))
  end
  @column_families
end

#fetch_column_family_namesObject



86
87
88
89
# File 'lib/massive_record/adapters/thrift/table.rb', line 86

def fetch_column_family_names
  fetch_column_families
  column_family_names
end

#find(*args) ⇒ Object

Finds one or multiple ids

Returns nil if id is not found



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/massive_record/adapters/thrift/table.rb', line 162

def find(*args)
  options = args.extract_options!.symbolize_keys
  what_to_find = args.first

  if column_families_to_find = options[:select]
    column_families_to_find = column_families_to_find.collect { |c| c.to_s }
  end

  if what_to_find.is_a? Array
    what_to_find.collect! { |id| id.dup.force_encoding(Encoding::BINARY) }
    connection.getRowsWithColumns(name, what_to_find, column_families_to_find).collect do |t_row_result|
      Row.populate_from_trow_result(t_row_result, connection, name, column_families_to_find)
    end
  else
    if t_row_result = connection.getRowWithColumns(name, what_to_find.dup.force_encoding(Encoding::BINARY), column_families_to_find).first
      Row.populate_from_trow_result(t_row_result, connection, name, column_families_to_find)
    end
  end
end

#find_in_batches(opts = {}) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/massive_record/adapters/thrift/table.rb', line 182

def find_in_batches(opts = {})        
  results_limit = opts[:limit]
  results_found = 0
  
  scanner(opts) do |s|
    while (true) do
      s.limit = results_limit - results_found if !results_limit.nil? && results_limit <= results_found + s.limit
      
      rows = s.fetch_rows
      if rows.empty?
        break
      else
        results_found += rows.size
        yield rows
      end
    end
  end
end

#first(opts = {}) ⇒ Object



135
136
137
# File 'lib/massive_record/adapters/thrift/table.rb', line 135

def first(opts = {})
  all(opts.merge(:limit => 1)).first
end

#format_options_for_scanner(opts = {}) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/massive_record/adapters/thrift/table.rb', line 110

def format_options_for_scanner(opts = {})
  opts = self.class.warn_and_change_deprecated_finder_options(opts)

  start = opts[:starts_with] && opts[:starts_with].dup.force_encoding(Encoding::BINARY)
  offset = opts[:offset] && opts[:offset].dup.force_encoding(Encoding::BINARY)

  {
    :start_key  => start,
    :offset_key => offset,
    :created_at => opts[:created_at],
    :columns    => opts[:select], # list of column families to fetch from hbase
    :limit      => opts[:limit] || opts[:batch_size]
  }
end

#get(id, column_family_name, column_name) ⇒ Object

Fast way of fetching the value of the cell table.get(“my_id”, :info, :name) # => “Bob”



143
144
145
# File 'lib/massive_record/adapters/thrift/table.rb', line 143

def get(id, column_family_name, column_name)
  get_cell(id, column_family_name, column_name).try :value
end

#get_cell(id, column_family_name, column_name) ⇒ Object

Fast way of fetching one cell



151
152
153
154
155
# File 'lib/massive_record/adapters/thrift/table.rb', line 151

def get_cell(id, column_family_name, column_name)
  if cell = connection.get(name, id.dup.force_encoding(Encoding::BINARY), "#{column_family_name.to_s}:#{column_name.to_s}").first
    MassiveRecord::Wrapper::Cell.populate_from_tcell(cell)
  end
end

#init_column_familiesObject



35
36
37
38
# File 'lib/massive_record/adapters/thrift/table.rb', line 35

def init_column_families      
  @column_families = MassiveRecord::Wrapper::ColumnFamiliesCollection.new
  @column_families.table = self
end

#regionsObject



205
206
207
208
209
210
211
212
213
214
215
# File 'lib/massive_record/adapters/thrift/table.rb', line 205

def regions
  connection.getTableRegions(name).collect do |r|
    {
      :start_key => r.startKey,
      :end_key => r.endKey,
      :id => r.id,
      :name => r.name,
      :version => r.version
    }
  end
end

#saveObject



46
47
48
49
50
51
52
53
54
# File 'lib/massive_record/adapters/thrift/table.rb', line 46

def save
  begin
    client.createTable(name, @column_families.collect{|cf| cf.descriptor}).nil?
  rescue Apache::Hadoop::Hbase::Thrift::AlreadyExists => ex
    "The table already exists."
  rescue => ex
    raise ex
  end
end

#scanner(opts = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/massive_record/adapters/thrift/table.rb', line 95

def scanner(opts = {})
  scanner = Scanner.new(connection, name, column_family_names, format_options_for_scanner(opts))

  if block_given?
    begin
      scanner.open
      yield scanner
    ensure
      scanner.close
    end
  else
    scanner
  end
end