Skip to content

Commit

Permalink
fix: duplicated index recreation
Browse files Browse the repository at this point in the history
  • Loading branch information
tikazyq committed Dec 11, 2024
1 parent deccf4c commit 2a54762
Showing 1 changed file with 58 additions and 3 deletions.
61 changes: 58 additions & 3 deletions core/models/common/index_utils.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,68 @@
package common

import (
"encoding/json"
"fmt"

"github.com/apex/log"
"github.com/crawlab-team/crawlab/db/mongo"
"go.mongodb.org/mongo-driver/bson"
mongo2 "go.mongodb.org/mongo-driver/mongo"
)

// getIndexKeyString converts index keys to a consistent string representation
func getIndexKeyString(key interface{}) string {
switch v := key.(type) {
case mongo2.IndexModel:
return normalizeIndexKey(v.Keys)
default:
return normalizeIndexKey(key)
}
}

// normalizeIndexKey converts an index key to a standardized string representation
func normalizeIndexKey(key interface{}) string {
// Convert to bson.D to maintain order
var doc bson.D

switch v := key.(type) {
case bson.D:
doc = v
default:
// Convert to JSON bytes first
jsonBytes, err := json.Marshal(key)
if err != nil {
return fmt.Sprintf("%v", key)
}

// Try to unmarshal as array of key-value pairs first
var keyArray []struct {
Key string `json:"Key"`
Value interface{} `json:"Value"`
}
if err := json.Unmarshal(jsonBytes, &keyArray); err == nil {
// Convert array format to bson.D
doc = make(bson.D, len(keyArray))
for i, kv := range keyArray {
doc[i] = bson.E{Key: kv.Key, Value: kv.Value}
}
} else {
// Try to unmarshal as regular map
if err := bson.UnmarshalExtJSON(jsonBytes, true, &doc); err != nil {
return string(jsonBytes)
}
}
}

// Convert bson.D to consistent string representation
pairs := make([]string, 0, len(doc))
for _, elem := range doc {
pairs = append(pairs, fmt.Sprintf("%q:%v", elem.Key, elem.Value))
}

res, _ := bson.Marshal(pairs)
return string(res)
}

func RecreateIndexes(col *mongo.Col, desiredIndexes []mongo2.IndexModel) {
existingIndexes, err := col.ListIndexes()
if err != nil {
Expand All @@ -24,15 +79,15 @@ func RecreateIndexes(col *mongo.Col, desiredIndexes []mongo2.IndexModel) {
name, ok := idx["name"].(string)
if ok && name != "_id_" {
if key, ok := idx["key"]; ok {
keyStr := fmt.Sprintf("%v", key)
keyStr := getIndexKeyString(key)
existingKeys[keyStr] = true
}
}
}

// Check if desired indexes exist
for _, idx := range desiredIndexes {
keyStr := fmt.Sprintf("%v", idx.Keys)
keyStr := getIndexKeyString(idx.Keys)
if !existingKeys[keyStr] {
needsUpdate = true
break
Expand Down

0 comments on commit 2a54762

Please sign in to comment.