Class: HBase::Scoped

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Aggregation, Util
Defined in:
lib/hbase-jruby/scoped.rb,
lib/hbase-jruby/scoped/aggregation.rb

Overview

@return [HBase::Table] HBase::Table instance for this scope

Defined Under Namespace

Modules: Aggregation

Constant Summary

Constants included from Util

Util::JAVA_BYTE_ARRAY_CLASS, Util::JAVA_BYTE_ARRAY_EMPTY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

append_0, from_bytes, java_bytes?, parse_column_name, to_bytes, to_typed_bytes

Methods included from Aggregation

#aggregate

Instance Attribute Details

#tableObject (readonly)

Returns the value of attribute table.



11
12
13
# File 'lib/hbase-jruby/scoped.rb', line 11

def table
  @table
end

Instance Method Details

#at(ts) ⇒ HBase::Scoped

Returns an HBase::Scoped object with the specified timestamp

Parameters:

  • ts (Fixnum|Time)

    Timestamp

Returns:

  • (HBase::Scoped)

    HBase::Scoped object with the specified timestamp



183
184
185
# File 'lib/hbase-jruby/scoped.rb', line 183

def at ts
  spawn :@trange, time_to_long(ts)
end

#batch(b) ⇒ HBase::Scoped

Returns an HBase::Scoped object with the specified batch limit

Parameters:

  • b (Fixnum)

    Sets the maximum number of values to fetch each time

Returns:

  • (HBase::Scoped)

    HBase::Scoped object with the specified batch limit

Raises:

  • (ArgumentError)


227
228
229
230
# File 'lib/hbase-jruby/scoped.rb', line 227

def batch b
  raise ArgumentError, "Invalid batch size. Must be a positive integer." unless b.is_a?(Fixnum) && b > 0
  spawn :@batch, b
end

#caching(rows) ⇒ HBase::Scoped

Sets the number of rows for caching that will be passed to scanners.

Parameters:

  • rows (Fixnum)

    The number of rows to cache

Returns:

  • (HBase::Scoped)

    HBase::Scoped object with the caching option

Raises:

  • (ArgumentError)


83
84
85
86
# File 'lib/hbase-jruby/scoped.rb', line 83

def caching rows
  raise ArgumentError, "Invalid caching size. Must be a non-negative integer." unless rows.is_a?(Fixnum) && rows >= 0
  spawn :@caching, rows
end

#count(options = {}) ⇒ Fixnum, Bignum

Count the number of rows in the scope

Parameters:

  • options (Hash) (defaults to: {})

    Counting options

Options Hash (options):

  • :caching (Fixnum|nil)

    The number of rows for caching that will be passed to scanners. Use higher values for faster scan.

  • :cache_blocks (Boolean)

    Whether blocks should be cached for this scan

Returns:

  • (Fixnum, Bignum)

    The number of rows in the scope



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/hbase-jruby/scoped.rb', line 25

def count options = {}
  options = { :caching      => nil,
              :cache_blocks => true }.merge(options)

  scan = block_given? ? filtered_scan : filtered_scan_minimum
  scan.cache_blocks = options[:cache_blocks]
  if options[:caching] && (@mlimit.nil? || options[:caching] < @mlimit)
    scan.caching = options[:caching]
  end

  cnt = 0
  if block_given?
    iterate(scan) do |result|
      cnt += 1 if yield(Row.send(:new, @table, result))
    end
  else
    iterate(scan) { |r| cnt += 1 }
  end
  cnt
end

#each {|row| ... } ⇒ Object

Iterate through the scope.

Yields:

  • (row)

    Yields each row in the scope

Yield Parameters:



72
73
74
75
76
77
78
# File 'lib/hbase-jruby/scoped.rb', line 72

def each
  return enum_for(:each) unless block_given?

  iterate(filtered_scan) do |result|
    yield Row.send(:new, @table, result)
  end
end

#filter(*filters) ⇒ HBase::Scoped

Returns an HBase::Scoped object with the filters added

Parameters:

  • filters (Array<Hash, FilterBase, FilterList>)

Returns:

  • (HBase::Scoped)

    HBase::Scoped object also with the specified filters



151
152
153
# File 'lib/hbase-jruby/scoped.rb', line 151

def filter *filters
  spawn :@filters, @filters + parse_filter_input(filters)
end

#get(rowkey) ⇒ HBase::Row? #get(rowkeys) ⇒ Array<HBase::Row>

Performs GET operations

Overloads:

  • #get(rowkey) ⇒ HBase::Row?

    Single GET. Gets a record with the given rowkey. If the record is not found, nil is returned.

    Parameters:

    • rowkey (Object)

      Rowkey

    Returns:

  • #get(rowkeys) ⇒ Array<HBase::Row>

    Batch GET. Gets an array of records with the given rowkeys. Nonexistent records will be returned as nils.

    Parameters:

    • *rowkeys (Array<Object>)

      Rowkeys

    Returns:



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hbase-jruby/scoped.rb', line 57

def get rowkeys
  case rowkeys
  when Array
    htable.get(rowkeys.map { |rk| getify rk }).map { |result|
      result.isEmpty ? nil : Row.send(:new, @table, result)
    }
  else
    result = htable.get(getify rowkeys)
    result.isEmpty ? nil : Row.send(:new, @table, result)
  end
end

#limit(rows) ⇒ HBase::Scoped

Returns an HBase::Scoped object with the specified row number limit

Parameters:

  • rows (Fixnum|nil)

    Sets the maximum number of rows to return from scan

Returns:

  • (HBase::Scoped)

    HBase::Scoped object with the specified row number limit



165
166
167
168
169
170
# File 'lib/hbase-jruby/scoped.rb', line 165

def limit rows
  unless (rows.is_a?(Fixnum) && rows >= 0) || rows.nil?
    raise ArgumentError, "Invalid limit. Must be a non-negative integer or nil."
  end
  spawn :@limit, rows, :@mlimit, nil
end

#project(*columns) ⇒ HBase::Scoped

Returns an HBase::Scoped object with the specified projection

Parameters:

  • columns (Array<String>)

    Array of column expressions

Returns:

  • (HBase::Scoped)

    HBase::Scoped object with the specified projection



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/hbase-jruby/scoped.rb', line 190

def project *columns
  if columns.first.is_a?(Hash)
    hash = columns.first
    unless (hash.keys - [:prefix, :range, :limit, :offset]).empty?
      raise ArgumentError, "Invalid projection"
    end

    if l = hash[:limit]
      unless l.is_a?(Fixnum) && l >= 0
        raise ArgumentError, ":limit must be a non-negative integer"
      end
    end

    if o = hash[:offset]
      unless o.is_a?(Fixnum) && o >= 0
        raise ArgumentError, ":offset must be a non-negative integer"
      end
    end
  end
  spawn :@project, @project + columns.map { |c|
    cf, cq, type = @table.lookup_schema(c)
    cf ? [cf, cq] : c
  }
end

#range(start_key, opts = {}) ⇒ HBase::Scoped #range(start_key, stop_key, opts = {}) ⇒ HBase::Scoped #range(start_stop_range, opts = {}) ⇒ HBase::Scoped #range(opts) ⇒ HBase::Scoped

Overloads:

  • #range(start_key, opts = {}) ⇒ HBase::Scoped

    Returns an HBase::Scoped object with the specified rowkey range Overrides current range.

    Parameters:

    • start_key (Object)

      Start rowkey

    • opts (Hash) (defaults to: {})

      Prefix filter

    Options Hash (opts):

    • :prefix (Object, Array<Object>)

      Only rows matching any of the given prefixes are returned

    Returns:

  • #range(start_key, stop_key, opts = {}) ⇒ HBase::Scoped

    Returns an HBase::Scoped object with the specified rowkey range Overrides current range.

    Parameters:

    • start_key (Object, nil)

      Start rowkey. Can be nil.

    • stop_key (Object)

      Stop rowkey (exclusive)

    • opts (Hash) (defaults to: {})

      Prefix filter

    Options Hash (opts):

    • :prefix (Object, Array<Object>)

      Only rows matching any of the given prefixes are returned

    Returns:

  • #range(start_stop_range, opts = {}) ⇒ HBase::Scoped

    Returns an HBase::Scoped object with the specified rowkey range Overrides current range.

    Parameters:

    • start_stop_range (Range)

      Rowkey scan range

    • opts (Hash) (defaults to: {})

      Prefix filter

    Options Hash (opts):

    • :prefix (Object, Array<Object>)

      Only rows matching any of the given prefixes are returned

    Returns:

  • #range(opts) ⇒ HBase::Scoped

    Returns an HBase::Scoped object with the specified rowkey range Overrides current range.

    Examples:

    table.range(:prefix => '2012')
    table.range(:prefix => ['2010', '2012'])

    Parameters:

    • opts (Hash)

      Prefix filter

    Options Hash (opts):

    • :prefix (Object, Array<Object>)

      Only rows matching any of the given prefixes are returned

    Returns:

Raises:

  • (ArgumentError)


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/hbase-jruby/scoped.rb', line 119

def range *key_range
  if (last = key_range.last).is_a?(Hash)
    prefixes = arrayfy(last[:prefix]).compact
    last = last.reject { |k, v| k == :prefix }

    key_range = key_range[0...-1] # defensive
    key_range << last unless last.empty?
  else
    prefixes = []
  end

  if key_range[0].is_a?(Range)
    raise ArgumentError, "Invalid range" unless key_range.length == 1
  elsif !prefixes.empty?
    raise ArgumentError, "Invalid range" unless [0, 1, 2].include?(key_range.length)
  else
    raise ArgumentError, "Invalid range" unless [1, 2].include?(key_range.length)
  end

  raise ArgumentError, "Invalid range" if !key_range.empty? && key_range.all? { |e| e.nil? }

  spawn :@range,
        key_range[0].is_a?(Range) ?
            key_range[0] :
            (key_range.empty? ? nil : key_range.map { |e| e.nil? ? nil : Util.to_bytes(e) }),
        :@prefixes,
        prefixes
end

#time_range(min, max) ⇒ HBase::Scoped

Returns an HBase::Scoped object with the specified time range

Parameters:

  • min (Fixnum|Time)

    Minimum timestamp (inclusive)

  • max (Fixnum|Time)

    Maximum timestamp (exclusive)

Returns:

  • (HBase::Scoped)

    HBase::Scoped object with the specified time range



176
177
178
# File 'lib/hbase-jruby/scoped.rb', line 176

def time_range min, max
  spawn :@trange, [min, max].map { |e| time_to_long e }
end

#unscopeHBase::Scope

A clean HBase::Scoped object for the same table

Returns:

  • (HBase::Scope)

    A clean HBase::Scoped object for the same table



15
16
17
# File 'lib/hbase-jruby/scoped.rb', line 15

def unscope
  Scoped.send(:new, @table, @dcaching)
end

#versions(vs) ⇒ HBase::Scoped

Returns an HBase::Scoped object with the specified version number limit. If not set, all versions of each value are fetched by default.

Parameters:

  • vs (Fixnum)

    Sets the maximum number of versions

Returns:

  • (HBase::Scoped)

    HBase::Scoped object with the version number limit

Raises:

  • (ArgumentError)


219
220
221
222
# File 'lib/hbase-jruby/scoped.rb', line 219

def versions vs
  raise ArgumentError, "Invalid versions. Must be a positive integer." unless vs.is_a?(Fixnum) && vs > 0
  spawn :@versions, vs
end

#while(*filters) ⇒ HBase::Scoped

Returns an HBase::Scoped object with the additional filters which will cause early termination of scan

Parameters:

  • filters (Array<Hash, FilterBase, FilterList>)

Returns:

  • (HBase::Scoped)

    HBase::Scoped object with the additional filters which will cause early termination of scan



158
159
160
# File 'lib/hbase-jruby/scoped.rb', line 158

def while *filters
  spawn :@filters, @filters + parse_filter_input(filters).map { |filter| WhileMatchFilter.new(filter) }
end

#with_java_get {|org.apache.hadoop.hbase.client.Get| ... } ⇒ HBase::Scoped

Returns an HBase::Scoped object with the Get-customization block added The given block will be evaluated just before an actual get operation. With method-chaining, multiple blocks can be registered to be evaluated sequentially.

Yields:

  • (org.apache.hadoop.hbase.client.Get)

Returns:

Raises:

  • (ArgumentError)


248
249
250
251
252
# File 'lib/hbase-jruby/scoped.rb', line 248

def with_java_get &block
  raise ArgumentError, "Block not given" if block.nil?
  raise ArgumentError, "Invalid arity: should be 1" unless block.arity == 1
  spawn :@get_cbs, @get_cbs + [block]
end

#with_java_scan {|org.apache.hadoop.hbase.client.Scan| ... } ⇒ HBase::Scoped

Returns an HBase::Scoped object with the Scan-customization block added. The given block will be evaluated just before an actual scan operation. With method-chaining, multiple blocks can be registered to be evaluated sequentially.

Yields:

  • (org.apache.hadoop.hbase.client.Scan)

Returns:

Raises:

  • (ArgumentError)


237
238
239
240
241
# File 'lib/hbase-jruby/scoped.rb', line 237

def with_java_scan &block
  raise ArgumentError, "Block not given" if block.nil?
  raise ArgumentError, "Invalid arity: should be 1" unless block.arity == 1
  spawn :@scan_cbs, @scan_cbs + [block]
end