Class: Jubatus::Common::Datum

Inherits:
Object
  • Object
show all
Includes:
Jubatus::Common
Defined in:
lib/jubatus/common/datum.rb

Constant Summary collapse

TYPE =
TTuple.new(TList.new(TTuple.new(TString.new, TString.new)),
TList.new(TTuple.new(TString.new, TFloat.new)),
TList.new(TTuple.new(TString.new, TString.new)))

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Jubatus::Common

check_type, check_types

Constructor Details

#initialize(values = {}) ⇒ Datum

Returns a new instance of Datum.


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jubatus/common/datum.rb', line 11

def initialize(values = {})
  @string_values = []
  @num_values = []
  @binary_values = []
  values.each { |key, v|
    raise TypeError unless String === key || Symbol === key
    k = key.to_s
    if String === v
      @string_values << [k, v]
    elsif Integer === v
      @num_values << [k, v.to_f]
    elsif Float === v
      @num_values << [k, v]
    else
      raise TypeError
    end
  }
end

Instance Attribute Details

#binary_valuesObject (readonly)

Returns the value of attribute binary_values


82
83
84
# File 'lib/jubatus/common/datum.rb', line 82

def binary_values
  @binary_values
end

#num_valuesObject (readonly)

Returns the value of attribute num_values


82
83
84
# File 'lib/jubatus/common/datum.rb', line 82

def num_values
  @num_values
end

#string_valuesObject (readonly)

Returns the value of attribute string_values


82
83
84
# File 'lib/jubatus/common/datum.rb', line 82

def string_values
  @string_values
end

Class Method Details

.from_msgpack(m) ⇒ Object


64
65
66
67
68
69
70
71
# File 'lib/jubatus/common/datum.rb', line 64

def Datum.from_msgpack(m)
  val = TYPE.from_msgpack(m)
  d = Datum.new
  d.string_values.concat(m[0])
  d.num_values.concat(m[1])
  d.binary_values.concat(m[2])
  return d
end

Instance Method Details

#add_binary(key, value) ⇒ Object

Raises:


50
51
52
53
54
55
56
57
# File 'lib/jubatus/common/datum.rb', line 50

def add_binary(key, value)
  raise TypeError unless String === key
  if String === value
    @binary_values << [key, value]
  else
    raise TypeError
  end
end

#add_number(key, value) ⇒ Object

Raises:


39
40
41
42
43
44
45
46
47
48
# File 'lib/jubatus/common/datum.rb', line 39

def add_number(key, value)
  raise TypeError unless String === key
  if Integer === value
    @num_values << [key, value.to_f]
  elsif Float === value
    @num_values << [key, value]
  else
    raise TypeError
  end
end

#add_string(key, value) ⇒ Object

Raises:


30
31
32
33
34
35
36
37
# File 'lib/jubatus/common/datum.rb', line 30

def add_string(key, value)
  raise TypeError unless String === key
  if String === value
    @string_values << [key, value]
  else
    raise TypeError
  end
end

#to_msgpack(out = '') ⇒ Object


59
60
61
62
# File 'lib/jubatus/common/datum.rb', line 59

def to_msgpack(out = '')
  t = [@string_values, @num_values, @binary_values]
  return TYPE.to_msgpack(t)
end

#to_sObject


73
74
75
76
77
78
79
80
81
# File 'lib/jubatus/common/datum.rb', line 73

def to_s
  gen = Jubatus::Common::MessageStringGenerator.new
  gen.open("datum")
  gen.add("string_values", @string_values)
  gen.add("num_values", @num_values)
  gen.add("binary_values", @binary_values)
  gen.close()
  return gen.to_s
end