sos – github.com/hweidner/sos Index | Files

package sos

import "github.com/hweidner/sos"

SOS, a simple object store.

Package sos implements a simple, file system based object (key/value) store.

Objects are stored in a file system directory, one file per object. The file name is a SHA256, hex encoded hash of the key. The file's content is the value.

For performance reasons, the files are stored in a two layer directory structure. The subdirecories are the first and second byte of the key hash, in hex encoding. The filename is the remainder of the hash. For example, if the hash to a given key is

e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

then the object will be stored in the file

basedir/e3/b0/c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

For atomicity and lock-freeness, each new key/value pair is first stored in a temporary file and later moved to its final position. So, there is no danger of reading half-written values even when values are big.

The Get operation first creates a hard link to a temporary file before reading from that file. This ensures that a Store to the same key does not affect an already started Get operation. This is a bit slower than accessing the storage files directly, but works lock-free even with NFS.

Index

Types

type SOS

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

SOS is the controlling data structure for the object store

func New

func New(path string) (*SOS, error)

New creates a new simple object store at the directory path.

The path must point to a location which lies on a UNIX-like file system. It must support the open/read/write/close methods, and UNIX-style hard links. The directory under path must not cross file system boundaries. If the directory does not exist yet, it is created upon invocation.

func (*SOS) Delete

func (s *SOS) Delete(key string) error

Delete removes an object from the store.

func (*SOS) Destroy

func (s *SOS) Destroy()

Destroy will delete an object store and remove all of its content, and the directory itself.

Note: on NFS, this can break running Get operations.

func (*SOS) Get

func (s *SOS) Get(key string) ([]byte, error)

Get fetches an object from the store, identified by the key, and returns it as byte slice.

func (*SOS) GetString

func (s *SOS) GetString(key string) (string, error)

GetString fetches an object from the store, identified by the key, and returns it as a string

func (*SOS) GetTo

func (s *SOS) GetTo(key string, wr io.Writer) error

GetTo fetches an object from the store, identified by the key, and copies it into an io.Writer.

func (*SOS) Store

func (s *SOS) Store(key string, value []byte) error

Store stores a key/value pair, given as string and byte slice, in the object store.

func (*SOS) StoreFrom

func (s *SOS) StoreFrom(key string, rd io.Reader) error

StoreFrom stores a value, which is read from an io.Reader, under the given key in the object store.

func (*SOS) StoreString

func (s *SOS) StoreString(key, value string) error

StoreString stores a key/value pair, given as strings, in the object store.

Source Files

sos.go

Version
v0.1.0 (latest)
Published
May 17, 2021
Platform
linux/amd64
Imports
8 packages
Last checked
5 months ago

Tools for package owners.