Wrench

package module
v0.0.0-...-a4cce97 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2018 License: MIT Imports: 7 Imported by: 0

README

Wrench

Library for more easily using BBolt Based almost entirely on internal methods from my BoltInspector tool

About

Wrench is a tool I am working on to simplify implementing Bolt databases. The idea is to make reading the database as easy as simply using Insert and Delete commands (among others). Obviously, majorly simplifying the library also limits it in some ways, however I feel the tradeoffs are worthwhile for developers (like myself) who aren't stressing over perfect implementation and simply want a quick and easy way to store information.

Usage

Adding Wrench to Your Project

You can grab this library with Go Get

$ go get https://github.com/89yoyos/Wrench/...

Then import the library like any other

import ("https://github.com/89yoyos/Wrench")
Setup

To use Wrench, you need to declare a Wrench object.

w := Wrench.Wrench{}

Next, just use your Wrench to open the Bolt database you want to use.

err := w.OpenDB("File/Path.db")

The OpenDB command returns a normal Error object if it fails to open the file, so you can implement whatever error handling you're most comfortable with.

Adding a Bucket

An empty Bolt database cannot hold key value pairs, they need to be inside of a bucket. To create a bucket with Wrench, use the CreateBucket command.

w.CreateBucket("key")

If you've used Bolt before, you may have noticed that Wrench treats all keys as strings. This is intentional, though not in line with pure Bolt, which allows any array of bytes to be used as a key. I found this limitation immensely simplifies working with the database, though it may cause issues with databases with non-string keys.

Navigating Your Database

In Bolt, accessing buckets could be tedious, and accessing sub-buckets would only make it worse. Wrench attempts to alleviate this by treating the database like a filesystem. Your Wrench object knows its current location within the database, and anything you do is relative to position. To see where your Wrench is currently positioned, you can use the CurrentBucket and CurrentBucketString functions.

CurrentBucket returns an array of strings, starting at the root of the directory and working through all the buckets and sub-buckets to get to your current position.

CurrentBucketString works in a similar way, however it returns a single string representing that path.

To change where your Wrench is positioned, you can use the GoTo function.

w.GoTo(["~","Path","as","an","Array"])

Or, if you don't want to construct an array, you can use the built-in method to do that

w.GoToString("~/Path/as/a/String")

This function uses the StringToPath function to construct an array of string, and passes that to the GoTo function. The benefit being that the StringToPath function does some nifty things, like allowing relative paths to be used.

Additionally, note the ~ in both examples. A ~ denotes the root of the database for absolute paths. If you use the GoToString method or the StringToPath method, it will create an array that includes the ~, adding it to the path even if you forget to include it.

Inserting Values

Bolt does not support inserting values into the root of the database. So to insert a value, you must first create a bucket, then navigate your wrench into that bucket. With Wrench, this is very simple to do.

w := Wrench.Wrench{}
err := w.OpenDB("File/Path.db")
// Error Handling Here
w.CreateBucket("Example")
w.GoToString("~/Example")

In 4 lines of code, you created a Wrench, opened the database, created a bucket and are now ready to write to that bucket.

Inserting a value is just as easy.

w.Insert("key",[value as bytes])

Like buckets, values' keys are limited to strings when using Wrench. Their values are arrays of bytes. To convert a value to bytes, Go has built in methods.

w.Insert("key",[]byte("value"))

This method also works with non-string values.

Reading Values

Because of how Bolt works, the default method to read values from a bucket is to get all of them at once and sort through them. This can be done with the GetAll function.

w.GetAll()

This function, along with all others that read from the database, return a DBVal object. DBVals are just things in the database. They can be either Values or Buckets. They include the path to the value, the key for that value, and the value itself. If a DBVal represents a bucket in the database, the value will be nil.

DBVals can be used to manipulate entries in the database, however that functionality is still under development.

If you don't want all of the values, the GetOne function allows you to get just the one value you're looking for.

w.Get("key")

There are also several other Get functions.

w.GetValues() // Returns just the non-bucket values in the current bucket
w.GetBuckets() // Returns just the buckets in the current bucket
w.GetBoth() // Returns 2 arrays, one of values and one of buckets

There are also function to count the values in the current bucket.

w.Count() // Counts everything in the bucket
w.CountValues() // Counts the non-bucket values
w.CountBuckets() // Counts the buckets
w.CountBoth() // Returns the counts of the buckets and of the non-buckets as separate numbers
Deleting Values

There are 2 commands that can be used to delete database entries.

w.Delete("key")

The Delete command can delete both values and buckets, and it works exactly how you would expect.

w.Empty()

The Empty command is a bit less intuitive, however it can be very useful in some circumstances. This command deletes all values and buckets in the Wrench's current location. For example, let's say your database looks like this:

~ [Root]
- TestBucket
- - TestVal1
- - TestVal2

If you point your Wrench at ~/TestBucket and run w.Empty(), it will delete ~/TestBucket/TestVal1 and ~/TestBucket/TestVal2, but ~/TestBucket will remain in place, now empty of any values.

Conclusion

Those are the basics of using Wrench. It's simple, but powerful. Additional functionality will likely be added in the future as I use this library in my own projects.

Among planned updates is the expansion of the DBVal class. I'd like to make it a more useful tool for manipulating data rather than simply reading it. The DBVal.AsWrench() function is a working, but unsatisfying version of this for now, which returns a new Wrench object that can then be used to manipulate data. Future versions will likely add functions to DBVal to update and delete the referenced entries.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DBVal

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

func (DBVal) AsWrench

func (d DBVal) AsWrench() Wrench

func (DBVal) BucketString

func (d DBVal) BucketString() string

This returns the path to the bucket this value is in

func (DBVal) IsBucket

func (d DBVal) IsBucket() bool

func (DBVal) K

func (d DBVal) K() []byte

func (DBVal) Key

func (d DBVal) Key() string

Get the key as a string

func (DBVal) Path

func (d DBVal) Path() []string

func (DBVal) ToString

func (d DBVal) ToString(verbose bool) string

func (DBVal) V

func (d DBVal) V() []byte

func (DBVal) Val

func (d DBVal) Val() string

Get the value as a string

type Wrench

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

func (Wrench) Count

func (w Wrench) Count() int

return the total number of entries in the bucket

func (Wrench) CountBoth

func (w Wrench) CountBoth() (int, int)

func (Wrench) CountBuckets

func (w Wrench) CountBuckets() int

return the number of buckets nested inside this bucket

func (Wrench) CountValues

func (w Wrench) CountValues() int

return the number of non-bucket values stored in this bucket

func (Wrench) CreateBucket

func (w Wrench) CreateBucket(name string) error

func (Wrench) CurrentBucket

func (w Wrench) CurrentBucket() []string

func (Wrench) CurrentBucketString

func (w Wrench) CurrentBucketString() string

func (Wrench) Delete

func (w Wrench) Delete(key string) bool

delete the value or bucket at the specified key

func (Wrench) Empty

func (w Wrench) Empty() bool

delete all values and buckets located within this bucket

func (Wrench) Exists

func (w Wrench) Exists() bool

This function tests that the current bucket exists and is a bucket (not a value)

func (Wrench) File

func (w Wrench) File() string

func (Wrench) FileName

func (w Wrench) FileName() string

func (Wrench) Get

func (w Wrench) Get(key string) (DBVal, bool)

This returns an individual key from the bucket, as well as whether the requested key exists

func (Wrench) GetAll

func (w Wrench) GetAll() []DBVal

This returns an array of all key/value pairs in the current bucket

func (Wrench) GetBoth

func (w Wrench) GetBoth() ([]DBVal, []DBVal)

returns 2 separate arrays, one for buckets and one for values. More efficient if getting both

func (Wrench) GetBuckets

func (w Wrench) GetBuckets() []DBVal

return just the buckets, not the values

func (Wrench) GetValues

func (w Wrench) GetValues() []DBVal

return just the values, not the buckets

func (*Wrench) GoTo

func (w *Wrench) GoTo(path []string) bool

func (*Wrench) GoToString

func (w *Wrench) GoToString(path string) bool

func (Wrench) Insert

func (w Wrench) Insert(key string, val []byte) bool

func (Wrench) IsRoot

func (w Wrench) IsRoot() bool

func (*Wrench) OpenDB

func (w *Wrench) OpenDB(file string) error

func (Wrench) OpenDBStrict

func (w Wrench) OpenDBStrict(file string) error

func (Wrench) Path

func (w Wrench) Path() []string

func (Wrench) PathString

func (w Wrench) PathString() string

func (*Wrench) Reset

func (w *Wrench) Reset()

func (Wrench) StringToPath

func (w Wrench) StringToPath(target string) []string

Jump to

Keyboard shortcuts

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