本文整理汇总了Golang中google/golang.org/cloud/datastore.NewKey函数的典型用法代码示例。如果您正苦于以下问题:Golang NewKey函数的具体用法?Golang NewKey怎么用?Golang NewKey使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewKey函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ExampleNewKey_withParent
func ExampleNewKey_withParent() {
ctx := context.Background()
// [START key_with_parent]
parentKey := datastore.NewKey(ctx, "TaskList", "default", 0, nil)
taskKey := datastore.NewKey(ctx, "Task", "sampleTask", 0, parentKey)
// [END key_with_parent]
_ = taskKey // Use the task key for datastore operations.
}
示例2: ExampleNewKey_withMultipleParents
func ExampleNewKey_withMultipleParents() {
ctx := context.Background()
// [START key_with_multilevel_parent]
userKey := datastore.NewKey(ctx, "User", "alice", 0, nil)
parentKey := datastore.NewKey(ctx, "TaskList", "default", 0, userKey)
taskKey := datastore.NewKey(ctx, "Task", "sampleTask", 0, parentKey)
// [END key_with_multilevel_parent]
_ = taskKey // Use the task key for datastore operations.
}
示例3: ExampleGetMulti
func ExampleGetMulti() {
ctx := Example_auth()
keys := []*datastore.Key{
datastore.NewKey(ctx, "Post", "post1", 0, nil),
datastore.NewKey(ctx, "Post", "post2", 0, nil),
datastore.NewKey(ctx, "Post", "post3", 0, nil),
}
posts := make([]Post, 3)
if err := datastore.GetMulti(ctx, keys, posts); err != nil {
log.Println(err)
}
}
示例4: pollCommits
func pollCommits(dir string) {
cmd := execGit(dir, nil, "pull", "origin")
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Error running git pull origin master in %s: %v\n%s", dir, err, out)
return
}
log.Printf("Ran git pull.")
// TODO: see if .git/refs/remotes/origin/master
// changed. (quicker than running recentCommits each time)
hashes, err := recentCommits(dir)
if err != nil {
log.Print(err)
return
}
latestHash.Lock()
latestHash.s = hashes[0]
latestHash.Unlock()
for _, commit := range hashes {
if knownCommit[commit] {
continue
}
if dsClient != nil {
ctx := context.Background()
key := datastore.NewKey(ctx, "git_commit", commit, 0, nil)
var gc GitCommit
if err := dsClient.Get(ctx, key, &gc); err == nil && gc.Emailed {
log.Printf("Already emailed about commit %v; skipping", commit)
knownCommit[commit] = true
continue
}
}
if err := emailCommit(dir, commit); err == nil {
log.Printf("Emailed commit %s", commit)
knownCommit[commit] = true
if dsClient != nil {
ctx := context.Background()
key := datastore.NewKey(ctx, "git_commit", commit, 0, nil)
_, err := dsClient.Put(ctx, key, &GitCommit{Emailed: true})
log.Printf("datastore put of git_commit(%v): %v", commit, err)
}
}
}
if githubSSHKey != "" {
if err := syncToGithub(dir, hashes[0]); err != nil {
log.Printf("Failed to push commit %v to github: %v", hashes[0], err)
}
}
}
示例5: Sync
func (txio *DBTransactionLogIO) Sync() error {
txio.mu.Lock()
batch := txio.nextbatch
txio.nextbatch = make([]inodedb.DBTransaction, 0)
txio.mu.Unlock()
if len(batch) == 0 {
return nil
}
ctx := txio.getContext()
keys := make([]*datastore.Key, 0, len(batch))
stxs := make([]*storedbtx, 0, len(batch))
for _, tx := range batch {
keys = append(keys, datastore.NewKey(ctx, kindTransaction, "", int64(tx.TxID), txio.rootKey))
stx, err := encode(txio.c, tx)
if err != nil {
return err
}
stxs = append(stxs, stx)
}
if _, err := datastore.PutMulti(ctx, keys, stxs); err != nil {
return err
}
log.Printf("Committed %d txs", len(stxs))
return nil
}
示例6: ExampleTransaction
func ExampleTransaction() {
ctx := Example_auth()
const retries = 3
// Increment a counter.
// See https://cloud.google.com/appengine/articles/sharding_counters for
// a more scalable solution.
type Counter struct {
Count int
}
key := datastore.NewKey(ctx, "counter", "CounterA", 0, nil)
for i := 0; i < retries; i++ {
tx, err := datastore.NewTransaction(ctx)
if err != nil {
break
}
var c Counter
if err := tx.Get(key, &c); err != nil && err != datastore.ErrNoSuchEntity {
break
}
c.Count++
if _, err := tx.Put(key, &c); err != nil {
break
}
// Attempt to commit the transaction. If there's a conflict, try again.
if _, err := tx.Commit(); err != datastore.ErrConcurrentTransaction {
break
}
}
}
示例7: getBiosphereTopo
func (fe *FeServiceImpl) getBiosphereTopo(ctx context.Context, biosphereId uint64) (BiosphereTopology, *api.BiosphereEnvConfig, error, *api.TimingTrace) {
trace := InitTrace("getBiosphereTopo")
fe.bsMetaCacheMutex.Lock()
defer fe.bsMetaCacheMutex.Unlock()
bsMeta, ok := fe.bsMetaCache[biosphereId]
if !ok {
authTrace := InitTrace("AuthDatastore")
client, err := fe.AuthDatastore(ctx)
if err != nil {
return nil, nil, err, trace
}
FinishTrace(authTrace, trace)
key := datastore.NewKey(ctx, "BiosphereMeta", "", int64(biosphereId), nil)
var meta BiosphereMeta
err = client.Get(ctx, key, &meta)
if err != nil {
return nil, nil, err, trace
}
fe.bsMetaCache[biosphereId] = &meta
bsMeta = &meta
log.Printf("BiosphereMeta cache entry (bsId: %d) created. Now #(cache entry)=%d", biosphereId, len(fe.bsMetaCache))
}
log.Printf("Found config of %d x %d", bsMeta.Nx, bsMeta.Ny)
envConfig := api.BiosphereEnvConfig{}
err := proto.Unmarshal(bsMeta.Env, &envConfig)
if err != nil {
return nil, nil, err, trace
}
return NewCylinderTopology(biosphereId, int(bsMeta.Nx), int(bsMeta.Ny)), &envConfig, nil, trace
}
示例8: ExampleNewKey
func ExampleNewKey() {
ctx := context.Background()
// [START named_key]
taskKey := datastore.NewKey(ctx, "Task", "sampletask", 0, nil)
// [END named_key]
_ = taskKey // Use the task key for datastore operations.
}
示例9: fetchDatastoreSnapshot
func (ck *CkServiceImpl) fetchDatastoreSnapshot(dsKey int64) *api.ChunkState {
strictCtx, _ := context.WithTimeout(context.Background(), 2500*time.Millisecond)
client, err := ck.AuthDatastore(strictCtx)
if err != nil {
log.Printf("ERROR datastore auth failed %#v", err)
return nil
}
key := datastore.NewKey(strictCtx, "PersistentChunkSnapshot", "", dsKey, nil)
snapshot := new(PersistentChunkSnapshot)
err = client.Get(strictCtx, key, snapshot)
if err != nil {
log.Printf("ERROR datastore.Get(%#v) failed %#v", key, err)
return nil
}
snapshotProto := &api.ChunkSnapshot{}
err = proto.Unmarshal(snapshot.Snapshot, snapshotProto)
if err != nil {
log.Printf("ERROR failed to unmarshal; corrupt datastore entry at key %d %#v", dsKey, err)
return nil
}
// Convert to a shard w/o any outgoing grains.
return &api.ChunkState{
Shards: []*api.ChunkShard{
&api.ChunkShard{
Dp: &api.ChunkRel{Dx: 0, Dy: 0},
Grains: snapshotProto.Grains,
},
},
}
}
示例10: GetEntity
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
}
示例11: NewDBTransactionLogIO
func NewDBTransactionLogIO(cfg *Config) *DBTransactionLogIO {
return &DBTransactionLogIO{
cfg: cfg,
rootKey: datastore.NewKey(ctxNoNamespace, kindTransaction, cfg.rootKeyStr, 0, nil),
nextbatch: make([]inodedb.DBTransaction, 0),
}
}
示例12: tryPutOnce
func (loc *INodeDBSSLocator) tryPutOnce(blobpath string, txid int64) error {
start := time.Now()
e := sslocentry{BlobPath: blobpath, TxID: txid, CreatedAt: start}
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
}
key := datastore.NewKey(ctxNoNamespace, kindINodeDBSS, "", int64(e.TxID), loc.rootKey)
if _, err := dstx.Put(key, &e); err != nil {
dstx.Rollback()
return err
}
if _, err := dstx.Commit(); err != nil {
return err
}
logger.Infof(sslog, "Put(%s, %d) took %s.", blobpath, txid, time.Since(start))
return nil
}
示例13: ExamplePut
func ExamplePut() {
ctx := context.Background()
client, err := datastore.NewClient(ctx, "project-id")
if err != nil {
log.Fatal(err)
}
type Article struct {
Title string
Description string
Body string `datastore:",noindex"`
Author *datastore.Key
PublishedAt time.Time
}
newKey := datastore.NewIncompleteKey(ctx, "Article", nil)
_, err = client.Put(ctx, newKey, &Article{
Title: "The title of the article",
Description: "The description of the article...",
Body: "...",
Author: datastore.NewKey(ctx, "Author", "jbd", 0, nil),
PublishedAt: time.Now(),
})
if err != nil {
log.Fatal(err)
}
}
示例14: ExampleGetMulti
func ExampleGetMulti() {
ctx := context.Background()
client, err := datastore.NewClient(ctx, "project-id")
if err != nil {
log.Fatal(err)
}
keys := []*datastore.Key{
datastore.NewKey(ctx, "Post", "post1", 0, nil),
datastore.NewKey(ctx, "Post", "post2", 0, nil),
datastore.NewKey(ctx, "Post", "post3", 0, nil),
}
posts := make([]Post, 3)
if err := client.GetMulti(ctx, keys, posts); err != nil {
log.Println(err)
}
}
示例15: ExampleQuery_keyFilter
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.
}