Skip to content

Commit

Permalink
Update Go version to 1.21 and fix offset issue in cmd.go
Browse files Browse the repository at this point in the history
  • Loading branch information
diiyw committed Apr 28, 2024
1 parent 951271d commit 25d5bb9
Show file tree
Hide file tree
Showing 12 changed files with 445 additions and 149 deletions.
14 changes: 9 additions & 5 deletions ds/str/str.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (s *String) Decr(step int64) int64 {
}

// SetBit set a bit in a key
func (s *String) SetBit(offset int64, value bool) int {
func (s *String) SetBit(offset int64, value bool) int64 {
if offset < 0 {
return 0
}
Expand All @@ -71,13 +71,17 @@ func (s *String) SetBit(offset int64, value bool) int {
s.V = append(s.V, make([]byte, i-int64(len(s.V))+1)...)
}
by := s.V[i]
bit := byte(1 << uint(offset%8))
bit := byte(1 << (7 - uint(offset%8)))
old := by & bit
if value {
s.V[i] = by | bit
} else {
s.V[i] = by &^ bit
}
return 1
if old != 0 {
return 1
}
return 0
}

// GetBit get a bit in a key
Expand All @@ -87,11 +91,11 @@ func (s *String) GetBit(offset int64) int64 {

func (s *String) getBit(offset int64) int64 {
i := offset / 8
if offset < 0 || i > int64(len(s.V)) {
if offset < 0 || len(s.V) == 0 || i > int64(len(s.V))-1 {
return 0
}
by := s.V[i]
bit := byte(1 << uint(offset%8))
bit := byte(1 << (7 - uint(offset%8)))
if by&bit != 0 {
return 1
}
Expand Down
8 changes: 4 additions & 4 deletions ds/zset/sorted_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,13 @@ func (sortedSet *SortedSet) ZExists(member string) bool {
}

// ZRangeByScore returns members which score or member within the given border
func (sortedSet *SortedSet) ZRangeByScore(min float64, max float64) []*Item {
return sortedSet.zRange(min, max, 0, -1, false)
func (sortedSet *SortedSet) ZRangeByScore(min float64, max float64, offset, count int64) []*Item {
return sortedSet.zRange(min, max, offset, count, false)
}

// ZRevRangeByScore returns members which score or member within the given border
func (sortedSet *SortedSet) ZRevRangeByScore(min float64, max float64) []*Item {
return sortedSet.zRange(min, max, 0, -1, true)
func (sortedSet *SortedSet) ZRevRangeByScore(min float64, max float64, offset, count int64) []*Item {
return sortedSet.zRange(min, max, offset, count, true)
}

// ZIncrBy increases the score of the given member
Expand Down
10 changes: 5 additions & 5 deletions ds/zset/sorted_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestSortedSet_ZRangeByScore(t *testing.T) {
ss.ZAdd("member3", 0.5)

// Test if the range is correct
members := ss.ZRangeByScore(0.5, 2.5)
members := ss.ZRangeByScore(0.5, 2.5, 0, -1)
if len(members) != 3 {
t.Errorf("Range error expected 3 got %d", len(members))
}
Expand Down Expand Up @@ -185,11 +185,11 @@ func TestSortedSet_ZRevRangeByScore(t *testing.T) {
ss.ZAdd("member3", 0.5)

// Test if the range is correct
if len(ss.ZRevRangeByScore(1, 2)) != 1 {
t.Errorf("Range error expected 1 got %d", len(ss.ZRevRangeByScore(1, 2)))
if len(ss.ZRevRangeByScore(1, 2, 0, -1)) != 1 {
t.Errorf("Range error expected 1 got %d", len(ss.ZRevRangeByScore(1, 2, 0, -1)))
}
if len(ss.ZRevRangeByScore(0, 3)) != 3 {
t.Errorf("Range error expected 3 got %d", len(ss.ZRevRangeByScore(0, 3)))
if len(ss.ZRevRangeByScore(0, 3, 0, -1)) != 3 {
t.Errorf("Range error expected 3 got %d", len(ss.ZRevRangeByScore(0, 3, 0, -1)))
}
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/diiyw/nodis

go 1.20
go 1.21

require (
github.com/gorilla/websocket v1.5.1
Expand Down
Loading

0 comments on commit 25d5bb9

Please sign in to comment.