kvstore

package
v3.24.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2024 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package kvstore implements model.KeyValueStore.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNoSuchKey = errors.New("no such key")

ErrNoSuchKey indicates that there's no value for the given key.

Functions

This section is empty.

Types

type FS

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

FS is a file-system based KVStore.

func NewFS

func NewFS(basedir string) (kvs *FS, err error)

NewFS creates a new kvstore.FileSystem.

func (*FS) Get

func (kvs *FS) Get(key string) ([]byte, error)

Get returns the specified key's value. In case of error, the error type is such that errors.Is(err, ErrNoSuchKey).

func (*FS) Set

func (kvs *FS) Set(key string, value []byte) error

Set sets the value of a specific key.

type Memory

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

Memory is an in-memory key-value store.

The zero value is ready to use.

Example
package main

import (
	"errors"
	"fmt"
	"log"
	"reflect"

	"github.com/ooni/probe-cli/v3/internal/kvstore"
)

func main() {
	kvs := &kvstore.Memory{}
	if _, err := kvs.Get("akey"); !errors.Is(err, kvstore.ErrNoSuchKey) {
		log.Fatal("unexpected error", err)
	}
	val := []byte("value")
	if err := kvs.Set("akey", val); err != nil {
		log.Fatal("unexpected error", err)
	}
	out, err := kvs.Get("akey")
	if err != nil {
		log.Fatal("unexpected error", err)
	}
	fmt.Printf("%+v\n", reflect.DeepEqual(val, out))
}
Output:

true

func (*Memory) Get

func (kvs *Memory) Get(key string) ([]byte, error)

Get returns the specified key's value. In case of error, the error type is such that errors.Is(err, ErrNoSuchKey).

func (*Memory) Set

func (kvs *Memory) Set(key string, value []byte) error

Set sets a key into the key-value store.

Jump to

Keyboard shortcuts

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