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.



8
9
10
11
12
# File 'lib/massive_record/adapters/thrift/table.rb', line 8

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



19
20
21
22
23
# File 'lib/massive_record/adapters/thrift/table.rb', line 19

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

Instance Method Details

#all(opts = {}) ⇒ Object



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

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

#clientObject



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

def client
  connection
end

#column_family_namesObject



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

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

#column_namesObject



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

def column_names
  first.column_names
end

#create_column_families(column_family_names) ⇒ Object



48
49
50
# File 'lib/massive_record/adapters/thrift/table.rb', line 48

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

#destroyObject



43
44
45
46
# File 'lib/massive_record/adapters/thrift/table.rb', line 43

def destroy
  disable
  client.deleteTable(name).nil?
end

#disableObject



39
40
41
# File 'lib/massive_record/adapters/thrift/table.rb', line 39

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

#exists?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/massive_record/adapters/thrift/table.rb', line 150

def exists?
  connection.tables.include?(name)
end

#fetch_column_familiesObject



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

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



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

def fetch_column_family_names
  fetch_column_families
  column_family_names
end

#find(*args) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/massive_record/adapters/thrift/table.rb', line 120

def find(*args)
  arg  = args[0]
  opts = args[1] || {}
  if arg.is_a?(Array)
    arg.collect{|id| first(opts.merge(:start => id))}
  else
    # need to replace by connection.getRowWithColumns("companies_development", "NO0000000812676342", ["info:name", "info:org_num"]).first
    first(opts.merge(:start => arg))
  end
end

#find_in_batches(opts = {}) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/massive_record/adapters/thrift/table.rb', line 131

def find_in_batches(opts = {})        
  results_limit = opts.delete(: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



108
109
110
# File 'lib/massive_record/adapters/thrift/table.rb', line 108

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

#format_options_for_scanner(opts = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/massive_record/adapters/thrift/table.rb', line 88

def format_options_for_scanner(opts = {})
  {
    :start_key  => opts[:start],
    :offset_key => opts[: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”



116
117
118
# File 'lib/massive_record/adapters/thrift/table.rb', line 116

def get(id, column_family_name, column_name)
  MassiveRecord::Wrapper::Cell.new(:value => connection.get(name, id, "#{column_family_name.to_s}:#{column_name.to_s}").first.value).value
end

#init_column_familiesObject



14
15
16
17
# File 'lib/massive_record/adapters/thrift/table.rb', line 14

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

#regionsObject



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/massive_record/adapters/thrift/table.rb', line 154

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



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

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/massive_record/adapters/thrift/table.rb', line 73

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