storage

package module
v0.0.0-...-fa626c8 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2021 License: BSD-3-Clause Imports: 3 Imported by: 0

README

Storage.go

This is an abstraction on top of exisiting filesystem to provide tamperproff feature using merkle-tree.

Features

  • Provides a simple one method interfaces
type Putter interface {
	Put(ctx context.Context, r io.Reader) ([]byte, int64, error)
}

type Getter interface {
	Get(ctx context.Context, hash []byte) (io.ReadCloser, error)
}

type Remover interface {
	Remove(ctx context.Context, hash []byte) error
}

type Lister interface {
	List() (IteratorFunc, CancelFunc)
}

type Closer interface {
	Close(ctx context.Context, hash []byte) error
}
  • Optimized merkle tree for fast write
  • Support io.Reader out of the box
  • Dedup files by default using SHA-256 hash
  • Secure Read and Write using ChaCha20Stream
  • Lots of backend drivers (memory, file, boltdb, pogreb, sqlite)

Example

let's build a simple command line tools to put content into merkle storage and retrive them using their root hash

package main

import (
	"context"
	"io"
	"os"

	"github.com/alinz/hash.go"

	"github.com/alinz/storage.go/local"
	"github.com/alinz/storage.go/merkle"
)

const BlockSize = 10
const StoragePath = "./merkle_storage"

func main() {
	os.MkdirAll(StoragePath, os.ModePerm)

	local := local.New(StoragePath)
	merkle := merkle.New(local, local, BlockSize)

	switch os.Args[1] {
	case "put":
		value, n, err := merkle.Put(context.Background(), os.Stdin)
		if err != nil {
			println(err)
			os.Exit(1)
		}

		println("size: ", n)
		println("key", hash.Format(value))

	case "get":
		key := os.Args[2]
		value, err := hash.ValueFromString(key)
		if err != nil {
			println(err)
			os.Exit(1)
		}

		r, err := merkle.Get(context.Background(), value)
		if err != nil {
			println(err)
			os.Exit(1)
		}
		defer r.Close()
		io.Copy(os.Stdout, r)

	default:
		os.Exit(1)
	}
}

now run the following command to put the content in to merkle storage

echo "hello world" | go run main.go put

size:  12
key sha256-cc86f92f36f12d9aeb48391a3a47ad559110eae2b770a3b7650cb7fb8854f07f

and run the following to retrive it

go run main.go get sha256-cc86f92f36f12d9aeb48391a3a47ad559110eae2b770a3b7650cb7fb8854f07f

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrIteratorDone = errors.New("iterator is done")
)
View Source
var (
	ErrNotFound = errors.New("not found")
)

Functions

func Iterator

func Iterator(mapper MapperFunc) (IteratorFunc, CancelFunc)

Types

type CancelFunc

type CancelFunc func()

type Closer

type Closer interface {
	Close() error
}

type Getter

type Getter interface {
	Get(ctx context.Context, hash []byte) (io.ReadCloser, error)
}

type IteratorFunc

type IteratorFunc func(ctx context.Context) ([]byte, error)

type Lister

type Lister interface {
	List() (IteratorFunc, CancelFunc)
}

type MapperFunc

type MapperFunc func(YieldFunc)

type Putter

type Putter interface {
	Put(ctx context.Context, r io.Reader) (hash []byte, n int64, err error)
}

type Remover

type Remover interface {
	Remove(ctx context.Context, hash []byte) error
}

type YieldFunc

type YieldFunc func([]byte, error) bool

Directories

Path Synopsis
cmd
internal
kv

Jump to

Keyboard shortcuts

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