Skip to content

Commit

Permalink
Feat: refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
hmoog committed Oct 24, 2023
1 parent e96ddf7 commit 67d97b8
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions kvstore/typedvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ type TypedValue[V any] struct {
vToBytes ObjectToBytes[V]
bytesToV BytesToObject[V]

valueCached *V
valueExistsCached *bool
mutex syncutils.Mutex
valueCached *V
hasCached *bool
mutex syncutils.Mutex
}

// NewTypedValue is the constructor for TypedValue.
Expand Down Expand Up @@ -43,7 +43,7 @@ func (t *TypedValue[V]) Get() (value V, err error) {
t.mutex.Lock()
defer t.mutex.Unlock()

if t.valueExistsCached != nil && !*t.valueExistsCached {
if t.hasCached != nil && !*t.hasCached {
return value, ErrKeyNotFound
}

Expand All @@ -53,7 +53,7 @@ func (t *TypedValue[V]) Get() (value V, err error) {

if valueBytes, valueBytesErr := t.kv.Get(t.keyBytes); valueBytesErr != nil {
if ierrors.Is(valueBytesErr, ErrKeyNotFound) {
t.valueExistsCached = &falsePtr
t.hasCached = &falsePtr
}

return value, ierrors.Wrap(valueBytesErr, "failed to retrieve value from KV store")
Expand All @@ -62,7 +62,7 @@ func (t *TypedValue[V]) Get() (value V, err error) {
}

t.valueCached = &value
t.valueExistsCached = &truePtr
t.hasCached = &truePtr

return value, nil
}
Expand All @@ -72,13 +72,13 @@ func (t *TypedValue[V]) Has() (has bool, err error) {
t.mutex.Lock()
defer t.mutex.Unlock()

if t.valueExistsCached != nil {
return *t.valueExistsCached, nil
if t.hasCached != nil {
return *t.hasCached, nil
} else if has, err = t.kv.Has(t.keyBytes); err != nil {
return false, ierrors.Wrap(err, "failed to check whether key exists")
}

t.valueExistsCached = &has
t.hasCached = &has

return has, nil
}
Expand All @@ -88,13 +88,8 @@ func (t *TypedValue[V]) Compute(computeFunc func(currentValue V, exists bool) (n
t.mutex.Lock()
defer t.mutex.Unlock()

var currentValue V
var exists bool

if t.valueCached != nil {
currentValue = *t.valueCached
exists = true
} else if t.valueExistsCached == nil || *t.valueExistsCached {
currentValue, exists := t.cachedValue()
if !exists && t.hasCached == nil || *t.hasCached {
if valueBytes, valueBytesErr := t.kv.Get(t.keyBytes); valueBytesErr != nil {
if !ierrors.Is(valueBytesErr, ErrKeyNotFound) {
return newValue, ierrors.Wrap(valueBytesErr, "failed to retrieve value from KV store")
Expand All @@ -111,7 +106,7 @@ func (t *TypedValue[V]) Compute(computeFunc func(currentValue V, exists bool) (n
}

t.valueCached = &newValue
t.valueExistsCached = &truePtr
t.hasCached = &truePtr

return newValue, nil
}
Expand All @@ -128,7 +123,7 @@ func (t *TypedValue[V]) Set(value V) error {
}

t.valueCached = &value
t.valueExistsCached = &truePtr
t.hasCached = &truePtr

return nil
}
Expand All @@ -143,11 +138,20 @@ func (t *TypedValue[V]) Delete() (err error) {
}

t.valueCached = nil
t.valueExistsCached = &falsePtr
t.hasCached = &falsePtr

return nil
}

// cachedValue returns the cached value and a boolean indicating whether the value is cached.
func (t *TypedValue[V]) cachedValue() (value V, isCached bool) {
if t.valueCached == nil {
return value, false
}

return *t.valueCached, true
}

// truePtr is a pointer to a true value.
var truePtr = true

Expand Down

0 comments on commit 67d97b8

Please sign in to comment.