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

feat:Add support for wildcards for include/exclude backups #4904

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
49 changes: 25 additions & 24 deletions entities/backup/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package backup

import (
"fmt"
"strings"
"time"
)

Expand All @@ -37,6 +38,26 @@ type DistributedBackupDescriptor struct {
Error string `json:"error"`
}

// Function to check if a string matches a wildcard pattern
func matchesWildcardPattern(s, pattern string) bool {
return strings.HasPrefix(s, pattern)
}

// Predicate function to include classes based on wildcard pattern
func includeByWildcard(s string, patterns []string) bool {
for _, pattern := range patterns {
if matchesWildcardPattern(s, pattern) {
return true
}
}
return false
}

// Predicate function to exclude classes based on wildcard pattern
func excludeByWildcard(s string, patterns []string) bool {
return !includeByWildcard(s, patterns)
}

// Len returns how many nodes exist in d
func (d *DistributedBackupDescriptor) Len() int {
return len(d.Nodes)
Expand Down Expand Up @@ -98,13 +119,8 @@ func (d *DistributedBackupDescriptor) Include(classes []string) {
if len(classes) == 0 {
return
}
set := make(map[string]struct{}, len(classes))
for _, cls := range classes {
set[cls] = struct{}{}
}
pred := func(s string) bool {
_, ok := set[s]
return ok
return includeByWildcard(s, classes)
}
d.Filter(pred)
}
Expand All @@ -114,13 +130,8 @@ func (d *DistributedBackupDescriptor) Exclude(classes []string) {
if len(classes) == 0 {
return
}
set := make(map[string]struct{}, len(classes))
for _, cls := range classes {
set[cls] = struct{}{}
}
pred := func(s string) bool {
_, ok := set[s]
return !ok
return excludeByWildcard(s, classes)
}
d.Filter(pred)
}
Expand Down Expand Up @@ -296,13 +307,8 @@ func (d *BackupDescriptor) Include(classes []string) {
if len(classes) == 0 {
return
}
set := make(map[string]struct{}, len(classes))
for _, cls := range classes {
set[cls] = struct{}{}
}
pred := func(s string) bool {
_, ok := set[s]
return ok
return includeByWildcard(s, classes)
}
d.Filter(pred)
}
Expand All @@ -312,13 +318,8 @@ func (d *BackupDescriptor) Exclude(classes []string) {
if len(classes) == 0 {
return
}
set := make(map[string]struct{}, len(classes))
for _, cls := range classes {
set[cls] = struct{}{}
}
pred := func(s string) bool {
_, ok := set[s]
return !ok
return excludeByWildcard(s, classes)
}
d.Filter(pred)
}
Expand Down
50 changes: 50 additions & 0 deletions entities/backup/descriptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,53 @@ func TestShardDescriptorClear(t *testing.T) {
s.ClearTemporary()
assert.Equal(t, want, s)
}

func TestBackupDescriptorValidate(t *testing.T) {
Bhavyajain21 marked this conversation as resolved.
Show resolved Hide resolved
// Create a BackupDescriptor instance with valid attributes
validBackup := &BackupDescriptor{
StartedAt: time.Now(),
CompletedAt: time.Now(),
ID: "backup123",
Version: "1.0",
ServerVersion: "2.0",
Error: "",
Classes: []ClassDescriptor{
{
Name: "ClassA",
ShardingState: []byte{1, 2, 3},
Schema: []byte{4, 5, 6},
Shards: []*ShardDescriptor{
{
Name: "ShardA",
Node: "NodeA",
DocIDCounterPath: "/path/to/counter",
PropLengthTrackerPath: "/path/to/tracker",
ShardVersionPath: "/path/to/version",
},
},
},
},
}

// Test validation for a valid BackupDescriptor with the same schema version
err := validBackup.Validate(false)
if err != nil {
t.Errorf("Validation failed for a valid BackupDescriptor with the same schema version: %v", err)
}

// Test validation for a valid BackupDescriptor with a new schema version
err = validBackup.Validate(true)
if err != nil {
t.Errorf("Validation failed for a valid BackupDescriptor with a new schema version: %v", err)
}

// Modify the BackupDescriptor to make it invalid
invalidBackup := validBackup
invalidBackup.Version = "" // Making version attribute empty

// Test validation for an invalid BackupDescriptor
err = invalidBackup.Validate(false)
if err == nil {
t.Error("Validation passed for an invalid BackupDescriptor, expected error")
}
}