Skip to content

Commit

Permalink
Moving to more idiomatic code (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
proway2 authored Jun 30, 2020
1 parent 1859f68 commit e737038
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 196 deletions.
6 changes: 3 additions & 3 deletions element/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// Element - структура описывающая один элемент хранилища
type Element struct {
Val string
Timestamp time.Time
QueueElement *list.Element
Val string // the actual value of the element
Timestamp time.Time // time when element is created or updated
QueueElement *list.Element // pointer to the position in the queue (LIFO stack)
}
45 changes: 23 additions & 22 deletions kvstorage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ func NewStorage() *KVStorage {
}
}

// Set - установка ключ-значение
func (kv *KVStorage) Set(key, value string) bool {
// Set adds new or updates existing element into the storage
func (kv *KVStorage) Set(key, value string) error {
if !kv.initialized || len(key) == 0 {
return false
return errors.New("set: Storage is not initialized or key is empty")
}
kv.mux.Lock()
defer kv.mux.Unlock()
Expand All @@ -42,43 +42,43 @@ func (kv *KVStorage) Set(key, value string) bool {
// вместо него будет новый с таким же ключом
kv.queue.Remove(elem.QueueElement)
}
// in order to maintain LIFO new elements push back
// in order to maintain LIFO new elements pushed back
elem := &element.Element{
Val: value,
Timestamp: time.Now(),
QueueElement: kv.queue.PushBack(key),
}
kv.kvstorage[key] = elem

return true
return nil
}

// Get - получение значения по ключу,
// второе возвращаемое значение указывает на успешность получения значения по ключу
func (kv *KVStorage) Get(key string) (string, bool) {
// Get returns value by it's key
func (kv *KVStorage) Get(key string) ([]byte, error) {
if !kv.initialized || len(key) == 0 {
return "", false
return nil, errors.New("get: Storage is not initialized or key is empty")
}
kv.mux.Lock()
defer kv.mux.Unlock()
elem, ok := kv.kvstorage[key]
if ok {
return elem.Val, true
return []byte(elem.Val), nil
}
return "", false
// element with the key is not found, but this is not an error
return nil, nil
}

// OldestElementTime - получить метку времени старейшего элемента
func (kv *KVStorage) OldestElementTime() (time.Time, error) {
if !kv.initialized {
return time.Time{}, errors.New("storage is not initialized")
return time.Time{}, errors.New("oldestelementtime: Storage is not initialized")
}
kv.mux.Lock()
defer kv.mux.Unlock()

oldestElemInQueue := kv.queue.Front()
if oldestElemInQueue == nil {
return time.Time{}, errors.New("not found in storage")
return time.Time{}, errors.New("oldestelementtime: Element is not found in storage")
}

key := oldestElemInQueue.Value.(string)
Expand All @@ -90,32 +90,32 @@ func (kv *KVStorage) OldestElementTime() (time.Time, error) {
panic("element is in the queue, but not in the map")
}

// Delete - delete element from storage by its key
func (kv *KVStorage) Delete(key string) bool {
// Delete removes element from storage by its key
func (kv *KVStorage) Delete(key string) (bool, error) {
if !kv.initialized || len(key) == 0 {
return false
return false, errors.New("delete: Storage is not initialized or key is empty")
}
kv.mux.Lock()
defer kv.mux.Unlock()
_, ok := kv.kvstorage[key]
if ok {
kv.purgeElement(key)
}
return ok
return ok, nil
}

// DeleteFrontIfOlder - removes front element if its older than ctxTime
func (kv *KVStorage) DeleteFrontIfOlder(ctxTime time.Time) bool {
func (kv *KVStorage) DeleteFrontIfOlder(ctxTime time.Time) (bool, error) {
if !kv.initialized {
return false
return false, errors.New("deletefrontifolder: Storage is not initialized")
}
kv.mux.Lock()
defer kv.mux.Unlock()

// first need to check if there is something in the queue
oldestElemInQueue := kv.queue.Front()
if oldestElemInQueue == nil {
return false
return false, nil
}

// map key is needed to test the element against input time
Expand All @@ -125,9 +125,10 @@ func (kv *KVStorage) DeleteFrontIfOlder(ctxTime time.Time) bool {
elem, _ := kv.kvstorage[key]
if elem.Timestamp.Before(ctxTime) {
kv.purgeElement(key)
return true
return true, nil
}
return false
// the element is not in the storage right now
return false, nil
}

func (kv *KVStorage) purgeElement(key string) {
Expand Down
Loading

0 comments on commit e737038

Please sign in to comment.