Skip to content

Commit

Permalink
Added Insert API for docdb
Browse files Browse the repository at this point in the history
  • Loading branch information
vkuznet committed Dec 6, 2024
1 parent 10101e2 commit ff2aef4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
15 changes: 14 additions & 1 deletion docdb/apis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package docdb

import (
"log"

srvConfig "github.com/CHESSComputing/golib/config"
embed "github.com/CHESSComputing/golib/embed/badger"
"github.com/CHESSComputing/golib/mongo"
Expand All @@ -12,12 +14,23 @@ func InitDocDB(uri string) {
srvConfig.Init()
}
if srvConfig.Config.Embed.DocDb != "" {
embed.InitDB(uri)
log.Printf("Use embed db %s", srvConfig.Config.Embed.DocDb)
embed.InitDB(srvConfig.Config.Embed.DocDb)
return
}
log.Printf("Use mongo db %s", uri)
mongo.InitMongoDB(uri)
}

// Insert record into document-oriented db
func Insert(dbname, collname string, records []map[string]any) {
if srvConfig.Config.Embed.DocDb != "" {
embed.Insert(dbname, collname, records)
return
}
mongo.Insert(dbname, collname, records)
}

// Upsert records into document-oriented db
func Upsert(dbname, collname, attr string, records []map[string]any) error {
if srvConfig.Config.Embed.DocDb != "" {
Expand Down
5 changes: 5 additions & 0 deletions embed/badger/badger.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func ensureDirExists(dir string) error {
return nil
}

// Insert records into BadgerDB
func Insert(dbname, collname string, records []map[string]any) {
upsert(collname, records)
}

// Upsert records into BadgerDB
func Upsert(dbname, collname, attr string, records []map[string]any) error {
return upsert(collname, records)
Expand Down
5 changes: 5 additions & 0 deletions embed/clover/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func InitDB(dbDir string) {
log.Printf("Clover database initialized at %s", dbDir)
}

// Insert records into document-oriented db
func Insert(dbname, collname string, records []map[string]any) {
Upsert(dbname, collname, "", records)
}

// Upsert records into document-oriented db
func Upsert(dbname, collname, attr string, records []map[string]any) error {
if err := db.CreateCollection(collname); err != nil && err != clover.ErrCollectionExist {
Expand Down
5 changes: 5 additions & 0 deletions embed/tiedot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ func InitDB(uri string) {
}
}

// Insert records into document-oriented db
func Insert(dbname, collname string, records []map[string]any) {
Upsert(dbname, collname, "", records)
}

// Upsert records into document-oriented db
func Upsert(dbname, collname, attr string, records []map[string]any) error {
var err error
Expand Down

0 comments on commit ff2aef4

Please sign in to comment.