go.mercari.io/datastore@v1.8.2/interfaces.go (about)

     1  package datastore
     2  
     3  import (
     4  	"context"
     5  )
     6  
     7  // FromContext provides default ClientGenerator.
     8  // this variable will be injected by go.mercari.io/datastore/aedatastore or go.mercari.io/datastore/clouddatastore package's init function.
     9  //
    10  // Deprecated: use aedatastore.FromContext or clouddatastore.FromContext instead.
    11  var FromContext ClientGenerator
    12  
    13  // ClientGenerator represents the type of function for generating Client.
    14  //
    15  // Deprecated:
    16  type ClientGenerator func(ctx context.Context, opts ...ClientOption) (Client, error)
    17  
    18  // Client is a client for reading and writing data in a datastore dataset.
    19  type Client interface {
    20  	// Get loads the entity stored for key into dst, which must be a struct pointer or implement PropertyLoadSaver.
    21  	//
    22  	// If there is no such entity for the key, Get returns ErrNoSuchEntity.
    23  	// The values of dst's unmatched struct fields are not modified, and matching slice-typed fields are not reset before appending to them.
    24  	// In particular, it is recommended to pass a pointer to a zero valued struct on each Get call.
    25  	//
    26  	// If you set false to SuppressErrFieldMismatch variable, act like the original Datastore.
    27  	// 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.
    28  	Get(ctx context.Context, key Key, dst interface{}) error
    29  
    30  	// GetMulti is a batch version of Get.
    31  	//
    32  	// 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.
    33  	// If an []I, each element must be a valid dst for Get: it must be a struct pointer or implement PropertyLoadSaver.
    34  	//
    35  	// As a special case, PropertyList is an invalid type for dst, even though a PropertyList is a slice of structs.
    36  	// It is treated as invalid to avoid being mistakenly passed when []PropertyList was intended.
    37  	GetMulti(ctx context.Context, keys []Key, dst interface{}) error
    38  
    39  	// Put saves the entity src into the datastore with key k.
    40  	// src must be a struct pointer or implement PropertyLoadSaver; if a struct pointer then any unexported fields of that struct will be skipped.
    41  	// If k is an incomplete key, the returned key will be a unique key generated by the datastore.
    42  	Put(ctx context.Context, key Key, src interface{}) (Key, error)
    43  
    44  	// PutMulti is a batch version of Put.
    45  	//
    46  	// src must satisfy the same conditions as the dst argument to GetMulti.
    47  	PutMulti(ctx context.Context, keys []Key, src interface{}) ([]Key, error)
    48  
    49  	// Delete deletes the entity for the given key.
    50  	Delete(ctx context.Context, key Key) error
    51  
    52  	// DeleteMulti is a batch version of Delete.
    53  	DeleteMulti(ctx context.Context, keys []Key) error
    54  
    55  	// NewTransaction starts a new transaction.
    56  	NewTransaction(ctx context.Context) (Transaction, error)
    57  
    58  	// RunInTransaction runs f in a transaction. f is invoked with a Transaction that f should use for all the transaction's datastore operations.
    59  	//
    60  	// f must not call Commit or Rollback on the provided Transaction.
    61  	//
    62  	// If f returns nil, RunInTransaction commits the transaction, returning the Commit and a nil error if it succeeds.
    63  	// If the commit fails due to a conflicting transaction, RunInTransaction gives up and returns ErrConcurrentTransaction immediately.
    64  	// If you want to retry operation, You have to retry by yourself.
    65  	//
    66  	// If f returns non-nil, then the transaction will be rolled back and RunInTransaction will return the same error.
    67  	//
    68  	// Note that when f returns, the transaction is not committed. Calling code must not assume that any of f's changes have been committed until RunInTransaction returns nil.
    69  	RunInTransaction(ctx context.Context, f func(tx Transaction) error) (Commit, error)
    70  
    71  	// Run runs the given query in the given context.
    72  	Run(ctx context.Context, q Query) Iterator
    73  
    74  	// AllocateIDs accepts a slice of incomplete keys and returns a slice of complete keys that are guaranteed to be valid in the datastore.
    75  	AllocateIDs(ctx context.Context, keys []Key) ([]Key, error)
    76  
    77  	// Count returns the number of results for the given query.
    78  	//
    79  	// The running time and number of API calls made by Count scale linearly with with the sum of the query's offset and limit.
    80  	// 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.
    81  	Count(ctx context.Context, q Query) (int, error)
    82  
    83  	// GetAll runs the provided query in the given context and returns all keys that match that query, as well as appending the values to dst.
    84  	//
    85  	// 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.
    86  	//
    87  	// As a special case, *PropertyList is an invalid type for dst, even though a PropertyList is a slice of structs.
    88  	// It is treated as invalid to avoid being mistakenly passed when *[]PropertyList was intended.
    89  	//
    90  	// The keys returned by GetAll will be in a 1-1 correspondence with the entities added to dst.
    91  	//
    92  	// If q is a “keys-only” query, GetAll ignores dst and only returns the keys.
    93  	//
    94  	// The running time and number of API calls made by GetAll scale linearly with with the sum of the query's offset and limit.
    95  	// 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.
    96  	GetAll(ctx context.Context, q Query, dst interface{}) ([]Key, error)
    97  
    98  	// IncompleteKey creates a new incomplete key.
    99  	// The supplied kind cannot be empty.
   100  	// The namespace of the new key is empty.
   101  	IncompleteKey(kind string, parent Key) Key
   102  
   103  	// NameKey creates a new key with a name.
   104  	// The supplied kind cannot be empty.
   105  	// The supplied parent must either be a complete key or nil.
   106  	// The namespace of the new key is empty.
   107  	NameKey(kind, name string, parent Key) Key
   108  
   109  	// IDKey creates a new key with an ID.
   110  	// The supplied kind cannot be empty.
   111  	// The supplied parent must either be a complete key or nil.
   112  	// The namespace of the new key is empty.
   113  	IDKey(kind string, id int64, parent Key) Key
   114  
   115  	// NewQuery creates a new Query for a specific entity kind.
   116  	//
   117  	// An empty kind means to return all entities, including entities created and managed by other App Engine features, and is called a kindless query.
   118  	// Kindless queries cannot include filters or sort orders on property values.
   119  	NewQuery(kind string) Query
   120  
   121  	// Close closes the Client.
   122  	Close() error
   123  
   124  	// DecodeKey decodes a key from the opaque representation returned by Encode.
   125  	DecodeKey(encoded string) (Key, error)
   126  
   127  	// DecodeCursor from its base-64 string representation.
   128  	DecodeCursor(s string) (Cursor, error)
   129  
   130  	// Batch creates batch mode objects.
   131  	Batch() *Batch
   132  	// AppendMiddleware to client.
   133  	// Middleware will apply First-In First-Apply
   134  	AppendMiddleware(middleware Middleware)
   135  	// RemoveMiddleware from client.
   136  	RemoveMiddleware(middleware Middleware) bool
   137  	// Context returns this client's context.
   138  	Context() context.Context
   139  	// SetContext to this client.
   140  	SetContext(ctx context.Context)
   141  }
   142  
   143  // Key represents the datastore key for a stored entity.
   144  type Key interface {
   145  	Kind() string
   146  	ID() int64
   147  	Name() string
   148  	ParentKey() Key
   149  	Namespace() string
   150  	SetNamespace(namespace string)
   151  
   152  	String() string
   153  	GobEncode() ([]byte, error)
   154  	GobDecode(buf []byte) error
   155  	MarshalJSON() ([]byte, error)
   156  	UnmarshalJSON(buf []byte) error
   157  	Encode() string
   158  	Equal(o Key) bool
   159  	Incomplete() bool
   160  }
   161  
   162  // PendingKey represents the key for newly-inserted entity. It can be
   163  // resolved into a Key by calling the Key method of Commit.
   164  type PendingKey interface {
   165  	StoredContext() context.Context
   166  }
   167  
   168  // Transaction represents a set of datastore operations to be committed atomically.
   169  //
   170  // Operations are enqueued by calling the Put and Delete methods on Transaction
   171  // (or their Multi-equivalents).  These operations are only committed when the
   172  // Commit method is invoked. To ensure consistency, reads must be performed by
   173  // using Transaction's Get method or by using the Transaction method when
   174  // building a query.
   175  //
   176  // A Transaction must be committed or rolled back exactly once.
   177  type Transaction interface {
   178  	// Get is the transaction-specific version of the package function Get.
   179  	// All reads performed during the transaction will come from a single consistent snapshot.
   180  	// Furthermore, if the transaction is set to a serializable isolation level,
   181  	// another transaction cannot concurrently modify the data that is read or modified by this transaction.
   182  	Get(key Key, dst interface{}) error
   183  	// GetMulti is a batch version of Get.
   184  	GetMulti(keys []Key, dst interface{}) error
   185  	// Put is the transaction-specific version of the package function Put.
   186  	//
   187  	// Put returns a PendingKey which can be resolved into a Key using the return value from a successful Commit.
   188  	// If key is an incomplete key, the returned pending key will resolve to a unique key generated by the datastore.
   189  	Put(key Key, src interface{}) (PendingKey, error)
   190  	// PutMulti is a batch version of Put. One PendingKey is returned for each element of src in the same order.
   191  	PutMulti(keys []Key, src interface{}) ([]PendingKey, error)
   192  	// Delete is the transaction-specific version of the package function Delete.
   193  	// Delete enqueues the deletion of the entity for the given key,
   194  	// to be committed atomically upon calling Commit.
   195  	Delete(key Key) error
   196  	// DeleteMulti is a batch version of Delete.
   197  	DeleteMulti(keys []Key) error
   198  
   199  	// Commit applies the enqueued operations atomically.
   200  	Commit() (Commit, error)
   201  	// Rollback abandons a pending transaction.
   202  	Rollback() error
   203  
   204  	Batch() *TransactionBatch
   205  }
   206  
   207  // Commit represents the result of a committed transaction.
   208  type Commit interface {
   209  	Key(p PendingKey) Key
   210  }
   211  
   212  // GeoPoint represents a location as latitude/longitude in degrees.
   213  type GeoPoint struct {
   214  	Lat, Lng float64
   215  }
   216  
   217  // Query represents a datastore query.
   218  type Query interface {
   219  	Ancestor(ancestor Key) Query
   220  	EventualConsistency() Query
   221  	Namespace(ns string) Query
   222  	Transaction(t Transaction) Query
   223  	Filter(filterStr string, value interface{}) Query
   224  	Order(fieldName string) Query
   225  	Project(fieldNames ...string) Query
   226  	Distinct() Query
   227  	DistinctOn(fieldNames ...string) Query
   228  	KeysOnly() Query
   229  	Limit(limit int) Query
   230  	Offset(offset int) Query
   231  	Start(c Cursor) Query
   232  	End(c Cursor) Query
   233  
   234  	Dump() *QueryDump
   235  }
   236  
   237  // Iterator is the result of running a query.
   238  type Iterator interface {
   239  	// Next returns the key of the next result. When there are no more results,
   240  	// iterator.Done is returned as the error.
   241  	//
   242  	// If the query is not keys only and dst is non-nil, it also loads the entity
   243  	// stored for that key into the struct pointer or PropertyLoadSaver dst, with
   244  	// the same semantics and possible errors as for the Get function.
   245  	Next(dst interface{}) (Key, error)
   246  	// Cursor returns a cursor for the iterator's current location.
   247  	Cursor() (Cursor, error)
   248  }
   249  
   250  // Cursor is an iterator's position. It can be converted to and from an opaque
   251  // string. A cursor can be used from different HTTP requests, but only with a
   252  // query with the same kind, ancestor, filter and order constraints.
   253  //
   254  // The zero Cursor can be used to indicate that there is no start and/or end
   255  // constraint for a query.
   256  type Cursor interface {
   257  	String() string
   258  }
   259  
   260  // PropertyTranslator is for converting the value of Property when saving and loading.
   261  type PropertyTranslator interface {
   262  	ToPropertyValue(ctx context.Context) (interface{}, error)
   263  	FromPropertyValue(ctx context.Context, p Property) (dst interface{}, err error)
   264  }
   265  
   266  // TODO ComplexPropertyTranslator e.g. ToProperties(ctx context.Context) ([]Property, error)