Documentation
¶
Index ¶
- Variables
- type Bucket
- func (b *Bucket) Create(key string) (out *File, err error)
- func (b *Bucket) CreateBucket(key string) (out *Bucket, err error)
- func (b *Bucket) Cursor(fn func(*Cursor) error) (err error)
- func (b *Bucket) Delete(key string) (err error)
- func (b *Bucket) ForEach(fn func(*File) error) (err error)
- func (b *Bucket) Get(key string) (out *File, ok bool)
- func (b *Bucket) GetBucket(key string) (out *Bucket, ok bool)
- func (b *Bucket) GetOrCreate(key string) (out *File, err error)
- func (b *Bucket) GetOrCreateBucket(key string) (out *Bucket, err error)
- func (e Bucket) Key() (out string)
- type Cursor
- type DB
- type File
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrEmptyKey is returned when a key argument is an empty string. ErrEmptyKey = errors.New("invalid key, cannot be empty") // ErrInvalidKeyFormat is returned when a key contains disallowed characters or separators. ErrInvalidKeyFormat = errors.New("invalid key format: only letters, digits, '.', '_', '-' are allowed; key must not contain path separators") )
Functions ¶
This section is empty.
Types ¶
type Bucket ¶
type Bucket struct {
// contains filtered or unexported fields
}
Bucket groups child buckets and files under a shared directory path.
func (*Bucket) Create ¶
Create validates key and creates a file when it is not already present.
Example ¶
var (
f *File
err error
)
if f, err = exampleBucket.Create("my_file"); err != nil {
log.Fatal(err)
}
fmt.Println("File", f)
func (*Bucket) CreateBucket ¶
CreateBucket validates key and creates a child bucket when missing.
Example ¶
var (
b *Bucket
err error
)
if b, err = exampleBucket.CreateBucket("my_bucket"); err != nil {
log.Fatal(err)
}
fmt.Println("Bucket", b)
func (*Bucket) Cursor ¶ added in v0.2.0
Cursor executes fn with a read-only cursor over files in key order.
fn executes while the bucket read lock is held.
Callbacks must not call methods that may require a write lock on this same bucket (for example: CreateBucket, GetOrCreateBucket, Create, GetOrCreate, or Delete), because doing so can deadlock.
The cursor is only valid for the duration of fn.
Example ¶
var err error
if err = exampleBucket.Cursor(func(c *Cursor) error {
var (
f *File
ok bool
)
if f, ok = c.First(); !ok {
return errors.New("empty bucket")
}
fmt.Println("First file", f)
return nil
}); err != nil {
log.Fatal(err)
}
func (*Bucket) Delete ¶ added in v0.3.0
Delete removes key from the bucket if present.
Delete validates key, closes the file buffer when initialized, removes the file from disk, and then removes the key from the in-memory index.
If key is not present, Delete returns nil.
Example ¶
var err error
if err = exampleBucket.Delete("my_file"); err != nil {
log.Fatal(err)
}
fmt.Println("Deleted my_file")
func (*Bucket) ForEach ¶ added in v0.2.0
ForEach calls fn for each file in key order until fn returns an error.
fn executes while the bucket read lock is held.
Callbacks must not call methods that may require a write lock on this same bucket (for example: CreateBucket, GetOrCreateBucket, Create, GetOrCreate, or Delete), because doing so can deadlock.
Example ¶
var err error
if err = exampleBucket.ForEach(func(f *File) error {
fmt.Println("File", f)
return nil
}); err != nil {
log.Fatal(err)
}
func (*Bucket) Get ¶
Get returns the file for key when it exists.
Example ¶
var (
f *File
ok bool
)
if f, ok = exampleBucket.Get("my_file"); !ok {
log.Fatalf("my_file not found")
}
fmt.Println("File", f)
func (*Bucket) GetBucket ¶
GetBucket returns the child bucket for key when it exists.
Example ¶
var (
b *Bucket
ok bool
)
if b, ok = exampleBucket.GetBucket("my_bucket"); !ok {
log.Fatalf("my_bucket not found")
}
fmt.Println("Bucket", b)
func (*Bucket) GetOrCreate ¶
GetOrCreate returns an existing file for key or creates it.
Example ¶
var (
f *File
err error
)
if f, err = exampleBucket.GetOrCreate("my_file"); err != nil {
log.Fatal(err)
}
fmt.Println("File", f)
func (*Bucket) GetOrCreateBucket ¶
GetOrCreateBucket returns an existing child bucket or creates it.
Example ¶
var (
b *Bucket
err error
)
if b, err = exampleBucket.GetOrCreateBucket("my_bucket"); err != nil {
log.Fatal(err)
}
fmt.Println("Bucket", b)
type DB ¶
type DB struct {
*Bucket
}
DB is the root container for buckets and files under a database path.
type File ¶
type File struct {
// contains filtered or unexported fields
}
File represents a single persisted value in the database.
func (*File) Append ¶
Append writes to the current stream buffer via fn.
During a concurrent Update, append operations may fail with streambuf.ErrIsClosed if they target a buffer being rotated out.
Example ¶
var err error
if err = exampleFile.Append(func(w io.Writer) (err error) {
return nil
}); err != nil {
log.Fatal(err)
}
fmt.Println("Append success")
func (File) Key ¶
func (e File) Key() (out string)
Key returns the entry key used as the on-disk file or directory name.
func (*File) Read ¶
Read opens a non-streaming reader for the current stream buffer and passes it to fn. The reader is closed after fn returns.
If Read acquires a reader before or during Update, it may continue reading from the previous buffer.
Reads that start after Update returns observe the updated file contents.
Example ¶
var err error
if err = exampleFile.Read(func(r io.Reader) (err error) {
return nil
}); err != nil {
log.Fatal(err)
}
fmt.Println("Read success")
func (*File) StreamingRead ¶
StreamingRead opens a tail-style streaming reader for the current stream buffer and passes it to fn.
The reader is closed when fn returns or when ctx is canceled, whichever happens first.
While active, the reader can observe new data appended through Append against the same active buffer.
Like Read, if StreamingRead acquires a reader before or during Update, it may continue reading from the previous buffer according to streambuf close semantics. It does not automatically move to the new buffer created by Update.
When no bytes are available and the streaming reader or backing buffer is closed, streambuf returns streambuf.ErrIsClosed.
Example ¶
var err error
if err = exampleFile.StreamingRead(context.Background(), func(r io.Reader) (err error) {
return nil
}); err != nil {
log.Fatal(err)
}
fmt.Println("StreamingRead success")
func (*File) Update ¶
Update writes file contents through fn and atomically replaces the file.
Update replaces the on-disk file via rename, syncs the parent directory for durability, then rotates the in-memory stream buffer.
While rotation is in progress, in-flight readers on the previous buffer continue using that buffer. Non-streaming readers drain available bytes and then reach EOF-equivalent behavior, while streaming readers unblock and end according to streambuf close semantics. Appends using a buffer being closed may fail with streambuf.ErrIsClosed.
On successful return from Update, newly started Read and StreamingRead calls observe updated contents. In-flight reads and appends may still complete against the previous buffer according to streambuf close semantics.
Example ¶
var err error
if err = exampleFile.Update(func(w io.Writer) (err error) {
return nil
}); err != nil {
log.Fatal(err)
}
fmt.Println("Update success")

