func ExampleSortEntries() { c := &SliceCursor{ sstable.Entry{[]byte{3}, []byte{}}, sstable.Entry{[]byte{2}, []byte{}}, sstable.Entry{[]byte{4}, []byte{}}, sstable.Entry{[]byte{1}, []byte{}}, } 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] []} }
func ExampleMerge() { f, _ := ioutil.TempFile("", "") name := f.Name() defer os.Remove(name) w := sstable.NewWriter(f) cs := []sstable.Cursor{&SliceCursor{ sstable.Entry{[]byte{2}, []byte{}}, sstable.Entry{[]byte{5}, []byte{}}, sstable.Entry{[]byte{10}, []byte{}}, sstable.Entry{[]byte{15}, []byte{}}, }, &SliceCursor{ sstable.Entry{[]byte{1}, []byte{}}, sstable.Entry{[]byte{4}, []byte{}}, sstable.Entry{[]byte{11}, []byte{}}, sstable.Entry{[]byte{12}, []byte{}}, }, &SliceCursor{ sstable.Entry{[]byte{6}, []byte{}}, sstable.Entry{[]byte{8}, []byte{}}, sstable.Entry{[]byte{9}, []byte{}}, sstable.Entry{[]byte{14}, []byte{}}, }} 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] []} }
// lss prints the list of keys of each path in the SSTable. func lss(tablePaths []string) { for _, tablePath := range tablePaths { f, err := os.Open(tablePath) if err != nil { log.Println("Error on opening path:", err) } defer f.Close() tbl, err := sstable.NewSSTable(f) if err != nil { log.Println("Error on path", tablePath, ":", err) return } for c := tbl.ScanFrom(nil); !c.Done(); c.Next() { fmt.Println(string(c.Entry().Key)) } } }
// cats prints the value of the key in the SSTable. func cats(tablePath string, key string) { f, err := os.Open(tablePath) if err != nil { log.Println("Error on opening path", tablePath, ":", err) return } defer f.Close() tbl, err := sstable.NewSSTable(f) if err != nil { log.Println(err) return } c := tbl.ScanFrom([]byte(key)) if c.Done() || bytes.Compare(c.Entry().Key, []byte(key)) != 0 { return } value := c.Entry().Value fmt.Println(string(value)) }
func main() { flag.Parse() if *path == "" { log.Fatal("Please specify --path flag.") } f, err := os.Open(*path) if err != nil { log.Fatal("Open file failed:", err) } tbl, err = sstable.NewSSTable(f) if err != nil { log.Fatal("SSTable creation failed:", err) } http.HandleFunc("/", indexHandler) http.HandleFunc("/lookup", lookupHandler) if err := http.ListenAndServe(*addr, nil); err != nil { log.Fatal("ListenAndServe failed: ", err) } }
func ExampleSort() { c := &SliceCursor{ sstable.Entry{[]byte{3}, []byte{}}, sstable.Entry{[]byte{2}, []byte{}}, sstable.Entry{[]byte{4}, []byte{}}, sstable.Entry{[]byte{1}, []byte{}}, } var files []*os.File defer func() { 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) } var cursors []sstable.Cursor 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] []} }