Class: HBase::Schema

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/hbase-jruby/schema.rb

Instance Method Summary collapse

Constructor Details

#initializeSchema

Returns a new instance of Schema.



9
10
11
12
# File 'lib/hbase-jruby/schema.rb', line 9

def initialize
  @schema = {}
  @lookup = {}
end

Instance Method Details

#[]=(table, definition) ⇒ Object

cq
cf:cq

Parameters:

  • table (Symbol)
  • definition (Hash)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hbase-jruby/schema.rb', line 18

def []= table, definition
  if definition.nil? || definition.empty?
    delete table
    return nil
  end

  unless definition.is_a?(Hash)
    raise ArgumentError, 'invalid schema definition: Hash required'
  end
  definition = definition.dup.freeze
  lookup     = empty_lookup_table

  definition.each do |cf, cols|
    unless [Symbol, String].any? { |k| cf.is_a? k }
      raise ArgumentError,
        "invalid schema: use String or Symbol for column family name"
    end

    # CF:CQ => Type shortcut
    cf = cf.to_s
    if cf.index(':')
      cf, q = cf.to_s.split ':', 2
      cols = { q => cols }
    else
      raise ArgumentError, "invalid schema: expected Hash" unless cols.is_a?(Hash)
    end

    # Family => { Column => Type }
    cols.each do |cq, type|
      type = type.to_sym
      unless KNOWN_TYPES.include? type
        raise ArgumentError, "invalid schema: unknown type: #{type}"
      end

      # Pattern
      case cq
      when Regexp
        lookup[:pattern][cq] = [cf, nil, type]
      # Exact
      when String, Symbol
        cq = cq.to_s
        cfcq = [cf, cq].join(':')
        [cq, cq.to_sym, cfcq].each do |key|
          lookup[:exact][key] = [cf, cq.to_sym, type]
        end
      else
        raise ArgumentError, "invalid schema"
      end
    end
  end

  table = table.to_sym
  @lookup = @lookup.dup.tap { |h| h[table] = lookup }
  @schema = @schema.dup.tap { |h| h[table] = definition }

  definition
end

#delete(table) ⇒ Object

Delete schema for the table

Parameters:

  • table (Symbol)


106
107
108
109
110
111
# File 'lib/hbase-jruby/schema.rb', line 106

def delete table
  table = table.to_sym
  @lookup = @lookup.reject { |k, v| k == table }
  @schema = @schema.reject { |k, v| k == table }
  nil
end

#lookup(table, col) ⇒ Array

Returns CF, CQ, Type. When not found, nil.

Parameters:

  • table (Symbol)

Returns:

  • (Array)

    CF, CQ, Type. When not found, nil.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/hbase-jruby/schema.rb', line 79

def lookup table, col
  return nil unless lookup = @lookup[table]

  case col
  when String, Symbol
    if match = lookup[:exact][col]
      return match
    elsif pair = lookup[:pattern].find { |k, v| col.to_s =~ k }
      colsym = col.to_sym rescue nil
      return colsym && pair[1].dup.tap { |e| e[1] = colsym }
    end
  else
    return nil
  end
end

#lookup_and_parse(table, col, expect_cq) ⇒ Object

Parameters:

  • table (Symbol)

Raises:

  • (ArgumentError)


97
98
99
100
101
102
# File 'lib/hbase-jruby/schema.rb', line 97

def lookup_and_parse table, col, expect_cq
  cf, cq, type = lookup table, col
  cf, cq = Util.parse_column_name(cf ? [cf, cq] : col)
  raise ArgumentError, "Invalid column key: #{col}" if expect_cq && cq.nil?
  return [cf, cq, type]
end

#to_hHash

Returns:

  • (Hash)


114
115
116
# File 'lib/hbase-jruby/schema.rb', line 114

def to_h
  @schema
end