| 1 | package Jubatus::Common::Datum; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use utf8; | ||
| 6 | use autodie; | ||
| 7 | |||
| 8 | require Jubatus::Common::Types; | ||
| 9 | use Jubatus::Common::MessageStringGenerator; | ||
| 10 | |||
| 11 | our $TYPE = Jubatus::Common::TTuple->new([ | ||
| 12 | Jubatus::Common::TList->new( | ||
| 13 | Jubatus::Common::TTuple->new([ | ||
| 14 | Jubatus::Common::TString->new(), Jubatus::Common::TString->new()])), | ||
| 15 | Jubatus::Common::TList->new( | ||
| 16 | Jubatus::Common::TTuple->new([ | ||
| 17 | Jubatus::Common::TString->new(), Jubatus::Common::TFloat->new()])), | ||
| 18 | Jubatus::Common::TList->new( | ||
| 19 | Jubatus::Common::TTuple->new([ | ||
| 20 | Jubatus::Common::TString->new(), Jubatus::Common::TRaw->new()])), | ||
| 21 | ]); | ||
| 22 | |||
| 23 | sub new { | ||
| 24 | my ($self, $label_value_pairs) = @_; | ||
| 25 | my ($string_values, $num_values, $binary_values) = ([], [], []); | ||
| 26 | # $self->hash_ref_to_array_ref($string_values), | ||
| 27 | if ((defined $label_value_pairs) && (ref $label_value_pairs eq "ARRAY")) { | ||
| 28 | for (my $i = 0; $i <= $#$label_value_pairs; $i++) { | ||
| 29 | # $label_value_pairs->[$i] = [label(String), value(Any type)] | ||
| 30 | my $type = Jubatus::Common::Types::estimate_type($label_value_pairs->[$i]->[1]); | ||
| 31 | if ($type eq "String") { | ||
| 32 | push @{$string_values}, $label_value_pairs->[$i]; | ||
| 33 | } elsif ($type eq "Integer") { | ||
| 34 | my $tmp_float = [$label_value_pairs->[$i]->[0] , 1.1 + $label_value_pairs->[$i]->[1] - 1.1]; | ||
| 35 | push @{$num_values}, $tmp_float; | ||
| 36 | } elsif ($type eq "Float") { | ||
| 37 | push @{$num_values}, $label_value_pairs->[$i]; | ||
| 38 | } else { | ||
| 39 | Jubatus::Common::TypeException->show([$label_value_pairs->[$i]->[1], $type]); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | } elsif ((defined $label_value_pairs) && (ref $label_value_pairs eq "HASH")) { | ||
| 43 | foreach my $key (keys %{$label_value_pairs}) { | ||
| 44 | # $label_value_pairs->[$i] = [label(String), value(Any type)] | ||
| 45 | my $type = Jubatus::Common::Types::estimate_type($label_value_pairs->{$key}); | ||
| 46 | if ($type eq "String") { | ||
| 47 | my $tmp_string = [$key, $label_value_pairs->{$key}]; | ||
| 48 | push @{$string_values}, $tmp_string; | ||
| 49 | } elsif ($type eq "Integer") { | ||
| 50 | my $tmp_float = [$key, 1.1 + $label_value_pairs->{$key} - 1.1]; | ||
| 51 | push @{$num_values}, $tmp_float; | ||
| 52 | } elsif ($type eq "Float") { | ||
| 53 | my $tmp_float = [$key, 1.1 + $label_value_pairs->{$key} - 1.1]; | ||
| 54 | push @{$num_values}, $tmp_float; | ||
| 55 | } else { | ||
| 56 | Jubatus::Common::TypeException->show([$label_value_pairs->{$key}, $type]); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||
| 60 | my %hash = ( | ||
| 61 | 'type' => "Jubatus::Common::Datum", | ||
| 62 | 'string_values' => $string_values, | ||
| 63 | 'num_values' => $num_values, | ||
| 64 | 'binary_values' => $binary_values, | ||
| 65 | ); | ||
| 66 | bless \%hash, $self; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub add_string { | ||
| 70 | my ($self, $key, $value) = @_; | ||
| 71 | my $done_add = 0; | ||
| 72 | if (Jubatus::Common::Type::check_type($key, "String")) { | ||
| 73 | if (Jubatus::Common::Type::check_type($value, "String")) { | ||
| 74 | push @{$self->{string_values}}, [$key, $value]; | ||
| 75 | $done_add = 1; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | return $done_add; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub add_number { | ||
| 82 | my ($self, $key, $value) = @_; | ||
| 83 | my $done_add = 0; | ||
| 84 | if (Jubatus::Common::Type::check_type($key, "String")) { | ||
| 85 | my $value_type = (Jubatus::Common::Type::estimate_type($value)); | ||
| 86 | if ($value_type eq "Float") { | ||
| 87 | push @{$self->{num_values}}, [$key, $value]; | ||
| 88 | $done_add = 1; | ||
| 89 | } elsif ($value_type eq "Integer") { | ||
| 90 | push @{$self->{num_values}}, [$key, 1.1 + $value - 1.1]; | ||
| 91 | $done_add = 1; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | return $done_add; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub add_binary { | ||
| 98 | my ($self, $key, $value) = @_; | ||
| 99 | my $done_add = 0; | ||
| 100 | if (Jubatus::Common::Type::check_type($key, "String")) { | ||
| 101 | if (Jubatus::Common::Type::check_type($value, "String")) { | ||
| 102 | push @{$self->{binary_values}}, [$key, $value]; | ||
| 103 | $done_add = 1; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | return $done_add; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub to_msgpack { | ||
| 110 | my ($self) = @_; | ||
| 111 | return [ | ||
| 112 | $self->{string_values}, | ||
| 113 | $self->{num_values}, | ||
| 114 | $self->{binary_values}, | ||
| 115 | ]; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub from_msgpack { | ||
| 119 | my ($self, $arg) = @_; | ||
| 120 | my $value = $TYPE->from_msgpack($arg); | ||
| 121 | my $datum = Jubatus::Common::Datum->new(); | ||
| 122 | $datum->{string_values} = $value->[0]; | ||
| 123 | $datum->{num_values} = $value->[1]; | ||
| 124 | $datum->{binary_values} = $value->[2]; | ||
| 125 | return $datum; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub to_s { | ||
| 129 | my ($self) = @_; | ||
| 130 | my $gen = Jubatus::Common::MessageStringGenerator->new(); | ||
| 131 | $gen->open_buf("datum"); | ||
| 132 | $gen->add_buf("string_values", $self->{string_values}); | ||
| 133 | $gen->add_buf("num_values", $self->{num_values}); | ||
| 134 | $gen->add_buf("binary_values", $self->{binary_values}); | ||
| 135 | $gen->close_buf(); | ||
| 136 | return $gen->to_str(); | ||
| 137 | } | ||
| 138 | |||
| 139 | 1; | ||
| 140 | |||
| 141 | __END__ |