badgerutils

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2019 License: Apache-2.0 Imports: 11 Imported by: 0

README

Badger Utils GoDoc Go Report Card

Go package with utilities for interacting with Badger.

Table of Contents

Getting Started

IO Stream to Badger

To stream data Badger, use badgerutils.WriteStream.

Example

Creates a CLI tool that streams data from stdin.

// examples/writer_cli.go
package main

import (
	"errors"
	"flag"
	"fmt"
	"log"
	"os"
	"strings"

	"github.com/Surfline/badgerutils"
)

type sampleRecord struct {
	Key   string
	Value string
}

func csvToKeyValue(line string) (*badgerutils.KeyValue, error) {
	values := strings.Split(line, ":")
	if len(values) < 2 {
		return nil, fmt.Errorf("%v has less than 2 values", line)
	}

	return &badgerutils.KeyValue{
		Key:   []byte(kv[0]),
		Value: []byte(kv[1]),
	}, nil
}

func main() {
	dir := flag.String("dir", "", "Directory to save DB files")
	batchSize := flag.Int("batch-size", 1000, "Number of records to write per transaction")
	flag.Parse()

	if *dir == "" {
		log.Fatal(errors.New("dir flag is required"))
	}

	log.Printf("Directory: %v", *dir)
	log.Printf("Batch Size: %v", *batchSize)

	if err := badgerutils.WriteStream(os.Stdin, *dir, *batchSize, csvToKeyValue); err != nil {
		log.Fatal(err)
	}
}

The code above can be called with the following flags:

  • -dir - (required) The path to the directory to persist Badger files.
  • -batch-size - (default: 1000) The size of each transaction (or batch of writes). This can be tuned for optimal performance depending on the machine.

For example:

$ for i in {1..10}; do echo "field${i}1,field${i}2,field${i}3,field${i}4"; done | go run main.go -dir=temp -batch-size=1
Directory: temp
Batch Size: 3
...
Records: 3
Records: 6
Records: 9
Records: 10
Inserted 10 records in 474.69µs

Development

Dependency Management

dep is required for dependency management.

$ make install
Format Code

Run this before opening pull requests to ensure code is properly formatted.

$ make fmt
Unit Tests
$ make test

Documentation

Overview

Package badgerutils provides functions for interacting with Badger.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteStream added in v0.2.0

func WriteStream(reader io.Reader, dir string, batchSize int, lineToKeyValue func(string) (*KeyValue, error)) error

WriteStream translates io.Reader stream into key/value pairs that are written into the Badger. lineToKeyValue function parameter defines how stdin is translated to a value and how to define a key from that value.

Types

type KeyValue added in v0.3.0

type KeyValue struct {
	Key   []byte
	Value []byte
}

KeyValue struct defines a Key and a Value empty interface to be translated into a record.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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