Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Commit

Permalink
Merge pull request #788 from OpenBazaar/lock
Browse files Browse the repository at this point in the history
Revert back to using a regular mutex in the database
  • Loading branch information
cpacia authored Dec 11, 2017
2 parents 9e60aa0 + 23673eb commit d195a6f
Show file tree
Hide file tree
Showing 39 changed files with 173 additions and 135 deletions.
18 changes: 9 additions & 9 deletions repo/db/cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

type CasesDB struct {
db *sql.DB
lock sync.RWMutex
lock *sync.Mutex
}

func (c *CasesDB) Put(caseID string, state pb.OrderState, buyerOpened bool, claim string) error {
Expand Down Expand Up @@ -184,8 +184,8 @@ func (c *CasesDB) Delete(orderID string) error {
}

func (c *CasesDB) GetAll(stateFilter []pb.OrderState, searchTerm string, sortByAscending bool, sortByRead bool, limit int, exclude []string) ([]repo.Case, int, error) {
c.lock.RLock()
defer c.lock.RUnlock()
c.lock.Lock()
defer c.lock.Unlock()

q := query{
table: "cases",
Expand Down Expand Up @@ -287,8 +287,8 @@ func (c *CasesDB) GetAll(stateFilter []pb.OrderState, searchTerm string, sortByA
}

func (c *CasesDB) GetCaseMetadata(caseID string) (buyerContract, vendorContract *pb.RicardianContract, buyerValidationErrors, vendorValidationErrors []string, state pb.OrderState, read bool, timestamp time.Time, buyerOpened bool, claim string, resolution *pb.DisputeResolution, err error) {
c.lock.RLock()
defer c.lock.RUnlock()
c.lock.Lock()
defer c.lock.Unlock()
stmt, err := c.db.Prepare("select buyerContract, vendorContract, buyerValidationErrors, vendorValidationErrors, state, read, timestamp, buyerOpened, claim, disputeResolution from cases where caseID=?")
defer stmt.Close()
var buyerCon []byte
Expand Down Expand Up @@ -360,8 +360,8 @@ func (c *CasesDB) GetCaseMetadata(caseID string) (buyerContract, vendorContract
}

func (c *CasesDB) GetPayoutDetails(caseID string) (buyerContract, vendorContract *pb.RicardianContract, buyerPayoutAddress, vendorPayoutAddress string, buyerOutpoints, vendorOutpoints []*pb.Outpoint, state pb.OrderState, err error) {
c.lock.RLock()
defer c.lock.RUnlock()
c.lock.Lock()
defer c.lock.Unlock()
stmt, err := c.db.Prepare("select buyerContract, vendorContract, buyerPayoutAddress, vendorPayoutAddress, buyerOutpoints, vendorOutpoints, state from cases where caseID=?")
var buyerCon []byte
var vendorCon []byte
Expand Down Expand Up @@ -424,8 +424,8 @@ func (c *CasesDB) GetPayoutDetails(caseID string) (buyerContract, vendorContract
}

func (c *CasesDB) Count() int {
c.lock.RLock()
defer c.lock.RUnlock()
c.lock.Lock()
defer c.lock.Unlock()
row := c.db.QueryRow("select Count(*) from cases")
var count int
row.Scan(&count)
Expand Down
4 changes: 3 additions & 1 deletion repo/db/cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/OpenBazaar/openbazaar-go/pb"
"github.com/golang/protobuf/ptypes"
"sync"
)

var casesdb CasesDB
Expand All @@ -21,7 +22,8 @@ func init() {
conn, _ := sql.Open("sqlite3", ":memory:")
initDatabaseTables(conn, "")
casesdb = CasesDB{
db: conn,
db: conn,
lock: new(sync.Mutex),
}
contract = new(pb.RicardianContract)
listing := new(pb.Listing)
Expand Down
10 changes: 5 additions & 5 deletions repo/db/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

type ChatDB struct {
db *sql.DB
lock sync.RWMutex
lock *sync.Mutex
}

func (c *ChatDB) Put(messageId string, peerId string, subject string, message string, timestamp time.Time, read bool, outgoing bool) error {
Expand Down Expand Up @@ -55,8 +55,8 @@ func (c *ChatDB) Put(messageId string, peerId string, subject string, message st
}

func (c *ChatDB) GetConversations() []repo.ChatConversation {
c.lock.RLock()
defer c.lock.RUnlock()
c.lock.Lock()
defer c.lock.Unlock()
var ret []repo.ChatConversation

stm := "select distinct peerID from chat where subject='' order by timestamp desc;"
Expand Down Expand Up @@ -103,8 +103,8 @@ func (c *ChatDB) GetConversations() []repo.ChatConversation {
}

func (c *ChatDB) GetMessages(peerID string, subject string, offsetId string, limit int) []repo.ChatMessage {
c.lock.RLock()
defer c.lock.RUnlock()
c.lock.Lock()
defer c.lock.Unlock()
var ret []repo.ChatMessage

var peerStm string
Expand Down
4 changes: 3 additions & 1 deletion repo/db/chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

import (
"database/sql"
"sync"
"testing"
"time"
)
Expand All @@ -16,7 +17,8 @@ func setupDB() {
conn, _ := sql.Open("sqlite3", ":memory:")
initDatabaseTables(conn, "")
chdb = ChatDB{
db: conn,
db: conn,
lock: new(sync.Mutex),
}
}

Expand Down
6 changes: 3 additions & 3 deletions repo/db/coupons.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

type CouponDB struct {
db *sql.DB
lock sync.RWMutex
lock *sync.Mutex
}

func (c *CouponDB) Put(coupons []repo.Coupon) error {
Expand All @@ -30,8 +30,8 @@ func (c *CouponDB) Put(coupons []repo.Coupon) error {
}

func (c *CouponDB) Get(slug string) ([]repo.Coupon, error) {
c.lock.RLock()
defer c.lock.RUnlock()
c.lock.Lock()
defer c.lock.Unlock()
var stm string
stm = "select slug, code, hash from coupons where slug='" + slug + "';"
rows, err := c.db.Query(stm)
Expand Down
4 changes: 3 additions & 1 deletion repo/db/coupons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package db
import (
"database/sql"
"github.com/OpenBazaar/openbazaar-go/repo"
"sync"
"testing"
)

Expand All @@ -12,7 +13,8 @@ func init() {
conn, _ := sql.Open("sqlite3", ":memory:")
initDatabaseTables(conn, "")
coup = CouponDB{
db: conn,
db: conn,
lock: new(sync.Mutex),
}
}

Expand Down
22 changes: 11 additions & 11 deletions repo/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type SQLiteDatastore struct {
txMetadata repo.TxMetadata
moderatedStores repo.ModeratedStores
db *sql.DB
lock sync.RWMutex
lock *sync.Mutex
}

func Create(repoPath, password string, testnet bool) (*SQLiteDatastore, error) {
Expand All @@ -54,7 +54,7 @@ func Create(repoPath, password string, testnet bool) (*SQLiteDatastore, error) {
p := "pragma key='" + password + "';"
conn.Exec(p)
}
var l sync.RWMutex
l := new(sync.Mutex)
sqliteDB := &SQLiteDatastore{
config: &ConfigDB{
db: conn,
Expand Down Expand Up @@ -317,7 +317,7 @@ func initDatabaseTables(db *sql.DB, password string) error {

type ConfigDB struct {
db *sql.DB
lock sync.RWMutex
lock *sync.Mutex
path string
}

Expand Down Expand Up @@ -356,8 +356,8 @@ func (c *ConfigDB) Init(mnemonic string, identityKey []byte, password string, cr
}

func (c *ConfigDB) GetMnemonic() (string, error) {
c.lock.RLock()
defer c.lock.RUnlock()
c.lock.Lock()
defer c.lock.Unlock()
stmt, err := c.db.Prepare("select value from config where key=?")
defer stmt.Close()
var mnemonic string
Expand All @@ -369,8 +369,8 @@ func (c *ConfigDB) GetMnemonic() (string, error) {
}

func (c *ConfigDB) GetIdentityKey() ([]byte, error) {
c.lock.RLock()
defer c.lock.RUnlock()
c.lock.Lock()
defer c.lock.Unlock()
stmt, err := c.db.Prepare("select value from config where key=?")
if err != nil {
return nil, err
Expand All @@ -385,8 +385,8 @@ func (c *ConfigDB) GetIdentityKey() ([]byte, error) {
}

func (c *ConfigDB) GetCreationDate() (time.Time, error) {
c.lock.RLock()
defer c.lock.RUnlock()
c.lock.Lock()
defer c.lock.Unlock()
var t time.Time
stmt, err := c.db.Prepare("select value from config where key=?")
if err != nil {
Expand All @@ -402,8 +402,8 @@ func (c *ConfigDB) GetCreationDate() (time.Time, error) {
}

func (c *ConfigDB) IsEncrypted() bool {
c.lock.RLock()
defer c.lock.RUnlock()
c.lock.Lock()
defer c.lock.Unlock()
pwdCheck := "select count(*) from sqlite_master;"
_, err := c.db.Exec(pwdCheck) // Fails if wrong password is entered
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions repo/db/followers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

type FollowerDB struct {
db *sql.DB
lock sync.RWMutex
lock *sync.Mutex
}

func (f *FollowerDB) Put(follower string, proof []byte) error {
Expand All @@ -29,8 +29,8 @@ func (f *FollowerDB) Put(follower string, proof []byte) error {
}

func (f *FollowerDB) Get(offsetId string, limit int) ([]repo.Follower, error) {
f.lock.RLock()
defer f.lock.RUnlock()
f.lock.Lock()
defer f.lock.Unlock()
var stm string
if offsetId != "" {
stm = "select peerID, proof from followers order by rowid desc limit " + strconv.Itoa(limit) + " offset ((select coalesce(max(rowid)+1, 0) from followers)-(select rowid from followers where peerID='" + offsetId + "'))"
Expand Down Expand Up @@ -61,17 +61,17 @@ func (f *FollowerDB) Delete(follower string) error {
}

func (f *FollowerDB) Count() int {
f.lock.RLock()
defer f.lock.RUnlock()
f.lock.Lock()
defer f.lock.Unlock()
row := f.db.QueryRow("select Count(*) from followers")
var count int
row.Scan(&count)
return count
}

func (f *FollowerDB) FollowsMe(peerId string) bool {
f.lock.RLock()
defer f.lock.RUnlock()
f.lock.Lock()
defer f.lock.Unlock()
stmt, err := f.db.Prepare("select peerID from followers where peerID=?")
defer stmt.Close()
var follower string
Expand Down
4 changes: 3 additions & 1 deletion repo/db/followers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"database/sql"
"strconv"
"sync"
"testing"
)

Expand All @@ -13,7 +14,8 @@ func init() {
conn, _ := sql.Open("sqlite3", ":memory:")
initDatabaseTables(conn, "")
fdb = FollowerDB{
db: conn,
db: conn,
lock: new(sync.Mutex),
}
}

Expand Down
14 changes: 7 additions & 7 deletions repo/db/following.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

type FollowingDB struct {
db *sql.DB
lock sync.RWMutex
lock *sync.Mutex
}

func (f *FollowingDB) Put(follower string) error {
Expand All @@ -27,8 +27,8 @@ func (f *FollowingDB) Put(follower string) error {
}

func (f *FollowingDB) Get(offsetId string, limit int) ([]string, error) {
f.lock.RLock()
defer f.lock.RUnlock()
f.lock.Lock()
defer f.lock.Unlock()
var stm string
if offsetId != "" {
stm = "select peerID from following order by rowid desc limit " + strconv.Itoa(limit) + " offset ((select coalesce(max(rowid)+1, 0) from following)-(select rowid from following where peerID='" + offsetId + "'))"
Expand Down Expand Up @@ -57,17 +57,17 @@ func (f *FollowingDB) Delete(follower string) error {
}

func (f *FollowingDB) Count() int {
f.lock.RLock()
defer f.lock.RUnlock()
f.lock.Lock()
defer f.lock.Unlock()
row := f.db.QueryRow("select Count(*) from following")
var count int
row.Scan(&count)
return count
}

func (f *FollowingDB) IsFollowing(peerId string) bool {
f.lock.RLock()
defer f.lock.RUnlock()
f.lock.Lock()
defer f.lock.Unlock()
stmt, err := f.db.Prepare("select peerID from following where peerID=?")
defer stmt.Close()
var follower string
Expand Down
4 changes: 3 additions & 1 deletion repo/db/following_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package db
import (
"database/sql"
"strconv"
"sync"
"testing"
)

Expand All @@ -12,7 +13,8 @@ func init() {
conn, _ := sql.Open("sqlite3", ":memory:")
initDatabaseTables(conn, "")
fldb = FollowingDB{
db: conn,
db: conn,
lock: new(sync.Mutex),
}
}

Expand Down
14 changes: 7 additions & 7 deletions repo/db/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

type InventoryDB struct {
db *sql.DB
lock sync.RWMutex
lock *sync.Mutex
}

func (i *InventoryDB) Put(slug string, variantIndex int, count int) error {
Expand All @@ -35,8 +35,8 @@ func (i *InventoryDB) Put(slug string, variantIndex int, count int) error {
}

func (i *InventoryDB) GetSpecific(slug string, variantIndex int) (int, error) {
i.lock.RLock()
defer i.lock.RUnlock()
i.lock.Lock()
defer i.lock.Unlock()
stmt, err := i.db.Prepare("select count from inventory where slug=? and variantIndex=?")
if err != nil {
return 0, err
Expand All @@ -51,8 +51,8 @@ func (i *InventoryDB) GetSpecific(slug string, variantIndex int) (int, error) {
}

func (i *InventoryDB) Get(slug string) (map[int]int, error) {
i.lock.RLock()
defer i.lock.RUnlock()
i.lock.Lock()
defer i.lock.Unlock()
ret := make(map[int]int)
stmt, err := i.db.Prepare("select slug, variantIndex, count from inventory where slug=?")
if err != nil {
Expand All @@ -75,8 +75,8 @@ func (i *InventoryDB) Get(slug string) (map[int]int, error) {
}

func (i *InventoryDB) GetAll() (map[string]map[int]int, error) {
i.lock.RLock()
defer i.lock.RUnlock()
i.lock.Lock()
defer i.lock.Unlock()

ret := make(map[string]map[int]int)
stm := "select slug, variantIndex, count from inventory"
Expand Down
Loading

0 comments on commit d195a6f

Please sign in to comment.