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
# 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[table] = lookup
  @schema[table] = definition
end

#delete(table) ⇒ Object

Delete schema for the table

Parameters:

  • table (Symbol)


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

def delete table
  table = table.to_sym
  @lookup.delete table
  @schema.delete 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.



77
78
79
80
81
82
83
84
85
# File 'lib/hbase-jruby/schema.rb', line 77

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

  if match = lookup[:exact][col]
    return match
  elsif pair = lookup[:pattern].find { |k, v| col.to_s =~ k }
    return pair[1].dup.tap { |e| e[1] = col.to_sym }
  end
end

#lookup_and_parse(table, col, expect_cq) ⇒ Object

Parameters:

  • table (Symbol)

Raises:

  • (ArgumentError)


89
90
91
92
93
94
# File 'lib/hbase-jruby/schema.rb', line 89

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)


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

def to_h
  @schema
end