Skip to content

Commit

Permalink
Rename parameters to indicate direction (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
hezhizhen authored May 15, 2020
1 parent c2bc682 commit ddb22a8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
35 changes: 19 additions & 16 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,34 @@ func (object Object) HasExtension(extension string) bool {
}

// GetObjectSliceDiff takes two objects slices and returns an ObjectSliceDiff
func GetObjectSliceDiff(os1 []Object, os2 []Object, timestampTolerance time.Duration) ObjectSliceDiff {
func GetObjectSliceDiff(prev []Object, curr []Object, timestampTolerance time.Duration) ObjectSliceDiff {
var diff ObjectSliceDiff
om1 := make(map[string]Object)
om2 := make(map[string]Object)
for _, o1 := range os1 {
om1[o1.Path] = o1
pos := make(map[string]Object)
cos := make(map[string]Object)
for _, o := range prev {
pos[o.Path] = o
}
for _, o2 := range os2 {
om2[o2.Path] = o2
for _, o := range curr {
cos[o.Path] = o
}
for _, o1 := range os1 {
if o2, found := om2[o1.Path]; found {
if o2.LastModified.Sub(o1.LastModified) > timestampTolerance {
diff.Updated = append(diff.Updated, o2)
// for every object in the previous slice, if it exists in the current slice, check if it is *considered as* updated;
// otherwise, mark it as removed
for _, p := range prev {
if c, found := cos[p.Path]; found {
if c.LastModified.Sub(p.LastModified) > timestampTolerance {
diff.Updated = append(diff.Updated, c)
}
} else {
diff.Removed = append(diff.Removed, o1)
diff.Removed = append(diff.Removed, p)
}
}

for _, o2 := range os2 {
if _, found := om1[o2.Path]; !found {
diff.Added = append(diff.Added, o2)
// for every object in the current slice, if it does not exist in the previous slice, mark it as added
for _, c := range curr {
if _, found := pos[c.Path]; !found {
diff.Added = append(diff.Added, c)
}
}
// if any object is marked as removed or added or updated, set change to true
diff.Change = len(diff.Removed)+len(diff.Added)+len(diff.Updated) > 0
return diff
}
Expand Down
2 changes: 1 addition & 1 deletion storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (suite *StorageTestSuite) TestGetObjectSliceDiff() {
LastModified: now,
},
}
os2 := []Object{}
var os2 []Object
diff := GetObjectSliceDiff(os1, os2, time.Duration(0))
suite.True(diff.Change, "change detected")
suite.Equal(diff.Removed, os1, "removed slice populated")
Expand Down

0 comments on commit ddb22a8

Please sign in to comment.