Skip to content

Commit 5bd6f77

Browse files
authored
Expand backup with full index spec (#1088)
* Expand backup with full index spec * Add tests for index types and opts
1 parent efb1174 commit 5bd6f77

File tree

2 files changed

+496
-19
lines changed

2 files changed

+496
-19
lines changed

pbm/archive/backup.go

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,32 @@ var RouterConfigCollections = []string{
3131

3232
const MetaFileV2 = "meta.pbm"
3333

34+
var validIndexOptions = map[string]bool{
35+
"2dsphereIndexVersion": true,
36+
"background": true,
37+
"bits": true,
38+
"bucketSize": true,
39+
"coarsestIndexedLevel": true,
40+
"collation": true,
41+
"default_language": true,
42+
"expireAfterSeconds": true,
43+
"finestIndexedLevel": true,
44+
"key": true,
45+
"language_override": true,
46+
"max": true,
47+
"min": true,
48+
"name": true,
49+
"ns": true,
50+
"partialFilterExpression": true,
51+
"sparse": true,
52+
"storageEngine": true,
53+
"textIndexVersion": true,
54+
"unique": true,
55+
"v": true,
56+
"weights": true,
57+
"wildcardProjection": true,
58+
}
59+
3460
type ArchiveMetaV2 struct {
3561
Version string `bson:"version"`
3662
ServerVersion string `bson:"serverVersion"`
@@ -46,7 +72,7 @@ type NamespaceV2 struct {
4672
UUID string `bson:"uuid,omitempty"`
4773
Options bson.D `bson:"options,omitempty"`
4874

49-
Indexes []*IndexSpec `bson:"indexes"`
75+
Indexes []IndexSpec `bson:"indexes"`
5076

5177
CRC int64 `bson:"crc"`
5278
Size int64 `bson:"size"`
@@ -63,18 +89,7 @@ func (s *NamespaceV2) IsTimeseries() bool { return s.Type == "timeseries" }
6389
func (s *NamespaceV2) IsSystemCollection() bool { return strings.HasPrefix(s.Name, "system.") }
6490
func (s *NamespaceV2) IsBucketCollection() bool { return strings.HasPrefix(s.Name, "system.buckets") }
6591

66-
type IndexSpec struct {
67-
Version int32 `bson:"v"`
68-
69-
Key bson.D `bson:"key"`
70-
Name string `bson:"name"`
71-
72-
Sparse *bool `bson:"sparse,omitempty"`
73-
Unique *bool `bson:"unique,omitempty"`
74-
Clustered *bool `bson:"clustered,omitempty"`
75-
76-
ExpireAfterSeconds *int32 `bson:"expireAfterSeconds,omitempty"`
77-
}
92+
type IndexSpec bson.D
7893

7994
type BackupOptions struct {
8095
Client *mongo.Client
@@ -236,7 +251,7 @@ func (bcp *backupImpl) listDBNamespaces(ctx context.Context, db string) ([]*Name
236251
return nil, errors.Wrapf(err, "list indexes for %s", ns.Name)
237252
}
238253
} else {
239-
ns.Indexes = []*IndexSpec{}
254+
ns.Indexes = []IndexSpec{}
240255
}
241256

242257
rv = append(rv, ns)
@@ -245,15 +260,33 @@ func (bcp *backupImpl) listDBNamespaces(ctx context.Context, db string) ([]*Name
245260
return rv, errors.Wrap(cur.Err(), "cursor")
246261
}
247262

248-
func (bcp *backupImpl) listIndexes(ctx context.Context, db, coll string) ([]*IndexSpec, error) {
263+
// listIndexes fetches index definitions for the specified namespace.
264+
// It returns dynamic bson.D index representation which is filtered by allowed index
265+
// specification keys.
266+
func (bcp *backupImpl) listIndexes(ctx context.Context, db, coll string) ([]IndexSpec, error) {
249267
cur, err := bcp.conn.Database(db).Collection(coll).Indexes().List(ctx)
250268
if err != nil {
251-
return nil, err
269+
return nil, errors.Wrapf(err, "listIndexes cmd for ns: %s.%s", db, coll)
270+
}
271+
272+
idxs := []bson.D{}
273+
err = cur.All(ctx, &idxs)
274+
if err != nil {
275+
return nil, errors.Wrapf(err, "decode indexes for ns: %s.%s", db, coll)
276+
}
277+
278+
var idxSpecs []IndexSpec
279+
for i := range idxs {
280+
var idxSpec IndexSpec
281+
for _, opt := range idxs[i] {
282+
if _, ok := validIndexOptions[opt.Key]; ok {
283+
idxSpec = append(idxSpec, opt)
284+
}
285+
}
286+
idxSpecs = append(idxSpecs, idxSpec)
252287
}
253288

254-
var indexes []*IndexSpec
255-
err = cur.All(ctx, &indexes)
256-
return indexes, err
289+
return idxSpecs, nil
257290
}
258291

259292
func (bcp *backupImpl) dumpAllCollections(ctx context.Context, nss []*NamespaceV2) error {

0 commit comments

Comments
 (0)