Skip to content

Commit 81252c0

Browse files
authored
Code refactor and tuning (#6)
1 parent c6c507a commit 81252c0

File tree

7 files changed

+110
-148
lines changed

7 files changed

+110
-148
lines changed

entry/entry.go

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,26 @@ type Writer interface {
2222
WriteBatch(Batch) (common.ErrCode, error)
2323
}
2424

25-
// WriteFlusher interface.
26-
type WriteFlusher interface {
27-
io.Writer
28-
Flush() error
29-
}
30-
3125
// Entry represents queue entry.
3226
type Entry []byte
3327

3428
// Marshal writes entry to writer.
35-
func (e Entry) Marshal(w WriteFlusher, format common.EntryFormat, flushable bool) (code common.ErrCode, err error) {
29+
func (e Entry) Marshal(w io.Writer, format common.EntryFormat) (code common.ErrCode, err error) {
3630
switch format {
3731
case common.EntryV1:
38-
return e.marshalV1(w, flushable)
32+
return e.marshalV1(w)
3933

4034
default:
4135
return common.EntryUnsupportedFormat, common.ErrEntryUnsupportedFormat
4236
}
4337
}
4438

4539
// [Length - uint32][Checksum - uint32][Payload - bytes]
46-
func (e Entry) marshalV1(w WriteFlusher, flushable bool) (code common.ErrCode, err error) {
40+
func (e Entry) marshalV1(w io.Writer) (code common.ErrCode, err error) {
4741
var buf [8]byte
4842
common.Endianese.PutUint64(buf[:], uint64(len(e))<<32|uint64(crc32.ChecksumIEEE(e)))
4943
if _, err = w.Write(buf[:]); err == nil {
50-
if _, err = w.Write(e); err == nil {
51-
if flushable {
52-
err = w.Flush()
53-
}
54-
}
44+
_, err = w.Write(e)
5545
}
5646

5747
if err != nil {
@@ -176,19 +166,13 @@ func (b *Batch) Append(e Entry) {
176166
}
177167

178168
// Marshal into writer.
179-
func (b *Batch) Marshal(w WriteFlusher, format common.EntryFormat) (code common.ErrCode, err error) {
169+
func (b *Batch) Marshal(w io.Writer, format common.EntryFormat) (code common.ErrCode, err error) {
180170
if b.Len() > 0 {
181171
for _, e := range b.entries {
182-
if code, err = e.Marshal(w, format, false); err != nil {
172+
if code, err = e.Marshal(w, format); err != nil {
183173
return code, err
184174
}
185175
}
186-
187-
if err = w.Flush(); err != nil {
188-
code = common.EntryWriteErr
189-
} else {
190-
code = common.NoError
191-
}
192176
}
193177
return
194178
}

entry/entry_test.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,6 @@ func (e *errorWriter) Write([]byte) (int, error) { return 0, fmt.Errorf("fake er
125125

126126
func (e *errorWriter) Flush() error { return nil }
127127

128-
type errorFlusher struct{}
129-
130-
func (e *errorFlusher) Write([]byte) (int, error) { return 0, nil }
131-
132-
func (e *errorFlusher) Flush() error { return fmt.Errorf("fake error") }
133-
134128
type noopFlusher struct{ io.Writer }
135129

136130
func (f *noopFlusher) Flush() error { return nil }
@@ -139,25 +133,21 @@ func TestEntryMarshal(t *testing.T) {
139133
t.Run("UnsupportedFormat", func(t *testing.T) {
140134
var e Entry = []byte{1, 2, 3, 4}
141135

142-
code, err := e.Marshal(nil, 123, true)
136+
code, err := e.Marshal(nil, 123)
143137
require.Equal(t, common.ErrEntryUnsupportedFormat, err)
144138
require.Equal(t, common.EntryUnsupportedFormat, code)
145139
})
146140

147141
t.Run("ErrorHandling", func(t *testing.T) {
148142
var e Entry = []byte{1, 2, 3, 4}
149143

150-
code, err := e.Marshal(&errorWriter{}, common.EntryV1, true)
144+
code, err := e.Marshal(&errorWriter{}, common.EntryV1)
151145
require.Equal(t, common.EntryWriteErr, code)
152146
require.Error(t, err)
153147

154148
batch := NewBatch(2)
155149
batch.Append(e)
156150

157-
code, err = batch.Marshal(&errorFlusher{}, common.EntryV1)
158-
require.Equal(t, common.EntryWriteErr, code)
159-
require.Error(t, err)
160-
161151
code, err = batch.Marshal(&errorWriter{}, common.EntryV1)
162152
require.Equal(t, common.EntryWriteErr, code)
163153
require.Error(t, err)
@@ -167,7 +157,7 @@ func TestEntryMarshal(t *testing.T) {
167157
var e Entry = []byte{1, 2, 3, 4}
168158
var buf bytes.Buffer
169159

170-
code, err := e.Marshal(&noopFlusher{Writer: &buf}, common.EntryV1, true)
160+
code, err := e.Marshal(&noopFlusher{Writer: &buf}, common.EntryV1)
171161
require.NoError(t, err)
172162
require.Equal(t, code, common.NoError)
173163
require.EqualValues(t, []byte{0, 0, 0, 4, 0xb6, 0x3c, 0xfb, 0xcd, 1, 2, 3, 4}, buf.Bytes())
@@ -201,7 +191,7 @@ func TestEntry(t *testing.T) {
201191

202192
var buf bytes.Buffer
203193

204-
code, err := e.Marshal(&noopFlusher{Writer: &buf}, common.EntryV1, true)
194+
code, err := e.Marshal(&noopFlusher{Writer: &buf}, common.EntryV1)
205195
require.NoError(t, err)
206196
require.Equal(t, common.NoError, code)
207197

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ require (
1111
github.com/grandecola/mmap v0.6.0 // indirect
1212
github.com/hashicorp/errwrap v1.1.0 // indirect
1313
github.com/kr/pretty v0.2.0 // indirect
14+
github.com/kr/pty v1.1.1 // indirect
15+
github.com/kr/text v0.1.0 // indirect
1416
github.com/pmezard/go-difflib v1.0.0 // indirect
17+
github.com/stretchr/objx v0.1.0 // indirect
1518
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
1619
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
1720
)

queue_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func TestQueueRace(t *testing.T) {
148148
_ = q.Close()
149149
}()
150150

151-
// start reader
151+
// start readers
152152
var wg sync.WaitGroup
153153

154154
collectValue := make([]int, size)
@@ -195,6 +195,7 @@ func TestQueueRace(t *testing.T) {
195195
}()
196196
}
197197

198+
// start writers
198199
ch := make(chan uint32, 1)
199200
for i := 0; i < 4; i++ {
200201
wg.Add(1)

segment/v1/segment.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package segv1
22

33
import (
44
"io"
5-
"sync"
65
"sync/atomic"
76

87
"github.com/linxGnu/pqueue/common"
@@ -16,15 +15,12 @@ type Segment struct {
1615
readOnly bool
1716

1817
entryFormat common.EntryFormat
18+
w entry.Writer
1919

20-
r entry.Reader
21-
rLock sync.Mutex
2220
offset uint32
2321
numEntries uint32
2422
maxEntries uint32
25-
26-
w entry.Writer
27-
wLock sync.Mutex
23+
r entry.Reader
2824
}
2925

3026
// NewReadOnlySegment creates new Segment for readonly.
@@ -122,9 +118,6 @@ func (s *Segment) WriteEntry(e entry.Entry) (common.ErrCode, error) {
122118
}
123119

124120
func (s *Segment) writeEntry(e entry.Entry) (common.ErrCode, error) {
125-
s.wLock.Lock()
126-
defer s.wLock.Unlock()
127-
128121
if s.numEntries >= s.maxEntries {
129122
return common.SegmentNoMoreWrite, nil
130123
}
@@ -148,9 +141,6 @@ func (s *Segment) WriteBatch(b entry.Batch) (common.ErrCode, error) {
148141
}
149142

150143
func (s *Segment) writeBatch(b entry.Batch) (common.ErrCode, error) {
151-
s.wLock.Lock()
152-
defer s.wLock.Unlock()
153-
154144
if s.numEntries >= s.maxEntries {
155145
return common.SegmentNoMoreWrite, nil
156146
}
@@ -166,9 +156,6 @@ func (s *Segment) writeBatch(b entry.Batch) (common.ErrCode, error) {
166156

167157
// ReadEntry from segment.
168158
func (s *Segment) ReadEntry(e *entry.Entry) (common.ErrCode, int, error) {
169-
s.rLock.Lock()
170-
defer s.rLock.Unlock()
171-
172159
if !s.readOnly {
173160
// readable?
174161
if s.offset == atomic.LoadUint32(&s.numEntries) {

0 commit comments

Comments
 (0)