Skip to content

Commit

Permalink
Create ZSET
Browse files Browse the repository at this point in the history
Signed-off-by: Devansh Singh <[email protected]>
  • Loading branch information
Devansh3712 committed Feb 18, 2024
1 parent 84b2f39 commit a504921
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 9 deletions.
4 changes: 1 addition & 3 deletions set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (
"sync"
)

var (
ErrElementNotExists = errors.New("the element does not exist in set")
)
var ErrElementNotExists = errors.New("the element does not exist in set")

type Set struct {
Mutex *sync.RWMutex
Expand Down
3 changes: 3 additions & 0 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ import (
"time"

"github.com/Devansh3712/tandb/set"
"github.com/Devansh3712/tandb/zset"
)

type Store struct {
Mutex *sync.RWMutex
Records map[string]Value
Sets map[string]set.Set
ZSets map[string]zset.ZSet
}

func NewStore() Store {
return Store{
Mutex: &sync.RWMutex{},
Records: make(map[string]Value),
Sets: make(map[string]set.Set),
ZSets: make(map[string]zset.ZSet),
}
}

Expand Down
7 changes: 2 additions & 5 deletions zset/rbtree.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package zset

import "sync"

const (
RED = iota
BLACK
Expand All @@ -16,9 +14,8 @@ type Node struct {
}

type RBTree struct {
Mutex *sync.RWMutex
Root *Node
Count uint
Count int
}

func NewNode(value string) *Node {
Expand Down Expand Up @@ -267,7 +264,7 @@ func (t *RBTree) delete(value string) {
if !ok {
return
}

var temp *Node
if node.Left == nil || node.Right == nil {
temp = node
Expand Down
2 changes: 1 addition & 1 deletion zset/rbtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func createTestTree() *RBTree {

func TestInsert(t *testing.T) {
tree := createTestTree()

got := tree.members()
want := []string{"are", "hello", "how", "world", "you"}
if !reflect.DeepEqual(got, want) {
Expand Down
61 changes: 61 additions & 0 deletions zset/zset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package zset

import (
"errors"
"sync"
)

var ErrElementNotExists = errors.New("the element does not exist in set")

type ZSet struct {
Mutex *sync.RWMutex
Elements *RBTree
}

func NewZSet() ZSet {
return ZSet{
Mutex: &sync.RWMutex{},
Elements: NewRBTree(),
}
}

func (z *ZSet) Size() int {
z.Mutex.RLock()
defer z.Mutex.RUnlock()

return z.Elements.Count
}

func (z *ZSet) Add(element string) {
z.Mutex.Lock()
defer z.Mutex.Unlock()

z.Elements.insert(element)
}

func (z *ZSet) Exists(element string) bool {
z.Mutex.RLock()
defer z.Mutex.RUnlock()

_, ok := z.Elements.search(element)
return ok
}

func (z *ZSet) Remove(element string) error {
if ok := z.Exists(element); !ok {
return ErrElementNotExists
}

z.Mutex.Lock()
defer z.Mutex.Unlock()
z.Elements.delete(element)

return nil
}

func (z *ZSet) Members() []string {
z.Mutex.RLock()
defer z.Mutex.RUnlock()

return z.Elements.members()
}

0 comments on commit a504921

Please sign in to comment.