func ExampleQuery() { ctx := context.Background() client, err := datastore.NewClient(ctx, "project-id") if err != nil { log.Fatal(err) } // Count the number of the post entities. q := datastore.NewQuery("Post") n, err := client.Count(ctx, q) if err != nil { log.Fatal(err) } log.Println("There are %d posts.", n) // List the posts published since yesterday. yesterday := time.Now().Add(-24 * time.Hour) q = datastore.NewQuery("Post").Filter("PublishedAt >", yesterday) it := client.Run(ctx, q) // Use the iterator. _ = it // Order the posts by the number of comments they have recieved. datastore.NewQuery("Post").Order("-Comments") // Start listing from an offset and limit the results. datastore.NewQuery("Post").Offset(20).Limit(10) }
func ExampleQuery_kindless() { var lastSeenKey *datastore.Key // [START kindless_query] query := datastore.NewQuery("").Filter("__key__ >", lastSeenKey) // [END kindless_query] _ = query // Use client.Run or client.GetAll to execute the query. }
func (ds *Datastore) GetEntity(t, subject string) (reply []x.Instruction, rerr error) { skey := datastore.NewKey(ds.ctx, t+"Entity", subject, 0, nil) log.Infof("skey: %+v", skey) dkeys, rerr := datastore.NewQuery(t+"Instruction").Ancestor(skey).GetAll(ds.ctx, &reply) log.Debugf("Got num keys: %+v", len(dkeys)) return }
func (loc *INodeDBSSLocator) tryLocateOnce(history int) (string, error) { start := time.Now() cli, err := loc.cfg.getClient(context.TODO()) if err != nil { return "", err } dstx, err := cli.NewTransaction(context.TODO(), datastore.Serializable) if err != nil { return "", err } q := datastore.NewQuery(kindINodeDBSS).Ancestor(loc.rootKey).Order("-TxID").Offset(history).Limit(1).Transaction(dstx) it := cli.Run(context.TODO(), q) var e sslocentry if _, err := it.Next(&e); err != nil { dstx.Rollback() if err == datastore.Done { return "", EEMPTY } return "", err } if _, err := dstx.Commit(); err != nil { return "", err } logger.Infof(sslog, "LocateSnapshot(%d) took %s. Found entry: %+v", history, time.Since(start), e) return e.BlobPath, nil }
func query() (key *AuthorizedKey, username string, err error) { ctx, err := connect() if err != nil { return } rawFingerprint, err := decodeFingerprint(fingerprint) if err != nil { return } key = new(AuthorizedKey) it := datastore.NewQuery(keyKind).Filter("Fingerprint =", rawFingerprint).Limit(1).Run(ctx) _, err = it.Next(key) if err != nil { return } var coder Coder err = datastore.Get(ctx, key.Coder, &coder) if err != nil { return } username = coder.Nickname return }
func ExampleIterator_Cursor() { ctx := context.Background() client, _ := datastore.NewClient(ctx, "my-proj") cursorStr := "" // [START cursor_paging] const pageSize = 5 query := datastore.NewQuery("Tasks").Limit(pageSize) if cursorStr != "" { cursor, err := datastore.DecodeCursor(cursorStr) if err != nil { log.Fatalf("Bad cursor %q: %v", cursorStr, err) } query = query.Start(cursor) } // Read the tasks. var tasks []Task var task Task it := client.Run(ctx, query) _, err := it.Next(&task) for err == nil { tasks = append(tasks, task) _, err = it.Next(&task) } if err != datastore.Done { log.Fatalf("Failed fetching results: %v", err) } // Get the cursor for the next page of results. nextCursor, err := it.Cursor() // [END cursor_paging] _ = err // Check the error. _ = nextCursor // Use nextCursor.String as the next page's token. }
func ExampleQuery_keyFilter() { ctx := context.Background() // [START key_filter] key := datastore.NewKey(ctx, "Task", "someTask", 0, nil) query := datastore.NewQuery("Task").Filter("__key__ >", key) // [END key_filter] _ = query // Use client.Run or client.GetAll to execute the query. }
func ExampleQuery_invalidInequalitySortA() { // [START inequality_sort_invalid_not_same] query := datastore.NewQuery("Task"). Filter("Priority >", 3). Order("Created") // [END inequality_sort_invalid_not_same] _ = query // The query is invalid. }
func ExampleQuery_Filter_arrayEquality() { // [START array_value_equality] query := datastore.NewQuery("Task"). Filter("Tag =", "fun"). Filter("Tag =", "programming") // [END array_value_equality] _ = query // Use client.Run or client.GetAll to execute the query. }
func ExampleQuery_Filter_inequality() { // [START inequality_range] query := datastore.NewQuery("Task"). Filter("Created >", time.Date(1990, 1, 1, 0, 0, 0, 0, time.UTC)). Filter("Created <", time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)) // [END inequality_range] _ = query // Use client.Run or client.GetAll to execute the query. }
func ExampleQuery_EventualConsistency() { ctx := context.Background() // [START eventual_consistent_query] ancestor := datastore.NewKey(ctx, "TaskList", "default", 0, nil) query := datastore.NewQuery("Task").Ancestor(ancestor).EventualConsistency() // [END eventual_consistent_query] _ = query // Use client.Run or client.GetAll to execute the query. }
func ExampleQuery_Filter_invalidInequality() { // [START inequality_invalid] query := datastore.NewQuery("Task"). Filter("Created >", time.Date(1990, 1, 1, 0, 0, 0, 0, time.UTC)). Filter("Priority >", 3) // [END inequality_invalid] _ = query // The query is invalid. }
func ExampleQuery_Filter_arrayInequality() { // [START array_value_inequality_range] query := datastore.NewQuery("Task"). Filter("Tag >", "learn"). Filter("Tag <", "math") // [END array_value_inequality_range] _ = query // Use client.Run or client.GetAll to execute the query. }
func ExampleQuery_inequalitySort() { // [START inequality_sort] query := datastore.NewQuery("Task"). Filter("Priority >", 3). Order("Priority"). Order("Created") // [END inequality_sort] _ = query // Use client.Run or client.GetAll to execute the query. }
func ExampleQuery_Filter_mixed() { // [START equal_and_inequality_range] query := datastore.NewQuery("Task"). Filter("Priority =", 4). Filter("Done =", false). Filter("Created >", time.Date(1990, 1, 1, 0, 0, 0, 0, time.UTC)). Filter("Created <", time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)) // [END equal_and_inequality_range] _ = query // Use client.Run or client.GetAll to execute the query. }
func queryVisits(ctx context.Context, limit int64) ([]*visit, error) { // Print out previous visits. q := datastore.NewQuery("Visit"). Order("-Timestamp"). Limit(10) visits := make([]*visit, 0) _, err := datastoreClient.GetAll(ctx, q, &visits) return visits, err }
func (ds *Datastore) IsNew(t, id string) bool { dkey := datastore.NewKey(ds.ctx, t+"Entity", id, 0, nil) keys, err := datastore.NewQuery(t+"Instruction").Ancestor(dkey). Limit(1).KeysOnly().GetAll(ds.ctx, nil) if err != nil { return false } if len(keys) > 0 { return false } return true }
func ExampleQuery_KeysOnly() { ctx := context.Background() client, _ := datastore.NewClient(ctx, "my-proj") // [START keys_only_query] query := datastore.NewQuery("Task").KeysOnly() // [END keys_only_query] // [START run_keys_only_query] keys, err := client.GetAll(ctx, query, nil) // [END run_keys_only_query] _ = err // Make sure you check err. _ = keys // Keys contains keys for all stored tasks. }
func ExampleQuery_Distinct() { // [START distinct_query] query := datastore.NewQuery("Task"). Project("Priority", "PercentComplete"). Distinct(). Order("Type").Order("Priority") // [END distinct_query] _ = query // Use client.Run or client.GetAll to execute the query. // [START distinct_on_query] // DISTINCT ON not supported in Go API // [END distinct_on_query] }
func example2() { // [START high_replication_data_definition_code] g := Greeting{ /* ... */ } key := datastore.NewIncompleteKey(ctx, "Greeting", guestbookKey(ctx)) // [END high_replication_data_definition_code] // [START high_replication_query_code] q := datastore.NewQuery("Greeting").Ancestor(guestbookKey(ctx)).Order("-Date").Limit(10) // [END high_replication_query_code] _ = g _ = key _ = q }
func example() { // [START master_slave_data_definition_code] g := Greeting{ /* ... */ } key := datastore.NewIncompleteKey(ctx, "Greeting", nil) // [END master_slave_data_definition_code] // [START master_slave_query_code] q := datastore.NewQuery("Greeting").Order("-Date").Limit(10) // [END master_slave_query_code] _ = g _ = key _ = q }
func (ds *Datastore) GetEntity(subject string) (reply []x.Instruction, rerr error) { skey := datastore.NewKey(ds.ctx, ds.tablePrefix+"Entity", subject, 0, nil) log.Infof("skey: %+v", skey) client, err := datastore.NewClient(ds.ctx, ds.projectId) if err != nil { x.LogErr(log, err).Error("While creating client") return reply, err } var dkeys []*datastore.Key q := datastore.NewQuery(ds.tablePrefix + "Instruction").Ancestor(skey) dkeys, rerr = client.GetAll(ds.ctx, q, &reply) log.Debugf("Got num keys: %+v", len(dkeys)) return }
func Example_metadataKinds() { ctx := context.Background() client, _ := datastore.NewClient(ctx, "my-proj") // [START kind_run_query] query := datastore.NewQuery("__kind__").KeysOnly() keys, err := client.GetAll(ctx, query, nil) if err != nil { log.Fatalf("client.GetAll: %v", err) } kinds := make([]string, 0, len(keys)) for _, k := range keys { kinds = append(kinds, k.Name()) } // [END kind_run_query] }
// [START retrieve_entities] // ListTasks returns all the tasks in ascending order of creation time. func ListTasks(ctx context.Context, client *datastore.Client) ([]*Task, error) { var tasks []*Task // Create a query to fetch all queries, ordered by "created". query := datastore.NewQuery("Task").Order("created") keys, err := client.GetAll(ctx, query, &tasks) if err != nil { return nil, err } // Set the id field on each Task from the corresponding key. for i, key := range keys { tasks[i].id = key.ID() } return tasks, nil }
func Example_metadataPropertiesForKind() { ctx := context.Background() client, _ := datastore.NewClient(ctx, "my-proj") // [START property_by_kind_run_query] kindKey := datastore.NewKey(ctx, "__kind__", "Task", 0, nil) query := datastore.NewQuery("__property__").Ancestor(kindKey) type Prop struct { Repr []string `datastore:"property_representation"` } var props []Prop keys, err := client.GetAll(ctx, query, &props) // [END property_by_kind_run_query] _ = err // Check error. _ = keys // Use keys to find property names, and props for their representations. }
func ExampleTransaction_runQuery() { ctx := context.Background() client, _ := datastore.NewClient(ctx, "my-proj") // [START transactional_single_entity_group_read_only] tx, err := client.NewTransaction(ctx) if err != nil { log.Fatalf("client.NewTransaction: %v", err) } defer tx.Rollback() // Transaction only used for read. ancestor := datastore.NewKey(ctx, "TaskList", "default", 0, nil) query := datastore.NewQuery("Task").Ancestor(ancestor).Transaction(tx) var tasks []Task _, err = client.GetAll(ctx, query, &tasks) // [END transactional_single_entity_group_read_only] _ = err // Check error. }
// ListBooks returns a list of books, ordered by title. func (db *datastoreDB) ListBooks() ([]*Book, error) { ctx := context.Background() books := make([]*Book, 0) q := datastore.NewQuery("Book"). Order("Title") keys, err := db.client.GetAll(ctx, q, &books) if err != nil { return nil, fmt.Errorf("datastoredb: could not list books: %v", err) } for i, k := range keys { books[i].ID = k.ID() } return books, nil }
func Example_metadataProperties() { ctx := context.Background() client, _ := datastore.NewClient(ctx, "my-proj") // [START property_run_query] query := datastore.NewQuery("__property__").KeysOnly() keys, err := client.GetAll(ctx, query, nil) if err != nil { log.Fatalf("client.GetAll: %v", err) } props := make(map[string][]string) // Map from kind to slice of properties. for _, k := range keys { prop := k.Name() kind := k.Parent().Name() props[kind] = append(props[kind], prop) } // [END property_run_query] }
func (fe *FeServiceImpl) isValidNewConfig(ctx context.Context, dsClient *datastore.Client, config *api.BiosphereCreationConfig) (bool, error) { if config == nil { return false, nil } if config.Name == "" || config.Nx <= 0 || config.Ny <= 0 { return false, nil } // Name must be unique. qSameName := datastore.NewQuery("BiosphereMeta").Filter("Name =", config.Name) numSameName, err := dsClient.Count(ctx, qSameName) if err != nil { return false, err } if numSameName > 0 { return false, nil } return true, nil }
func (loc *INodeDBSSLocator) DeleteAll() ([]string, error) { start := time.Now() cli, err := loc.cfg.getClient(context.TODO()) if err != nil { return nil, err } dstx, err := cli.NewTransaction(context.TODO(), datastore.Serializable) if err != nil { return nil, err } keys := make([]*datastore.Key, 0) blobpaths := make([]string, 0) q := datastore.NewQuery(kindINodeDBSS).Ancestor(loc.rootKey).Transaction(dstx) it := cli.Run(context.TODO(), q) for { var e sslocentry k, err := it.Next(&e) if err != nil { if err == datastore.Done { break } dstx.Rollback() return nil, err } keys = append(keys, k) blobpaths = append(blobpaths, e.BlobPath) } logger.Debugf(sslog, "keys to delete: %v", keys) if err := dstx.DeleteMulti(keys); err != nil { dstx.Rollback() return nil, err } if _, err := dstx.Commit(); err != nil { return nil, err } logger.Infof(sslog, "DeleteAll() deleted %d entries. Took %s", len(keys), time.Since(start)) return blobpaths, nil }