Class: MassiveRecord::Wrapper::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/massive_record/wrapper/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.



7
8
9
10
11
# File 'lib/massive_record/wrapper/table.rb', line 7

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.



5
6
7
# File 'lib/massive_record/wrapper/table.rb', line 5

def column_families
  @column_families
end

#connectionObject

Returns the value of attribute connection.



5
6
7
# File 'lib/massive_record/wrapper/table.rb', line 5

def connection
  @connection
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/massive_record/wrapper/table.rb', line 5

def name
  @name
end

Class Method Details

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



18
19
20
21
22
# File 'lib/massive_record/wrapper/table.rb', line 18

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



97
98
99
100
101
# File 'lib/massive_record/wrapper/table.rb', line 97

def all(opts = {})
  scanner(opts) do |s|
    s.fetch_rows(opts)
  end
end

#clientObject



34
35
36
# File 'lib/massive_record/wrapper/table.rb', line 34

def client
  connection
end

#column_family_namesObject



59
60
61
# File 'lib/massive_record/wrapper/table.rb', line 59

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

#column_namesObject



68
69
70
# File 'lib/massive_record/wrapper/table.rb', line 68

def column_names
  first.column_names
end

#create_column_families(column_family_names) ⇒ Object



47
48
49
# File 'lib/massive_record/wrapper/table.rb', line 47

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

#destroyObject



42
43
44
45
# File 'lib/massive_record/wrapper/table.rb', line 42

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

#disableObject



38
39
40
# File 'lib/massive_record/wrapper/table.rb', line 38

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

#exists?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/massive_record/wrapper/table.rb', line 131

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

#fetch_column_familiesObject



51
52
53
54
55
56
57
# File 'lib/massive_record/wrapper/table.rb', line 51

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



63
64
65
66
# File 'lib/massive_record/wrapper/table.rb', line 63

def fetch_column_family_names
  fetch_column_families
  column_family_names
end

#find(*args) ⇒ Object



107
108
109
110
111
# File 'lib/massive_record/wrapper/table.rb', line 107

def find(*args)
  arg  = args[0]
  opts = args[1] || {}
  arg.is_a?(Array) ? arg.collect{|id| first(opts.merge(:start => id))} : first(opts.merge(:start => arg))
end

#find_in_batches(opts = {}) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/massive_record/wrapper/table.rb', line 113

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



103
104
105
# File 'lib/massive_record/wrapper/table.rb', line 103

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

#format_options_for_scanner(opts = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/massive_record/wrapper/table.rb', line 87

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

#init_column_familiesObject



13
14
15
16
# File 'lib/massive_record/wrapper/table.rb', line 13

def init_column_families      
  @column_families = ColumnFamiliesCollection.new
  @column_families.table = self
end

#regionsObject



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/massive_record/wrapper/table.rb', line 135

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



24
25
26
27
28
29
30
31
32
# File 'lib/massive_record/wrapper/table.rb', line 24

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



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

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