Documentation
¶
Overview ¶
Package the provides file append with header and checksum
example usage:
w, err := NewWriter(filename)
if err != nil {
panic(err)
}
docID, _, err := w.Append([]byte("hello world"))
if err != nil {
panic(err)
}
// ...
r, err := NewReader(filename, 4096)
if err != nil {
panic(err)
}
data, err := r.Read(docID)
if err != nil {
panic(err)
}
log.Printf("%s",string(data))
Index ¶
- Constants
- Variables
- func FixedLen(file *os.File, fixedSize uint64) (uint64, error)
- func FixedReadAt(file *os.File, index uint64, into []byte) error
- func FixedWriteAt(file *os.File, index uint64, encoded []byte) error
- func Hash(s []byte) uint64
- func ReadFromReader(reader io.ReaderAt, offset uint32, blockSize int) ([]byte, uint32, error)
- func ReadFromReader64(reader io.ReaderAt, offset uint64, blockSize int) ([]byte, error)
- func ScanFromReader(reader io.ReaderAt, offset uint32, blockSize int, ...) error
- func WriteAtWriter64(file io.WriterAt, offset uint64, encoded []byte) error
- type Monotonic
- func (m *Monotonic) Append(b []byte) (uint64, error)
- func (m *Monotonic) AppendAt(index uint64, b []byte) error
- func (m *Monotonic) Close() error
- func (m *Monotonic) Count() uint64
- func (m *Monotonic) Last() ([]byte, error)
- func (m *Monotonic) MustAppend(b []byte) uint64
- func (m *Monotonic) MustLast() []byte
- func (m *Monotonic) MustRead(id uint64) []byte
- func (m *Monotonic) Read(id uint64) ([]byte, error)
- func (m *Monotonic) Sync() error
- func (m *Monotonic) TruncateAt(id uint64) error
- type OffsetWriter
- type Reader
- type Writer
Constants ¶
const FixedHeaderSize = 8
Variables ¶
var EBADSLT = errors.New("checksum mismatch")
var EINVAL = errors.New("invalid argument")
var EOVERFLOW = errors.New("you can only overwrite with smaller or equal size")
var MAGIC = []byte{0xb, 0xe, 0xe, 0xf}
change it if you wish, but has to be 4 bytes
var PAD = uint32(64)
the offsets are 32 bit, but usually you want to store more than 4gb of data so we just pad things to minimum 64 byte chunks
Functions ¶
func FixedReadAt ¶
Read from specific index
func FixedWriteAt ¶
Write at specific index format is:
8 byte LE checksum // go-metro(data) data: ... ... fixed size data
func ReadFromReader ¶
Reads specific offset. returns data, nextOffset, error. You can ReadFromReader(nextOffset) if you want to read the next document, or use the Scan() helper
func ReadFromReader64 ¶
func ScanFromReader ¶
func ScanFromReader(reader io.ReaderAt, offset uint32, blockSize int, cb func([]byte, uint32, uint32) error) error
Scan ReaderAt, if the callback returns error this error is returned as the Scan error
func WriteAtWriter64 ¶
Append bytes to the end of file format is:
16 byte header XX variable length data header: 4 bytes LE len(data) [1] // LE = Little Endian 4 bytes LE HASH(data)[2] // go-metro 4 bytes MAGIC [3] // 0xbeef 4 bytes LE HASH(1 2 3) // hash of the first 12 bytes data: .. ..
Types ¶
type Monotonic ¶
type Monotonic struct {
// contains filtered or unexported fields
}
completely thread unsafe lock accordingly
func NewMonotonic ¶
func (*Monotonic) MustAppend ¶
func (*Monotonic) TruncateAt ¶
This function is super racy, if you are going to use it protect the whole Monotonic object with a lock it truncates two files the requested id
type OffsetWriter ¶
type OffsetWriter struct {
// contains filtered or unexported fields
}
func NewOffsetWriter ¶
func NewOffsetWriter(fn string) (*OffsetWriter, error)
Creates new OffsetWriter, used to store one integer in a file with a checksum. Usually used for when you want to store your position in a log file that you want to scan from certain offset. example:
ow, err := NewOffsetWriter(filename)
if err != nil {
panic(err)
}
offset, err := ow.ReadOrDefault(0) // whatever the stored value is or 0
if err == nil {
panic(err)
}
func (*OffsetWriter) Close ¶
func (ow *OffsetWriter) Close() error
func (*OffsetWriter) ReadOrDefault ¶
func (ow *OffsetWriter) ReadOrDefault(def int64) int64
Read the offset or if empty/corrupt/not existant return default value
func (*OffsetWriter) SetOffset ¶
func (ow *OffsetWriter) SetOffset(offset int64) error
Write the offsed and its checksum
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
func NewReader ¶
Create New AppendReader (you just nice wrapper around ReadFromReader adn ScanFromReader) it is *safe* to use it concurrently Example usage
r, err := NewReader(filename, 4096)
if err != nil {
panic(err)
}
// read specific offset
data, _, err := r.Read(docID)
if err != nil {
panic(err)
}
// scan from specific offset
err = r.Scan(0, func(data []byte, offset, next uint32) error {
log.Printf("%v",data)
return nil
})
each Read requires 2 syscalls, one to read the header and one to read the data (since the length of the data is in the header). You can reduce that to 1 syscall if your data fits within 1 block, do not set blockSize < 16 because this is the header length. blockSize 0 means 16
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
func NewWriter ¶
Creates new writer and seeks to the end The writer is *safe* to be used concurrently, because it uses bump pointer like allocation of the offset. example usage:
w, err := NewWriter(filename)
if err != nil {
panic(err)
}
docID, _, err := w.Append([]byte("hello world"))
if err != nil {
panic(err)
}
r, err := NewReader(filename, 4096)
if err != nil {
panic(err)
}
data, _, err := r.Read(docID)
if err != nil {
panic(err)
}
log.Printf("%s",string(data))
func (*Writer) Append ¶
Append bytes to the end of file format is:
16 byte header XX variable length data header: 4 bytes LE len(data) [1] // LE = Little Endian 4 bytes LE HASH(data)[2] // go-metro 4 bytes MAGIC [3] // 0xbeef 4 bytes LE HASH(1 2 3) // hash of the first 12 bytes data: .. ..
Then the blob(header + data) is padded to PAD size using ((uint32(blobSize) + PAD - 1) / PAD).
it returns the addressable offset that you can use ReadFromReader() on