-
Notifications
You must be signed in to change notification settings - Fork 2
/
source_metadata_repository.go
59 lines (51 loc) · 1.71 KB
/
source_metadata_repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package memory
import (
"context"
"github.com/ottogroup/penelope/pkg/repository"
"go.opencensus.io/trace"
)
// SourceMetadataRepository storage for a source metadata
type SourceMetadataRepository struct {
sourceMetadatas []*repository.SourceMetadata
}
// Add create a new source metadata
func (r *SourceMetadataRepository) Add(ctxIn context.Context, sourceMetadata []*repository.SourceMetadata) ([]*repository.SourceMetadata, error) {
_, span := trace.StartSpan(ctxIn, "(*SourceMetadataRepository).Add")
defer span.End()
maxID := 0
for _, s := range r.sourceMetadatas {
if maxID < s.ID {
maxID = s.ID
}
}
for _, input := range sourceMetadata {
maxID++
input.ID = maxID
r.sourceMetadatas = append(r.sourceMetadatas, input)
}
return sourceMetadata, nil
}
// GetLastByBackupID list all source metadata for a backup
func (r *SourceMetadataRepository) GetLastByBackupID(ctxIn context.Context, backupID string) (sourceMetadata []*repository.SourceMetadata, err error) {
_, span := trace.StartSpan(ctxIn, "(*SourceMetadataRepository).GetLastByBackupID")
defer span.End()
for _, s := range r.sourceMetadatas {
if s.BackupID == backupID {
sourceMetadata = append(sourceMetadata, s)
}
}
return sourceMetadata, err
}
// MarkDeleted mark table as deleted
func (r *SourceMetadataRepository) MarkDeleted(ctxIn context.Context, id int) error {
_, span := trace.StartSpan(ctxIn, "(*SourceMetadataRepository).MarkDeleted")
defer span.End()
for i, s := range r.sourceMetadatas {
if s.ID == id {
r.sourceMetadatas[i] = r.sourceMetadatas[len(r.sourceMetadatas)-1] // Replace it with the last one.
r.sourceMetadatas = r.sourceMetadatas[:len(r.sourceMetadatas)-1] // Chop off the last one.
break
}
}
return nil
}