Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BACK-2895] Cleanup unused indexes, add missing indexes #703

Merged
merged 3 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 2 additions & 63 deletions data/store/mongo/mongo_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mongo

import (
"context"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -199,66 +198,6 @@ func (d *DataRepository) mongoClient() *mongo.Client {
return d.DatumRepository.Database().Client()
}

// MergeSortedUploads combines the unique Uploads by UploadID into a new slice.
func MergeSortedUploads(newUploads, prevUploads []*upload.Upload) []*upload.Upload {
combined := make([]*upload.Upload, 0, len(newUploads)+len(prevUploads))

// Merge the two datasets like the merge step in merge sort. Note we don't
// use sort.Slice/sort.SliceStable from the standard library as the
// sorting criteria may change (?) in the Repositories in the future.
newCounter := 0
// Prefer the uploads in prevUploads as that will maintain proper
// Pagination in the case that not all records are in the new collection
// yet because all existing uploads are already in the old collection but
// might not be in the new one.
for _, dataSet := range prevUploads {
for newCounter < len(newUploads) && *newUploads[newCounter].UploadID < *dataSet.UploadID {
combined = append(combined, newUploads[newCounter])
newCounter++
}
// Always add the dataSet/upload in the "old" deviceData collection
// because the dataSet/upload may not have been finished migrating
// into the new deviceDataSets collection
combined = append(combined, dataSet)
// Skip duplicate of newUploads in prevUploads if it exists.
if newCounter < len(newUploads) && *newUploads[newCounter].UploadID == *dataSet.UploadID {
newCounter++
}
}
combined = append(combined, newUploads[newCounter:]...)
return combined
}

// MergeSortedDataSets combines the unique Uploads by UploadID into a new slice.
func MergeSortedDataSets(newDataSets, prevDataSets data.DataSets) data.DataSets {
combined := make(data.DataSets, 0, len(newDataSets)+len(prevDataSets))

// Merge the two datasets like the merge step in merge sort. Note we don't
// use sort.Slice/sort.SliceStable from the standard library as the
// sorting criteria may change (?) in the Repositories in the future.
newCounter := 0
// Prefer the dataSets in prevDataSets as that will maintain proper
// Pagination in the case that not all records are in the new collection
// yet because all existing DataSets are already in the old collection but
// might not be in the new one.
for _, dataSet := range prevDataSets {
for newCounter < len(newDataSets) && *newDataSets[newCounter].UploadID < *dataSet.UploadID {
combined = append(combined, newDataSets[newCounter])
newCounter++
}
// Always add the dataSet/upload in the "old" deviceData collection
// because the dataSet/upload may not have been finished migrating
// into the new deviceDataSets collection
combined = append(combined, dataSet)
// Skip duplicate of newDataSets in prevDataSets if it exists.
if newCounter < len(newDataSets) && *newDataSets[newCounter].UploadID == *dataSet.UploadID {
newCounter++
}
}
combined = append(combined, newDataSets[newCounter:]...)
return combined
}

func isTypeUpload(typ []string) bool {
return slices.Contains(typ, strings.ToLower(upload.Type))
func isTypeUpload(typ string) bool {
return strings.ToLower(typ) == strings.ToLower(upload.Type)
}
26 changes: 16 additions & 10 deletions data/store/mongo/mongo_data_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func (d *DataSetRepository) EnsureIndexes() error {
{Key: "time", Value: -1},
},
Options: options.Index().
SetBackground(true).
SetName("UserIdTypeWeighted_v2"),
},
{
Expand All @@ -49,7 +48,6 @@ func (d *DataSetRepository) EnsureIndexes() error {
{Key: "_active", Value: 1},
},
Options: options.Index().
SetBackground(true).
SetName("OriginId"),
},
{
Expand All @@ -68,25 +66,33 @@ func (d *DataSetRepository) EnsureIndexes() error {
{Key: "_active", Value: 1},
},
Options: options.Index().
SetBackground(true).
SetName("UploadId"),
},
{
Keys: bson.D{
{Key: "_userId", Value: 1},
{Key: "client.name", Value: 1},
{Key: "type", Value: 1},
{Key: "createdTime", Value: -1},
},
Options: options.Index().
SetName("ListUserDataSets").
SetPartialFilterExpression(bson.D{
{Key: "_active", Value: true},
}),
},
{
Keys: bson.D{
{Key: "_userId", Value: 1},
{Key: "deviceId", Value: 1},
{Key: "type", Value: 1},
{Key: "_active", Value: 1},
{Key: "_deduplicator.hash", Value: 1},
{Key: "createdTime", Value: -1},
},
Options: options.Index().
SetBackground(true).
SetName("ListUserDataSetsDeviceId").
SetPartialFilterExpression(bson.D{
{Key: "_active", Value: true},
{Key: "_deduplicator.hash", Value: bson.D{{Key: "$exists", Value: true}}},
{Key: "deviceId", Value: bson.D{{Key: "$exists", Value: true}}},
}).
SetName("DeduplicatorHash"),
}),
},
})
}
Expand Down
4 changes: 0 additions & 4 deletions data/store/mongo/mongo_datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func (d *DatumRepository) EnsureIndexes() error {
{Key: "time", Value: -1},
},
Options: options.Index().
SetBackground(true).
SetName("UserIdTypeWeighted_v2"),
},
{
Expand Down Expand Up @@ -80,7 +79,6 @@ func (d *DatumRepository) EnsureIndexes() error {
{Key: "_active", Value: 1},
},
Options: options.Index().
SetBackground(true).
SetName("OriginId"),
},
{
Expand All @@ -91,7 +89,6 @@ func (d *DatumRepository) EnsureIndexes() error {
{Key: "_active", Value: 1},
},
Options: options.Index().
SetBackground(true).
SetName("UploadId"),
},
{
Expand All @@ -103,7 +100,6 @@ func (d *DatumRepository) EnsureIndexes() error {
{Key: "_deduplicator.hash", Value: 1},
},
Options: options.Index().
SetBackground(true).
SetPartialFilterExpression(bson.D{
{Key: "_active", Value: true},
{Key: "_deduplicator.hash", Value: bson.D{{Key: "$exists", Value: true}}},
Expand Down
Loading