Skip to content

Commit 1ead303

Browse files
committed
extend insertData function to manage duplicate records
1 parent 9b61698 commit 1ead303

1 file changed

Lines changed: 28 additions & 5 deletions

File tree

data.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
srvConfig "github.com/CHESSComputing/golib/config"
1212
mongo "github.com/CHESSComputing/golib/mongo"
1313
utils "github.com/CHESSComputing/golib/utils"
14+
bson "go.mongodb.org/mongo-driver/bson"
1415
)
1516

1617
// helper function to validate input data record against schema
@@ -59,7 +60,7 @@ func preprocess(rec map[string]any) map[string]any {
5960
*/
6061

6162
// helper function to insert data into backend DB
62-
func insertData(sname string, rec map[string]any, attrs, sep, div string) (string, error) {
63+
func insertData(sname string, rec map[string]any, attrs, sep, div string, updateRecord bool) (string, error) {
6364
// load our schema
6465
if _, err := _smgr.Load(sname); err != nil {
6566
msg := fmt.Sprintf("unable to load %s error %v", sname, err)
@@ -101,7 +102,13 @@ func insertData(sname string, rec map[string]any, attrs, sep, div string) (strin
101102

102103
// check if given path exist on file system
103104
_, err := os.Stat(path)
104-
if err == nil {
105+
if err != nil {
106+
msg := fmt.Sprintf("No files found associated with DataLocationRaw=%s, error=%v", path, err)
107+
log.Printf("ERROR: %s", msg)
108+
return did, errors.New(msg)
109+
}
110+
// based on updateRecord decide if we'll insert or update record
111+
if updateRecord {
105112
rec["path"] = path
106113
// add record to mongo DB
107114
var records []map[string]any
@@ -115,7 +122,23 @@ func insertData(sname string, rec map[string]any, attrs, sep, div string) (strin
115122
}
116123
return did, err
117124
}
118-
msg := fmt.Sprintf("No files found associated with DataLocationRaw=%s, error=%v", path, err)
119-
log.Printf("ERROR: %s", msg)
120-
return did, errors.New(msg)
125+
126+
// check if did already exist in MongoDB
127+
spec := bson.M{"did": did}
128+
records := mongo.Get(
129+
srvConfig.Config.CHESSMetaData.MongoDB.DBName,
130+
srvConfig.Config.CHESSMetaData.MongoDB.DBColl,
131+
spec, 0, 1)
132+
if len(records) > 0 {
133+
msg := fmt.Sprintf("Record with did=%s found in MetaData database %+v", did, records)
134+
return did, errors.New(msg)
135+
}
136+
137+
// insert record to mongodb
138+
err = mongo.InsertRecord(
139+
srvConfig.Config.CHESSMetaData.MongoDB.DBName,
140+
srvConfig.Config.CHESSMetaData.MongoDB.DBColl,
141+
rec)
142+
143+
return did, err
121144
}

0 commit comments

Comments
 (0)