memorystorage

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package memorystorage provides a concurrent in-memory storage driver. Its Open and Stat methods wrap storage.ErrNotFound when a key is absent. Builder.ListLimit changes the page size List uses when a request sets no limit.

New takes Builder policies for ListLimit. They run directly once in order on a fresh draft; nil entries panic and expanded nil/empty slices supply no edits. Nonpositive limits resolve to 100 after the final policy. Publication detaches settings, so retained drafts cannot change pagination. Construction performs no object I/O and starts no background work.

Open returns an independent seekable snapshot; callers close it. Put never closes a supplied reader and checks cancellation between reads, but cannot interrupt a reader already blocked inside Read. Listing checks cancellation while scanning and copying metadata, returns no partial page on cancellation, and retains ordinary lexical cursor behavior. Sorting and mutex waits remain synchronous; pages across separate calls are not a transactional snapshot.

Example:

driver := memorystorage.New(func(store *memorystorage.Builder) {
	store.ListLimit(25)
})
store := storage.New(driver)
_ = store.Driver()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

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

Builder configures New before publication. Callbacks run directly once, synchronously in order on one fresh draft. Nil callback entries panic; expanded nil or empty callback slices supply no edits. Retaining the draft cannot change published settings. Methods return the same draft.

Example:

memorystorage.New(func(store *memorystorage.Builder) {
	store.ListLimit(25)
})

func (*Builder) ListLimit

func (store *Builder) ListLimit(limit int) *Builder

ListLimit sets the page size used when ListOptions has no positive limit. Zero or negative values resolve to 100 after all callbacks. Explicit positive request limits retain precedence over this construction default.

This setting replaces the previous value on the construction draft. Services and loggers remain shared; publication detaches owned settings.

Example:

memorystorage.New(func(store *memorystorage.Builder) {
	store.ListLimit(25)
})

type BuilderFunc

type BuilderFunc = func(*Builder)

BuilderFunc is a construction policy for New. Policies run directly once in order on one draft; retained drafts cannot change an already published driver. Nil entries panic, while an expanded nil or empty policy slice makes no edits.

Example:

policies := []memorystorage.BuilderFunc{
	func(store *memorystorage.Builder) {
		store.ListLimit(25)
	},
}
driver := memorystorage.New(policies...)
page, err := driver.List(ctx, storage.ListOptions{})
_ = page
return err

type Driver

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

Driver stores owned object snapshots in process memory and is safe for concurrent use. Create it with New; process exit loses every object. It needs no Start/Stop calls and cannot issue externally fetchable signed URLs.

Example:

driver := memorystorage.New()
info, err := driver.Put(ctx, "report.txt", strings.NewReader("report"))
if err != nil {
	return err
}
fmt.Println(info.Size) // 6

func New

func New(configure ...BuilderFunc) *Driver

New creates an empty concurrent Driver with the completed construction policies.

Construction callbacks run directly once, synchronously in order on one fresh draft. Nil entries panic; expanded nil or empty slices omit edits. Publication detaches owned settings, not live dependencies or captures.

Example:

memorystorage.New(func(store *memorystorage.Builder) {
	store.ListLimit(25)
})

func (*Driver) Delete

func (driver *Driver) Delete(ctx context.Context, key string) error

Delete removes key and succeeds when it is absent. Cancellation precedes key validation; open snapshots remain readable after a successful deletion.

Example:

driver := memorystorage.New()
if err := driver.Delete(ctx, "obsolete.txt"); err != nil {
	return err
}

func (*Driver) List

func (driver *Driver) List(ctx context.Context, options storage.ListOptions) (storage.Page, error)

List returns an owned, lexically sorted page and a cursor only when more keys match. One read lock covers selection, metadata, and lookahead. Scanning and metadata copying check cancellation between entries; sorting and mutex waits are synchronous and are checked after completion, not forcibly interrupted. A canceled operation returns no partial page. Concurrent changes between calls do not provide a multi-page snapshot.

Example:

page, err := driver.List(ctx, storage.ListOptions{Prefix: "reports/", Limit: 20})
if err != nil {
	return err
}
for _, info := range page.Objects {
	fmt.Println(info.Key)
}

func (*Driver) Open

func (driver *Driver) Open(ctx context.Context, key string) (storage.Object, error)

Open returns an independent snapshot of the object contents, whose Contents also implements io.ReadSeeker. Missing objects return an error wrapping storage.ErrNotFound. Replacing or deleting the stored object never changes an already opened body.

Example:

object, err := driver.Open(ctx, "report.txt")
if err != nil {
	return err
}
_, err = io.Copy(destination, object.Contents)
return errors.Join(err, object.Contents.Close())

func (*Driver) Put

func (driver *Driver) Put(ctx context.Context, key string, contents io.Reader) (storage.ObjectInfo, error)

Put reads contents once and replaces key with an independent copy. It checks cancellation between reads and before publication, leaving an existing object untouched on failure. It neither closes contents nor interrupts a Read already blocked inside the caller's reader.

Example:

driver := memorystorage.New()
info, err := driver.Put(ctx, "reports/a.txt", strings.NewReader("report"))
if err != nil {
	return err
}
fmt.Println(info.Size) // 6

func (*Driver) SignURL

func (driver *Driver) SignURL(ctx context.Context, key string, options storage.SignOptions) (*url.URL, error)

SignURL validates the request in the order documented on storage.Driver.SignURL and then returns an error wrapping storage.ErrUnsupported: objects in process memory have no URL a client could fetch.

Example:

driver := memorystorage.New()
_, err := driver.SignURL(context.Background(), "report.pdf", storage.SignOptions{TTL: time.Minute})
fmt.Println(errors.Is(err, storage.ErrUnsupported)) // true

func (*Driver) Stat

func (driver *Driver) Stat(ctx context.Context, key string) (storage.ObjectInfo, error)

Stat returns object metadata without opening its contents. Missing objects return an error wrapping storage.ErrNotFound. Metadata is returned by value and does not expose the stored byte slice.

Example:

info, err := driver.Stat(ctx, "report.txt")
if err != nil {
	return err
}
fmt.Println(info.Size)

Jump to

Keyboard shortcuts

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