Skip to content

Commit

Permalink
Add reload to SqliteStore
Browse files Browse the repository at this point in the history
  • Loading branch information
bcmmbaga committed Feb 5, 2024
1 parent b59c426 commit b9bdfa9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 23 deletions.
2 changes: 1 addition & 1 deletion management/server/geolocation/geolocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (gl *Geolocation) reload(newSha256sum []byte) error {

log.Infof("Successfully reloaded '%s'", gl.mmdbPath)

return nil
return gl.locationDB.reload()
}

func openDB(mmdbPath string) (*maxminddb.Reader, error) {
Expand Down
78 changes: 56 additions & 22 deletions management/server/geolocation/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import (
"fmt"
"path/filepath"
"runtime"
"sync"

log "github.com/sirupsen/logrus"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)

// SqliteStore represents a location storage backed by a Sqlite DB.
type SqliteStore struct {
db *gorm.DB
db *gorm.DB
filePath string
mux *sync.RWMutex
}

func NewSqliteStore(dataDir string) (*SqliteStore, error) {
Expand All @@ -22,31 +26,16 @@ func NewSqliteStore(dataDir string) (*SqliteStore, error) {
return nil, err
}

storeStr := ":memory:?cache=shared&mode=ro"
if runtime.GOOS == "windows" {
storeStr = ":memory:?&mode=ro"
}

db, err := gorm.Open(sqlite.Open(storeStr), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
PrepareStmt: true,
})
db, err := connectDB(file)
if err != nil {
return nil, err
}

if err := setupInMemoryDBFromFile(db, file); err != nil {
return nil, err
}

sql, err := db.DB()
if err != nil {
return nil, err
}
conns := runtime.NumCPU()
sql.SetMaxOpenConns(conns)

return &SqliteStore{db: db}, nil
return &SqliteStore{
db: db,
filePath: file,
mux: &sync.RWMutex{},
}, nil
}

// GetAllCountries returns a list of all countries in the store.
Expand Down Expand Up @@ -77,6 +66,51 @@ func (s *SqliteStore) GetCitiesByCountry(countryISOCode string) ([]City, error)
return cities, nil
}

func (s *SqliteStore) reload() error {
s.mux.Lock()
defer s.mux.Unlock()

log.Infof("Reloading '%s'", s.filePath)

db, err := connectDB(s.filePath)
if err != nil {
return err
}
s.db = db

log.Infof("Successfully reloaded '%s'", s.filePath)
return nil
}

// connectDB connects to an SQLite database and prepares it by setting up an in-memory database.
func connectDB(source string) (*gorm.DB, error) {
storeStr := ":memory:?cache=shared&mode=ro"
if runtime.GOOS == "windows" {
storeStr = ":memory:?&mode=ro"
}

db, err := gorm.Open(sqlite.Open(storeStr), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
PrepareStmt: true,
})
if err != nil {
return nil, err
}

if err := setupInMemoryDBFromFile(db, source); err != nil {
return nil, err
}

sql, err := db.DB()
if err != nil {
return nil, err
}
conns := runtime.NumCPU()
sql.SetMaxOpenConns(conns)

return db, nil
}

// setupInMemoryDBFromFile prepares an in-memory DB by attaching a file database and,
// copies the data from the attached database to the in-memory database.
func setupInMemoryDBFromFile(db *gorm.DB, source string) error {
Expand Down

0 comments on commit b9bdfa9

Please sign in to comment.