Skip to content

Commit

Permalink
Merge pull request #36 from starskey-io/starskey-alpha-23
Browse files Browse the repository at this point in the history
use read locks where applicable
  • Loading branch information
guycipher authored Jan 21, 2025
2 parents f29e617 + 3ddb7b7 commit 40c6755
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions starskey.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,19 @@ type VLogRecord struct {

// Starskey represents the main struct for the package
type Starskey struct {
wal *pager.Pager // Write-ahead log
memtable *ttree.TTree // Memtable
levels []*Level // Disk levels
config *Config // Starskey configuration
lock *sync.Mutex // Mutex for thread safety
logFile *os.File // Debug log file
wal *pager.Pager // Write-ahead log
memtable *ttree.TTree // Memtable
levels []*Level // Disk levels
config *Config // Starskey configuration
lock *sync.RWMutex // Mutex for thread safety
logFile *os.File // Debug log file
}

// Txn represents a transaction
type Txn struct {
db *Starskey // The db instance
operations []*TxnOperation // Operations in the transaction
lock *sync.Mutex // Mutex for thread safety
lock *sync.RWMutex // Mutex for thread safety
}

// TxnOperation represents an operation in a transaction
Expand Down Expand Up @@ -251,7 +251,7 @@ func Open(config *Config) (*Starskey, error) {

log.Println("Levels opened successfully")

skey.lock = &sync.Mutex{}
skey.lock = &sync.RWMutex{}

log.Println("Replaying WAL")

Expand All @@ -271,15 +271,15 @@ func Open(config *Config) (*Starskey, error) {
func (skey *Starskey) BeginTxn() *Txn {
return &Txn{
operations: make([]*TxnOperation, 0),
lock: &sync.Mutex{},
lock: &sync.RWMutex{},
db: skey,
}
}

// Get retrieves a key-value pair from a transaction
func (txn *Txn) Get(key []byte) ([]byte, error) {
txn.lock.Lock()
defer txn.lock.Unlock()
txn.lock.RLock()
defer txn.lock.RUnlock()

// Check if the key is in the transaction operations
for _, op := range txn.operations {
Expand Down Expand Up @@ -522,8 +522,8 @@ func (skey *Starskey) Put(key, value []byte) error {
// Get retrieves a key from the database
func (skey *Starskey) Get(key []byte) ([]byte, error) {
// Lock for thread safety
skey.lock.Lock()
defer skey.lock.Unlock()
skey.lock.RLock()
defer skey.lock.RUnlock()

// Check memtable first
if value, exists := skey.memtable.Get(key); exists {
Expand Down Expand Up @@ -600,8 +600,8 @@ func (skey *Starskey) Delete(key []byte) error {

// Range retrieves a range of values from the database
func (skey *Starskey) Range(startKey, endKey []byte) ([][]byte, error) {
skey.lock.Lock()
defer skey.lock.Unlock()
skey.lock.RLock()
defer skey.lock.RUnlock()

var result [][]byte
seenKeys := make(map[string]struct{})
Expand Down Expand Up @@ -669,8 +669,8 @@ func (skey *Starskey) Range(startKey, endKey []byte) ([][]byte, error) {

// FilterKeys retrieves values from the database that match a key filter
func (skey *Starskey) FilterKeys(compare func(key []byte) bool) ([][]byte, error) {
skey.lock.Lock()
defer skey.lock.Unlock()
skey.lock.RLock()
defer skey.lock.RUnlock()

var result [][]byte
seenKeys := make(map[string]struct{})
Expand Down

0 comments on commit 40c6755

Please sign in to comment.