sort

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: 3 Imported by: 0

Documentation

Overview

Package sort implements external merge sort.

Example (Heap)
h := &Entries{}
heap.Push(h, HeapEntry{sstable.Entry{
	Key:   []byte("key3"),
	Value: []byte("value"),
}, 1})
heap.Push(h, HeapEntry{sstable.Entry{
	Key:   []byte("key1"),
	Value: []byte("value"),
}, 2})
heap.Push(h, HeapEntry{sstable.Entry{
	Key:   []byte("key4"),
	Value: []byte("value"),
}, 3})
heap.Push(h, HeapEntry{sstable.Entry{
	Key:   []byte("key2"),
	Value: []byte("value2"),
}, 4})
heap.Push(h, HeapEntry{sstable.Entry{
	Key:   []byte("key2"),
	Value: []byte("value"),
}, 5})
fmt.Println(heap.Pop(h))
fmt.Println(heap.Pop(h))
fmt.Println(heap.Pop(h))
fmt.Println(heap.Pop(h))
fmt.Println(heap.Pop(h))
Output:

{{[107 101 121 49] [118 97 108 117 101]} 2}
{{[107 101 121 50] [118 97 108 117 101]} 5}
{{[107 101 121 50] [118 97 108 117 101 50]} 4}
{{[107 101 121 51] [118 97 108 117 101]} 1}
{{[107 101 121 52] [118 97 108 117 101]} 3}
Example (Sort)
c := &SliceCursor{
	sstable.Entry{Key: []byte{3}},
	sstable.Entry{Key: []byte{2}},
	sstable.Entry{Key: []byte{4}},
	sstable.Entry{Key: []byte{1}},
}

var files []*os.File
defer func() { //nolint:wsl
	for _, f := range files {
		f.Close()
	}
}()

for !c.Done() {
	f, _ := ioutil.TempFile("", "")

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

	w := sstable.NewWriter(f)
	defer w.Close()

	fmt.Println(SortEntries(c, 20, w))
	fmt.Println("Cursor is done:", c.Done())

	r, _ := os.Open(name)
	files = append(files, r)
}

cursors := make([]sstable.Cursor, 0, len(files))

for _, f := range files {
	s, _ := sstable.NewSSTable(f)

	c := s.ScanFrom(nil)
	if c == nil {
		fmt.Println(c)
		return
	}

	cursors = append(cursors, c)
}

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

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

w := sstable.NewWriter(f)
if err := Merge(cursors, w); err != nil {
	fmt.Println(err)
	return
}

w.Close()

f2, _ := os.Open(name)
s, _ := sstable.NewSSTable(f2)

results := s.ScanFrom(nil)
if results == nil {
	fmt.Println(results)
	return
}

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

3 <nil>
Cursor is done: false
1 <nil>
Cursor is done: true
&{[1] []}
&{[2] []}
&{[3] []}
&{[4] []}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Merge

func Merge(cursors []sstable.Cursor, w *sstable.Writer) error

Merge merges from multiple cursors and write SSTable to w.

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

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

w := sstable.NewWriter(f)

cs := []sstable.Cursor{&SliceCursor{
	sstable.Entry{Key: []byte{2}},
	sstable.Entry{Key: []byte{5}},
	sstable.Entry{Key: []byte{10}},
	sstable.Entry{Key: []byte{15}},
}, &SliceCursor{
	sstable.Entry{Key: []byte{1}},
	sstable.Entry{Key: []byte{4}},
	sstable.Entry{Key: []byte{11}},
	sstable.Entry{Key: []byte{12}},
}, &SliceCursor{
	sstable.Entry{Key: []byte{6}},
	sstable.Entry{Key: []byte{8}},
	sstable.Entry{Key: []byte{9}},
	sstable.Entry{Key: []byte{14}},
}}
if err := Merge(cs, w); err != nil {
	fmt.Println(err)
	return
}

w.Close()

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

s, err := sstable.NewSSTable(f2)
if err != nil {
	fmt.Println(err)
	return
}

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

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

&{[1] []}
&{[2] []}
&{[4] []}
&{[5] []}
&{[6] []}
&{[8] []}
&{[9] []}
&{[10] []}
&{[11] []}
&{[12] []}
&{[14] []}
&{[15] []}

func SortEntries

func SortEntries(c sstable.Cursor, maxSize uint64, w *sstable.Writer) (n int, err error)

SortEntries sorts entries from c in memory and write to w in slightly over the maxSize bytes. Returns number of entries written and error.

Example
c := &SliceCursor{
	sstable.Entry{Key: []byte{3}},
	sstable.Entry{Key: []byte{2}},
	sstable.Entry{Key: []byte{4}},
	sstable.Entry{Key: []byte{1}},
}
f, _ := ioutil.TempFile("", "")

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

w := sstable.NewWriter(f)
fmt.Println(SortEntries(c, 100, w))
fmt.Println("Cursor is done:", c.Done())

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

s, _ := sstable.NewSSTable(outf)

results := s.ScanFrom([]byte{})
if results == nil {
	fmt.Println(results)
	return
}

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

4 <nil>
Cursor is done: true
&{[1] []}
&{[2] []}
&{[3] []}
&{[4] []}

Types

type Entries

type Entries []HeapEntry

Entries implements the heap.Interface interface.

func (Entries) Len

func (es Entries) Len() int

Len implements the sort.Interface interface.

func (Entries) Less

func (es Entries) Less(i, j int) bool

Less implements the sort.Interface interface.

func (*Entries) Pop

func (es *Entries) Pop() interface{}

Pop implements the heap.Interface interface.

func (*Entries) Push

func (es *Entries) Push(x interface{})

Push implements the heap.Interface interface.

func (Entries) Swap

func (es Entries) Swap(i, j int)

Swap implements the sort.Interface interface.

type HeapEntry

type HeapEntry struct {
	sstable.Entry
	// contains filtered or unexported fields
}

HeapEntry is a struct with an entry and an additional data.

Jump to

Keyboard shortcuts

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