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



147
148
149
150
151
# File 'lib/gcloud/datastore/proto.rb', line 147

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



143
144
145
# File 'lib/gcloud/datastore/proto.rb', line 143

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

.from_proto_properties(proto_properties) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/gcloud/datastore/proto.rb', line 86

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:



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

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



103
104
105
# File 'lib/gcloud/datastore/proto.rb', line 103

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

.new_composite_filterObject



172
173
174
175
176
177
# File 'lib/gcloud/datastore/proto.rb', line 172

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



168
169
170
# File 'lib/gcloud/datastore/proto.rb', line 168

def self.new_filter
  Filter.new
end

.new_mutationObject



179
180
181
182
183
184
185
186
187
# File 'lib/gcloud/datastore/proto.rb', line 179

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



232
233
234
235
236
237
# File 'lib/gcloud/datastore/proto.rb', line 232

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



221
222
223
224
225
226
227
228
229
230
# File 'lib/gcloud/datastore/proto.rb', line 221

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



203
204
205
206
207
# File 'lib/gcloud/datastore/proto.rb', line 203

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

.new_property_expressions(*names) ⇒ Object



197
198
199
200
201
# File 'lib/gcloud/datastore/proto.rb', line 197

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

.new_property_filter(name, operator, value) ⇒ Object



189
190
191
192
193
194
195
# File 'lib/gcloud/datastore/proto.rb', line 189

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



215
216
217
218
219
# File 'lib/gcloud/datastore/proto.rb', line 215

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

.new_property_references(*names) ⇒ Object



209
210
211
212
213
# File 'lib/gcloud/datastore/proto.rb', line 209

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

.new_run_query_request(query_proto) ⇒ Object



239
240
241
242
243
# File 'lib/gcloud/datastore/proto.rb', line 239

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

.time_from_microseconds(microseconds) ⇒ Object



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

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

.to_more_results_string(more_results) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/gcloud/datastore/proto.rb', line 153

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



130
131
132
133
# File 'lib/gcloud/datastore/proto.rb', line 130

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

.to_prop_order_direction(direction) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/gcloud/datastore/proto.rb', line 135

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



94
95
96
97
98
99
100
101
# File 'lib/gcloud/datastore/proto.rb', line 94

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



58
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
# File 'lib/gcloud/datastore/proto.rb', line 58

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 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