mirror of
https://github.com/pikami/cosmium.git
synced 2025-12-19 17:00:37 +00:00
Added support for initial data loading when using badger db
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
package badgerdatastore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
"github.com/pikami/cosmium/internal/datastore"
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
)
|
||||
|
||||
@@ -13,6 +17,7 @@ type BadgerDataStore struct {
|
||||
}
|
||||
|
||||
type BadgerDataStoreOptions struct {
|
||||
InitialDataFilePath string
|
||||
PersistDataFilePath string
|
||||
}
|
||||
|
||||
@@ -35,6 +40,8 @@ func NewBadgerDataStore(options BadgerDataStoreOptions) *BadgerDataStore {
|
||||
gcTicker: gcTicker,
|
||||
}
|
||||
|
||||
ds.initializeDataStore(options.InitialDataFilePath)
|
||||
|
||||
go ds.runGarbageCollector()
|
||||
|
||||
return ds
|
||||
@@ -64,3 +71,53 @@ func (r *BadgerDataStore) runGarbageCollector() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *BadgerDataStore) initializeDataStore(initialDataFilePath string) {
|
||||
if initialDataFilePath == "" {
|
||||
return
|
||||
}
|
||||
|
||||
stat, err := os.Stat(initialDataFilePath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if stat.IsDir() {
|
||||
logger.ErrorLn("Argument '-Persist' must be a path to file, not a directory.")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
jsonData, err := os.ReadFile(initialDataFilePath)
|
||||
if err != nil {
|
||||
log.Fatalf("Error reading state JSON file: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
var state datastore.InitialDataModel
|
||||
if err := json.Unmarshal([]byte(jsonData), &state); err != nil {
|
||||
log.Fatalf("Error parsing state JSON file: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
for dbName, dbModel := range state.Databases {
|
||||
r.CreateDatabase(dbModel)
|
||||
for colName, colModel := range state.Collections[dbName] {
|
||||
r.CreateCollection(dbName, colModel)
|
||||
for _, docModel := range state.Documents[dbName][colName] {
|
||||
r.CreateDocument(dbName, colName, docModel)
|
||||
}
|
||||
|
||||
for _, triggerModel := range state.Triggers[dbName][colName] {
|
||||
r.CreateTrigger(dbName, colName, triggerModel)
|
||||
}
|
||||
|
||||
for _, spModel := range state.StoredProcedures[dbName][colName] {
|
||||
r.CreateStoredProcedure(dbName, colName, spModel)
|
||||
}
|
||||
|
||||
for _, udfModel := range state.UserDefinedFunctions[dbName][colName] {
|
||||
r.CreateUserDefinedFunction(dbName, colName, udfModel)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user