Skip to content

Commit

Permalink
all: imp docs
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Dec 27, 2023
1 parent 0b17cfc commit 5f7d205
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 36 deletions.
18 changes: 5 additions & 13 deletions internal/aghalg/ringbuffer.go
@@ -1,23 +1,15 @@
package aghalg

import (
"github.com/AdguardTeam/golibs/errors"
)

// RingBuffer is the implementation of ring buffer data structure.
type RingBuffer[T any] struct {
buf []T
cur int
cur uint
full bool
}

// NewRingBuffer initializes the new instance of ring buffer. size must be
// greater or equal to zero.
func NewRingBuffer[T any](size int) (rb *RingBuffer[T]) {
if size < 0 {
panic(errors.Error("ring buffer: size must be greater or equal to zero"))
}

func NewRingBuffer[T any](size uint) (rb *RingBuffer[T]) {
return &RingBuffer[T]{
buf: make([]T, size),
}
Expand All @@ -30,7 +22,7 @@ func (rb *RingBuffer[T]) Append(e T) {
}

rb.buf[rb.cur] = e
rb.cur = (rb.cur + 1) % cap(rb.buf)
rb.cur = (rb.cur + 1) % uint(cap(rb.buf))
if rb.cur == 0 {
rb.full = true
}
Expand Down Expand Up @@ -87,12 +79,12 @@ func (rb *RingBuffer[T]) splitCur() (before, after []T) {
}

// Len returns a length of the buffer.
func (rb *RingBuffer[T]) Len() (l int) {
func (rb *RingBuffer[T]) Len() (l uint) {
if !rb.full {
return rb.cur
}

return cap(rb.buf)
return uint(cap(rb.buf))
}

// Clear clears the buffer.
Expand Down
30 changes: 13 additions & 17 deletions internal/aghalg/ringbuffer_test.go
Expand Up @@ -36,35 +36,31 @@ func TestNewRingBuffer(t *testing.T) {
for i := 0; i < 10; i++ {
b.Append(i)
}
assert.Equal(t, []int{5, 6, 7, 8, 9}, elements(b, b.Len(), false))
assert.Equal(t, []int{5, 6, 7, 8, 9}, elements(b, int(b.Len()), false))

b.Clear()
assert.Zero(t, b.Len())
})

t.Run("negative_size", func(t *testing.T) {
assert.PanicsWithError(t, "ring buffer: size must be greater or equal to zero", func() {
aghalg.NewRingBuffer[int](-5)
})
})

t.Run("zero", func(t *testing.T) {
b := aghalg.NewRingBuffer[int](0)
for i := 0; i < 10; i++ {
b.Append(i)
assert.Equal(t, 0, b.Len())
assert.Empty(t, elements(b, b.Len(), false))
assert.Empty(t, elements(b, b.Len(), true))
bufLen := int(b.Len())
assert.Equal(t, 0, bufLen)
assert.Empty(t, elements(b, bufLen, false))
assert.Empty(t, elements(b, bufLen, true))
}
})

t.Run("single", func(t *testing.T) {
b := aghalg.NewRingBuffer[int](1)
for i := 0; i < 10; i++ {
b.Append(i)
assert.Equal(t, 1, b.Len())
assert.Equal(t, []int{i}, elements(b, b.Len(), false))
assert.Equal(t, []int{i}, elements(b, b.Len(), true))
bufLen := int(b.Len())
assert.Equal(t, 1, bufLen)
assert.Equal(t, []int{i}, elements(b, bufLen, false))
assert.Equal(t, []int{i}, elements(b, bufLen, true))
}
})
}
Expand Down Expand Up @@ -102,7 +98,7 @@ func TestRingBuffer_Range(t *testing.T) {
b.Append(i)
}

bufLen := b.Len()
bufLen := int(b.Len())
assert.Equal(t, tc.length, bufLen)

want := tc.want
Expand Down Expand Up @@ -163,11 +159,11 @@ func TestRingBuffer_Range_increment(t *testing.T) {
for i, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
b.Append(i)

assert.Equal(t, tc.want, elements(b, b.Len(), false))
bufLen := int(b.Len())
assert.Equal(t, tc.want, elements(b, bufLen, false))

slices.Reverse(tc.want)
assert.Equal(t, tc.want, elements(b, b.Len(), true))
assert.Equal(t, tc.want, elements(b, bufLen, true))
})
}
}
5 changes: 3 additions & 2 deletions internal/querylog/qlog.go
Expand Up @@ -196,13 +196,13 @@ func newLogEntry(params *AddParams) (entry *logEntry) {
// Add implements the [QueryLog] interface for *queryLog.
func (l *queryLog) Add(params *AddParams) {
var isEnabled, fileIsEnabled bool
var memSize int
var memSize uint
func() {
l.confMu.RLock()
defer l.confMu.RUnlock()

isEnabled, fileIsEnabled = l.conf.Enabled, l.conf.FileEnabled
memSize = int(l.conf.MemSize)
memSize = l.conf.MemSize
}()

if !isEnabled {
Expand Down Expand Up @@ -230,6 +230,7 @@ func (l *queryLog) Add(params *AddParams) {
if !l.flushPending && fileIsEnabled && l.buffer.Len() >= memSize {
l.flushPending = true

// TODO(s.chzhen): Fix occasional rewrite of entires.
go func() {
flushErr := l.flushLogBuffer()
if flushErr != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/querylog/querylog.go
Expand Up @@ -143,10 +143,10 @@ func newQueryLog(conf Config) (l *queryLog, err error) {
}
}

memSize := int(conf.MemSize)
memSize := conf.MemSize
if memSize == 0 {
// If query log is not diabled, we still need to write entries to a
// file. And all writing goes through a buffer.
// If query log is enabled, we still need to write entries to a file.
// And all writing goes through a buffer.
memSize = 1
}

Expand Down
2 changes: 1 addition & 1 deletion internal/querylog/search.go
Expand Up @@ -73,7 +73,7 @@ func (l *queryLog) searchMemory(params *searchParams, cache clientCache) (entrie
return true
})

return entries, l.buffer.Len()
return entries, int(l.buffer.Len())
}

// search - searches log entries in the query log using specified parameters
Expand Down

0 comments on commit 5f7d205

Please sign in to comment.