Skip to content

Commit

Permalink
implements data structure, Set(ByteStorage#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
saeid-a committed Jul 22, 2023
1 parent 0076122 commit 104cdab
Show file tree
Hide file tree
Showing 2 changed files with 233 additions and 0 deletions.
82 changes: 82 additions & 0 deletions structure/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package structure

import (
"github.com/ByteStorage/FlyDB/config"
"github.com/ByteStorage/FlyDB/engine"
)

type SetStructure struct {
db *engine.DB
}

func NewSetStructure(options config.Options) (*SetStructure, error) {
db, err := engine.NewDB(options)
if err != nil {
return nil, err
}
return &SetStructure{db: db}, nil
}

// SAdd adds a member to the set stored at key.
//
// If the set did not exist, a new set will be created
// and the member will be added to it.
func (s *SetStructure) SAdd(key, member string) error {

return nil
}

// SAdds adds multiple members to a set
func (s *SetStructure) SAdds(key string, members ...string) error {
return nil
}

// SRem removes a member from a set
func (s *SetStructure) SRem(key, member string) error {
return nil
}

// SRems removes multiple members from a set
func (s *SetStructure) SRems(key string, members ...string) error {
return nil
}

// SCard gets the cardinality (size) of a set
func (s *SetStructure) SCard(key string) (int, error) {
return 0, nil
}

// SMembers gets all members of a set
func (s *SetStructure) SMembers(key string) ([]string, error) {
return nil, nil
}

// SIsMember checks if a member exists in a set
func (s *SetStructure) SIsMember(key, member string) (bool, error) {
return false, nil
}

// SUnion gets the union of multiple sets
func (s *SetStructure) SUnion(keys ...string) ([]string, error) {
return nil, nil
}

// SInter gets the intersection of multiple sets
func (s *SetStructure) SInter(keys ...string) ([]string, error) {
return nil, nil
}

// SDiff gets the difference between two sets
func (s *SetStructure) SDiff(key1, key2 string) ([]string, error) {
return nil, nil
}

// SUnionStore stores the union of multiple sets in a destination set
func (s *SetStructure) SUnionStore(destination, key1, key2 string) error {
return nil
}

// SInterStore stores the intersection of multiple sets in a destination set
func (s *SetStructure) SInterStore(destination, key1, key2 string) error {
return nil
}
151 changes: 151 additions & 0 deletions structure/set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package structure

import (
"github.com/ByteStorage/FlyDB/config"
"github.com/ByteStorage/FlyDB/engine"
"github.com/stretchr/testify/assert"
"os"
"testing"
)

func initTestSetDb() (*SetStructure, *config.Options) {
opts := config.DefaultOptions
dir, _ := os.MkdirTemp("", "TestSetStructure")
opts.DirPath = dir
str, _ := NewSetStructure(opts)
return str, &opts
}
func TestSAdd(t *testing.T) {
s, _ := initTestSetDb()

err := s.SAdd("destination", "key1")
if err != nil {
t.Errorf("Expected nil error, got: %v", err)
}

}
func TestSAdds(t *testing.T) {
s, _ := initTestSetDb()

err := s.SAdds("destination", "key1", "key2")
if err != nil {
t.Errorf("Expected nil error, got: %v", err)
}

}
func TestSRems(t *testing.T) {
s, _ := initTestSetDb()

err := s.SRems("destination", "key1", "key2")
if err != nil {
t.Errorf("Expected nil error, got: %v", err)
}

}
func TestSRem(t *testing.T) {
s, _ := initTestSetDb()

err := s.SRem("destination", "key1")
if err != nil {
t.Errorf("Expected nil error, got: %v", err)
}

}
func TestSCard(t *testing.T) {
s, _ := initTestSetDb()

got, err := s.SCard("destination")
if err != nil {
t.Errorf("Expected nil error, got: %v", err)
}
assert.NotNil(t, got)

}
func TestSMembers(t *testing.T) {
s, _ := initTestSetDb()

mem, err := s.SMembers("destination")
if err != nil {
t.Errorf("Expected nil error, got: %v", err)
}
assert.IsType(t, []string{}, mem)

}
func TestSIsMember(t *testing.T) {
s, _ := initTestSetDb()

got, err := s.SIsMember("destination", "key1")
if err != nil {
t.Errorf("Expected nil error, got: %v", err)
}
assert.False(t, got)

}

func TestNewSetStructure(t *testing.T) {
// expect error
opts := config.DefaultOptions
opts.DirPath = "" // the cause of error
setDB, err := NewSetStructure(opts)
assert.NotNil(t, err)
assert.Nil(t, setDB)
// expect no error
opts = config.DefaultOptions
dir, _ := os.MkdirTemp("", "TestSetStructure")
opts.DirPath = dir
setDB, _ = NewSetStructure(opts)
assert.NotNil(t, setDB)
assert.IsType(t, &engine.DB{}, setDB.db)
}

func TestSUnion(t *testing.T) {
s, _ := initTestSetDb()
keys := []string{"key1", "key2"}

_, err := s.SUnion(keys...)
if err != nil {
t.Errorf("Expected nil error, got: %v", err)
}

}

func TestSInter(t *testing.T) {
s, _ := initTestSetDb()
keys := []string{"key1", "key2"}

_, err := s.SInter(keys...)
if err != nil {
t.Errorf("Expected nil error, got: %v", err)
}

}

func TestSDiff(t *testing.T) {
s, _ := initTestSetDb()

_, err := s.SDiff("key1", "key2")
if err != nil {
t.Errorf("Expected nil error, got: %v", err)
}

}

func TestSUnionStore(t *testing.T) {
s, _ := initTestSetDb()

err := s.SUnionStore("destination", "key1", "key2")
if err != nil {
t.Errorf("Expected nil error, got: %v", err)
}

}

func TestSInterStore(t *testing.T) {
s, _ := initTestSetDb()

err := s.SInterStore("destination", "key1", "key2")
if err != nil {
t.Errorf("Expected nil error, got: %v", err)
}

}

0 comments on commit 104cdab

Please sign in to comment.