collection

package
v0.0.0-...-89def8d Latest Latest
Warning

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

Go to latest
Published: May 24, 2021 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Name is the name of the threaddb collection used for buckets.
	Name = "buckets"
	// SeedName is the file name reserved for a random bucket seed.
	SeedName = ".textileseed"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket

type Bucket struct {
	Key       string              `json:"_id"`
	Owner     did.DID             `json:"owner"`
	Name      string              `json:"name"`
	Version   int                 `json:"version"`
	LinkKey   string              `json:"key,omitempty"`
	Path      string              `json:"path"` // @todo: This really should be Cid but will require a migration.
	Metadata  map[string]Metadata `json:"metadata"`
	CreatedAt int64               `json:"created_at"`
	UpdatedAt int64               `json:"updated_at"`
}

Bucket represents the buckets threaddb collection schema.

func (*Bucket) Copy

func (b *Bucket) Copy() *Bucket

Copy returns a copy of the bucket.

func (*Bucket) GetFileEncryptionKeyForPath

func (b *Bucket) GetFileEncryptionKeyForPath(pth string) ([]byte, error)

GetFileEncryptionKeyForPath returns the encryption key for path. Version 0 buckets use the link key for all paths. Version 1 buckets use a different key defined in path metadata.

func (*Bucket) GetFileEncryptionKeysForPrefix

func (b *Bucket) GetFileEncryptionKeysForPrefix(pre string) (map[string][]byte, error)

GetFileEncryptionKeysForPrefix returns a map of keys for every path under prefix.

func (*Bucket) GetLinkEncryptionKey

func (b *Bucket) GetLinkEncryptionKey() []byte

GetLinkEncryptionKey returns the bucket encryption key as bytes if present. Version 0 buckets use the link key for all files and folders. Version 1 buckets only use the link for folders.

func (*Bucket) GetMetadataForPath

func (b *Bucket) GetMetadataForPath(pth string, requireKey bool) (md Metadata, at string, ok bool)

GetMetadataForPath returns metadata for path. The returned metadata could be from an exact path match or the nearest parent, i.e., path was added as part of a folder. If requireKey is true, metadata w/o a key is ignored and the search continues toward root.

func (*Bucket) IsPrivate

func (b *Bucket) IsPrivate() bool

IsPrivate returns whether or not the bucket is private.

func (*Bucket) IsReadablePath

func (b *Bucket) IsReadablePath(pth string, id did.DID) bool

IsReadablePath returns whether or not a bucket path is readable by a did.DID.

func (*Bucket) IsWritablePath

func (b *Bucket) IsWritablePath(pth string, id did.DID) bool

IsWritablePath returns whether or not a bucket path is writable by a did.DID.

func (*Bucket) RotateFileEncryptionKeysForPrefix

func (b *Bucket) RotateFileEncryptionKeysForPrefix(pre string) error

RotateFileEncryptionKeysForPrefix re-generates existing metadata keys for every path under prefix.

func (*Bucket) SetMetadataAtPath

func (b *Bucket) SetMetadataAtPath(pth string, md Metadata)

SetMetadataAtPath create new or merges existing metadata at path.

func (*Bucket) UnsetMetadataWithPrefix

func (b *Bucket) UnsetMetadataWithPrefix(pre string)

UnsetMetadataWithPrefix removes metadata with the path prefix.

type BucketOption

type BucketOption func(*BucketOptions)

BucketOption holds a bucket option.

func WithBucketKey

func WithBucketKey(k []byte) BucketOption

WithBucketKey sets the bucket encryption key.

func WithBucketName

func WithBucketName(n string) BucketOption

WithBucketName specifies a name for a bucket. Note: This is only valid when creating a new bucket.

type BucketOptions

type BucketOptions struct {
	Name string
	Key  []byte
}

BucketOptions defines options for interacting with buckets.

type Buckets

type Buckets struct {
	Collection
}

Buckets is the threaddb collection for buckets.

func NewBuckets

func NewBuckets(c *dbc.Client) (*Buckets, error)

NewBuckets returns a new buckets collection mananger.

func (*Buckets) GetSafe

func (b *Buckets) GetSafe(ctx context.Context, thread core.ID, key string, opts ...Option) (*Bucket, error)

GetSafe gets a bucket instance and inflates any values that are missing due to schema updates.

func (*Buckets) New

func (b *Buckets) New(
	ctx context.Context,
	thread core.ID,
	key string,
	owner did.DID,
	pth path.Path,
	created time.Time,
	metadata map[string]Metadata,
	identity did.Token,
	opts ...BucketOption,
) (*Bucket, error)

New creates a bucket instance. Owner must be the identity token's subject.

type Collection

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

Collection wraps a ThreadDB collection with some convenience methods.

func (*Collection) Create

func (c *Collection) Create(ctx context.Context, thread core.ID, instance interface{}, opts ...Option) (
	coredb.InstanceID, error)

Create a collection instance.

func (*Collection) Delete

func (c *Collection) Delete(ctx context.Context, thread core.ID, id string, opts ...Option) error

Delete a collection instance.

func (*Collection) Get

func (c *Collection) Get(ctx context.Context, thread core.ID, key string, instance interface{}, opts ...Option) error

Get a collection instance.

func (*Collection) List

func (c *Collection) List(ctx context.Context, thread core.ID, query *db.Query, instance interface{}, opts ...Option) (
	interface{}, error)

List collection instances.

func (*Collection) Save

func (c *Collection) Save(ctx context.Context, thread core.ID, instance interface{}, opts ...Option) error

Save a collection instance.

func (*Collection) Verify

func (c *Collection) Verify(ctx context.Context, thread core.ID, instance interface{}, opts ...Option) error

Verify verifies instance changes.

func (*Collection) WriteTxn

func (c *Collection) WriteTxn(ctx context.Context, thread core.ID, opts ...Option) (*WriteTxn, error)

WriteTxn returns a write transaction in the collection. Call WriteTxn.End to commit the transaction. Using a defer statement and a named err param is the usual pattern:

func MyFunc() (err error) {
  defer func() {
    if e := txn.End(err); err == nil {
      err = e
    }
  }()
  ...
  if err = txn.Save(...); err != nil {
    return nil, err
  }
  ...
  if err = txn.Save(...); err != nil {
    return nil, err
  }
  ...
}

See WriteTxn.End for more.

type Metadata

type Metadata struct {
	Key       string                 `json:"key,omitempty"`
	Roles     map[did.DID]Role       `json:"roles"`
	Info      map[string]interface{} `json:"info,omitempty"`
	UpdatedAt int64                  `json:"updated_at"`
}

Metadata contains metadata about a bucket item (a file or folder).

func NewDefaultMetadata

func NewDefaultMetadata(owner did.DID, key []byte, ts time.Time) Metadata

NewDefaultMetadata returns the default metadata for a path.

func (*Metadata) SetFileEncryptionKey

func (m *Metadata) SetFileEncryptionKey(key []byte)

SetFileEncryptionKey sets the file encryption key.

type Option

type Option func(*Options)

Option holds a collection option.

func WithIdentity

func WithIdentity(identity did.Token) Option

WithIdentity sets an identity token.

type Options

type Options struct {
	Identity did.Token
}

Options defines options for interacting with a collection.

type Role

type Role int

Role describes an access role for a bucket item.

const (
	// NoneRole is the default role.
	NoneRole Role = iota
	// ReaderRole can read from the associated path and its descendants.
	ReaderRole
	// WriterRole can read from and write to the associated path and its descendants.
	WriterRole
	// AdminRole can read from, write to, and modify roles at the associated path and its descendants.
	AdminRole
)

func NewRoleFromString

func NewRoleFromString(s string) (Role, error)

NewRoleFromString returns the role associated with the given string.

func (Role) String

func (r Role) String() string

String returns the string representation of the role.

type Version

type Version int

Version is a concrete type for bucket version.

var (
	// Version1 is the current bucket collection version.
	Version1 Version = 1
)

type WriteTxn

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

WriteTxn wraps a write transaction in a collection.

func (*WriteTxn) Delete

func (t *WriteTxn) Delete(_ context.Context, id string) error

Delete a collection instance in the transaction.

func (*WriteTxn) Discard

func (t *WriteTxn) Discard()

Discard the transaction.

func (*WriteTxn) End

func (t *WriteTxn) End(err error) error

End ends the underlying transaction. A non-nil err results in the transaction being discarded before it's ended.

func (*WriteTxn) Save

func (t *WriteTxn) Save(ctx context.Context, instance interface{}) error

Save a collection instance in the transaction.

func (*WriteTxn) Verify

func (t *WriteTxn) Verify(ctx context.Context, instance interface{}) error

Verify a collection instance in the transaction.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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