|
| 1 | +// SPDX-FileCopyrightText: 2022-present Intel Corporation |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package meta |
| 6 | + |
| 7 | +import ( |
| 8 | + runtimev1 "github.com/atomix/runtime/api/atomix/runtime/v1" |
| 9 | + "github.com/atomix/runtime/pkg/time" |
| 10 | +) |
| 11 | + |
| 12 | +func Equal(m1, m2 runtimev1.ObjectMeta) bool { |
| 13 | + return FromProto(m1).Equal(FromProto(m2)) |
| 14 | +} |
| 15 | + |
| 16 | +// NewRevision creates a new object metadata with the given revision |
| 17 | +func NewRevision(revision Revision) ObjectMeta { |
| 18 | + return ObjectMeta{ |
| 19 | + Revision: revision, |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +// NewTimestamped creates a new object metadata with the given timestamp |
| 24 | +func NewTimestamped(timestamp time.Timestamp) ObjectMeta { |
| 25 | + return ObjectMeta{ |
| 26 | + Timestamp: timestamp, |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// FromProto creates new object metadata from the given proto metadata |
| 31 | +func FromProto(meta runtimev1.ObjectMeta) ObjectMeta { |
| 32 | + var revision Revision |
| 33 | + if meta.Revision != 0 { |
| 34 | + revision = Revision(meta.Revision) |
| 35 | + } |
| 36 | + var timestamp time.Timestamp |
| 37 | + if meta.Timestamp != nil { |
| 38 | + timestamp = time.NewTimestamp(*meta.Timestamp) |
| 39 | + } |
| 40 | + return ObjectMeta{ |
| 41 | + Revision: revision, |
| 42 | + Timestamp: timestamp, |
| 43 | + Tombstone: meta.Type == runtimev1.ObjectMeta_TOMBSTONE, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// Object is an interface for objects |
| 48 | +type Object interface { |
| 49 | + // Meta returns the object metadata |
| 50 | + Meta() ObjectMeta |
| 51 | +} |
| 52 | + |
| 53 | +// ObjectMeta contains metadata about an object |
| 54 | +type ObjectMeta struct { |
| 55 | + Revision Revision |
| 56 | + Timestamp time.Timestamp |
| 57 | + Tombstone bool |
| 58 | +} |
| 59 | + |
| 60 | +// AsObject returns the metadata as a non-tombstone |
| 61 | +func (m ObjectMeta) AsObject() ObjectMeta { |
| 62 | + copy := m |
| 63 | + copy.Tombstone = false |
| 64 | + return copy |
| 65 | +} |
| 66 | + |
| 67 | +// AsTombstone returns the metadata as a tombstone |
| 68 | +func (m ObjectMeta) AsTombstone() ObjectMeta { |
| 69 | + copy := m |
| 70 | + copy.Tombstone = true |
| 71 | + return copy |
| 72 | +} |
| 73 | + |
| 74 | +// Meta implements the Object interface |
| 75 | +func (m ObjectMeta) Meta() ObjectMeta { |
| 76 | + return m |
| 77 | +} |
| 78 | + |
| 79 | +// Proto returns the metadata in Protobuf format |
| 80 | +func (m ObjectMeta) Proto() runtimev1.ObjectMeta { |
| 81 | + meta := runtimev1.ObjectMeta{} |
| 82 | + if m.Revision > 0 { |
| 83 | + meta.Revision = runtimev1.Revision(m.Revision) |
| 84 | + } |
| 85 | + if m.Timestamp != nil { |
| 86 | + timestamp := m.Timestamp.Scheme().Codec().EncodeTimestamp(m.Timestamp) |
| 87 | + meta.Timestamp = ×tamp |
| 88 | + } |
| 89 | + if m.Tombstone { |
| 90 | + meta.Type = runtimev1.ObjectMeta_TOMBSTONE |
| 91 | + } else { |
| 92 | + meta.Type = runtimev1.ObjectMeta_OBJECT |
| 93 | + } |
| 94 | + return meta |
| 95 | +} |
| 96 | + |
| 97 | +func (m ObjectMeta) Equal(meta ObjectMeta) bool { |
| 98 | + if m.Revision != meta.Revision { |
| 99 | + return false |
| 100 | + } |
| 101 | + if m.Timestamp != nil && meta.Timestamp != nil && !m.Timestamp.Equal(meta.Timestamp) { |
| 102 | + return false |
| 103 | + } |
| 104 | + return true |
| 105 | +} |
| 106 | + |
| 107 | +func (m ObjectMeta) Before(meta ObjectMeta) bool { |
| 108 | + if m.Revision != 0 && meta.Revision != 0 && m.Revision >= meta.Revision { |
| 109 | + return false |
| 110 | + } |
| 111 | + if m.Timestamp != nil && meta.Timestamp != nil && !m.Timestamp.Before(meta.Timestamp) { |
| 112 | + return false |
| 113 | + } |
| 114 | + return true |
| 115 | +} |
| 116 | + |
| 117 | +func (m ObjectMeta) After(meta ObjectMeta) bool { |
| 118 | + if m.Revision != 0 && meta.Revision != 0 && m.Revision <= meta.Revision { |
| 119 | + return false |
| 120 | + } |
| 121 | + if m.Timestamp != nil && meta.Timestamp != nil && !m.Timestamp.After(meta.Timestamp) { |
| 122 | + return false |
| 123 | + } |
| 124 | + return true |
| 125 | +} |
| 126 | + |
| 127 | +// Revision is a revision number |
| 128 | +type Revision uint64 |
0 commit comments