ringbuffer

package module
v0.0.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2021 License: MIT Imports: 7 Imported by: 37

README

ringbuffer

自动扩容的循环缓冲区实现

Github Actions Go Report Card Codacy Badge GoDoc LICENSE Code Size

使用

package main

import (
	"fmt"
	"github.com/Allenxuxu/ringbuffer"
)

func main() {
	rb := ringbuffer.New(2)

	// 自动扩容
	fmt.Println(rb.Capacity())  //2
	fmt.Println(rb.Length())    //0

	rb.Write([]byte("ab"))
	fmt.Println(rb.Capacity())  //2
	fmt.Println(rb.Length())    //2

	rb.Write([]byte("cd"))
	fmt.Println(rb.Capacity())  //4
	fmt.Println(rb.Length())    //4

	// VirtualXXX 函数便捷操作
	rb = New(1024)
	_, _ = rb.Write([]byte("abcd"))
	fmt.Println(rb.Length())
	fmt.Println(rb.free())
	buf := make([]byte, 4)

	_, _ = rb.Read(buf)
	fmt.Println(string(buf))

	rb.Write([]byte("1234567890"))
	rb.VirtualRead(buf)
	fmt.Println(string(buf))
	fmt.Println(rb.Length())
	fmt.Println(rb.VirtualLength())
	rb.VirtualFlush()
	fmt.Println(rb.Length())
	fmt.Println(rb.VirtualLength())

	rb.VirtualRead(buf)
	fmt.Println(string(buf))
	fmt.Println(rb.Length())
	fmt.Println(rb.VirtualLength())
	rb.VirtualRevert()
	fmt.Println(rb.Length())
	fmt.Println(rb.VirtualLength())
	// Output: 4
	// 1020
	// abcd
	// 1234
	// 10
	// 6
	// 6
	// 6
	// 5678
	// 6
	// 2
	// 6
	// 6
}

参考

https://github.com/smallnest/ringbuffer

感谢

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrIsEmpty = errors.New("ring buffer is empty")

ErrIsEmpty 缓冲区为空

Functions

func PutInPool added in v0.0.7

func PutInPool(b *RingBuffer)

PutInPool returns byte buffer to the pool.

ByteBuffer.B mustn't be touched after returning it to the pool. Otherwise data races will occur.

Types

type Pool added in v0.0.7

type Pool struct {
	// contains filtered or unexported fields
}

Pool represents byte buffer pool.

Distinct pools may be used for distinct types of byte buffers. Properly determined byte buffer types with their own pools may help reducing memory waste.

func (*Pool) Get added in v0.0.7

func (p *Pool) Get() *RingBuffer

Get returns new byte buffer with zero length.

The byte buffer may be returned to the pool via Put after the use in order to minimize GC overhead.

func (*Pool) Put added in v0.0.7

func (p *Pool) Put(b *RingBuffer)

Put releases byte buffer obtained via Get to the pool.

The buffer mustn't be accessed after returning to the pool.

type RingBuffer

type RingBuffer struct {
	// contains filtered or unexported fields
}

RingBuffer 自动扩容循环缓冲区

Example
rb := New(1024)
_, _ = rb.Write([]byte("abcd"))
fmt.Println(rb.Length())
fmt.Println(rb.free())
buf := make([]byte, 4)

_, _ = rb.Read(buf)
fmt.Println(string(buf))

rb.Write([]byte("1234567890"))
rb.VirtualRead(buf)
fmt.Println(string(buf))
fmt.Println(rb.Length())
fmt.Println(rb.VirtualLength())
rb.VirtualFlush()
fmt.Println(rb.Length())
fmt.Println(rb.VirtualLength())

rb.VirtualRead(buf)
fmt.Println(string(buf))
fmt.Println(rb.Length())
fmt.Println(rb.VirtualLength())
rb.VirtualRevert()
fmt.Println(rb.Length())
fmt.Println(rb.VirtualLength())
Output:

4
1020
abcd
1234
10
6
6
6
5678
6
2
6
6

func GetFromPool added in v0.0.7

func GetFromPool() *RingBuffer

GetFromPool returns an empty byte buffer from the pool.

Got byte buffer may be returned to the pool via Put call. This reduces the number of memory allocations required for byte buffer management.

func New

func New(size int) *RingBuffer

New 返回一个初始大小为 size 的 RingBuffer

func NewWithData

func NewWithData(data []byte) *RingBuffer

NewWithData 特殊场景使用,RingBuffer 会持有data,不会自己申请内存去拷贝

func (*RingBuffer) Bytes

func (r *RingBuffer) Bytes() (buf []byte)

Bytes 返回所有可读数据,此操作不会移动读指针,仅仅是拷贝全部数据

func (*RingBuffer) Capacity

func (r *RingBuffer) Capacity() int

func (*RingBuffer) IsEmpty

func (r *RingBuffer) IsEmpty() bool

func (*RingBuffer) IsFull

func (r *RingBuffer) IsFull() bool

func (*RingBuffer) Length

func (r *RingBuffer) Length() int

func (*RingBuffer) Peek

func (r *RingBuffer) Peek(len int) (first []byte, end []byte)

func (*RingBuffer) PeekAll

func (r *RingBuffer) PeekAll() (first []byte, end []byte)

func (*RingBuffer) PeekUint16 added in v0.0.5

func (r *RingBuffer) PeekUint16() uint16

func (*RingBuffer) PeekUint32 added in v0.0.5

func (r *RingBuffer) PeekUint32() uint32

func (*RingBuffer) PeekUint64 added in v0.0.5

func (r *RingBuffer) PeekUint64() uint64

func (*RingBuffer) PeekUint8 added in v0.0.5

func (r *RingBuffer) PeekUint8() uint8

func (*RingBuffer) Read

func (r *RingBuffer) Read(p []byte) (n int, err error)

func (*RingBuffer) ReadByte

func (r *RingBuffer) ReadByte() (b byte, err error)

func (*RingBuffer) Reset

func (r *RingBuffer) Reset()

func (*RingBuffer) Retrieve

func (r *RingBuffer) Retrieve(len int)

func (*RingBuffer) RetrieveAll

func (r *RingBuffer) RetrieveAll()

func (*RingBuffer) String

func (r *RingBuffer) String() string

func (*RingBuffer) VirtualFlush

func (r *RingBuffer) VirtualFlush()

VirtualFlush 刷新虚读指针 VirtualXXX 系列配合使用

func (*RingBuffer) VirtualLength

func (r *RingBuffer) VirtualLength() int

VirtualLength 虚拟长度,虚读后剩余可读数据长度 VirtualXXX 系列配合使用

func (*RingBuffer) VirtualRead

func (r *RingBuffer) VirtualRead(p []byte) (n int, err error)

VirtualRead 虚读,不移动 read 指针,需要配合 VirtualFlush 和 VirtualRevert 使用 VirtualXXX 系列配合使用

func (*RingBuffer) VirtualRevert

func (r *RingBuffer) VirtualRevert()

VirtualRevert 还原虚读指针 VirtualXXX 系列配合使用

func (*RingBuffer) WithData added in v0.0.8

func (r *RingBuffer) WithData(data []byte)

func (*RingBuffer) Write

func (r *RingBuffer) Write(p []byte) (n int, err error)

func (*RingBuffer) WriteByte

func (r *RingBuffer) WriteByte(c byte) error

func (*RingBuffer) WriteString

func (r *RingBuffer) WriteString(s string) (n int, err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL