sstable

package
v0.0.0-...-ec6159d Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2019 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cursor

type Cursor interface {
	Entry() *Entry
	Done() bool
	Next()
}

Cursor is an interface to iterate.

func NewRecordIOReader

func NewRecordIOReader(r io.Reader, size uint64) Cursor

NewRecordIOReader returns a cursor that reads RecordIO from r. It requires size.

type CursorToOffset

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

CursorToOffset is a Cursor that read until the endOffset.

func (*CursorToOffset) Done

func (c *CursorToOffset) Done() bool

Done returns true when there is no more entry to read.

func (*CursorToOffset) Entry

func (c *CursorToOffset) Entry() *Entry

Entry returns the current entry.

func (*CursorToOffset) Next

func (c *CursorToOffset) Next()

Next moves the cursor to the next entry.

type Entry

type Entry struct {
	// KeyLength   uint32
	// ValueLength uint32
	Key   []byte
	Value []byte
}

Entry struct is a key value pair.

func ReadEntry

func ReadEntry(r io.Reader) (*Entry, error)

ReadEntry reads an entry from r.

Example
f := bytes.NewReader([]byte{0, 0, 0, 3, 0, 0, 0, 4, 1, 2, 3, 5, 6, 7, 8})
e, _ := ReadEntry(f)
fmt.Println(e)
Output:

&{[1 2 3] [5 6 7 8]}

func ReadEntryAt

func ReadEntryAt(r io.ReaderAt, offset uint64) (*Entry, error)

ReadEntryAt reads an entry from the offset of r.

Example
f := bytes.NewReader([]byte{0, 0, 0, 3, 0, 0, 0, 4, 1, 2, 3, 5, 6, 7, 8})
e, _ := ReadEntryAt(f, 0)
fmt.Println(e)
Output:

&{[1 2 3] [5 6 7 8]}

func (*Entry) MarshalBinary

func (e *Entry) MarshalBinary() ([]byte, error)

MarshalBinary implements the encoding.BinaryMarshaler interface.

Example
e := Entry{
	Key:   []byte{1, 2, 3},
	Value: []byte{5, 6, 7, 8},
}

data, err := e.MarshalBinary()
if err != nil {
	fmt.Println(err)
}

fmt.Println(data)
Output:

[0 0 0 3 0 0 0 4 1 2 3 5 6 7 8]

func (*Entry) Size

func (e *Entry) Size() uint64

Size returns number of bytes in this entry.

func (*Entry) UnmarshalBinary

func (e *Entry) UnmarshalBinary(data []byte) error

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.

Example
var e Entry

err := e.UnmarshalBinary([]byte{0, 0, 0, 3, 0, 0, 0, 4, 1, 2, 3, 5, 6, 7, 8})
if err != nil {
	fmt.Println(err)
}

fmt.Println(e)
Output:

{[1 2 3] [5 6 7 8]}

func (*Entry) WriteTo

func (e *Entry) WriteTo(w io.Writer) (n int64, err error)

WriteTo implements the io.WriterTo interface.

type SSTable

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

SSTable implements read only random access of the SSTable.

Example
f, _ := ioutil.TempFile("", "")

name := f.Name()
defer os.Remove(name)

w := NewWriter(f)

entries := []Entry{
	{Key: []byte{1, 2, 3}, Value: []byte{5, 6, 7, 8}},
	{Key: []byte{2, 2, 3}, Value: []byte{8, 5, 6, 7, 8}},
}
for _, entry := range entries {
	if err := w.Write(entry); err != nil {
		fmt.Println(err)
	}
}

w.Close()

f2, _ := os.Open(name)
defer f2.Close()

s, _ := NewSSTable(f2)

c := s.ScanFrom([]byte{1, 2, 3})
if c == nil {
	fmt.Println(c)
	return
}

for !c.Done() {
	fmt.Println(c.Entry())
	c.Next()
}

fmt.Println("---")

c = s.ScanFrom([]byte{1, 2, 3, 0})
for !c.Done() {
	fmt.Println(c.Entry())
	c.Next()
}
Output:

&{[1 2 3] [5 6 7 8]}
&{[2 2 3] [8 5 6 7 8]}
---
&{[2 2 3] [8 5 6 7 8]}
Example (Reader)
f, _ := ioutil.TempFile("", "")

name := f.Name()
defer os.Remove(name)

w := NewWriter(f)

entries := []Entry{
	{Key: []byte{1, 2, 3}, Value: []byte{5, 6, 7, 8}},
	{Key: []byte{2, 2, 3}, Value: []byte{8, 5, 6, 7, 8}},
}
for _, entry := range entries {
	if err := w.Write(entry); err != nil {
		fmt.Println(err)
	}
}

w.Close()

b, _ := ioutil.ReadFile(name)
// bytes.Buffer does not support random access.
s, _ := NewSSTable(bytes.NewBuffer(b))

c := s.ScanFrom([]byte{1, 2, 3})
if c == nil {
	fmt.Println(c)
	return
}

for !c.Done() {
	fmt.Println(c.Entry())
	c.Next()
}
Output:

&{[1 2 3] [5 6 7 8]}
&{[2 2 3] [8 5 6 7 8]}

func NewSSTable

func NewSSTable(r interface{}) (*SSTable, error)

NewSSTable creates a SSTable struct

func (*SSTable) ScanFrom

func (s *SSTable) ScanFrom(key []byte) Cursor

ScanFrom scans from the key to the end of the SSTable. If key is nil, scan from the beginning.

type Writer

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

Writer is used to build a SSTable binary with Write function.

Example
f, _ := ioutil.TempFile("", "")

name := f.Name()
defer os.Remove(name)

w := NewWriter(f)

entries := []Entry{
	{Key: []byte{1, 2, 3}, Value: []byte{5, 6, 7, 8}},
	{Key: []byte{2, 2, 3}, Value: []byte{8, 5, 6, 7, 8}},
}
for _, entry := range entries {
	if err := w.Write(entry); err != nil {
		fmt.Println(err)
	}
}

w.Close()

b, _ := ioutil.ReadFile(name)
fmt.Print(hex.Dump(b))
Output:

00000000  00 00 00 02 00 00 00 01  00 00 00 00 00 00 00 2f  |.............../|
00000010  00 00 00 03 00 00 00 04  01 02 03 05 06 07 08 00  |................|
00000020  00 00 03 00 00 00 05 02  02 03 08 05 06 07 08 00  |................|
00000030  00 00 03 00 00 00 00 00  00 00 10 00 00 00 1f 01  |................|
00000040  02 03                                             |..|

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter creates a Writer. The given writer w should be either WriterAt or WriteSeeker for random access.

func (*Writer) Close

func (w *Writer) Close() error

Close closes the writer. It writes index at the end and overwrite header at front.

func (*Writer) Write

func (w *Writer) Write(e Entry) error

Write writes an entry. Multiple calls to the function appends entries to the SSTable. The call should be made in sorted order of the keys.

Jump to

Keyboard shortcuts

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