Documentation
¶
Overview ¶
Package nwenc is the implementation of encoder and decoder for the nextword data.
The PosEncoder and PosDecoder can encode/decode between an int64 position value and a string. There are several implementation for PosMapper. They have different performance.
Encoder and Decoder can encode/decode bwtween an int64 position value and bytes.
PosEncoder/PosDecoder example:
f, err := os.Open(filepath.Join("testdata", "words.txt"))
pm, err := NewAllReadPosMapper(f)
pm.PosEncode("a") // 0 ("a" is at 0 byte in testdata/words.txt)
pm.PosEncode("bcd") // 43 ("bcd" is at 43 bytes in testdata/words.txt)
pm.PosDecode(0) // "a"
pm.PosDecode(43) // "bcd"
Encoder/Decoder example:
f, err := os.Open(filepath.Join("testdata", "words.txt"))
pm, err := NewAllReadPosMapper(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 pos == 0
enc.EncodeString(buf, pm, "bcd") // writes "bcd"
dec, err := NewDecoder(byteLen)
dec.Decode(buf) // 0 ("a")
dec.DecodeString(buf, pm) // "bcd"
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AllReadPosMapper ¶
type AllReadPosMapper struct {
// contains filtered or unexported fields
}
AllReadPosMapper is one of the implementation of PosMapper. It reads all of io.Reader in advance in order to map fast.
func NewAllReadPosMapper ¶
func NewAllReadPosMapper(r io.Reader) (*AllReadPosMapper, error)
NewAllReadPosMapper returns an AllReadPosMapper. This function reads all of io.Reader in advance in order to map fast.
type CachedSeekPosMapper ¶
type CachedSeekPosMapper struct {
// contains filtered or unexported fields
}
CachedSeekPosMapper is the implementation of PosMapper. It seeks io.ReaderAt when methods are called, but caches the results. It's takes shorter time rather than SeekPosMapper.
func NewCachedSeekPosMapper ¶
func NewCachedSeekPosMapper(r io.ReaderAt, size int64) *CachedSeekPosMapper
NewCachedSeekPosMapper returns a CachedSeekPosMapper. 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 SeekPosMapper.
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder decodes bytes to an int64 or a string.
func NewDecoder ¶
NewDecoder returns Decoder. The byteLen is the length of bytes. The byteLen must be 1 <= bytelen <= 8.
func (*Decoder) DecodeString ¶
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 ¶
NewEncoder returns an Encoder. The byteLen is the length of encoded bytes. The byteLen must be 1 <= byteLen <= 8.
func (*Encoder) EncodeString ¶
EncodeString encodes s to bytes and writes it to w.
type PosDecodeError ¶
type PosDecodeError struct {
// contains filtered or unexported fields
}
PosDecodeError is returned when PosDecode fails.
func (*PosDecodeError) Error ¶
func (e *PosDecodeError) Error() string
type PosDecoder ¶
PosDecoder is the interface which can map an int64 position to a string. The position is the offset where the string appears in a text file.
type PosEncodeError ¶
type PosEncodeError struct {
// contains filtered or unexported fields
}
PosEncodeError is returned when PosEncode fails.
func (*PosEncodeError) Error ¶
func (e *PosEncodeError) Error() string
type PosEncoder ¶
PosEncoder is the interface which can map a string to an int64 position. The position is the offset where the string appears in a text file.
type PosMapper ¶
type PosMapper interface {
PosEncoder
PosDecoder
}
PosMapper implements both PosEncoder and PosDecoder.
type SeekPosMapper ¶
type SeekPosMapper struct {
// contains filtered or unexported fields
}
SeekPosMapper is the implementation of PosMapper. It seeks file each time when PosEncode or PosDecode are called, so it works slowly.
func NewSeekPosMapper ¶
func NewSeekPosMapper(r io.ReaderAt, size int64) *SeekPosMapper
NewSeekPosMapper returns an NewSeekPosMapper. The size is the total bytes of r. It seeks file each time when PosEncode or PosDecode are called, so it works slowly.