Documentation ¶
Index ¶
- type BlobRef
- func (b *BlobRef) Fail() error
- func (b *BlobRef) Load(ps []datastore.Property) error
- func (b *BlobRef) LoadKey(k *datastore.Key) error
- func (b *BlobRef) MarkForDeletion() error
- func (b *BlobRef) ObjectPath() string
- func (b *BlobRef) Ready() error
- func (b *BlobRef) Save() ([]datastore.Property, error)
- type BlobRefCursor
- type BlobRefStatus
- type Driver
- type MetaDB
- func (m *MetaDB) Connect(ctx context.Context) error
- func (m *MetaDB) CreateStore(ctx context.Context, store *Store) (*Store, error)
- func (m *MetaDB) DeleteBlobRef(ctx context.Context, key uuid.UUID) error
- func (m *MetaDB) DeleteRecord(ctx context.Context, storeKey, key string) error
- func (m *MetaDB) DeleteStore(ctx context.Context, key string) error
- func (m *MetaDB) Disconnect(ctx context.Context) error
- func (m *MetaDB) FindStoreByName(ctx context.Context, name string) (*Store, error)
- func (m *MetaDB) GetBlobRef(ctx context.Context, key uuid.UUID) (*BlobRef, error)
- func (m *MetaDB) GetCurrentBlobRef(ctx context.Context, storeKey, recordKey string) (*BlobRef, error)
- func (m *MetaDB) GetRecord(ctx context.Context, storeKey, key string) (*Record, error)
- func (m *MetaDB) GetStore(ctx context.Context, key string) (*Store, error)
- func (m *MetaDB) InsertBlobRef(ctx context.Context, blob *BlobRef) (*BlobRef, error)
- func (m *MetaDB) InsertRecord(ctx context.Context, storeKey string, record *Record) (*Record, error)
- func (m *MetaDB) ListBlobRefsByStatus(ctx context.Context, status BlobRefStatus, olderThan time.Time) (BlobRefCursor, error)
- func (m *MetaDB) PromoteBlobRefToCurrent(ctx context.Context, blob *BlobRef) (*Record, *BlobRef, error)
- func (m *MetaDB) RemoveBlobFromRecord(ctx context.Context, storeKey string, recordKey string) (*Record, *BlobRef, error)
- func (m *MetaDB) UpdateBlobRef(ctx context.Context, blob *BlobRef) (*BlobRef, error)
- func (m *MetaDB) UpdateRecord(ctx context.Context, storeKey string, key string, updater RecordUpdater) (*Record, error)
- type PropertyMap
- type PropertyValue
- type Record
- type RecordUpdater
- type Store
- type Timestamps
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BlobRef ¶
type BlobRef struct { // Key is the primary key for the blob entry Key uuid.UUID `datastore:"-"` // Size is the byte size of the blob Size int64 // Status is the current status of the blob Status BlobRefStatus // StoreKey is the key of the store that the blob belongs to StoreKey string // RecordKey is the key of the record that the blob belongs to // It can be non-existent (e.g. deleted already) but then the Status // should not be Blob StatusReady. RecordKey string // Timestamps keeps track of creation and modification times and stores a randomly // generated UUID to maintain consistency. Timestamps Timestamps }
BlobRef is a metadata document to keep track of blobs stored in an external blob store.
func NewBlobRef ¶
NewBlobRef creates a new BlobRef as follows:
- Set a new UUID to Key
- Initialize Size and ObjectName as specified
- Set Status to BlobRefStatusInitializing
- Set current time to Timestamps (both created and updated at)
func (*BlobRef) Fail ¶
Fail marks the BlobRef as BlobRefStatusError and updates Timestamps. Any state can transition to BlobRefStatusError.
func (*BlobRef) Load ¶
Load implements the Datastore PropertyLoadSaver interface and converts Datstore properties to the Properties field.
func (*BlobRef) LoadKey ¶
LoadKey implements the KeyLoader interface and sets the value to the Key field.
func (*BlobRef) MarkForDeletion ¶
MarkForDeletion marks the BlobRef as BlobRefStatusPendingDeletion and updates Timestamps. Returns an error if the current Status is not BlobRefStatusReady.
func (*BlobRef) ObjectPath ¶
ObjectPath returns an object path for the backend blob storage.
type BlobRefCursor ¶
type BlobRefCursor interface { // Next advances the iterator and returns the next value. // Returns nil and an iterator.Done at the end of the iterator. Next() (*BlobRef, error) }
BlobRefCursor is a database cursor for BlobRef.
type BlobRefStatus ¶
type BlobRefStatus int16
BlobRefStatus represents a current blob status.
Life of a Blob ¶
[New Record created] --> [new BlobRef entity with BlobRefStatusInitializing]
/ \ / fail \ success v v [BlobRefStatusError] [BlobRefStatusReady] | x | Upload new blob | \ fail | Record deleted or new blob uploaded or | \ v delete the record | -----[BlobRefStatusPendingDeletion] v / [Delete the blob entity] <-------------/ Garbage collection
const ( // BlobRefStatusUnknown represents internal error. BlobRefStatusUnknown BlobRefStatus = iota // BlobRefStatusInitializing means the blob is currently being prepared, i.e. // being uploaded to the blob store. BlobRefStatusInitializing // BlobRefStatusReady means the blob is committed and ready for use. BlobRefStatusReady // BlobRefStatusPendingDeletion means the blob is no longer referenced by // any Record entities and needs to be deleted. BlobRefStatusPendingDeletion // BlobRefStatusError means the blob was not uploaded due to client or server // errors and the corresponding record needs to be updated (either by // retrying blob upload or deleting the entry). BlobRefStatusError )
type Driver ¶
type Driver interface { Connect(ctx context.Context) error Disconnect(ctx context.Context) error CreateStore(ctx context.Context, store *Store) (*Store, error) GetStore(ctx context.Context, key string) (*Store, error) FindStoreByName(ctx context.Context, name string) (*Store, error) DeleteStore(ctx context.Context, key string) error InsertRecord(ctx context.Context, storeKey string, record *Record) (*Record, error) UpdateRecord(ctx context.Context, storeKey string, key string, updater RecordUpdater) (*Record, error) GetRecord(ctx context.Context, storeKey, key string) (*Record, error) DeleteRecord(ctx context.Context, storeKey, key string) error InsertBlobRef(ctx context.Context, blob *BlobRef) (*BlobRef, error) UpdateBlobRef(ctx context.Context, blob *BlobRef) (*BlobRef, error) GetBlobRef(ctx context.Context, key uuid.UUID) (*BlobRef, error) GetCurrentBlobRef(ctx context.Context, storeKey, recordKey string) (*BlobRef, error) PromoteBlobRefToCurrent(ctx context.Context, blob *BlobRef) (*Record, *BlobRef, error) RemoveBlobFromRecord(ctx context.Context, storeKey string, recordKey string) (*Record, *BlobRef, error) DeleteBlobRef(ctx context.Context, key uuid.UUID) error ListBlobRefsByStatus(ctx context.Context, status BlobRefStatus, olderThan time.Time) (BlobRefCursor, error) TimestampPrecision() time.Duration }
Driver interface defines common operations for the metadata server. MetaDB uses this interface to perform actual operations on the backend service.
type MetaDB ¶
type MetaDB struct {
// contains filtered or unexported fields
}
MetaDB is a metadata database manager of Open Saves. It performs operations through the Driver interface. The methods return gRPC error codes. Here are some common error codes returned. For additional details, please look at the method help. Common errors:
- NotFound: entity or object is not found
- Aborted: transaction is aborted
- InvalidArgument: key or value provided is not valid
- Internal: internal unrecoverable error
func (*MetaDB) CreateStore ¶
CreateStore creates a new store.
func (*MetaDB) DeleteBlobRef ¶
DeleteBlobRef deletes the BlobRef object from the database. Returned errors:
- NotFound: the blobref object is not found
- FailedPrecondition: the blobref status is Ready and can't be deleted
func (*MetaDB) DeleteRecord ¶
DeleteRecord deletes a record with key in store storeKey. It doesn't return error even if the key is not found in the database.
func (*MetaDB) DeleteStore ¶
DeleteStore deletes the store with specified key. Returns error if the store has any child records.
func (*MetaDB) Disconnect ¶
Disconnect terminates the database connection. Make sure to call this method to release resources (e.g. using defer). The MetaDB instance will not be available after Disconnect().
func (*MetaDB) FindStoreByName ¶
FindStoreByName finds and fetch a store based on the name (complete match).
func (*MetaDB) GetBlobRef ¶
GetBlobRef returns a BlobRef object specified by the key. Returns errors:
- NotFound: the object is not found.
func (*MetaDB) GetCurrentBlobRef ¶
func (m *MetaDB) GetCurrentBlobRef(ctx context.Context, storeKey, recordKey string) (*BlobRef, error)
GetCurrentBlobRef gets a BlobRef object associated with a record. Returned errors:
- NotFound: the record is not found.
- FailedPrecondition: the record doesn't have a blob.
func (*MetaDB) GetRecord ¶
GetRecord fetches and returns a record with key in store storeKey. Returns error if not found.
func (*MetaDB) GetStore ¶
GetStore fetches a store based on the key provided. Returns error if the key is not found.
func (*MetaDB) InsertBlobRef ¶
InsertBlobRef inserts a new BlobRef object to the datastore.
func (*MetaDB) InsertRecord ¶
func (m *MetaDB) InsertRecord(ctx context.Context, storeKey string, record *Record) (*Record, error)
InsertRecord creates a new Record in the store specified with storeKey. Returns error if there is already a record with the same key.
func (*MetaDB) ListBlobRefsByStatus ¶
func (m *MetaDB) ListBlobRefsByStatus(ctx context.Context, status BlobRefStatus, olderThan time.Time) (BlobRefCursor, error)
ListBlobRefsByStatus returns a cursor that iterates over BlobRefs where Status = status and UpdatedAt < olderThan.
func (*MetaDB) PromoteBlobRefToCurrent ¶
func (m *MetaDB) PromoteBlobRefToCurrent(ctx context.Context, blob *BlobRef) (*Record, *BlobRef, error)
PromoteBlobRefToCurrent promotes the provided BlobRef object as a current external blob reference. Returned errors:
- NotFound: the specified record or the blobref was not found
- Internal: BlobRef status transition error
func (*MetaDB) RemoveBlobFromRecord ¶
func (m *MetaDB) RemoveBlobFromRecord(ctx context.Context, storeKey string, recordKey string) (*Record, *BlobRef, error)
RemoveBlobFromRecord removes the ExternalBlob from the record specified by storeKey and recordKey. It also changes the status of the blob object to BlobRefStatusPendingDeletion. Returned errors:
- NotFound: the specified record or the blobref was not found
- FailedPrecondition: the record doesn't have an external blob
- Internal: BlobRef status transition error
func (*MetaDB) UpdateBlobRef ¶
UpdateBlobRef updates a BlobRef object with the new property values. Returns a NotFound error if the key is not found.
func (*MetaDB) UpdateRecord ¶
func (m *MetaDB) UpdateRecord(ctx context.Context, storeKey string, key string, updater RecordUpdater) (*Record, error)
UpdateRecord updates the record in the store specified with storeKey. Pass a callback function to updater and change values there. The callback will be protected by a transaction. Returns error if the store doesn't have a record with the key provided.
type PropertyMap ¶
type PropertyMap map[string]*PropertyValue
PropertyMap represents user-defined custom properties.
func NewPropertyMapFromProto ¶
func NewPropertyMapFromProto(proto map[string]*pb.Property) PropertyMap
NewPropertyMapFromProto creates a new Property instance from a proto. Passing nil returns an empty map.
func (*PropertyMap) Load ¶
func (m *PropertyMap) Load(ps []datastore.Property) error
Load implements the Datastore PropertyLoadSaver interface and converts individual properties to PropertyMap.
type PropertyValue ¶
type PropertyValue struct { Type pb.Property_Type IntegerValue int64 StringValue string BooleanValue bool }
PropertyValue is an internal representation of the user-defined property. See the Open Saves API definition for details.
func NewPropertyValueFromProto ¶
func NewPropertyValueFromProto(proto *pb.Property) *PropertyValue
NewPropertyValueFromProto creates a new Property instance from a proto. Passing nil returns a zero-initialized Property.
func (*PropertyValue) ToProto ¶
func (p *PropertyValue) ToProto() *pb.Property
ToProto converts the struct to a proto.
type Record ¶
type Record struct { Key string `datastore:"-"` Blob []byte `datastore:",noindex"` BlobSize int64 ExternalBlob uuid.UUID `datastore:"-"` Properties PropertyMap OwnerID string Tags []string // Timestamps keeps track of creation and modification times and stores a randomly // generated UUID to maintain consistency. Timestamps Timestamps }
Record represents a Open Saves record in the metadata database. See the Open Saves API definition for details.
func NewRecordFromProto ¶
NewRecordFromProto creates a new Record instance from a proto. Passing nil returns a zero-initialized proto.
func (*Record) Load ¶
Load implements the Datastore PropertyLoadSaver interface and converts Datastore properties to corresponding struct fields.
func (*Record) LoadKey ¶
LoadKey implements the KeyLoader interface and sets the value to the Key field.
type RecordUpdater ¶
RecordUpdater is a callback function for record updates. Returning a non-nil error aborts the transaction.
type Store ¶
type Store struct { Key string `datastore:"-"` Name string Tags []string OwnerID string // Timestamps keeps track of creation and modification times and stores a randomly // generated UUID to maintain consistency. Timestamps Timestamps }
Store represents a Open Saves store in the metadata database. See the Open Saves API definition for details.
func NewStoreFromProto ¶
NewStoreFromProto creates a new Store instance from a proto. Passing nil returns a zero-initialized Store.
func (*Store) Load ¶
Load implements the Datastore PropertyLoadSaver interface and converts Datstore properties to the Properties field.
func (*Store) LoadKey ¶
LoadKey implements the KeyLoader interface and sets the value to the Key field.
type Timestamps ¶
type Timestamps struct { // CreatedAt is the timestamp of the record creation time // Automatically set by MetaDB CreatedAt time.Time // UpdatedAt is the timestamp of the last modification time // Automatically set and managed by MetaDB UpdatedAt time.Time // Signature is a UUID that is randomly created each time the record is updated // Automatically set and managed by MetaDB Signature uuid.UUID `datastore:"-"` }
Timestamps keeps keeps when each record is created or updated as well as a randomly generated UUID to keep consistency under concurrent writes. It should be embedded to metadata entities such as Record and Store.
func (*Timestamps) Load ¶
func (t *Timestamps) Load(ps []datastore.Property) error
Load implements the Datastore PropertyLoadSaver interface and converts Datastore properties to corresponding struct fields.
func (*Timestamps) NewTimestamps ¶
func (t *Timestamps) NewTimestamps(d time.Duration)
NewTimestamps sets CreatedAt and UpdatedAt to time.Now() and Signature to uuid.New().
func (*Timestamps) Save ¶
func (t *Timestamps) Save() ([]datastore.Property, error)
Save implements the Datastore PropertyLoadSaver interface and converts the properties field in the struct to separate Datastore properties.
func (*Timestamps) UpdateTimestamps ¶
func (t *Timestamps) UpdateTimestamps(d time.Duration)
UpdateTimestamps updates the UpdatedAt and Signature fields with time.Now() and uuid.New().