tImplement skip function when retrieving keys - cdb - Constant database manipulation utility
(HTM) git clone git://git.z3bra.org/cdb.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit e0826edd95958d320872f63bd0163b3b9cc8b66c
(DIR) parent 89ac8e8768591d2aba27e74acaab2cec4ca685b3
(HTM) Author: Willy Goiffon <dev@z3bra.org>
Date: Thu, 1 Sep 2022 01:56:51 +0200
Implement skip function when retrieving keys
Diffstat:
M cdb.go | 23 ++++++++++++++++++-----
1 file changed, 18 insertions(+), 5 deletions(-)
---
(DIR) diff --git a/cdb.go b/cdb.go
t@@ -4,6 +4,7 @@ import (
"bufio"
"flag"
"fmt"
+ "io"
"io/ioutil"
"log"
"os"
t@@ -41,17 +42,29 @@ func makedb(db string, tmp *os.File) {
exitOnErr(os.Rename(tmpname, db))
}
-func get(db string, key string, skip int64) {
+func get(db string, key string, skip int) {
+ var r *io.SectionReader
+
c, err := cdb.Open(db)
if err != nil {
log.Fatal(err)
}
defer c.Close()
- data, err:= c.Data([]byte(key))
+
+ for n := 0; n <= skip; n++ {
+ r, err = c.FindNext([]byte(key))
+ if err == io.EOF {
+ os.Exit(100)
+ }
+ if err != nil {
+ log.Fatal(err)
+ }
+ }
+ _, err = io.Copy(os.Stdout, r)
if err != nil {
log.Fatal(err)
}
- fmt.Printf("%s\n", string(data))
+ fmt.Printf("\n")
}
func main() {
t@@ -60,12 +73,12 @@ func main() {
var key string
var db string
- var skip int64
+ var skip int
var dump bool
var make bool
flag.StringVar(&key, "k", "", "Print record with key k")
- flag.Int64Var(&skip, "s", 0, "Skip the first s matching records")
+ flag.IntVar(&skip, "s", 0, "Skip the first s matching records")
flag.BoolVar(&dump, "d", false, "Print records suitable for cbdmake")
flag.BoolVar(&make, "m", false, "Writes a constant database to f")
flag.Parse()