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
- func ByteArrayToString(buf []byte) string
- func ReadBufAsStringSlice(buf Records) ([]string, error)
- func StringToByteArray(v string) []byte
- type Iterator
- type IteratorPos
- type Reader
- func (bbi *Reader) Buf() []byte
- func (bbi *Reader) CurrentPos() IteratorPos
- func (bbi *Reader) End() bool
- func (bbi *Reader) Get(ctx context.Context) (Record, error)
- func (bbi *Reader) Len() int
- func (bbi *Reader) Next(ctx context.Context)
- func (bbi *Reader) Release()
- func (bbi *Reader) Reset(buf []byte, check bool) error
- func (bbi *Reader) SetBackward(bool)
- type Record
- type Records
- type Writer
Constants ¶
const (
IteratorPosUnknown = 0
)
Variables ¶
This section is empty.
Functions ¶
func ByteArrayToString ¶
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 ¶
ReadBufAsStringSlice receives a records buffer, iterates it over the buffer and returns the records as a slice of strings
func StringToByteArray ¶
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 ¶
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) CurrentPos ¶
func (bbi *Reader) CurrentPos() IteratorPos
func (*Reader) Get ¶
Get returns current element. It receives ctx, but ignores it, because the function is not blocking here.
func (*Reader) Next ¶
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 ¶
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 ¶
type Record ¶
type Record []byte
Record is a slice of byte, which contains the record information.
func (Record) AsWeakString ¶
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) WritableSize ¶
WritableSize 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 | +--------------+-----------------+-- --+--------------+------------------+
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer allows to form Records structure in a slice of bytes.
func (*Writer) Allocate ¶
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 ¶
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 ¶
FreeLastAllocation releases the last allocation. The ln contains last allocation size. Will return error if the last allocation was not the pprovided size
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. |