Package datastore
Overview ▹
Index ▹
Variables
var ( // ErrInvalidEntityType is returned when functions like Get or Next are // passed a dst or src argument of invalid type. ErrInvalidEntityType = errors.New("datastore: invalid entity type") // ErrInvalidKey is returned when an invalid key is presented. ErrInvalidKey = errors.New("datastore: invalid key") // ErrNoSuchEntity is returned when no entity was found for a given key. ErrNoSuchEntity = errors.New("datastore: no such entity") )
Done is returned when a query iteration has completed.
var Done = errors.New("datastore: query has no more results")
ErrConcurrentTransaction is returned when a transaction is rolled back due to a conflict with a concurrent transaction.
var ErrConcurrentTransaction = errors.New("datastore: concurrent transaction")
func AllocateIDs ¶
func AllocateIDs(c context.Context, kind string, parent *Key, n int) (low, high int64, err error)
AllocateIDs returns a range of n integer IDs with the given kind and parent combination. kind cannot be empty; parent may be nil. The IDs in the range returned will not be used by the datastore's automatic ID sequence generator and may be used with NewKey without conflict.
The range is inclusive at the low end and exclusive at the high end. In other words, valid intIDs x satisfy low <= x && x < high.
If no error is returned, low + n == high.
func Delete ¶
func Delete(c context.Context, key *Key) error
Delete deletes the entity for the given key.
func DeleteMulti ¶
func DeleteMulti(c context.Context, key []*Key) error
DeleteMulti is a batch version of Delete.
func Get ¶
func Get(c context.Context, key *Key, dst interface{}) error
Get loads the entity stored for k into dst, which must be a struct pointer or implement PropertyLoadSaver. If there is no such entity for the key, Get returns ErrNoSuchEntity.
The values of dst's unmatched struct fields are not modified, and matching slice-typed fields are not reset before appending to them. In particular, it is recommended to pass a pointer to a zero valued struct on each Get call.
ErrFieldMismatch is returned when a field is to be loaded into a different type than the one it was stored from, or when a field is missing or unexported in the destination struct. ErrFieldMismatch is only returned if dst is a struct pointer.
func GetMulti ¶
func GetMulti(c context.Context, key []*Key, dst interface{}) error
GetMulti is a batch version of Get.
dst must be a []S, []*S, []I or []P, for some struct type S, some interface type I, or some non-interface non-pointer type P such that P or *P implements PropertyLoadSaver. If an []I, each element must be a valid dst for Get: it must be a struct pointer or implement PropertyLoadSaver.
As a special case, PropertyList is an invalid type for dst, even though a PropertyList is a slice of structs. It is treated as invalid to avoid being mistakenly passed when []PropertyList was intended.
func KindProperties ¶
func KindProperties(ctx context.Context, kind string) (map[string][]string, error)
KindProperties returns all the indexed properties for the given kind. The properties are returned as a map of property names to a slice of the representation types. The representation types for the supported Go property types are:
"INT64": signed integers and time.Time "DOUBLE": float32 and float64 "BOOLEAN": bool "STRING": string, []byte and ByteString "POINT": appengine.GeoPoint "REFERENCE": *Key "USER": (not used in the Go runtime)
func Kinds ¶
func Kinds(ctx context.Context) ([]string, error)
Kinds returns the names of all the kinds in the current namespace.
func LoadStruct ¶
func LoadStruct(dst interface{}, p []Property) error
LoadStruct loads the properties from p to dst. dst must be a struct pointer.
func Namespaces ¶
func Namespaces(ctx context.Context) ([]string, error)
Namespaces returns all the datastore namespaces.
func PutMulti ¶
func PutMulti(c context.Context, key []*Key, src interface{}) ([]*Key, error)
PutMulti is a batch version of Put.
src must satisfy the same conditions as the dst argument to GetMulti.
func RunInTransaction ¶
func RunInTransaction(c context.Context, f func(tc context.Context) error, opts *TransactionOptions) error
RunInTransaction runs f in a transaction. It calls f with a transaction context tc that f should use for all App Engine operations.
If f returns nil, RunInTransaction attempts to commit the transaction, returning nil if it succeeds. If the commit fails due to a conflicting transaction, RunInTransaction retries f, each time with a new transaction context. It gives up and returns ErrConcurrentTransaction after three failed attempts. The number of attempts can be configured by specifying TransactionOptions.Attempts.
If f returns non-nil, then any datastore changes will not be applied and RunInTransaction returns that same error. The function f is not retried.
Note that when f returns, the transaction is not yet committed. Calling code must be careful not to assume that any of f's changes have been committed until RunInTransaction returns nil.
Since f may be called multiple times, f should usually be idempotent. datastore.Get is not idempotent when unmarshaling slice fields.
Nested transactions are not supported; c may not be a transaction context.
func SaveStruct ¶
func SaveStruct(src interface{}) ([]Property, error)
SaveStruct returns the properties from src as a slice of Properties. src must be a struct pointer.
type ByteString ¶
ByteString is a short byte slice (up to 1500 bytes) that can be indexed.
type ByteString []byte
type Cursor ¶
Cursor is an iterator's position. It can be converted to and from an opaque string. A cursor can be used from different HTTP requests, but only with a query with the same kind, ancestor, filter and order constraints.
type Cursor struct {
// contains filtered or unexported fields
}
func DecodeCursor ¶
func DecodeCursor(s string) (Cursor, error)
Decode decodes a cursor from its base-64 string representation.
func (Cursor) String ¶
func (c Cursor) String() string
String returns a base-64 string representation of a cursor.
type Entity ¶
An Entity is the value type for a nested struct. This type is only used for a Property's Value.
type Entity struct { Key *Key Properties []Property }
type ErrFieldMismatch ¶
ErrFieldMismatch is returned when a field is to be loaded into a different type than the one it was stored from, or when a field is missing or unexported in the destination struct. StructType is the type of the struct pointed to by the destination argument passed to Get or to Iterator.Next.
type ErrFieldMismatch struct { StructType reflect.Type FieldName string Reason string }
func (*ErrFieldMismatch) Error ¶
func (e *ErrFieldMismatch) Error() string
type Iterator ¶
Iterator is the result of running a query.
type Iterator struct {
// contains filtered or unexported fields
}
func (*Iterator) Cursor ¶
func (t *Iterator) Cursor() (Cursor, error)
Cursor returns a cursor for the iterator's current location.
func (*Iterator) Next ¶
func (t *Iterator) Next(dst interface{}) (*Key, error)
Next returns the key of the next result. When there are no more results, Done is returned as the error.
If the query is not keys only and dst is non-nil, it also loads the entity stored for that key into the struct pointer or PropertyLoadSaver dst, with the same semantics and possible errors as for the Get function.
type Key ¶
Key represents the datastore key for a stored entity, and is immutable.
type Key struct {
// contains filtered or unexported fields
}
func DecodeKey ¶
func DecodeKey(encoded string) (*Key, error)
DecodeKey decodes a key from the opaque representation returned by Encode.
func NewIncompleteKey ¶
func NewIncompleteKey(c context.Context, kind string, parent *Key) *Key
NewIncompleteKey creates a new incomplete key. kind cannot be empty.
func NewKey ¶
func NewKey(c context.Context, kind, stringID string, intID int64, parent *Key) *Key
NewKey creates a new key. kind cannot be empty. Either one or both of stringID and intID must be zero. If both are zero, the key returned is incomplete. parent must either be a complete key or nil.
func Put ¶
func Put(c context.Context, key *Key, src interface{}) (*Key, error)
Put saves the entity src into the datastore with key k. src must be a struct pointer or implement PropertyLoadSaver; if a struct pointer then any unexported fields of that struct will be skipped. If k is an incomplete key, the returned key will be a unique key generated by the datastore.
func (*Key) AppID ¶
func (k *Key) AppID() string
AppID returns the key's application ID.
func (*Key) Encode ¶
func (k *Key) Encode() string
Encode returns an opaque representation of the key suitable for use in HTML and URLs. This is compatible with the Python and Java runtimes.
func (*Key) Equal ¶
func (k *Key) Equal(o *Key) bool
Equal returns whether two keys are equal.
func (*Key) GobDecode ¶
func (k *Key) GobDecode(buf []byte) error
func (*Key) GobEncode ¶
func (k *Key) GobEncode() ([]byte, error)
func (*Key) Incomplete ¶
func (k *Key) Incomplete() bool
Incomplete returns whether the key does not refer to a stored entity. In particular, whether the key has a zero StringID and a zero IntID.
func (*Key) IntID ¶
func (k *Key) IntID() int64
IntID returns the key's integer ID, which may be 0.
func (*Key) Kind ¶
func (k *Key) Kind() string
Kind returns the key's kind (also known as entity type).
func (*Key) MarshalJSON ¶
func (k *Key) MarshalJSON() ([]byte, error)
func (*Key) Namespace ¶
func (k *Key) Namespace() string
Namespace returns the key's namespace.
func (*Key) Parent ¶
func (k *Key) Parent() *Key
Parent returns the key's parent key, which may be nil.
func (*Key) String ¶
func (k *Key) String() string
String returns a string representation of the key.
func (*Key) StringID ¶
func (k *Key) StringID() string
StringID returns the key's string ID (also known as an entity name or key name), which may be "".
func (*Key) UnmarshalJSON ¶
func (k *Key) UnmarshalJSON(buf []byte) error
type Property ¶
Property is a name/value pair plus some metadata. A datastore entity's contents are loaded and saved as a sequence of Properties. An entity can have multiple Properties with the same name, provided that p.Multiple is true on all of that entity's Properties with that name.
type Property struct { // Name is the property name. Name string // Value is the property value. The valid types are: // - int64 // - bool // - string // - float64 // - ByteString // - *Key // - time.Time // - appengine.BlobKey // - appengine.GeoPoint // - []byte (up to 1 megabyte in length) // - *Entity (representing a nested struct) // This set is smaller than the set of valid struct field types that the // datastore can load and save. A Property Value cannot be a slice (apart // from []byte); use multiple Properties instead. Also, a Value's type // must be explicitly on the list above; it is not sufficient for the // underlying type to be on that list. For example, a Value of "type // myInt64 int64" is invalid. Smaller-width integers and floats are also // invalid. Again, this is more restrictive than the set of valid struct // field types. // // A Value will have an opaque type when loading entities from an index, // such as via a projection query. Load entities into a struct instead // of a PropertyLoadSaver when using a projection query. // // A Value may also be the nil interface value; this is equivalent to // Python's None but not directly representable by a Go struct. Loading // a nil-valued property into a struct will set that field to the zero // value. Value interface{} // NoIndex is whether the datastore cannot index this property. NoIndex bool // Multiple is whether the entity can have multiple properties with // the same name. Even if a particular instance only has one property with // a certain name, Multiple should be true if a struct would best represent // it as a field of type []T instead of type T. Multiple bool }
type PropertyList ¶
PropertyList converts a []Property to implement PropertyLoadSaver.
type PropertyList []Property
func (*PropertyList) Load ¶
func (l *PropertyList) Load(p []Property) error
Load loads all of the provided properties into l. It does not first reset *l to an empty slice.
func (*PropertyList) Save ¶
func (l *PropertyList) Save() ([]Property, error)
Save saves all of l's properties as a slice or Properties.
type PropertyLoadSaver ¶
PropertyLoadSaver can be converted from and to a slice of Properties.
type PropertyLoadSaver interface { Load([]Property) error Save() ([]Property, error) }
type Query ¶
Query represents a datastore query.
type Query struct {
// contains filtered or unexported fields
}
func NewQuery ¶
func NewQuery(kind string) *Query
NewQuery creates a new Query for a specific entity kind.
An empty kind means to return all entities, including entities created and managed by other App Engine features, and is called a kindless query. Kindless queries cannot include filters or sort orders on property values.
func (*Query) Ancestor ¶
func (q *Query) Ancestor(ancestor *Key) *Query
Ancestor returns a derivative query with an ancestor filter. The ancestor should not be nil.
func (*Query) Count ¶
func (q *Query) Count(c context.Context) (int, error)
Count returns the number of results for the query.
The running time and number of API calls made by Count scale linearly with the sum of the query's offset and limit. Unless the result count is expected to be small, it is best to specify a limit; otherwise Count will continue until it finishes counting or the provided context expires.
func (*Query) Distinct ¶
func (q *Query) Distinct() *Query
Distinct returns a derivative query that yields de-duplicated entities with respect to the set of projected fields. It is only used for projection queries.
func (*Query) End ¶
func (q *Query) End(c Cursor) *Query
End returns a derivative query with the given end point.
func (*Query) EventualConsistency ¶
func (q *Query) EventualConsistency() *Query
EventualConsistency returns a derivative query that returns eventually consistent results. It only has an effect on ancestor queries.
func (*Query) Filter ¶
func (q *Query) Filter(filterStr string, value interface{}) *Query
Filter returns a derivative query with a field-based filter. The filterStr argument must be a field name followed by optional space, followed by an operator, one of ">", "<", ">=", "<=", or "=". Fields are compared against the provided value using the operator. Multiple filters are AND'ed together.
func (*Query) GetAll ¶
func (q *Query) GetAll(c context.Context, dst interface{}) ([]*Key, error)
GetAll runs the query in the given context and returns all keys that match that query, as well as appending the values to dst.
dst must have type *[]S or *[]*S or *[]P, for some struct type S or some non- interface, non-pointer type P such that P or *P implements PropertyLoadSaver.
As a special case, *PropertyList is an invalid type for dst, even though a PropertyList is a slice of structs. It is treated as invalid to avoid being mistakenly passed when *[]PropertyList was intended.
The keys returned by GetAll will be in a 1-1 correspondence with the entities added to dst.
If q is a “keys-only” query, GetAll ignores dst and only returns the keys.
The running time and number of API calls made by GetAll scale linearly with with the sum of the query's offset and limit. Unless the result count is expected to be small, it is best to specify a limit; otherwise GetAll will continue until it finishes collecting results or the provided context expires.
func (*Query) KeysOnly ¶
func (q *Query) KeysOnly() *Query
KeysOnly returns a derivative query that yields only keys, not keys and entities. It cannot be used with projection queries.
func (*Query) Limit ¶
func (q *Query) Limit(limit int) *Query
Limit returns a derivative query that has a limit on the number of results returned. A negative value means unlimited.
func (*Query) Offset ¶
func (q *Query) Offset(offset int) *Query
Offset returns a derivative query that has an offset of how many keys to skip over before returning results. A negative value is invalid.
func (*Query) Order ¶
func (q *Query) Order(fieldName string) *Query
Order returns a derivative query with a field-based sort order. Orders are applied in the order they are added. The default order is ascending; to sort in descending order prefix the fieldName with a minus sign (-).
func (*Query) Project ¶
func (q *Query) Project(fieldNames ...string) *Query
Project returns a derivative query that yields only the given fields. It cannot be used with KeysOnly.
func (*Query) Run ¶
func (q *Query) Run(c context.Context) *Iterator
Run runs the query in the given context.
func (*Query) Start ¶
func (q *Query) Start(c Cursor) *Query
Start returns a derivative query with the given start point.
type TransactionOptions ¶
TransactionOptions are the options for running a transaction.
type TransactionOptions struct { // XG is whether the transaction can cross multiple entity groups. In // comparison, a single group transaction is one where all datastore keys // used have the same root key. Note that cross group transactions do not // have the same behavior as single group transactions. In particular, it // is much more likely to see partially applied transactions in different // entity groups, in global queries. // It is valid to set XG to true even if the transaction is within a // single entity group. XG bool // Attempts controls the number of retries to perform when commits fail // due to a conflicting transaction. If omitted, it defaults to 3. Attempts int }