Module: Gcloud::Datastore::Proto

Defined in:
lib/gcloud/datastore/proto.rb,
lib/gcloud/proto/datastore_v1.pb.rb

Overview

Proto is the namespace that contains all Protocol Buffer objects.

The methods in this module are for convenience in using the Protocol Buffer objects and as such can change in the future. Neither the convenience methods nor the Protocol Buffer objects are not part of the gcloud public API. These methods, and even this module’s existance, may change in the future.

You have been warned.

Defined Under Namespace

Classes: AllocateIdsRequest, AllocateIdsResponse, BeginTransactionRequest, BeginTransactionResponse, CommitRequest, CommitResponse, CompositeFilter, Entity, EntityResult, Filter, GqlQuery, GqlQueryArg, Key, KindExpression, LookupRequest, LookupResponse, Mutation, MutationResult, PartitionId, Property, PropertyExpression, PropertyFilter, PropertyOrder, PropertyReference, Query, QueryResultBatch, ReadOptions, RollbackRequest, RollbackResponse, RunQueryRequest, RunQueryResponse, Value

Constant Summary collapse

PROP_FILTER_OPS =

:nodoc:

{
"<"   => PropertyFilter::Operator::LESS_THAN,
"lt"  => PropertyFilter::Operator::LESS_THAN,
"<="  => PropertyFilter::Operator::LESS_THAN_OR_EQUAL,
"lte" => PropertyFilter::Operator::LESS_THAN_OR_EQUAL,
">"   => PropertyFilter::Operator::GREATER_THAN,
"gt"  => PropertyFilter::Operator::GREATER_THAN,
">="  => PropertyFilter::Operator::GREATER_THAN_OR_EQUAL,
"gte" => PropertyFilter::Operator::GREATER_THAN_OR_EQUAL,
"="   => PropertyFilter::Operator::EQUAL,
"eq"  => PropertyFilter::Operator::EQUAL,
"eql" => PropertyFilter::Operator::EQUAL,
"~"            => PropertyFilter::Operator::HAS_ANCESTOR,
"~>"           => PropertyFilter::Operator::HAS_ANCESTOR,
"ancestor"     => PropertyFilter::Operator::HAS_ANCESTOR,
"has_ancestor" => PropertyFilter::Operator::HAS_ANCESTOR,
"has ancestor" => PropertyFilter::Operator::HAS_ANCESTOR }

Class Method Summary collapse

Class Method Details

.decode_cursor(cursor) ⇒ Object



150
151
152
153
154
# File 'lib/gcloud/datastore/proto.rb', line 150

def self.decode_cursor cursor
  dc = cursor.to_s.unpack("m").first.force_encoding Encoding::ASCII_8BIT
  dc = nil if dc.empty?
  dc
end

.encode_cursor(cursor) ⇒ Object



146
147
148
# File 'lib/gcloud/datastore/proto.rb', line 146

def self.encode_cursor cursor
  Array(cursor.to_s).pack("m").chomp
end

.from_proto_properties(proto_properties) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/gcloud/datastore/proto.rb', line 89

def self.from_proto_properties proto_properties
  hash_properties = {}
  Array(proto_properties).each do |p|
    hash_properties[p.name] = Proto.from_proto_value p.value
  end
  hash_properties
end

.from_proto_value(proto_value) ⇒ Object

:nodoc:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gcloud/datastore/proto.rb', line 34

def self.from_proto_value proto_value
  if !proto_value.timestamp_microseconds_value.nil?
    microseconds = proto_value.timestamp_microseconds_value
    self.time_from_microseconds microseconds
  elsif !proto_value.key_value.nil?
    Gcloud::Datastore::Key.from_proto(proto_value.key_value)
  elsif !proto_value.entity_value.nil?
    Gcloud::Datastore::Entity.from_proto(proto_value.entity_value)
  elsif !proto_value.boolean_value.nil?
    proto_value.boolean_value
  elsif !proto_value.double_value.nil?
    proto_value.double_value
  elsif !proto_value.integer_value.nil?
    proto_value.integer_value
  elsif !proto_value.string_value.nil?
    return proto_value.string_value
  elsif !proto_value.list_value.nil?
    return Array(proto_value.list_value).map do |item|
      from_proto_value item
    end
  else
    nil
  end
end

.microseconds_from_time(time) ⇒ Object



106
107
108
# File 'lib/gcloud/datastore/proto.rb', line 106

def self.microseconds_from_time time
  (time.utc.to_f * 1000000).to_i
end

.new_composite_filterObject



175
176
177
178
179
180
# File 'lib/gcloud/datastore/proto.rb', line 175

def self.new_composite_filter
  CompositeFilter.new.tap do |cf|
    cf.operator = Proto::CompositeFilter::Operator::AND
    cf.filter = []
  end
end

.new_filterObject

Convenience methods to create protocol buffer objects



171
172
173
# File 'lib/gcloud/datastore/proto.rb', line 171

def self.new_filter
  Filter.new
end

.new_mutationObject



182
183
184
185
186
187
188
189
190
# File 'lib/gcloud/datastore/proto.rb', line 182

def self.new_mutation
  Mutation.new.tap do |m|
    m.upsert = []
    m.update = []
    m.insert = []
    m.insert_auto_id = []
    m.delete = []
  end
end

.new_partition_id(new_dataset_id, new_namespace) ⇒ Object



235
236
237
238
239
240
# File 'lib/gcloud/datastore/proto.rb', line 235

def self.new_partition_id new_dataset_id, new_namespace
  PartitionId.new.tap do |pi|
    pi.dataset_id = new_dataset_id
    pi.namespace  = new_namespace
  end
end

.new_path_element(new_kind, new_id_or_name) ⇒ Object



224
225
226
227
228
229
230
231
232
233
# File 'lib/gcloud/datastore/proto.rb', line 224

def self.new_path_element new_kind, new_id_or_name
  Key::PathElement.new.tap do |pe|
    pe.kind = new_kind
    if new_id_or_name.is_a? Integer
      pe.id = new_id_or_name
    else
      pe.name = new_id_or_name
    end
  end
end

.new_property_expression(name) ⇒ Object



206
207
208
209
210
# File 'lib/gcloud/datastore/proto.rb', line 206

def self.new_property_expression name
  PropertyExpression.new.tap do |pe|
    pe.property = new_property_reference name
  end
end

.new_property_expressions(*names) ⇒ Object



200
201
202
203
204
# File 'lib/gcloud/datastore/proto.rb', line 200

def self.new_property_expressions *names
  names.map do |name|
    new_property_expression name
  end
end

.new_property_filter(name, operator, value) ⇒ Object



192
193
194
195
196
197
198
# File 'lib/gcloud/datastore/proto.rb', line 192

def self.new_property_filter name, operator, value
  PropertyFilter.new.tap do |pf|
    pf.property = new_property_reference name
    pf.operator = Proto.to_prop_filter_op operator
    pf.value = Proto.to_proto_value value
  end
end

.new_property_reference(name) ⇒ Object



218
219
220
221
222
# File 'lib/gcloud/datastore/proto.rb', line 218

def self.new_property_reference name
  PropertyReference.new.tap do |pr|
    pr.name = name
  end
end

.new_property_references(*names) ⇒ Object



212
213
214
215
216
# File 'lib/gcloud/datastore/proto.rb', line 212

def self.new_property_references *names
  names.map do |name|
    new_property_reference name
  end
end

.new_run_query_request(query_proto) ⇒ Object



242
243
244
245
246
# File 'lib/gcloud/datastore/proto.rb', line 242

def self.new_run_query_request query_proto
  RunQueryRequest.new.tap do |rq|
    rq.query = query_proto
  end
end

.time_from_microseconds(microseconds) ⇒ Object



110
111
112
# File 'lib/gcloud/datastore/proto.rb', line 110

def self.time_from_microseconds microseconds
  Time.at(microseconds / 1000000, microseconds % 1000000).utc
end

.to_more_results_string(more_results) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/gcloud/datastore/proto.rb', line 156

def self.to_more_results_string more_results
  if QueryResultBatch::MoreResultsType::NOT_FINISHED == more_results
    "NOT_FINISHED"
  elsif QueryResultBatch::MoreResultsType::MORE_RESULTS_AFTER_LIMIT == more_results
    "MORE_RESULTS_AFTER_LIMIT"
  elsif QueryResultBatch::MoreResultsType::NO_MORE_RESULTS == more_results
    "NO_MORE_RESULTS"
  else
    nil
  end
end

.to_prop_filter_op(str) ⇒ Object



133
134
135
136
# File 'lib/gcloud/datastore/proto.rb', line 133

def self.to_prop_filter_op str
  PROP_FILTER_OPS[str.to_s.downcase] ||
  PropertyFilter::Operator::EQUAL
end

.to_prop_order_direction(direction) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/gcloud/datastore/proto.rb', line 138

def self.to_prop_order_direction direction
  if direction.to_s.downcase.start_with? "d"
    PropertyOrder::Direction::DESCENDING
  else
    PropertyOrder::Direction::ASCENDING
  end
end

.to_proto_properties(hash_properties) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/gcloud/datastore/proto.rb', line 97

def self.to_proto_properties hash_properties
  hash_properties.map do |name, value|
    Proto::Property.new.tap do |p|
      p.name = name.to_s
      p.value = Proto.to_proto_value value
    end
  end
end

.to_proto_value(value) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/gcloud/datastore/proto.rb', line 59

def self.to_proto_value value
  v = Gcloud::Datastore::Proto::Value.new
  if Time === value
    v.timestamp_microseconds_value = self.microseconds_from_time value
  elsif Gcloud::Datastore::Key === value
    v.key_value = value.to_proto
  elsif Gcloud::Datastore::Entity === value
    v.entity_value = value.to_proto
  elsif NilClass === value
    # The correct behavior is to not set a value property
  elsif TrueClass === value
    v.boolean_value = true
  elsif FalseClass === value
    v.boolean_value = false
  elsif Float === value
    v.double_value = value
  elsif defined?(BigDecimal) && BigDecimal === value
    v.double_value = value
  elsif Integer === value
    v.integer_value = value
  elsif String === value
    v.string_value = value
  elsif Array === value
    v.list_value = value.map { |item| to_proto_value item }
  else
    fail PropertyError, "A property of type #{value.class} is not supported."
  end
  v
end