Skip to content

Commit

Permalink
use in-memory lock for database access
Browse files Browse the repository at this point in the history
  • Loading branch information
schollz committed May 3, 2018
1 parent 3d9a974 commit 367b5f2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
30 changes: 16 additions & 14 deletions server/main/src/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/schollz/find3/server/main/src/models"
"github.com/schollz/sqlite3dump"
"github.com/schollz/stringsizer"
flock "github.com/theckman/go-flock"
)

// MakeTables creates two tables, a `keystore` table:
Expand Down Expand Up @@ -196,22 +195,22 @@ func (d *Database) AddPrediction(timestamp int64, aidata []models.LocationPredic
}
tx, err := d.db.Begin()
if err != nil {
return errors.Wrap(err, "AddPrediction")
return errors.Wrap(err, "begin AddPrediction")
}
stmt, err := tx.Prepare("insert or replace into location_predictions (timestamp,prediction) values (?, ?)")
if err != nil {
return errors.Wrap(err, "AddPrediction")
return errors.Wrap(err, "stmt AddPrediction")
}
defer stmt.Close()

_, err = stmt.Exec(timestamp, string(b))
if err != nil {
return errors.Wrap(err, "AddPrediction")
return errors.Wrap(err, "exec AddPrediction")
}

err = tx.Commit()
if err != nil {
return errors.Wrap(err, "AddPrediction")
return errors.Wrap(err, "commit AddPrediction")
}
return
}
Expand Down Expand Up @@ -832,14 +831,19 @@ func Open(family string, readOnly ...bool) (d *Database, err error) {

// obtain a lock on the database
// logger.Log.Debugf("getting filelock on %s", d.name+".lock")
d.fileLock = flock.NewFlock(d.name + ".lock")
for {
locked, err := d.fileLock.TryLock()
if err == nil && locked {
var ok bool
databaseLock.Lock()
if _, ok = databaseLock.Locked[d.name]; !ok {
databaseLock.Locked[d.name] = true
}
databaseLock.Unlock()
if !ok {
break
}
time.Sleep(10 * time.Millisecond)
}
// logger.Log.Debugf("got filelock")

// check if it is a new database
newDatabase := false
Expand Down Expand Up @@ -887,12 +891,10 @@ func (d *Database) Close() (err error) {
}

// close filelock
err = d.fileLock.Unlock()
if err != nil {
logger.Log.Error(err)
} else {
os.Remove(d.name + ".lock")
}
// logger.Log.Debug("closing lock")
databaseLock.Lock()
delete(databaseLock.Locked, d.name)
databaseLock.Unlock()
d.isClosed = true
return
}
Expand Down
17 changes: 15 additions & 2 deletions server/main/src/database/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package database

import (
"database/sql"
"sync"

_ "github.com/mattn/go-sqlite3"
"github.com/schollz/find3/server/main/src/logging"
flock "github.com/theckman/go-flock"
)

// DataFolder is set to where you want each Sqlite3 database to be stored
Expand All @@ -17,7 +17,20 @@ type Database struct {
name string
family string
db *sql.DB
fileLock *flock.Flock
logger *logging.SeelogWrapper
isClosed bool
}

type DatabaseLock struct {
Locked map[string]bool
sync.RWMutex
}

var databaseLock *DatabaseLock

func init() {
databaseLock = new(DatabaseLock)
databaseLock.Lock()
defer databaseLock.Unlock()
databaseLock.Locked = make(map[string]bool)
}
2 changes: 1 addition & 1 deletion server/main/src/learning/nb2/nb2.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func (a *Algorithm) Fit(datas []models.SensorData) (err error) {
if err != nil {
return
}
defer db.Close()
err = db.Set("NB2", a.Data)
db.Close()
return
}

Expand Down

0 comments on commit 367b5f2

Please sign in to comment.