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 =
{
"<"   => 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



158
159
160
161
162
# File 'lib/gcloud/datastore/proto.rb', line 158

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



154
155
156
# File 'lib/gcloud/datastore/proto.rb', line 154

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

.from_proto_properties(proto_properties) ⇒ Object



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

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



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

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
  elsif !proto_value.blob_value.nil?
    return StringIO.new(proto_value.blob_value.force_encoding("ASCII-8BIT"))
  else
    nil
  end
end

.microseconds_from_time(time) ⇒ Object



114
115
116
# File 'lib/gcloud/datastore/proto.rb', line 114

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

.new_composite_filterObject



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

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



179
180
181
# File 'lib/gcloud/datastore/proto.rb', line 179

def self.new_filter
  Filter.new
end

.new_mutationObject



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

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



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

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



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

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



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

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

.new_property_expressions(*names) ⇒ Object



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

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

.new_property_filter(name, operator, value) ⇒ Object



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

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



226
227
228
229
230
# File 'lib/gcloud/datastore/proto.rb', line 226

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

.new_property_references(*names) ⇒ Object



220
221
222
223
224
# File 'lib/gcloud/datastore/proto.rb', line 220

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

.new_run_query_request(query_proto) ⇒ Object



250
251
252
253
254
# File 'lib/gcloud/datastore/proto.rb', line 250

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

.time_from_microseconds(microseconds) ⇒ Object



118
119
120
# File 'lib/gcloud/datastore/proto.rb', line 118

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

.to_more_results_string(more_results) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/gcloud/datastore/proto.rb', line 164

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



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

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

.to_prop_order_direction(direction) ⇒ Object



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

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



105
106
107
108
109
110
111
112
# File 'lib/gcloud/datastore/proto.rb', line 105

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gcloud/datastore/proto.rb', line 64

def self.to_proto_value value
  v = Gcloud::Datastore::Proto::Value.new
  if 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 }
  elsif value.respond_to?(:to_time)
    v.timestamp_microseconds_value = self.microseconds_from_time value.to_time
  elsif value.respond_to?(:read) && value.respond_to?(:rewind)
    value.rewind
    v.blob_value = value.read.force_encoding("ASCII-8BIT")
  else
    fail PropertyError, "A property of type #{value.class} is not supported."
  end
  v
end