Skip to content

Commit 847b1dc

Browse files
committed
feat: add Seti method for updating shard records
- Implemented Seti method to update data in shard memory based on index. - Added tests for Seti method, including validation for out-of-bounds index. - Enhanced existing tests for shard functionality.
1 parent 4f0dc8d commit 847b1dc

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

shard.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ func (s *Shard) Set(data []byte) (i int) {
4141
return
4242
}
4343

44+
// Seti updates data in shard memory based on index. To preserve performance,
45+
// it does not check if index is valid. It is responsibility of caller to ensure
46+
// that index is valid and within bounds of shard.
47+
func (s *Shard) Seti(i int, data []byte) {
48+
s.Lock() // Lock for writing and reading
49+
s.slots[i].Set(data)
50+
s.Unlock() // Unlock for writing and reading
51+
}
52+
4453
// Get returns bytes from shard memory based on index. If array on output is
4554
// empty, then record is not exists.
4655
func (s *Shard) Get(index int) (v []byte) {

shard_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,53 @@ import (
55
"testing"
66
)
77

8+
func TestShardSeti(t *testing.T) {
9+
shard := NewShard(10, 4)
10+
// Use Set to get a valid index, then SetI to update
11+
idx := shard.Set([]byte{0, 0, 0, 0})
12+
shard.Seti(idx, []byte{1, 2, 3, 4})
13+
got := shard.Get(idx)
14+
want := []byte{1, 2, 3, 4}
15+
if !reflect.DeepEqual(got, want) {
16+
t.Errorf("SetI: got %v, want %v", got, want)
17+
}
18+
19+
// SetI with out-of-bounds index (should panic, so recover)
20+
defer func() {
21+
if r := recover(); r == nil {
22+
t.Errorf("SetI should panic for out-of-bounds index")
23+
}
24+
}()
25+
shard.Seti(20, []byte{9, 9, 9, 9})
26+
}
27+
28+
func TestShardFreeAndIsEmpty(t *testing.T) {
29+
shard := NewShard(5, 4)
30+
idx := shard.Set([]byte{1, 2, 3, 4})
31+
if shard.IsEmpty() {
32+
t.Errorf("Shard should not be empty after Set")
33+
}
34+
shard.Free(idx)
35+
if !shard.IsEmpty() {
36+
t.Errorf("Shard should be empty after Free")
37+
}
38+
}
39+
40+
func TestShardGetSlotsAvail(t *testing.T) {
41+
shard := NewShard(3, 2)
42+
if avail := shard.GetSlotsAvail(); avail != 3 {
43+
t.Errorf("Expected 3 slots available, got %d", avail)
44+
}
45+
idx := shard.Set([]byte{1, 2})
46+
if avail := shard.GetSlotsAvail(); avail != 2 {
47+
t.Errorf("Expected 2 slots available after Set, got %d", avail)
48+
}
49+
shard.Free(idx)
50+
if avail := shard.GetSlotsAvail(); avail != 3 {
51+
t.Errorf("Expected 3 slots available after Free, got %d", avail)
52+
}
53+
}
54+
855
func TestShardSimple(t *testing.T) {
956
for _, c := range []struct {
1057
recordCount int

0 commit comments

Comments
 (0)