records

package
v0.0.0-...-f9b9731 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2024 License: Apache-2.0 Imports: 8 Imported by: 6

Documentation

Overview

Copyright 2018-2019 The logrange Authors

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

records package contains main types and objects used for different data manipulations. Record is a slice of bytes with unknown semantic. It means that the package doesn't care about what the records are, but just describes the main types the records selecting like records stores and iterators.

Index

Constants

View Source
const (
	IteratorPosUnknown = 0
)

Variables

This section is empty.

Functions

func ByteArrayToString

func ByteArrayToString(buf []byte) string

ByteArrayToString turns a slice of bytes to string, without extra memory allocations

NOTE! Using this function is extremely dangerous, so it can be done with extra care with clear understanding how it works

func ReadBufAsStringSlice

func ReadBufAsStringSlice(buf Records) ([]string, error)

ReadBufAsStringSlice receives a records buffer, iterates it over the buffer and returns the records as a slice of strings

func StringToByteArray

func StringToByteArray(v string) []byte

StringToByteArray gets a string and turn it to []byte without extra memoy allocations

NOTE! Using this function is extremely dangerous, so it can be done with extra care with clear understanding how it works

Types

type Iterator

type Iterator interface {

	// Next moves the iterator current position to the next record. Implementation
	// must define the order and which record should be the next.
	//
	// Next expects ctx to be used in case of the call is going to be blocked
	// some implementations can ignore this parameter if the the
	// operation can be perform without blocking the context.
	//
	// For some implementations, calling the function makes the result, returned
	// by previous call of Get() not relevant. It means, that NO results,
	// that were previously received by calling Get(), must be used after
	// the Next() call. In case of if the result is needed it must be copied
	// to another record before calling the Next() function.
	Next(ctx context.Context)

	// Get returns the current record the iterator points to. If there is
	// no current records (all ones are iterated), or the collection is empty
	// the method will return io.EOF in the error.
	//
	// Get receives ctx to be used in case of the call is going to be blocked.
	// Some implementations can ignore this parameter if the the
	// operation can be perform without blocking the context.
	//
	// If error is nil, then the method returns slice of bytes, which is the
	// current record. The slice could be nil as well, which is valid.
	Get(ctx context.Context) (Record, error)

	// Release is an implementation specific function which allows to
	// release underlying resources. It can be called by the iterator using
	// code to let the implementation know that underlying resources can
	// be released. There is no guarantee that the iterator will be used after
	// the call again. Implementation should guarantee that it will behave
	// same way after the call as if it is never called.
	Release()

	// SetBackword allows to change the itearator direction
	// not all implementations can support it
	SetBackward(bool)

	// CurrentPos returns an object which describes the current iterator position
	CurrentPos() IteratorPos
}

Iterator allows to iterate over a collection of records. The interface allows to select records in an order (either forward, backward or any other one) and returns them via Get() function.

When the iterator is initialized it points to the record, which will be selected first. Certain implementation defines an order, so the implementation defines which record will be returned first, which one is second etc.

If no more records could be returned the Get() function will return io.EOF

func SrtingsIterator

func SrtingsIterator(strs ...string) Iterator

SrtingsIterator receives strings and return an iterator over them. The function is not fast, uses many allocations and should be used in test code only.

type IteratorPos

type IteratorPos interface{}

IteratorPos is a current position in the iterated collection

type Reader

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

Reader can iterate over Records. It implements data.Iterator interface with accessing to the records in forward direction.

func (*Reader) Buf

func (bbi *Reader) Buf() []byte

Buf returns underlying buffer

func (*Reader) CurrentPos

func (bbi *Reader) CurrentPos() IteratorPos

func (*Reader) End

func (bbi *Reader) End() bool

End returns whether the end of the records list is reached.

func (*Reader) Get

func (bbi *Reader) Get(ctx context.Context) (Record, error)

Get returns current element. It receives ctx, but ignores it, because the function is not blocking here.

func (*Reader) Len

func (bbi *Reader) Len() int

Len returns number of records found in the buf

func (*Reader) Next

func (bbi *Reader) Next(ctx context.Context)

Next switches to the next element. Data() allows to access to the current one. Has no effect if the end is reached

The ctx param is ignored, because the method is not blocking

func (*Reader) Release

func (bbi *Reader) Release()

Release does nothing for the reader. Just part of Iterator

func (*Reader) Reset

func (bbi *Reader) Reset(buf []byte, check bool) error

Reset initializes Reader. Two params are expected - the buffer (buf) and whether the buffer must be checked for consistency (check).

It returns an error if the structure is incorrect. If the function returns an error, buffer will be reset to an empty value

If the check is not performed (check == false), the function always returns nil (no errors)

func (*Reader) SetBackward

func (bbi *Reader) SetBackward(bool)

type Record

type Record []byte

Record is a slice of byte, which contains the record information.

func (Record) AsWeakString

func (r Record) AsWeakString() string

AsWeakString represents the record as a string, but doesn't allocate the new buffer for it The method must be used with care

func (Record) MakeCopy

func (r Record) MakeCopy() Record

func (Record) WritableSize

func (r Record) WritableSize() int

WritableSize is a part of bytes.Writable interface.

func (Record) WriteTo

func (r Record) WriteTo(ow *xbinary.ObjectsWriter) (int, error)

WriteTo is a part of bytes.Writable interface

type Records

type Records []byte

Records is a slice of bytes which has a special format. Every record in the Records buffer is represented by 2 fields - the record size and its data. The data field could be skipped, if its size is 0 or the end of records marker (EofMarker) is met. The EofMarker is the size field which has 0xFFFFFFFF value:

+--------------+-----------------+--------------+-- -+----------+-----+ | 1st rec sizd | 1st record data | 2nd rec size | ... |0xFFFFFFFF|.... | +--------------+-----------------+--------------+-- -+----------+-----+

Records buffer could be built without EofMaker if the last record is ended at the end of the slice (which is defined by its LENGTH, but NOT A CAPACITY):

+--------------+-----------------+-- --+--------------+------------------+ | 1st rec sizd | 1st record data | ... |last rec size | last record data | +--------------+-----------------+-- --+--------------+------------------+

func Check

func Check(buf []byte) (Records, int, error)

Check receives a slice of bytes and checks whether it is formatted as Records slice or not. The function returns Records object, number of records found and an error if any. If an error is returned, the Records and number of records values don't make a sense.

type Writer

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

Writer allows to form Records structure in a slice of bytes.

func (*Writer) Allocate

func (bbw *Writer) Allocate(ln int, extend bool) ([]byte, error)

Allocate reserves ln bytes for data and writes its size before the data. The method returns slice of allocated bytes. In case of the allocation is not possible, it returns error.

The extend param defines the caller desire to extend the buffer if the size is not big enough or return a error, without extension. If the buffer is not extendable, it will return an error in any case when the buffers size is insufficient. If the buffer is extendable, it will try to re-allocate the buffer only if the extend == true

func (*Writer) Close

func (bbw *Writer) Close() (Records, error)

Close puts EOF marker or completes the writing process. Consequentive Allocate() calls will return errors. The Close() method returns Records or an error if any

func (*Writer) FreeLastAllocation

func (bbw *Writer) FreeLastAllocation(ln int) error

FreeLastAllocation releases the last allocation. The ln contains last allocation size. Will return error if the last allocation was not the pprovided size

func (*Writer) Reset

func (bbw *Writer) Reset(buf []byte, extendable bool)

Reset Initializes the writer by provided slice. The extendable flag allows the buffer will be re-allocated in case of not enough space in Allocate method (see below)

func (*Writer) String

func (bbw *Writer) String() string

String - returns the string for the writer value

Directories

Path Synopsis
Package chunk contains Chunk interface which defines functions for accessing to a chunk.
Package chunk contains Chunk interface which defines functions for accessing to a chunk.
chunkfs
Package chunkfs contains structures and functions which allow to work with chunks stored in files.
Package chunkfs contains structures and functions which allow to work with chunks stored in files.

Jump to

Keyboard shortcuts

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