bolt

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2022 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package bolt implements inmem resource collection backing store in BoltDB (github.com/etcd-io/bbolt).

Example
package main

import (
	"log"

	"go.etcd.io/bbolt"

	"github.com/cosi-project/runtime/pkg/resource"
	"github.com/cosi-project/runtime/pkg/state"
	"github.com/cosi-project/runtime/pkg/state/impl/inmem"
	"github.com/cosi-project/runtime/pkg/state/impl/namespaced"
	"github.com/cosi-project/runtime/pkg/state/impl/store"
	"github.com/cosi-project/runtime/pkg/state/impl/store/bolt"
)

func main() {
	// use protobuf marshaler, resources should implement protobuf marshaling
	marshaler := store.ProtobufMarshaler{}

	// open the backing store, it will be shared across namespaces
	store, err := bolt.NewBackingStore(
		func() (*bbolt.DB, error) {
			return bbolt.Open("cosi.db", 0o600, nil)
		},
		marshaler,
	)
	if err != nil {
		log.Fatalf("error opening bolt db: %v", err)
	}

	defer store.Close() //nolint:errcheck

	// create resource state with following namespaces
	// * backed by BoltDB: persistent, system
	// * in-memory: runtime
	resources := state.WrapCore(namespaced.NewState(
		func(ns resource.Namespace) state.CoreState {
			switch ns {
			case "persistent", "system":
				// use in-memory state backed by BoltDB
				return inmem.NewStateWithOptions(
					inmem.WithBackingStore(store.WithNamespace(ns)),
				)(ns)
			case "runtime":
				return inmem.NewState(ns)
			default:
				panic("unexpected namespace")
			}
		},
	))

	_ = resources
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BackingStore

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

BackingStore implements inmem.BackingStore using BoltDB.

Layout of the database:

  -> top-level bucket: $namespace
	 	-> bucket: $resourceType
			-> key: $resourceID
			-> value: marshaled resource

func NewBackingStore

func NewBackingStore(opener func() (*bbolt.DB, error), marshaler store.Marshaler) (*BackingStore, error)

NewBackingStore opens the BoltDB store with the given marshaler.

func (*BackingStore) Close

func (store *BackingStore) Close() error

Close the database.

func (*BackingStore) WithNamespace

func (store *BackingStore) WithNamespace(namespace resource.Namespace) *NamespacedBackingStore

WithNamespace returns an implementation of inmem.BackingStore interface for a given namespace.

type NamespacedBackingStore

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

NamespacedBackingStore implements inmem.BackingStore for a given namespace.

func (*NamespacedBackingStore) Destroy

func (store *NamespacedBackingStore) Destroy(ctx context.Context, resourceType resource.Type, ptr resource.Pointer) error

Destroy implements inmem.BackingStore.

func (*NamespacedBackingStore) Load

func (store *NamespacedBackingStore) Load(ctx context.Context, handler inmem.LoadHandler) error

Load implements inmem.BackingStore.

func (*NamespacedBackingStore) Put

func (store *NamespacedBackingStore) Put(ctx context.Context, resourceType resource.Type, res resource.Resource) error

Put implements inmem.BackingStore.

Jump to

Keyboard shortcuts

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