Skip to content

Commit 6331068

Browse files
committed
Adds segment
1 parent 0710b87 commit 6331068

File tree

6 files changed

+158
-0
lines changed

6 files changed

+158
-0
lines changed

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ go 1.24
44

55
require (
66
github.com/davecgh/go-spew v1.1.1 // indirect
7+
github.com/fatih/color v1.18.0 // indirect
8+
github.com/mattn/go-colorable v0.1.14 // indirect
9+
github.com/mattn/go-isatty v0.0.20 // indirect
710
github.com/pmezard/go-difflib v1.0.0 // indirect
11+
github.com/rakyll/gotest v0.0.6 // indirect
812
github.com/stretchr/testify v1.10.0 // indirect
13+
golang.org/x/sys v0.32.0 // indirect
914
gopkg.in/yaml.v3 v3.0.1 // indirect
1015
)

go.sum

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
22
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
4+
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
5+
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
6+
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
7+
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
8+
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
9+
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
10+
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
11+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
12+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
313
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
414
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
15+
github.com/rakyll/gotest v0.0.6 h1:hBTqkO3jiuwYW/M9gL4bu0oTYcm8J6knQAAPUsJsz1I=
16+
github.com/rakyll/gotest v0.0.6/go.mod h1:SkoesdNCWmiD4R2dljIUcfSnNdVZ12y8qK4ojDkc2Sc=
517
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
618
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
19+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
20+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
21+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
22+
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
23+
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
724
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
825
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
926
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/storage/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package storage
2+
3+
type Config struct {
4+
segmentsRoot string
5+
}

internal/storage/index.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package storage
2+
3+
type Index struct {
4+
entries map[int]int
5+
elementId int
6+
}
7+
8+
func NewIndex() *Index {
9+
return &Index{
10+
entries: make(map[int]int),
11+
elementId: 0,
12+
}
13+
}
14+
15+
func (i *Index) Append(offset int) int {
16+
currentElementId := i.elementId
17+
i.entries[currentElementId] = offset
18+
i.elementId++
19+
return currentElementId
20+
}
21+
22+
func (i *Index) GetOffset(elementId int) (int, bool) {
23+
v, ok := i.entries[elementId]
24+
return v, ok
25+
}

internal/storage/segment.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package storage
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type Segment struct {
8+
store *Store
9+
index *Index
10+
}
11+
12+
func NewSegment(startingIndex int, config *Config, index *Index) (*Segment, error) {
13+
filePath := fmt.Sprintf("%s/segment-%d", config.segmentsRoot, startingIndex)
14+
store, err := NewStore(filePath)
15+
if err != nil {
16+
return nil, err
17+
}
18+
19+
return &Segment{store: store, index: index}, nil
20+
}
21+
22+
func (s *Segment) Append(data []byte) (int, error) {
23+
offset, err := s.store.Append(data)
24+
if err != nil {
25+
return 0, err
26+
}
27+
elementIndex := s.index.Append(offset)
28+
return elementIndex, nil
29+
}
30+
31+
func (s *Segment) Read(id int) ([]byte, error) {
32+
offset, ok := s.index.GetOffset(id)
33+
if !ok {
34+
return nil, fmt.Errorf("unknown message id: %d", id)
35+
}
36+
return s.store.Read(offset)
37+
}
38+
39+
func (s *Segment) Close() error {
40+
return s.store.Close()
41+
}

internal/storage/segment_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package storage
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestAppendToTheSegment(t *testing.T) {
12+
config := &Config{segmentsRoot: os.TempDir()}
13+
defer os.Remove(fmt.Sprintf("%s/segment_10", os.TempDir()))
14+
15+
index := NewIndex()
16+
17+
segment, err := NewSegment(10, config, index)
18+
assert.NoError(t, err)
19+
defer segment.Close()
20+
21+
id, err := segment.Append([]byte("Hello World"))
22+
assert.NoError(t, err)
23+
assert.Equal(t, id, 0)
24+
_, ok := index.entries[0]
25+
assert.True(t, ok)
26+
}
27+
28+
func TestReadFromTheSegment(t *testing.T) {
29+
config := &Config{segmentsRoot: os.TempDir()}
30+
defer os.Remove(fmt.Sprintf("%s/segment_10", os.TempDir()))
31+
32+
index := NewIndex()
33+
34+
segment, err := NewSegment(10, config, index)
35+
assert.NoError(t, err)
36+
defer segment.Close()
37+
38+
id1, err := segment.Append([]byte("Hello World"))
39+
assert.NoError(t, err)
40+
41+
id2, err := segment.Append([]byte("Bye World"))
42+
assert.NoError(t, err)
43+
44+
data2, err := segment.Read(id2)
45+
assert.NoError(t, err)
46+
assert.Equal(t, []byte("Bye World"), data2)
47+
48+
data1, err := segment.Read(id1)
49+
assert.NoError(t, err)
50+
assert.Equal(t, []byte("Hello World"), data1)
51+
}
52+
53+
func TestReadNonExistingMessage(t *testing.T) {
54+
config := &Config{segmentsRoot: os.TempDir()}
55+
defer os.Remove(fmt.Sprintf("%s/segment_12", os.TempDir()))
56+
57+
index := NewIndex()
58+
59+
segment, err := NewSegment(12, config, index)
60+
assert.NoError(t, err)
61+
defer segment.Close()
62+
63+
_, err = segment.Read(13)
64+
assert.Error(t, err)
65+
}

0 commit comments

Comments
 (0)