storage

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2025 License: MPL-2.0 Imports: 4 Imported by: 0

README

Tests

Storage

Golang storage providers

Install

go get github.com/rlshukhov/storage

Example

package main

import (
	"fmt"
	"github.com/rlshukhov/nullable"
	"github.com/rlshukhov/storage"
	"github.com/rlshukhov/storage/file"
)

type User struct {
	ID   uint64 `yaml:"id"`
	Name string `yaml:"name"`
}

func main() {
	db, err := storage.GetKeyValueProviderFromConfig[uint64, User](storage.KeyValueConfig{
		File: nullable.FromValue(file.Config{
			Path: "./users.yaml",
		}),
	})
	if err != nil {
		panic(err)
	}

	err = db.Setup()
	if err != nil {
		panic(err)
	}
	defer func() {
		err := db.Shutdown()
		if err != nil {
			panic(err)
		}
	}()

	users := []User{
		{ID: 0, Name: "John"},
		{ID: 1, Name: "Paul"},
	}
	for _, user := range users {
		err := db.Store(user.ID, user)
		if err != nil {
			panic(err)
		}
	}

	fmt.Println(db.Get(1))
}
rlshukhov@MacBook-Pro-Lane main % go run main.go
{1 Paul} <nil>
rlshukhov@MacBook-Pro-Lane main % cat users.yaml
data:
    0:
        id: 0
        name: John
    1:
        id: 1
        name: Paul

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyValueConfig

type KeyValueConfig struct {
	Badger nullable.Nullable[badger.Config] `yaml:"badger"`
	File   nullable.Nullable[file.Config]   `yaml:"file"`
}

type KeyValueProvider

type KeyValueProvider[K ~string | ~uint64, V any] interface {
	Setup() error
	Shutdown() error

	Store(key K, value V) error
	Get(key K) (V, error)
	Remove(key K) error
	ForEach(fn func(key K, value V) bool) error
	GetMultiple(keys []K) ([]V, error)

	StoreReference(reference K, key K) error
	RemoveReference(reference K) error
	GetByReference(reference K) (V, error)
}

func GetKeyValueProviderFromConfig

func GetKeyValueProviderFromConfig[K ~string | ~uint64, V any](keyValueConfig KeyValueConfig) (KeyValueProvider[K, V], error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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