Class: Oplogjam::Insert

Inherits:
Object
  • Object
show all
Defined in:
lib/oplogjam/insert.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h, ts, ns, o) ⇒ Insert

Returns a new instance of Insert.



18
19
20
21
22
23
# File 'lib/oplogjam/insert.rb', line 18

def initialize(h, ts, ns, o)
  @h = Integer(h)
  @ts = Oplogjam::Timestamp(ts)
  @ns = String(ns)
  @o = Oplogjam::Document(o)
end

Instance Attribute Details

#hObject (readonly) Also known as: id

Returns the value of attribute h.



5
6
7
# File 'lib/oplogjam/insert.rb', line 5

def h
  @h
end

#nsObject (readonly) Also known as: namespace

Returns the value of attribute ns.



5
6
7
# File 'lib/oplogjam/insert.rb', line 5

def ns
  @ns
end

#oObject (readonly) Also known as: document

Returns the value of attribute o.



5
6
7
# File 'lib/oplogjam/insert.rb', line 5

def o
  @o
end

#tsObject (readonly)

Returns the value of attribute ts.



5
6
7
# File 'lib/oplogjam/insert.rb', line 5

def ts
  @ts
end

Class Method Details

.from(bson) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/oplogjam/insert.rb', line 7

def self.from(bson)
  h = bson.fetch(H)
  ts = bson.fetch(TS)
  ns = bson.fetch(NS)
  o = bson.fetch(O)

  new(h, ts, ns, o)
rescue KeyError => e
  raise InvalidInsert, "missing field: #{e}"
end

Instance Method Details

#==(other) ⇒ Object



33
34
35
36
37
# File 'lib/oplogjam/insert.rb', line 33

def ==(other)
  return false unless other.is_a?(Insert)

  id == other.id
end

#apply(mapping) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/oplogjam/insert.rb', line 39

def apply(mapping)
  table = mapping[namespace]
  return unless table

  row_id = document.fetch(ID).to_json

  table
    .insert_conflict(
      target: :id,
      conflict_where: { deleted_at: nil },
      update: {
        document: Sequel[:excluded][:document],
        updated_at: Time.now.utc
      }
    )
    .insert(
      id: row_id,
      document: Sequel.pg_jsonb(document),
      created_at: Time.now.utc,
      updated_at: Time.now.utc
    )
end

#timestampObject



29
30
31
# File 'lib/oplogjam/insert.rb', line 29

def timestamp
  Time.at(ts.seconds, ts.increment)
end