summaryrefslogtreecommitdiffstats
path: root/pkg/common/sync_subject.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:36:04 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:36:04 +0000
commitb09c6d56832eb1718c07d74abf3bc6ae3fe4e030 (patch)
treed2caec2610d4ea887803ec9e9c3cd77136c448ba /pkg/common/sync_subject.go
parentInitial commit. (diff)
downloadicingadb-b09c6d56832eb1718c07d74abf3bc6ae3fe4e030.tar.xz
icingadb-b09c6d56832eb1718c07d74abf3bc6ae3fe4e030.zip
Adding upstream version 1.1.0.upstream/1.1.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'pkg/common/sync_subject.go')
-rw-r--r--pkg/common/sync_subject.go71
1 files changed, 71 insertions, 0 deletions
diff --git a/pkg/common/sync_subject.go b/pkg/common/sync_subject.go
new file mode 100644
index 0000000..a39d6df
--- /dev/null
+++ b/pkg/common/sync_subject.go
@@ -0,0 +1,71 @@
+package common
+
+import (
+ "github.com/icinga/icingadb/pkg/contracts"
+ v1 "github.com/icinga/icingadb/pkg/icingadb/v1"
+ "github.com/icinga/icingadb/pkg/utils"
+)
+
+// SyncSubject defines information about entities to be synchronized.
+type SyncSubject struct {
+ entity contracts.Entity
+ factory contracts.EntityFactoryFunc
+ withChecksum bool
+}
+
+// NewSyncSubject returns a new SyncSubject.
+func NewSyncSubject(factoryFunc contracts.EntityFactoryFunc) *SyncSubject {
+ e := factoryFunc()
+
+ var factory contracts.EntityFactoryFunc
+ if _, ok := e.(contracts.Initer); ok {
+ factory = func() contracts.Entity {
+ e := factoryFunc()
+ e.(contracts.Initer).Init()
+
+ return e
+ }
+ } else {
+ factory = factoryFunc
+ }
+
+ _, withChecksum := e.(contracts.Checksumer)
+
+ return &SyncSubject{
+ entity: e,
+ factory: factory,
+ withChecksum: withChecksum,
+ }
+}
+
+// Entity returns one value from the factory. Always returns the same entity.
+func (s SyncSubject) Entity() contracts.Entity {
+ return s.entity
+}
+
+// Factory returns the entity factory function that calls Init() on the created contracts.Entity if applicable.
+func (s SyncSubject) Factory() contracts.EntityFactoryFunc {
+ return s.factory
+}
+
+// FactoryForDelta behaves like Factory() unless s is WithChecksum().
+// In the latter case it returns a factory for EntityWithChecksum instead.
+// Rationale: Sync#ApplyDelta() uses its input entities which are WithChecksum() only for the delta itself
+// and not for insertion into the database, so EntityWithChecksum is enough. And it consumes less memory.
+func (s SyncSubject) FactoryForDelta() contracts.EntityFactoryFunc {
+ if s.withChecksum {
+ return v1.NewEntityWithChecksum
+ }
+
+ return s.factory
+}
+
+// Name returns the declared name of the entity.
+func (s SyncSubject) Name() string {
+ return utils.Name(s.entity)
+}
+
+// WithChecksum returns whether entities from the factory implement contracts.Checksumer.
+func (s SyncSubject) WithChecksum() bool {
+ return s.withChecksum
+}