nwenc

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2019 License: MIT Imports: 7 Imported by: 0

README

nwenc

TravisCI CodeCov Go Report Card GoDoc

Package nwenc is the implementation of encoder and decoder for the nextword data.

OffsetEncoder/OffsetDecoder

The OffsetEncoder and OffsetDecoder can encode/decode between an int64 offset value and a string. There are several implementation for OffsetMapper. They have different performance.

OffsetEncoder/OffsetDecoder example:

f, err := os.Open(filepath.Join("testdata", "words.txt"))
om, err := NewAllReadOffsetMapper(f)

om.OffsetEncode("a")   // 0 ("a" is at 0 byte in testdata/words.txt)
om.OffsetEncode("bcd") // 43 ("bcd" is at 43 bytes in testdata/words.txt)

om.OffsetDecode(0)  // "a"
om.OffsetDecode(43) // "bcd"

Encoder/Decoder

Encoder and Decoder can encode/decode bwtween an int64 offset value and bytes. Encoder/Decoder example:

f, err := os.Open(filepath.Join("testdata", "words.txt"))
om, err := NewAllReadOffsetMapper(f)

buf := new(bytes.Buffer)

byteLen := 3 // It encodes/decodes 3 bytes (24 bits) data.
enc, err := NewEncoder(byteLen)

enc.Encode(buf, 0)               // writes "a" at offset == 0
enc.EncodeString(buf, om, "bcd") // writes "bcd"

dec, err := NewDecoder(byteLen)

dec.Decode(buf)           // 0 ("a")
dec.DecodeString(buf, om) // "bcd"

Documentation

Overview

Package nwenc is the implementation of encoder and decoder for the nextword data.

The OffsetEncoder and OffsetDecoder can encode/decode between an int64 offset value and a string. The offset means the byte offset in a file that the string appears. There are several implementation for OffsetMapper. They have different performance.

Encoder and Decoder can encode/decode between an int64 offset value and bytes.

OffsetEncoder/OffsetDecoder example:

f, err := os.Open(filepath.Join("testdata", "words.txt"))
om, err := NewAllReadOffsetMapper(f)

om.OffsetEncode("a")   // 0 ("a" is at 0 byte in testdata/words.txt)
om.OffsetEncode("bcd") // 43 ("bcd" is at 43 bytes in testdata/words.txt)

om.OffsetDecode(0)  // "a"
om.OffsetDecode(43) // "bcd"

Encoder/Decoder example:

f, err := os.Open(filepath.Join("testdata", "words.txt"))
om, err := NewAllReadOffsetMapper(f)

buf := new(bytes.Buffer)

byteLen := 3 // It encodes/decodes 3 bytes (24 bits) data.
enc, err := NewEncoder(byteLen)

enc.Encode(buf, 0)               // writes "a" at offset == 0
enc.EncodeString(buf, om, "bcd") // writes "bcd"

dec, err := NewDecoder(byteLen)

dec.Decode(buf)           // 0 ("a")
dec.DecodeString(buf, om) // "bcd"

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AllReadOffsetMapper added in v0.2.0

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

AllReadOffsetMapper is one of the implementation of OffsetMapper. It reads all of io.Reader in advance in order to map fast.

func NewAllReadOffsetMapper added in v0.2.0

func NewAllReadOffsetMapper(r io.Reader) (*AllReadOffsetMapper, error)

NewAllReadOffsetMapper returns an AllReadOffsetMapper. This function reads all of io.Reader in advance in order to map fast.

func (*AllReadOffsetMapper) OffsetDecode added in v0.2.0

func (m *AllReadOffsetMapper) OffsetDecode(offset int64) (s string, err error)

OffsetDecode is the implementation of OffsetDecode. It works fast. When offset is not found, it will return OffsetDecodeError.

func (*AllReadOffsetMapper) OffsetEncode added in v0.2.0

func (m *AllReadOffsetMapper) OffsetEncode(s string) (offset int64, err error)

OffsetEncode is the implementation of OffsetEncoder. It works fast. When s is not found, it will return OffsetEncodeError.

type CachedSeekOffsetMapper added in v0.2.0

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

CachedSeekOffsetMapper is the implementation of OffsetMapper. It seeks io.ReaderAt when methods are called, but caches the results. It's takes shorter time rather than SeekOffsetMapper.

func NewCachedSeekOffsetMapper added in v0.2.0

func NewCachedSeekOffsetMapper(r io.ReaderAt, size int64) *CachedSeekOffsetMapper

NewCachedSeekOffsetMapper returns a CachedSeekOffsetMapper. The size is total bytes of r. It seeks io.ReaderAt when methods are called, but caches the results. It's takes shorter time rather than SeekOffsetMapper.

func (*CachedSeekOffsetMapper) OffsetDecode added in v0.2.0

func (om *CachedSeekOffsetMapper) OffsetDecode(offset int64) (s string, err error)

OffsetDecode is the implementation of OffsetDecoder.

func (*CachedSeekOffsetMapper) OffsetEncode added in v0.2.0

func (om *CachedSeekOffsetMapper) OffsetEncode(s string) (offset int64, err error)

OffsetEncode is the implementation of OffsetEncoder. This method makes cache when it is called.

type Decoder

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

Decoder decodes bytes to an int64 or a string.

func NewDecoder

func NewDecoder(byteLen int) (*Decoder, error)

NewDecoder returns Decoder. The byteLen is the length of bytes. The byteLen must be 1 <= bytelen <= 8.

func (*Decoder) Decode

func (d *Decoder) Decode(r io.Reader) (offset int64, err error)

Decode reads r and decodes to the offset.

func (*Decoder) DecodeString

func (d *Decoder) DecodeString(r io.Reader, od OffsetDecoder) (s string, err error)

DecodeString reads r and decodes to s.

type Encoder

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

Encoder encodes an int64 to bytes.

func NewEncoder

func NewEncoder(byteLen int) (*Encoder, error)

NewEncoder returns an Encoder. The byteLen is the length of encoded bytes. The byteLen must be 1 <= byteLen <= 8.

func (*Encoder) Encode

func (e *Encoder) Encode(w io.Writer, offset int64) error

Encode encodes offset to bytes and writes it to w.

func (*Encoder) EncodeString

func (e *Encoder) EncodeString(w io.Writer, oe OffsetEncoder, s string) error

EncodeString encodes s to bytes and writes it to w.

type OffsetDecodeError added in v0.2.0

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

OffsetDecodeError is returned when OffsetDecode fails.

func (*OffsetDecodeError) Error added in v0.2.0

func (e *OffsetDecodeError) Error() string

type OffsetDecoder added in v0.2.0

type OffsetDecoder interface {
	OffsetDecode(offset int64) (s string, err error)
}

OffsetDecoder is the interface which can map an int64 offset to a string. The offset is the offset where the string appears in a text file.

type OffsetEncodeError added in v0.2.0

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

OffsetEncodeError is returned when OffsetEncode fails.

func (*OffsetEncodeError) Error added in v0.2.0

func (e *OffsetEncodeError) Error() string

type OffsetEncoder added in v0.2.0

type OffsetEncoder interface {
	OffsetEncode(s string) (offset int64, err error)
}

OffsetEncoder is the interface which can map a string to an int64 offset. The offset is the offset where the string appears in a text file.

type OffsetMapper added in v0.2.0

type OffsetMapper interface {
	OffsetEncoder
	OffsetDecoder
}

OffsetMapper implements both OffsetEncoder and OffsetDecoder.

type SeekOffsetMapper added in v0.2.0

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

SeekOffsetMapper is the implementation of OffsetMapper. It seeks file each time when OffsetEncode or OffsetDecode are called, so it works slowly.

func NewSeekOffsetMapper added in v0.2.0

func NewSeekOffsetMapper(r io.ReaderAt, size int64) *SeekOffsetMapper

NewSeekOffsetMapper returns an NewSeekOffsetMapper. The size is the total bytes of r. It seeks file each time when OffsetEncode or OffsetDecode are called, so it works slowly.

func (*SeekOffsetMapper) OffsetDecode added in v0.2.0

func (om *SeekOffsetMapper) OffsetDecode(offset int64) (s string, err error)

OffsetDecode is the implementation of OffsetDecoder. This function works slow because it needs io.ReadAt seeking each time.

func (*SeekOffsetMapper) OffsetEncode added in v0.2.0

func (om *SeekOffsetMapper) OffsetEncode(s string) (offset int64, err error)

OffsetEncode is the implementation of OffsetEncoder. This function works slow because it needs io.ReadAt seeking each time.

Jump to

Keyboard shortcuts

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