bytewriter - ActiveState ActiveGo 1.8
...

Package bytewriter

import "github.com/performancecopilot/speed/bytewriter"
Overview
Index

Overview ▾

Package bytewriter implements writers that support concurrent writing within a fixed length block

initially tried to use bytes.Buffer but the main restriction with that is that it does not allow the freedom to move around in the buffer. Further, it always writes at the end of the buffer

another attempt was to maintain a position identifier that was always passed to any function that wrote anything and the function *had* to return the next writable location, which resulted in calls like

“`go pos = writeString(buffer, pos, "mmv") “`

which became unmaintainable after a while, and along with all the side maintainance looked extremely ugly

then implemented a minimal buffer wrapper that gives the freedom to move around and write anywhere you want, which worked if you only need one position identifier, i.e. only one write operation happens at a time

this implements a writer that supports multiple concurrent writes within a fixed length block

Index ▾

type ByteWriter
    func NewByteWriter(n int) *ByteWriter
    func NewByteWriterSlice(buffer []byte) *ByteWriter
    func (w *ByteWriter) Bytes() []byte
    func (w *ByteWriter) Len() int
    func (w *ByteWriter) MustWrite(data []byte, offset int) int
    func (w *ByteWriter) MustWriteFloat32(val float32, offset int) int
    func (w *ByteWriter) MustWriteFloat64(val float64, offset int) int
    func (w *ByteWriter) MustWriteInt32(val int32, offset int) int
    func (w *ByteWriter) MustWriteInt64(val int64, offset int) int
    func (w *ByteWriter) MustWriteString(val string, offset int) int
    func (w *ByteWriter) MustWriteUint32(val uint32, offset int) int
    func (w *ByteWriter) MustWriteUint64(val uint64, offset int) int
    func (w *ByteWriter) MustWriteVal(val interface{}, offset int) int
    func (w *ByteWriter) Write(data []byte, offset int) (int, error)
    func (w *ByteWriter) WriteFloat32(val float32, offset int) (int, error)
    func (w *ByteWriter) WriteFloat64(val float64, offset int) (int, error)
    func (w *ByteWriter) WriteInt32(val int32, offset int) (int, error)
    func (w *ByteWriter) WriteInt64(val int64, offset int) (int, error)
    func (w *ByteWriter) WriteString(val string, offset int) (int, error)
    func (w *ByteWriter) WriteUint32(val uint32, offset int) (int, error)
    func (w *ByteWriter) WriteUint64(val uint64, offset int) (int, error)
    func (w *ByteWriter) WriteVal(val interface{}, offset int) (int, error)
type MemoryMappedWriter
    func NewMemoryMappedWriter(loc string, size int) (*MemoryMappedWriter, error)
    func (b *MemoryMappedWriter) Unmap(removefile bool) error
type Writer

Package files

bytewriter.go memorymappedwriter.go writer.go

type ByteWriter

ByteWriter is a simple wrapper over a byte slice that supports writing anywhere

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

func NewByteWriter

func NewByteWriter(n int) *ByteWriter

NewByteWriter creates a new ByteWriter of the specified size

func NewByteWriterSlice

func NewByteWriterSlice(buffer []byte) *ByteWriter

NewByteWriterSlice creates a new ByteWriter using the passed slice

func (*ByteWriter) Bytes

func (w *ByteWriter) Bytes() []byte

Bytes returns the internal byte array of the ByteWriter

func (*ByteWriter) Len

func (w *ByteWriter) Len() int

Len returns the maximum size of the ByteWriter

func (*ByteWriter) MustWrite

func (w *ByteWriter) MustWrite(data []byte, offset int) int

MustWrite is a write that will panic if Write returns an error

func (*ByteWriter) MustWriteFloat32

func (w *ByteWriter) MustWriteFloat32(val float32, offset int) int

MustWriteFloat32 panics if WriteFloat32 fails

func (*ByteWriter) MustWriteFloat64

func (w *ByteWriter) MustWriteFloat64(val float64, offset int) int

MustWriteFloat64 panics if WriteFloat64 fails

func (*ByteWriter) MustWriteInt32

func (w *ByteWriter) MustWriteInt32(val int32, offset int) int

MustWriteInt32 panics if WriteInt32 fails

func (*ByteWriter) MustWriteInt64

func (w *ByteWriter) MustWriteInt64(val int64, offset int) int

MustWriteInt64 panics if WriteInt64 fails

func (*ByteWriter) MustWriteString

func (w *ByteWriter) MustWriteString(val string, offset int) int

MustWriteString panics if WriteString fails

func (*ByteWriter) MustWriteUint32

func (w *ByteWriter) MustWriteUint32(val uint32, offset int) int

MustWriteUint32 panics if WriteInt32 fails

func (*ByteWriter) MustWriteUint64

func (w *ByteWriter) MustWriteUint64(val uint64, offset int) int

MustWriteUint64 panics if WriteUint64 fails

func (*ByteWriter) MustWriteVal

func (w *ByteWriter) MustWriteVal(val interface{}, offset int) int

MustWriteVal panics if WriteVal fails

func (*ByteWriter) Write

func (w *ByteWriter) Write(data []byte, offset int) (int, error)

func (*ByteWriter) WriteFloat32

func (w *ByteWriter) WriteFloat32(val float32, offset int) (int, error)

WriteFloat32 writes an float32 to the buffer

func (*ByteWriter) WriteFloat64

func (w *ByteWriter) WriteFloat64(val float64, offset int) (int, error)

WriteFloat64 writes an float64 to the buffer

func (*ByteWriter) WriteInt32

func (w *ByteWriter) WriteInt32(val int32, offset int) (int, error)

WriteInt32 writes an int32 to the buffer

func (*ByteWriter) WriteInt64

func (w *ByteWriter) WriteInt64(val int64, offset int) (int, error)

WriteInt64 writes an int64 to the buffer

func (*ByteWriter) WriteString

func (w *ByteWriter) WriteString(val string, offset int) (int, error)

WriteString writes a string to the buffer

func (*ByteWriter) WriteUint32

func (w *ByteWriter) WriteUint32(val uint32, offset int) (int, error)

WriteUint32 writes an uint32 to the buffer

func (*ByteWriter) WriteUint64

func (w *ByteWriter) WriteUint64(val uint64, offset int) (int, error)

WriteUint64 writes an uint64 to the buffer

func (*ByteWriter) WriteVal

func (w *ByteWriter) WriteVal(val interface{}, offset int) (int, error)

WriteVal writes an arbitrary value to the buffer

type MemoryMappedWriter

MemoryMappedWriter is a ByteBuffer that is also mapped into memory

type MemoryMappedWriter struct {
    *ByteWriter
    // contains filtered or unexported fields
}

func NewMemoryMappedWriter

func NewMemoryMappedWriter(loc string, size int) (*MemoryMappedWriter, error)

NewMemoryMappedWriter will create and return a new instance of a MemoryMappedWriter

func (*MemoryMappedWriter) Unmap

func (b *MemoryMappedWriter) Unmap(removefile bool) error

Unmap will manually delete the memory mapping of a mapped buffer

type Writer

Writer defines an abstraction for an object that allows writing of binary values anywhere within a fixed range

type Writer interface {
    Bytes() []byte
    Len() int

    Write([]byte, int) (int, error)
    WriteVal(interface{}, int) (int, error)
    WriteString(string, int) (int, error)
    WriteInt32(int32, int) (int, error)
    WriteInt64(int64, int) (int, error)
    WriteUint32(uint32, int) (int, error)
    WriteUint64(uint64, int) (int, error)
    WriteFloat32(float32, int) (int, error)
    WriteFloat64(float64, int) (int, error)

    MustWrite([]byte, int) int
    MustWriteVal(interface{}, int) int
    MustWriteString(string, int) int
    MustWriteInt32(int32, int) int
    MustWriteInt64(int64, int) int
    MustWriteUint32(uint32, int) int
    MustWriteUint64(uint64, int) int
    MustWriteFloat32(float32, int) int
    MustWriteFloat64(float64, int) int
}