class Oplogjam::Update

Attributes

h[R]
id[R]
namespace[R]
ns[R]
o[R]
o2[R]
query[R]
ts[R]
update[R]

Public Class Methods

from(bson) click to toggle source
# File lib/oplogjam/update.rb, line 9
def self.from(bson)
  h = bson.fetch(H)
  ts = bson.fetch(TS)
  ns = bson.fetch(NS)
  o2 = bson.fetch(O2)
  o = bson.fetch(O)

  new(h, ts, ns, o2, o)
rescue KeyError => e
  raise InvalidUpdate, "missing field: #{e}"
end
new(h, ts, ns, o2, o) click to toggle source
# File lib/oplogjam/update.rb, line 21
def initialize(h, ts, ns, o2, o)
  @h = Integer(h)
  @ts = Oplogjam::Timestamp(ts)
  @ns = String(ns)
  @o2 = Oplogjam::Document(o2)
  @o = Oplogjam::Document(o)
end

Public Instance Methods

==(other) click to toggle source
# File lib/oplogjam/update.rb, line 38
def ==(other)
  return false unless other.is_a?(Update)

  id == other.id
end
apply(mapping) click to toggle source
# File lib/oplogjam/update.rb, line 44
def apply(mapping)
  table = mapping[namespace]
  return unless table

  row_id = Sequel.object_to_json(query.fetch(ID))

  table
    .where(id: row_id, deleted_at: nil)
    .update(document: jsonb_update, updated_at: Time.now.utc)
end
timestamp() click to toggle source
# File lib/oplogjam/update.rb, line 34
def timestamp
  Time.at(ts.seconds, ts.increment)
end

Private Instance Methods

jsonb_update() click to toggle source
# File lib/oplogjam/update.rb, line 57
def jsonb_update
  return Sequel.pg_jsonb(query.merge(update)) if replacement?

  unsets_to_jsonb(sets_to_jsonb(Sequel.pg_jsonb_op(:document)))
end
replacement?() click to toggle source
# File lib/oplogjam/update.rb, line 75
def replacement?
  !update.key?(SET) && !update.key?(UNSET)
end
sets_to_jsonb(column) click to toggle source
# File lib/oplogjam/update.rb, line 63
def sets_to_jsonb(column)
  return column unless update.key?(SET)

  Operators::Set.from(update.fetch(SET)).update(column)
end
unsets_to_jsonb(column) click to toggle source
# File lib/oplogjam/update.rb, line 69
def unsets_to_jsonb(column)
  return column unless update.key?(UNSET)

  Operators::Unset.from(update.fetch(UNSET)).delete(column)
end