bolt

package
v1.17.1 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MPL-2.0 Imports: 6 Imported by: 3 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

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

Example
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"go.etcd.io/bbolt"

	"github.com/cosi-project/runtime/api/v1alpha1"
	"github.com/cosi-project/runtime/pkg/resource"
	"github.com/cosi-project/runtime/pkg/resource/meta"
	"github.com/cosi-project/runtime/pkg/resource/protobuf"
	"github.com/cosi-project/runtime/pkg/resource/typed"
	"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"
	"github.com/cosi-project/runtime/pkg/state/impl/store/encryption"
)

type ExampleSpec = protobuf.ResourceSpec[v1alpha1.Metadata, *v1alpha1.Metadata]

type ExampleExtension struct{}

func (ExampleExtension) ResourceDefinition() meta.ResourceDefinitionSpec {
	return meta.ResourceDefinitionSpec{
		Type: "testResource",
	}
}

func main() {
	// use encryption marshaler on top of protobuf marshaler, resources should implement protobuf marshaling
	marshaler := encryption.NewMarshaler(
		store.ProtobufMarshaler{},
		encryption.NewCipher(
			encryption.KeyProviderFunc(func() ([]byte, error) {
				return []byte("this key len is exactly 32 bytes"), nil
			}),
		),
	)

	tempDir, err := os.MkdirTemp("", "bolt-example")
	if err != nil {
		fmt.Println(err)

		return
	}

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

	defer func() {
		if storeErr := backingStore.Close(); storeErr != nil {
			fmt.Println(storeErr)

			return
		}
	}()

	// an inmem.State is either fully backed by the store, or fully ephemeral, so build a separate
	// persistent state, and route the namespaces to it with namespaced.NewState
	persistentState := inmem.NewStateWithOptions(
		inmem.WithBackingStore(backingStore),
	)

	// a single ephemeral state instance is shared by all the other namespaces, so that they share
	// its event history buffer instead of reserving one per namespace
	ephemeralState := inmem.NewState()

	// create resource state with following namespaces
	// * backed by BoltDB: persistent, system
	// * in-memory: any other namespace, e.g. runtime
	resources := state.WrapCore(namespaced.NewState(func(ns resource.Namespace) state.CoreState {
		switch ns {
		case "persistent", "system":
			return persistentState
		default:
			return ephemeralState
		}
	}))

	r1 := typed.NewResource[ExampleSpec, ExampleExtension](
		resource.NewMetadata(
			"persistent",
			"testResource",
			"example-resource-id",
			resource.VersionUndefined,
		),
		protobuf.NewResourceSpec(&v1alpha1.Metadata{Version: "v0.1.0"}),
	)

	if err = resources.Create(context.Background(), r1); err != nil {
		fmt.Println("error getting resource:", err)

		return
	}

	result, err := resources.Get(
		context.Background(),
		resource.NewMetadata(
			"persistent",
			"testResource",
			"example-resource-id",
			resource.VersionUndefined,
		),
	)
	if err != nil {
		fmt.Println("error getting resource:", err)

		return
	}

	fmt.Println(result.Metadata().ID())

	_, err = resources.Get(
		context.Background(),
		resource.NewMetadata(
			"persistent",
			"testResource",
			"non-existent-resource",
			resource.VersionUndefined,
		),
	)
	if err != nil {
		fmt.Println("error getting resource:", err)

		return
	}
}
Output:
example-resource-id
error getting resource: resource testResource(persistent/non-existent-resource@undefined) doesn't exist

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.

A BackingStore covers the whole database, so it should back a single inmem.State: Load walks every namespace bucket, and pointing two States at the same database would load the full contents into both of them. Open a database per State to split the persistent namespaces.

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) Destroy added in v1.17.0

func (store *BackingStore) Destroy(_ context.Context, ns resource.Namespace, resourceType resource.Type, ptr resource.Pointer) error

Destroy implements inmem.BackingStore.

func (*BackingStore) Load added in v1.17.0

func (store *BackingStore) Load(_ context.Context, handler inmem.LoadHandler) error

Load implements inmem.BackingStore.

Load calls the handler for every resource of every namespace in the database.

func (*BackingStore) Put added in v1.17.0

func (store *BackingStore) Put(_ context.Context, ns resource.Namespace, 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