protocache

package module
v0.0.0-...-7e17cb5 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2024 License: BSD-3-Clause Imports: 8 Imported by: 0

README

ProtoCache Go

Alternative flat binary format for Protobuf schema. It' works like FlatBuffers, but it's usually smaller and surpports map. Flat means no deserialization overhead. A benchmark shows the Protobuf has considerable deserialization overhead and significant reflection overhead. FlatBuffers is fast but wastes space. ProtoCache takes balance of data size and read speed, so it's useful in data caching.

Data Size Decode + Traverse Decode + Traverse(reflection)
Protobuf 574B 11451ns 25458ns
vtprotobuf 574B 5029ns 19907ns
ProtoCache 780B 1121ns 2266ns
FlatBuffers 1296B 2191ns No Go Api

See detail in C++ version.

Code Gen

protoc --pcgo_out=. test.proto

A protobuf compiler plugin called protoc-gen-pcgo is available to generate Go file. The generated file is short and human friendly. Don't mind to edit it if nessasery.

Basic APIs

raw, err := protocache.Serialize(pbMessage)
assert(t, err == nil)

root := pc.AS_Main(raw)
assert(t, root.IsValid())

Serializing a protobuf message with protocache.Serialize is the only way to create protocache binary at present. It's easy to access by wrapping the data with generated code.

Reflection

std::string err;
raw, err := os.ReadFile("test.proto")
assert(t, err == nil)

proto, err := compiler.ParseProto(raw) //CGO
assert(t, err == nil)

var pool reflect.DescriptorPool
assert(t, pool.Register(proto))

root := pool.Find("test.Main")
assert(t, root != nil)

field := root.Lookup("f64")
assert(t, field != nil)

The reflection apis are simliar to C++ version. An example can be found in the test. CGO is needed to parse schema.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Hash28

func Hash28(seed uint64, data []byte) [2]uint64

func Serialize

func Serialize(obj proto.Message) ([]byte, error)

Types

type Array

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

func AsArray

func AsArray(data []byte) Array

func (*Array) EnumValue

func (a *Array) EnumValue() []EnumValue

func (*Array) Float32

func (a *Array) Float32() []float32

func (*Array) Float64

func (a *Array) Float64() []float64

func (*Array) Get

func (a *Array) Get(i uint32) Field

func (*Array) Int32

func (a *Array) Int32() []int32

func (*Array) Int64

func (a *Array) Int64() []int64

func (*Array) IsValid

func (a *Array) IsValid() bool

func (*Array) Size

func (a *Array) Size() uint32

func (*Array) Uint32

func (a *Array) Uint32() []uint32

func (*Array) Uint64

func (a *Array) Uint64() []uint64

type BoolArray

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

func AsBoolArray

func AsBoolArray(data []byte) BoolArray

func (*BoolArray) Get

func (a *BoolArray) Get(i uint32) bool

func (*BoolArray) IsValid

func (a *BoolArray) IsValid() bool

func (*BoolArray) Raw

func (a *BoolArray) Raw() []bool

func (*BoolArray) Size

func (a *BoolArray) Size() uint32

type BytesArray

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

func AsBytesArray

func AsBytesArray(data []byte) BytesArray

func (*BytesArray) Get

func (a *BytesArray) Get(i uint32) []byte

func (*BytesArray) IsValid

func (a *BytesArray) IsValid() bool

func (*BytesArray) Size

func (a *BytesArray) Size() uint32

type Enum

type Enum interface {
	~int32
}

type EnumArray

type EnumArray[T Enum] struct {
	// contains filtered or unexported fields
}

func AsEnumArray

func AsEnumArray[T Enum](data []byte) EnumArray[T]

func (*EnumArray[T]) Get

func (a *EnumArray[T]) Get(i uint32) T

func (*EnumArray[T]) IsValid

func (a *EnumArray[T]) IsValid() bool

func (*EnumArray[T]) Raw

func (a *EnumArray[T]) Raw() []T

func (*EnumArray[T]) Size

func (a *EnumArray[T]) Size() uint32

type EnumValue

type EnumValue int32

type Field

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

func (*Field) GetArray

func (f *Field) GetArray() Array

func (*Field) GetBool

func (f *Field) GetBool() bool

func (*Field) GetBoolArray

func (f *Field) GetBoolArray() []bool

func (*Field) GetBytes

func (f *Field) GetBytes() []byte

func (*Field) GetEnumValue

func (f *Field) GetEnumValue() EnumValue

func (*Field) GetEnumValueArray

func (f *Field) GetEnumValueArray() []EnumValue

func (*Field) GetFloat32

func (f *Field) GetFloat32() float32

func (*Field) GetFloat32Array

func (f *Field) GetFloat32Array() []float32

func (*Field) GetFloat64

func (f *Field) GetFloat64() float64

func (*Field) GetFloat64Array

func (f *Field) GetFloat64Array() []float64

func (*Field) GetInt32

func (f *Field) GetInt32() int32

func (*Field) GetInt32Array

func (f *Field) GetInt32Array() []int32

func (*Field) GetInt64

func (f *Field) GetInt64() int64

func (*Field) GetInt64Array

func (f *Field) GetInt64Array() []int64

func (*Field) GetMap

func (f *Field) GetMap() Map

func (*Field) GetMessage

func (f *Field) GetMessage() Message

func (*Field) GetObject

func (f *Field) GetObject() []byte

func (*Field) GetString

func (f *Field) GetString() string

func (*Field) GetUint32

func (f *Field) GetUint32() uint32

func (*Field) GetUint32Array

func (f *Field) GetUint32Array() []uint32

func (*Field) GetUint64

func (f *Field) GetUint64() uint64

func (*Field) GetUint64Array

func (f *Field) GetUint64Array() []uint64

func (*Field) IsValid

func (f *Field) IsValid() bool

type Float32Array

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

func AsFloat32Array

func AsFloat32Array(data []byte) Float32Array

func (*Float32Array) Get

func (a *Float32Array) Get(i uint32) float32

func (*Float32Array) IsValid

func (a *Float32Array) IsValid() bool

func (*Float32Array) Raw

func (a *Float32Array) Raw() []float32

func (*Float32Array) Size

func (a *Float32Array) Size() uint32

type Float64Array

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

func AsFloat64Array

func AsFloat64Array(data []byte) Float64Array

func (*Float64Array) Get

func (a *Float64Array) Get(i uint32) float64

func (*Float64Array) IsValid

func (a *Float64Array) IsValid() bool

func (*Float64Array) Raw

func (a *Float64Array) Raw() []float64

func (*Float64Array) Size

func (a *Float64Array) Size() uint32

type Int32Array

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

func AsInt32Array

func AsInt32Array(data []byte) Int32Array

func (*Int32Array) Get

func (a *Int32Array) Get(i uint32) int32

func (*Int32Array) IsValid

func (a *Int32Array) IsValid() bool

func (*Int32Array) Raw

func (a *Int32Array) Raw() []int32

func (*Int32Array) Size

func (a *Int32Array) Size() uint32

type Int64Array

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

func AsInt64Array

func AsInt64Array(data []byte) Int64Array

func (*Int64Array) Get

func (a *Int64Array) Get(i uint32) int64

func (*Int64Array) IsValid

func (a *Int64Array) IsValid() bool

func (*Int64Array) Raw

func (a *Int64Array) Raw() []int64

func (*Int64Array) Size

func (a *Int64Array) Size() uint32

type KeySource

type KeySource interface {
	Reset()
	Total() int
	Next() []byte
}

type Map

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

func AsMap

func AsMap(data []byte) Map

func (*Map) FindByInt32

func (m *Map) FindByInt32(key int32) Field

func (*Map) FindByInt64

func (m *Map) FindByInt64(key int64) Field

func (*Map) FindByString

func (m *Map) FindByString(key string) Field

func (*Map) FindByUint32

func (m *Map) FindByUint32(key uint32) Field

func (*Map) FindByUint64

func (m *Map) FindByUint64(key uint64) Field

func (*Map) IsValid

func (m *Map) IsValid() bool

func (*Map) Key

func (m *Map) Key(i uint32) Field

func (*Map) Size

func (m *Map) Size() uint32

func (*Map) Value

func (m *Map) Value(i uint32) Field

type Message

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

func AsMessage

func AsMessage(data []byte) Message

func (*Message) GetField

func (m *Message) GetField(id uint16) Field

func (*Message) HashField

func (m *Message) HashField(id uint16) bool

func (*Message) IsValid

func (m *Message) IsValid() bool

type PerfectHash

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

func Build

func Build(src KeySource) PerfectHash

func (*PerfectHash) Data

func (h *PerfectHash) Data() []byte

func (*PerfectHash) Init

func (h *PerfectHash) Init(data []byte) bool

func (*PerfectHash) IsValid

func (h *PerfectHash) IsValid() bool

func (*PerfectHash) Locate

func (h *PerfectHash) Locate(key []byte) uint32

func (*PerfectHash) Size

func (h *PerfectHash) Size() uint32

type StringArray

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

func AsStringArray

func AsStringArray(data []byte) StringArray

func (*StringArray) Get

func (a *StringArray) Get(i uint32) string

func (*StringArray) IsValid

func (a *StringArray) IsValid() bool

func (*StringArray) Size

func (a *StringArray) Size() uint32

type Uint32Array

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

func AsUint32Array

func AsUint32Array(data []byte) Uint32Array

func (*Uint32Array) Get

func (a *Uint32Array) Get(i uint32) uint32

func (*Uint32Array) IsValid

func (a *Uint32Array) IsValid() bool

func (*Uint32Array) Raw

func (a *Uint32Array) Raw() []uint32

func (*Uint32Array) Size

func (a *Uint32Array) Size() uint32

type Uint64Array

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

func AsUint64Array

func AsUint64Array(data []byte) Uint64Array

func (*Uint64Array) Get

func (a *Uint64Array) Get(i uint32) uint64

func (*Uint64Array) IsValid

func (a *Uint64Array) IsValid() bool

func (*Uint64Array) Raw

func (a *Uint64Array) Raw() []uint64

func (*Uint64Array) Size

func (a *Uint64Array) Size() uint32

Directories

Path Synopsis
cmd
test
pc

Jump to

Keyboard shortcuts

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