Help us shape the future of Go! Take the 2026 Go Developer Survey

scribble

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2020 License: Apache-2.0 Imports: 9 Imported by: 0

README

Scribble

GoDoc Go Report Card

Flat-file database in Golang using choosable encoders.

Currently supports:

  • JSON
  • Binary
Installation

Install using go get github.com/nanohard/scribble.

Usage

Unless you have a need to use JSON, it is recommended to use the default Binary for reduced file size.

Initialize Database
import(
    "github.com/nanohard/scribble"

    // Needed for Unmarshaling when using db.ReadAll()
    "github.com/nanohard/scribble/codec/json"
    // Or
    "github.com/nanohard/scribble/codec/binary"
)

// A new scribble driver, providing the directory where it will be writing to,
// and a qualified logger if desired.
// Defaults to using encoding/binary (well, really kelindar/binary)
db, err := scribble.New(dir, nil) // `binary.Codec` is used by default
if err != nil {
    fmt.Println("Error", err)
}

// Or

options := scribble.Options{
    Codec: json.Codec,  // `binary.Codec` is used by default
}
db, err := scribble.New(dir, options)
if err != nil {
    fmt.Println("Error", err)
}
DB Functions
// Write a fish to the database
fish := Fish{}
if err := db.Write("fish", "onefish", fish); err != nil {
    fmt.Println("Error", err)
}

// Read a fish from the database (passing fish by reference)
onefish := Fish{}
if err := db.Read("fish", "onefish", &onefish); err != nil {
    fmt.Println("Error", err)
}

// Read all fish from the database, unmarshaling the response.
records, err := db.ReadAll("fish")
if err != nil {
    fmt.Println("Error", err)
}

fishies := []Fish{}
for _, f := range records {
    fishFound := Fish{}
    // binary.Codec.Unmarshal or json.Codec.Unmarshal
    if err := json.Codec.Unmarshal([]byte(f), &fishFound); err != nil {
        fmt.Println("Error", err)
    }
    fishies = append(fishies, fishFound)
}

// Delete a fish from the database
if err := db.Delete("fish", "onefish"); err != nil {
    fmt.Println("Error", err)
}

// Delete all fish from the database
if err := db.Delete("fish", ""); err != nil {
    fmt.Println("Error", err)
}

Documentation

  • Complete documentation is available on godoc.

Todo/Doing

  • More encoders?

Contributing

  • Rebase into one commit.
  • Have liberal code comments.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Driver

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

Driver is what is used to interact with the scribble database. It runs transactions, and provides log output

func New

func New(dir string, options *Options) (*Driver, error)

New creates a new scribble database at the desired directory location, and returns a *Driver to then use for interacting with the database

func (*Driver) Delete

func (d *Driver) Delete(collection, resource string) error

Delete locks that database and then attempts to remove the collection/resource specified by path

func (*Driver) List added in v1.1.0

func (d *Driver) List(collection string) ([]string, error)

List all records from a collection; this is returned as a slice of strings because there is no way of knowing what type the record is.

func (*Driver) Read

func (d *Driver) Read(collection, resource string, v interface{}) error

Read a record from the database

func (*Driver) ReadAll

func (d *Driver) ReadAll(collection string) ([]string, error)

ReadAll records from a collection; this is returned as a slice of strings because there is no way of knowing what type the record is.

func (*Driver) Write

func (d *Driver) Write(collection, resource string, v interface{}) error

Write locks the database and attempts to write the record to the database under the [collection] specified with the [resource] name given

type Logger

type Logger interface {
	Fatal(string, ...interface{})
	Error(string, ...interface{})
	Warn(string, ...interface{})
	Info(string, ...interface{})
	Debug(string, ...interface{})
	Trace(string, ...interface{})
}

Logger is a generic logger interface

type Options

type Options struct {
	Codec  codec.MarshalUnmarshaler
	Logger // the logger scribble will use (configurable)
}

Options uses for specification of working golang-scribble

Directories

Path Synopsis
Package codec contains sub-packages with different codecs that can be used to encode and decode entities in Scribble.
Package codec contains sub-packages with different codecs that can be used to encode and decode entities in Scribble.
binary
Package binary contains a codec to encode and decode entities in binary format
Package binary contains a codec to encode and decode entities in binary format
json
Package json contains a codec to encode and decode entities in JSON format
Package json contains a codec to encode and decode entities in JSON format

Jump to

Keyboard shortcuts

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