database

package
v0.1.39 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2022 License: GPL-3.0 Imports: 3 Imported by: 2

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Database

type Database uint32
Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/database"
	"bitbucket.org/taubyte/go-sdk/database"
	"bitbucket.org/taubyte/go-sdk/event"
)

var (
	testId   = uint32(5)
	testName = "someDatabase"
	testData = map[string][]byte{}
)

func databaseFunction(e event.Event) uint32 {
	db, err := database.New(testName)
	if err != nil {
		return 1
	}

	err = db.Put("value/hello", []byte("Hello, world"))
	if err != nil {
		return 1
	}

	err = db.Put("value/hello2", []byte("Hello, world"))
	if err != nil {
		return 1
	}

	keys, err := db.List("value")
	if len(keys) != 2 || err != nil {
		return 1
	}

	data, err := db.Get("value/hello")
	if err != nil {
		return 1
	}

	if string(data) != "Hello, world" {
		return 1
	}

	err = db.Delete("value/hello")
	if err != nil {
		return 1
	}

	data, err = db.Get("value/hello")
	if err == nil {
		return 1
	}

	err = db.Close()
	if err != nil {
		return 1
	}

	return 0
}

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	symbols.Mock(testId, testName, testData)

	e := databaseFunction(event.Event(1))
	if e != 0 {
		fmt.Println(e)
		return
	}

	fmt.Println("Success")
}
Output:

Success

func New

func New(name string) (Database, error)

New creates a new database Returns a database and error

Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/database"
	"bitbucket.org/taubyte/go-sdk/database"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	testName := "someDatabase"
	symbols.MockNew(16, testName)

	db, err := database.New(testName)
	if err != nil {
		return
	}

	fmt.Println(db)
}
Output:

16

func (Database) Close

func (d Database) Close() error

Close closes the database Returns an error

Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/database"
	"bitbucket.org/taubyte/go-sdk/database"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	testId := uint32(3)
	symbols.MockClose(testId)

	err := database.Database(testId).Close()

	fmt.Println(err)
}
Output:

<nil>

func (Database) Delete

func (d Database) Delete(key string) error

Delete removes an entry from the database Retuns an error

Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/database"
	"bitbucket.org/taubyte/go-sdk/database"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	testId := uint32(5)
	testKey := "someKey"
	symbols.MockDelete(testId, testKey, map[string][]byte{})

	err := database.Database(testId).Delete(testKey)

	fmt.Println(err)
}
Output:

<nil>

func (Database) Get

func (d Database) Get(key string) ([]byte, error)

Get retrieves the given key from the database. Returns the data of the key and an error.

Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/database"
	"bitbucket.org/taubyte/go-sdk/database"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	testId := uint32(5)
	testKey := "someKey"
	testData := map[string][]byte{
		testKey: []byte("Hello, world!"),
	}
	symbols.MockGet(testId, testData)

	data, err := database.Database(testId).Get(testKey)
	if err != nil {
		return
	}

	fmt.Println(string(data))
}
Output:

Hello, world!

func (Database) List

func (d Database) List(prefix string) ([]string, error)

List uses the prefix given to get a list of keys that use the prefix in the database Returns all keys found and an error

Example
package main

import (
	"fmt"
	"reflect"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/database"
	"bitbucket.org/taubyte/go-sdk/database"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	testId := uint32(5)
	testKey := "someKey"
	testData := map[string][]byte{
		testKey + "/a":    {},
		testKey + "/bb":   {},
		testKey + "/cccd": {},
	}
	symbols.MockList(testId, testKey, testData)

	data, err := database.Database(testId).List(testKey)
	if err != nil {
		return
	}

	expected := []string{"someKey/a", "someKey/bb", "someKey/cccd"}

	if reflect.DeepEqual(data, expected) == false {
		return
	}

	fmt.Println("Success")
}
Output:

Success

func (Database) Put

func (d Database) Put(key string, data []byte) error

Put writes the key and data into the database. Returns an error

Example
package main

import (
	"fmt"

	symbols "bitbucket.org/taubyte/go-sdk-symbols/database"
	"bitbucket.org/taubyte/go-sdk/database"
)

func main() {
	// Mocking the calls to the vm for usage in tests and playground
	testId := uint32(18)
	testPut := map[string][]byte{}
	symbols.MockPut(testId, testPut)

	testKey := "someKey"
	err := database.Database(testId).Put(testKey, []byte("Hello, world"))
	if err != nil {
		return
	}

	fmt.Println(string(testPut[testKey]))
}
Output:

Hello, world

type DatabaseData

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

Jump to

Keyboard shortcuts

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