bsony

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

README

bsony

Pool-backed BSON encoding/decoding

Documentation

Overview

package bsony ...

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Array

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

An Array ...

func (*Array) Add

func (a *Array) Add(xs ...interface{}) *Array

Add ...

func (*Array) AddArray

func (a *Array) AddArray(v *Array) *Array

AddArray ...

func (*Array) AddBinary

func (a *Array) AddBinary(v *primitive.Binary) *Array

AddBinary ...

func (*Array) AddBool

func (a *Array) AddBool(v bool) *Array

AddBool ...

func (*Array) AddCodeScope

func (a *Array) AddCodeScope(v CodeWithScope) *Array

AddCodeScope ...

func (*Array) AddDBPointer

func (a *Array) AddDBPointer(v primitive.DBPointer) *Array

AddDBPointer ...

func (*Array) AddDateTime

func (a *Array) AddDateTime(v primitive.DateTime) *Array

AddDateTime ...

func (*Array) AddDateTimeFromTime

func (a *Array) AddDateTimeFromTime(v time.Time) *Array

AddDateTimeFromTime ...

func (*Array) AddDecimal128

func (a *Array) AddDecimal128(v primitive.Decimal128) *Array

AddDecimal128 ...

func (*Array) AddDoc

func (a *Array) AddDoc(v *Doc) *Array

AddDoc ...

func (*Array) AddDouble

func (a *Array) AddDouble(v float64) *Array

AddDouble ...

func (*Array) AddInt32

func (a *Array) AddInt32(v int32) *Array

AddInt32 ...

func (*Array) AddInt64

func (a *Array) AddInt64(v int64) *Array

AddInt64 ...

func (*Array) AddJavaScript

func (a *Array) AddJavaScript(v primitive.JavaScript) *Array

AddJavaScript ...

func (*Array) AddMaxKey

func (a *Array) AddMaxKey() *Array

AddMaxKey ...

func (*Array) AddMinKey

func (a *Array) AddMinKey() *Array

AddMinKey ...

func (*Array) AddNull

func (a *Array) AddNull() *Array

AddNull ...

func (*Array) AddOID

func (a *Array) AddOID(v primitive.ObjectID) *Array

AddOID ...

func (*Array) AddRegex

func (a *Array) AddRegex(v primitive.Regex) *Array

AddRegex ...

func (*Array) AddString

func (a *Array) AddString(v string) *Array

AddString ...

func (*Array) AddSymbol

func (a *Array) AddSymbol(v primitive.Symbol) *Array

AddSymbol ...

func (*Array) AddTimestamp

func (a *Array) AddTimestamp(v primitive.Timestamp) *Array

AddTimestamp ...

func (*Array) AddUndefined

func (a *Array) AddUndefined() *Array

AddUndefined

func (*Array) Clone

func (a *Array) Clone() *Array

Clone ...

func (*Array) Concat

func (a *Array) Concat(src *Array) *Array

Concat ..

func (*Array) CopyTo

func (a *Array) CopyTo(dst []byte) int

CopyTo ...

func (*Array) Err

func (a *Array) Err() error

Err returns any error recorded on the array.

func (*Array) Iter

func (a *Array) Iter() *ArrayIter

Iter ...

func (*Array) Len

func (a *Array) Len() int

Len ...

func (*Array) Reader

func (a *Array) Reader() (io.Reader, error)

Reader ...

func (*Array) Release

func (a *Array) Release()

Release returns allocated space. After calling this method, the array is invalid.

func (*Array) Valid

func (a *Array) Valid() bool

Valid indicates if the array is valid for use. An array is invalid after its storage has been released.

type ArrayIter

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

An ArrayIter ...

WARNING: the ArrayIter directly references the underlying data; because buffers may be reused, you MUST NOT keep an ArrayIter beyond the lifetime of the source array.

func (*ArrayIter) Err

func (a *ArrayIter) Err() error

Err returns any error from parsing the current value of the iterator.

func (*ArrayIter) Get

func (a *ArrayIter) Get() interface{}

Get returns the value of the current value of the iterator or nil if the end of the document has been reached or if the value could not be parsed. The Get is always a copy of any underlying data; it is safe to keep the result of a Get and release the source document.

func (*ArrayIter) Index

func (i *ArrayIter) Index() int

Index returns a zero-based index for the current value of the iterator. If Next has not been called, or if the end of the array has been reached, or if the value could not be parsed, this method returns -1.

func (*ArrayIter) Next

func (i *ArrayIter) Next() bool

Next advances the iterator, if possible

func (*ArrayIter) Type

func (i *ArrayIter) Type() Type

Type returns the type for the current value of the iterator or TypeInvalid if the end of the array has reached or the array is corrupted.

func (*ArrayIter) Value

func (i *ArrayIter) Value() Value

Value ... (returns a copy)

func (*ArrayIter) ValueUnsafe

func (i *ArrayIter) ValueUnsafe() Value

ValueUnsafe returns an object with raw type and byte slice of data for the current value of the iterator. The Type field will be zero and the Data slice nil if the end of the array is reached. The Err field will be non-nil if an error occured parsing the ValueUnsafe.

WARNING: the ValueUnsafe directly references the underlying data: (1) you MUST NOT modify the bytes of a ValueUnsafe; (2) because buffers may be reused, you MUST NOT keep a ValueUnsafe beyond the lifetime of the source array.

type BytePool

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

A BytePool wraps a sync.Pool of byte slices, but constrains byte slices created/returned to be between a minimum and maximum capacity.

func NewBytePool

func NewBytePool(minCap, maxCap int) *BytePool

NewBytePool constructs a byte slice pool with minimum and maximum capacities for byte slices in the pool. If minCap is negative, new slices will have zero capacity. If maxCap is negative, no maximum will be applied.

func (*BytePool) Get

func (p *BytePool) Get() []byte

Get gives the caller a byte slice from the pool or a new byte slice with the pool's configured minimum slice capacity. The byte slice returned will have its storage zeroed and have length zero.

func (*BytePool) Put

func (p *BytePool) Put(buf []byte)

Put returns a byte slice to the pool if the capacity is less than or equal to the pool's configured maximum slice capacity.

func (*BytePool) Resize

func (p *BytePool) Resize(buf []byte, size int) []byte

Resize returns a slice of the desired length. If the underlying capacity is insufficient, a copy of the slice with doubled capacity is returned. This is an intentional leaky pool abstraction, which minimizes amortized allocations by avoiding recyling small slices back to the pool.

type ByteSlicePool

type ByteSlicePool interface {
	Get() []byte
	Put(buf []byte)
	Resize(buf []byte, size int) []byte
}

A ByteSlicePool provides an abstraction for a pool of []byte objects. It provides Get, Put, and Resize methods. The Resize method allows for more control over allocations than relying on the native `append` function to grow slices.

type CodeWithScope

type CodeWithScope struct {
	Code  string
	Scope *Doc
}

A CodeWithScope represents Javascript code with an associated scope. Unlike the MongoDB Go Driver's `primitive.CodeWithScope`, the CodeWithScope must be a `Doc` from this package.

type Doc

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

A Doc object represents a BSON document

func (*Doc) Add

func (d *Doc) Add(k string, v interface{}) *Doc

Add ...

XXX rethink which of these actually need pointer support? all or none?

func (*Doc) AddArray

func (d *Doc) AddArray(k string, v *Array) *Doc

AddArray ...

func (*Doc) AddBinary

func (d *Doc) AddBinary(k string, v *primitive.Binary) *Doc

AddBinary ...

func (*Doc) AddBool

func (d *Doc) AddBool(k string, v bool) *Doc

AddBool ...

func (*Doc) AddCodeScope

func (d *Doc) AddCodeScope(k string, v CodeWithScope) *Doc

AddCodeScope ...

func (*Doc) AddDBPointer

func (d *Doc) AddDBPointer(k string, v primitive.DBPointer) *Doc

AddDBPointer ...

func (*Doc) AddDateTime

func (d *Doc) AddDateTime(k string, v primitive.DateTime) *Doc

AddDateTime ...

func (*Doc) AddDateTimeFromTime

func (d *Doc) AddDateTimeFromTime(k string, v time.Time) *Doc

AddDateTimeFromTime ...

func (*Doc) AddDecimal128

func (d *Doc) AddDecimal128(k string, v primitive.Decimal128) *Doc

AddDecimal128 ...

func (*Doc) AddDoc

func (d *Doc) AddDoc(k string, v *Doc) *Doc

AddDoc ...

func (*Doc) AddDouble

func (d *Doc) AddDouble(k string, v float64) *Doc

AddDouble ...

func (*Doc) AddInt32

func (d *Doc) AddInt32(k string, v int32) *Doc

AddInt32 ...

func (*Doc) AddInt64

func (d *Doc) AddInt64(k string, v int64) *Doc

AddInt64 ...

func (*Doc) AddJavaScript

func (d *Doc) AddJavaScript(k string, v primitive.JavaScript) *Doc

AddJavaScript ...

func (*Doc) AddMaxKey

func (d *Doc) AddMaxKey(k string) *Doc

AddMaxKey ...

func (*Doc) AddMinKey

func (d *Doc) AddMinKey(k string) *Doc

AddMinKey ...

func (*Doc) AddNull

func (d *Doc) AddNull(k string) *Doc

AddNull ...

func (*Doc) AddOID

func (d *Doc) AddOID(k string, v primitive.ObjectID) *Doc

AddOID ...

func (*Doc) AddRegex

func (d *Doc) AddRegex(k string, v primitive.Regex) *Doc

AddRegex ...

func (*Doc) AddString

func (d *Doc) AddString(k string, v string) *Doc

AddString ...

func (*Doc) AddSymbol

func (d *Doc) AddSymbol(k string, v primitive.Symbol) *Doc

AddSymbol ...

func (*Doc) AddTimestamp

func (d *Doc) AddTimestamp(k string, v primitive.Timestamp) *Doc

AddTimestamp ...

func (*Doc) AddUndefined

func (d *Doc) AddUndefined(k string) *Doc

AddUndefined ...

func (*Doc) Clone

func (d *Doc) Clone() *Doc

Clone ...

func (*Doc) Concat

func (d *Doc) Concat(src *Doc) *Doc

Concat ..

func (*Doc) CopyTo

func (d *Doc) CopyTo(dst []byte) int

CopyTo ...

func (*Doc) Err

func (d *Doc) Err() error

Err returns any error recorded on the document.

func (*Doc) Iter

func (d *Doc) Iter() *DocIter

Iter ...

func (*Doc) Len

func (d *Doc) Len() int

Len ...

func (*Doc) Reader

func (d *Doc) Reader() (io.Reader, error)

Reader ...

func (*Doc) Release

func (d *Doc) Release()

Release returns allocated space. After calling this method, the document is invalid. Any prior error is replaced with a "buffer released" message.

func (*Doc) Valid

func (d *Doc) Valid() bool

Valid indicates if the document is valid for use. A document is invalid after its storage has been released.

type DocIter

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

A DocIter ...

An initial call to Next() is required to initialize the first value.

WARNING: the DocIter directly references the underlying data; because buffers may be reused, you MUST NOT keep a DocIter beyond the lifetime of the source document.

func (*DocIter) Err

func (i *DocIter) Err() error

Err returns any error from parsing the current value of the iterator.

func (*DocIter) Get

func (i *DocIter) Get() interface{}

Get returns the value of the current value of the iterator or nil if the end of the document has been reached or if the value could not be parsed. The Get is always a copy of any underlying data; it is safe to keep the result of a Get and release the source document.

func (*DocIter) Key

func (i *DocIter) Key() string

Key returns the key for the current value of the iterator. If the the end of the document has been reached, the empty string will be returned.

func (*DocIter) Next

func (i *DocIter) Next() bool

Next advances the iterator, if possible. It returns true if a value is available.

func (*DocIter) Type

func (i *DocIter) Type() Type

Type returns the type for the current value of the iterator or TypeInvalid if the end of the document has reached or the document is corrupted.

func (*DocIter) Value

func (i *DocIter) Value() Value

Value returns a copy of the current value of the iterator or nil if the end of the document has been reached or if the value could not be parsed. It is safe to keep the value copy and release the source document.

func (*DocIter) ValueUnsafe

func (i *DocIter) ValueUnsafe() Value

ValueUnsafe returns an object with raw type and byte slice of data for the current value of the iterator. The Type field will be zero and the Data slice nil if the end of the document is reached. The Err field will be non-nil if an error occured parsing the ValueUnsafe.

WARNING: the ValueUnsafe directly references the underlying data: (1) you MUST NOT modify the bytes of a ValueUnsafe; (2) because buffers may be reused, you MUST NOT keep a ValueUnsafe beyond the lifetime of the source document.

type Factory

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

A Factory object is a factory for generating BSON documents and arrays. If Pool is nil, byte slices will be created as needed and not recycled.

func New

func New() *Factory

New returns a new Factory based on a byte slice pool with minimum slice capacity of 256 bytes and no maximum slice capacity.

func NewFromPool

func NewFromPool(pool ByteSlicePool) *Factory

NewFromPool returns a new Factory from a provided ByteSlicePool capacity of 256 bytes and no maximum slice capacity.

func (*Factory) NewArray

func (f *Factory) NewArray(xs ...interface{}) *Array

NewArray returns a BSON array. Any arguments will be added to the array.

func (*Factory) NewDoc

func (f *Factory) NewDoc() *Doc

NewDoc returns a new, empty BSON document

func (*Factory) NewDocFromBytes

func (f *Factory) NewDocFromBytes(buf []byte) (*Doc, error)

Doc returns a BSON document based on a slice of bytes. The document takes ownership of buf and the caller should not use it after calling NewDocFromBytes.

type Type

type Type byte

Type represents a BSON type.

const (
	TypeInvalid          Type = 0x00
	TypeDouble           Type = 0x01
	TypeString           Type = 0x02
	TypeEmbeddedDocument Type = 0x03
	TypeArray            Type = 0x04
	TypeBinary           Type = 0x05
	TypeUndefined        Type = 0x06
	TypeObjectID         Type = 0x07
	TypeBoolean          Type = 0x08
	TypeDateTime         Type = 0x09
	TypeNull             Type = 0x0A
	TypeRegex            Type = 0x0B
	TypeDBPointer        Type = 0x0C
	TypeJavaScript       Type = 0x0D
	TypeSymbol           Type = 0x0E
	TypeCodeWithScope    Type = 0x0F
	TypeInt32            Type = 0x10
	TypeTimestamp        Type = 0x11
	TypeInt64            Type = 0x12
	TypeDecimal128       Type = 0x13
	TypeMinKey           Type = 0xFF
	TypeMaxKey           Type = 0x7F
)

These constants uniquely refer to each BSON type.

func (Type) String

func (bt Type) String() string

String returns the string representation of the BSON type's name.

type Value

type Value interface {
	Clone() Value
	CopyTo(dst []byte) int
	Err() error
	Get() interface{}
	Len() int
	Release()
	Type() Type
}

Jump to

Keyboard shortcuts

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