Skip to content

Commit

Permalink
Refactor store.go, options.go, resp.go, list.go, tx.go, server.go, an…
Browse files Browse the repository at this point in the history
…d set.go
  • Loading branch information
diiyw committed Apr 11, 2024
1 parent 36c35ef commit 35163f4
Show file tree
Hide file tree
Showing 13 changed files with 241 additions and 172 deletions.
25 changes: 0 additions & 25 deletions ds/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package list

import (
"bytes"
"time"

"github.com/diiyw/nodis/ds"
)
Expand Down Expand Up @@ -149,30 +148,6 @@ func (l *DoublyLinkedList) LLen() int64 {
return l.length
}

// BLPop removes and returns the first element of the list
func (l *DoublyLinkedList) BLPop(timeout time.Duration) []byte {
if l.head == nil {
time.Sleep(timeout)
}
result := l.LPop(1)
if len(result) == 0 {
return nil
}
return result[0]
}

// BRPop removes and returns the last element of the list
func (l *DoublyLinkedList) BRPop(timeout time.Duration) []byte {
if l.tail == nil {
time.Sleep(timeout)
}
result := l.RPop(1)
if len(result) == 0 {
return nil
}
return result[0]
}

// LIndex returns the element at index in the list
func (l *DoublyLinkedList) LIndex(index int64) []byte {
currentNode := l.head
Expand Down
14 changes: 11 additions & 3 deletions hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ func (n *Nodis) HSet(key string, field string, value []byte) {

func (n *Nodis) HGet(key string, field string) []byte {
tx := n.readKey(key)
defer tx.commit()
if !tx.isOk() {
return nil
}
defer tx.commit()
return tx.ds.(*hash.HashMap).HGet(field)
}

func (n *Nodis) HDel(key string, fields ...string) int64 {
tx := n.writeKey(key, nil)
if !tx.isOk() {
tx.commit()
return 0
}
tx.ds.(*hash.HashMap).HDel(fields...)
Expand All @@ -46,6 +47,7 @@ func (n *Nodis) HDel(key string, fields ...string) int64 {
func (n *Nodis) HLen(key string) int64 {
tx := n.readKey(key)
if !tx.isOk() {
tx.commit()
return 0
}
v := tx.ds.(*hash.HashMap).HLen()
Expand All @@ -56,6 +58,7 @@ func (n *Nodis) HLen(key string) int64 {
func (n *Nodis) HKeys(key string) []string {
tx := n.readKey(key)
if !tx.isOk() {
tx.commit()
return nil
}
v := tx.ds.(*hash.HashMap).HKeys()
Expand All @@ -66,6 +69,7 @@ func (n *Nodis) HKeys(key string) []string {
func (n *Nodis) HExists(key string, field string) bool {
tx := n.readKey(key)
if !tx.isOk() {
tx.commit()
return false
}
v := tx.ds.(*hash.HashMap).HExists(field)
Expand All @@ -76,6 +80,7 @@ func (n *Nodis) HExists(key string, field string) bool {
func (n *Nodis) HGetAll(key string) map[string][]byte {
tx := n.readKey(key)
if !tx.isOk() {
tx.commit()
return nil
}
v := tx.ds.(*hash.HashMap).HGetAll()
Expand Down Expand Up @@ -107,10 +112,10 @@ func (n *Nodis) HSetNX(key string, field string, value []byte) bool {
}
h := n.newHash()
n.dataStructs.Set(key, h)
k := newKey(h.Type())
k := newKey()
k.lastUse = time.Now().Unix()
n.keys.Set(key, k)
n.HSet(key, field, value)
h.(*hash.HashMap).HSet(field, value)
tx.commit()
return true
}
Expand All @@ -130,6 +135,7 @@ func (n *Nodis) HMSet(key string, fields map[string][]byte) {
func (n *Nodis) HMGet(key string, fields ...string) [][]byte {
tx := n.readKey(key)
if !tx.isOk() {
tx.commit()
return nil
}
v := tx.ds.(*hash.HashMap).HMGet(fields...)
Expand All @@ -144,6 +150,7 @@ func (n *Nodis) HClear(key string) {
func (n *Nodis) HScan(key string, cursor int64, match string, count int64) (int64, map[string][]byte) {
tx := n.readKey(key)
if !tx.isOk() {
tx.commit()
return 0, nil
}
c, v := tx.ds.(*hash.HashMap).HScan(cursor, match, count)
Expand All @@ -154,6 +161,7 @@ func (n *Nodis) HScan(key string, cursor int64, match string, count int64) (int6
func (n *Nodis) HVals(key string) [][]byte {
tx := n.readKey(key)
if !tx.isOk() {
tx.commit()
return nil
}
v := tx.ds.(*hash.HashMap).HVals()
Expand Down
Loading

0 comments on commit 35163f4

Please sign in to comment.