| 1 |
/* |
| 2 |
Copyright 2025 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package buffer |
| 18 |
|
| 19 |
import ( |
| 20 |
"errors" |
| 21 |
"io" |
| 22 |
) |
| 23 |
|
| 24 |
// Compile-time check that *TypedRingFixed[byte] implements io.Writer. |
| 25 |
var _ io.Writer = (*TypedRingFixed[byte])(nil) |
| 26 |
|
| 27 |
// ErrInvalidSize indicates size must be > 0 |
| 28 |
var ErrInvalidSize = errors.New("size must be positive") |
| 29 |
|
| 30 |
// TypedRingFixed is a fixed-size circular buffer for elements of type T. |
| 31 |
// Writes overwrite older data, keeping only the last N elements. |
| 32 |
// Not thread safe. |
| 33 |
type TypedRingFixed[T any] struct { |
| 34 |
data []T |
| 35 |
size int |
| 36 |
writeCursor int |
| 37 |
written int64 |
| 38 |
} |
| 39 |
|
| 40 |
// NewTypedRingFixed creates a circular buffer with the given capacity (must be > 0). |
| 41 |
func NewTypedRingFixed[T any](size int) (*TypedRingFixed[T], error) { |
| 42 |
if size <= 0 { |
| 43 |
return nil, ErrInvalidSize |
| 44 |
} |
| 45 |
return &TypedRingFixed[T]{ |
| 46 |
data: make([]T, size), |
| 47 |
size: size, |
| 48 |
}, nil |
| 49 |
} |
| 50 |
|
| 51 |
// Write writes p to the buffer, overwriting old data if needed. |
| 52 |
func (r *TypedRingFixed[T]) Write(p []T) (int, error) { |
| 53 |
originalLen := len(p) |
| 54 |
r.written += int64(originalLen) |
| 55 |
|
| 56 |
// If the input is larger than our buffer, only keep the last 'size' elements |
| 57 |
if originalLen > r.size { |
| 58 |
p = p[originalLen-r.size:] |
| 59 |
} |
| 60 |
|
| 61 |
// Copy data, handling wrap-around |
| 62 |
n := len(p) |
| 63 |
remain := r.size - r.writeCursor |
| 64 |
if n <= remain { |
| 65 |
copy(r.data[r.writeCursor:], p) |
| 66 |
} else { |
| 67 |
copy(r.data[r.writeCursor:], p[:remain]) |
| 68 |
copy(r.data, p[remain:]) |
| 69 |
} |
| 70 |
|
| 71 |
r.writeCursor = (r.writeCursor + n) % r.size |
| 72 |
return originalLen, nil |
| 73 |
} |
| 74 |
|
| 75 |
// Slice returns buffer contents in write order. Don't modify the returned slice. |
| 76 |
func (r *TypedRingFixed[T]) Slice() []T { |
| 77 |
if r.written == 0 { |
| 78 |
return nil |
| 79 |
} |
| 80 |
|
| 81 |
// Buffer hasn't wrapped yet |
| 82 |
if r.written < int64(r.size) { |
| 83 |
return r.data[:r.writeCursor] |
| 84 |
} |
| 85 |
|
| 86 |
// Buffer has wrapped - need to return data in correct order |
| 87 |
// Data from writeCursor to end is oldest, data from 0 to writeCursor is newest |
| 88 |
if r.writeCursor == 0 { |
| 89 |
return r.data |
| 90 |
} |
| 91 |
|
| 92 |
out := make([]T, r.size) |
| 93 |
copy(out, r.data[r.writeCursor:]) |
| 94 |
copy(out[r.size-r.writeCursor:], r.data[:r.writeCursor]) |
| 95 |
return out |
| 96 |
} |
| 97 |
|
| 98 |
// Size returns the buffer capacity. |
| 99 |
func (r *TypedRingFixed[T]) Size() int { |
| 100 |
return r.size |
| 101 |
} |
| 102 |
|
| 103 |
// Len returns how many elements are currently in the buffer. |
| 104 |
func (r *TypedRingFixed[T]) Len() int { |
| 105 |
if r.written < int64(r.size) { |
| 106 |
return int(r.written) |
| 107 |
} |
| 108 |
return r.size |
| 109 |
} |
| 110 |
|
| 111 |
// TotalWritten returns total elements ever written (including overwritten ones). |
| 112 |
func (r *TypedRingFixed[T]) TotalWritten() int64 { |
| 113 |
return r.written |
| 114 |
} |
| 115 |
|
| 116 |
// Reset clears the buffer. |
| 117 |
func (r *TypedRingFixed[T]) Reset() { |
| 118 |
r.writeCursor = 0 |
| 119 |
r.written = 0 |
| 120 |
} |