fs_db

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2024 License: MIT Imports: 5 Imported by: 0

README

FS DB

Test Coverage Go Reference

FS DB is a simple key-value database for storing files. FS DB has two clients that give you the option to inline database logic into your application or run an external server and send data using grpc.

Install

go get -u github.com/glebziz/fs_db

Usage

Store interface

FS DB provides the following store key-value interface:

type Store interface {
// Set sets the contents of b using the key.
Set(ctx context.Context, key string, b []byte) error

// SetReader sets the reader content using the key.
SetReader(ctx context.Context, key string, reader io.Reader, size uint64) error

// Get returns content by key.
Get(ctx context.Context, key string) ([]byte, error)

// GetReader returns content as io.ReadCloser by key.
GetReader(ctx context.Context, key string) (io.ReadCloser, error)

// Delete delete content by key.
Delete(ctx context.Context, key string) error
}
Transaction interface

FS DB provides the following transaction interface:

type TxOps interface {
// Commit commits the transaction.
Commit(ctx context.Context) error

// Rollback rolls back the transaction.
Rollback(ctx context.Context) error
}
DB interface

FS DB provides the following client interface:

type DB interface {
	Store

	// Begin starts a transaction with isoLevel.
	Begin(ctx context.Context, isoLevel ...model.TxIsoLevel) (Tx, error)
}

The database client supports starting a transaction with four standard isolation levels:

  • ReadUncommitted
  • ReadCommitted
  • RepeatableRead
  • Serializable

Since the set operation contains insert and update operations, the serializable level is equal to the repeatable read.

Use the following constants to select the isolation level:

const (
	IsoLevelReadUncommitted
	IsoLevelReadCommitted
	IsoLevelRepeatableRead
	IsoLevelSerializable
)
Tx interface

FS DB provides the following tx interface:

type Tx interface {
	TxOps
	Store
}
Example

See more examples here.

package main

import (
	"context"
	"fmt"
	"log"
	
	"github.com/glebziz/fs_db/pkg/inline"
)

func main() {
	db, err := inline.Open(context.Background(), &config.Storage{
		DbPath:      "test.db",
		MaxDirCount: 1,
		RootDirs:    []string{"./testStorage"},
	})
	if err != nil {
		log.Fatalln("Open db inline:", err)
	}

	err = db.Set(context.Background(), "someKey", []byte("some content"))
	if err != nil {
		log.Fatalln("Set:", err)
	}

	b, err := db.Get(context.Background(), "someKey")
	if err != nil {
		log.Fatalln("Get:", err)
	}

	fmt.Println(string(b))
}

License

MIT

Documentation

Overview

Package fs_db provides a client for a simple key-value database for storing files.

Index

Constants

View Source
const (
	// IsoLevelReadUncommitted transaction iso level read uncommitted.
	IsoLevelReadUncommitted = model.TxIsoLevel(iota)

	// IsoLevelReadCommitted transaction iso level read committed.
	IsoLevelReadCommitted

	// IsoLevelRepeatableRead transaction iso level repeatable read.
	IsoLevelRepeatableRead

	// IsoLevelSerializable transaction iso level serializable.
	//
	// Since the set operation contains insert and update operations, the serializable level is equal to the repeatable read.
	IsoLevelSerializable
)
View Source
const (
	// IsoLevelDefault the default transaction iso level is ReadCommitted
	IsoLevelDefault = IsoLevelReadCommitted
)

Variables

View Source
var (
	SizeErr           = errors.New("no free space")
	NotFoundErr       = errors.New("not found")
	EmptyKeyErr       = errors.New("empty name")
	HeaderNotFoundErr = errors.New("header not found")

	// Tx errors
	TxNotFoundErr      = errors.New("transaction not found")
	TxAlreadyExistsErr = errors.New("transaction already exists")
	TxSerializationErr = errors.New("serialization error")

	// Config errors
	EmptyDbPathErr = errors.New("empty db path")
	EmptyRootDirs  = errors.New("empty root dirs")
)

Functions

This section is empty.

Types

type DB

type DB interface {
	Store

	// Begin starts a transaction with isoLevel.
	Begin(ctx context.Context, isoLevel ...model.TxIsoLevel) (Tx, error)
}

DB provides fs db interface.

type Store added in v1.0.0

type Store interface {
	// Set sets the contents of b using the key.
	Set(ctx context.Context, key string, b []byte) error

	// SetReader sets the reader content using the key.
	SetReader(ctx context.Context, key string, reader io.Reader, size uint64) error

	// Get returns content by key.
	Get(ctx context.Context, key string) ([]byte, error)

	// GetReader returns content as io.ReadCloser by key.
	GetReader(ctx context.Context, key string) (io.ReadCloser, error)

	// Delete delete content by key.
	Delete(ctx context.Context, key string) error
}

Store provides KV operations with files.

type Tx added in v1.0.0

type Tx interface {
	TxOps
	Store
}

Tx provides fs db transaction interface.

func CreateTx added in v1.0.0

func CreateTx(store Store, t TxOps, ctxFn txCtxFn) Tx

CreateTx returns transaction fs db.

type TxOps added in v1.0.0

type TxOps interface {
	// Commit commits the transaction.
	Commit(ctx context.Context) error

	// Rollback rolls back the transaction.
	Rollback(ctx context.Context) error
}

TxOps provides transactional operations.

Directories

Path Synopsis
cmd
example
internal
db
delivery/grpc/store/mocks
Code generated by MockGen.
Code generated by MockGen.
usecase/cleaner/mocks
Code generated by MockGen.
Code generated by MockGen.
usecase/dir/mocks
Code generated by MockGen.
Code generated by MockGen.
usecase/root/mocks
Code generated by MockGen.
Code generated by MockGen.
usecase/store/mocks
Code generated by MockGen.
Code generated by MockGen.
usecase/transaction/mocks
Code generated by MockGen.
Code generated by MockGen.
utils/grpc/streamreader/mocks
Code generated by MockGen.
Code generated by MockGen.
pkg

Jump to

Keyboard shortcuts

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