recordio

package module
v0.0.0-...-a3a308c Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2020 License: MIT Imports: 3 Imported by: 1

README

recordio

Package recordio implements a file format for a sequence of records. It could be used to store, for example, serialized data structures to disk.

Records are stored as an unsigned varint specifying the length of the data, and then the data itself as a binary blob.

Example: reading with a Scanner

f, _ := os.Open("file.data")
defer f.Close()
scanner := recordio.NewScanner(f)
for scanner.Scan() {
	data := scanner.Bytes()
	// Do something with data
}
if err := scanner.Err(); err != nil {
	// Do error handling
}

Example: reading with a Reader

f, _ := os.Open("file.dat")
f.Close()
r := recordio.NewReader(f)
for {
	data, err := r.Next()
	if err == io.EOF {
		break
	}
	// Do something with data
}

Example: writing

f, _ := os.Create("file.data")
w := recordio.NewWriter(f)
w.Write([]byte("this is a record"))
w.Write([]byte("this is a second record"))
f.Close()

License

This project is licensed under the MIT license. See LICENSE for more details.

Documentation

Overview

Package recordio implements a file format for a sequence of records. It could be used to store, for example, serialized data structures to disk.

Records are stored as an unsigned varint specifying the length of the data, and then the data itself as a binary blob.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

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

func NewReader

func NewReader(r io.Reader) *Reader

NewReader returns a new reader. If r doesn't implement io.ByteReader, it will be wrapped in a bufio.Reader.

func (*Reader) Next

func (r *Reader) Next() ([]byte, error)

Next returns the next data record. It returns io.EOF if there are no more records.

type Scanner

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

A Scanner is a convenient method for reading records sequentially.

Example
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/eclesh/recordio"
)

func main() {
	f, err := os.Open("file.dat")
	if err != nil {
		log.Fatalln(err)
	}
	defer f.Close()
	scanner := recordio.NewScanner(f)
	for scanner.Scan() {
		fmt.Println(scanner.Bytes())
	}
	if err := scanner.Err(); err != nil {
		log.Fatalln(err)
	}
}
Output:

func NewScanner

func NewScanner(r io.Reader) *Scanner

NewScanner creates a new Scanner from reader r.

func (*Scanner) Bytes

func (s *Scanner) Bytes() []byte

Bytes returns the most recently scanned record. Subsequent calls may overwrite the returned data, so you must copy it if not using it immediately.

func (*Scanner) Err

func (s *Scanner) Err() error

Err returns the most recent error or nil if the error was EOF.

func (*Scanner) Scan

func (s *Scanner) Scan() bool

Scan chugs through the input record by record and stops at the first error or EOF.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}
Example
package main

import (
	"log"
	"os"

	"github.com/eclesh/recordio"
)

func main() {
	f, err := os.Create("file.dat")
	if err != nil {
		log.Fatalln(err)
	}
	w := recordio.NewWriter(f)
	w.Write([]byte("this is a record"))
	w.Write([]byte("this is a second record"))
	f.Close()
}
Output:

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter returns a new writer.

func (*Writer) Write

func (w *Writer) Write(data []byte) (int, error)

Write writes a data record.

Jump to

Keyboard shortcuts

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