Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Unified Diff: filter/dscache/plan.go

Issue 1269113005: A transparent cache for datastore, backed by memcache. (Closed) Base URL: https://github.com/luci/gae.git@add_meta
Patch Set: add test for per-model expiration Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: filter/dscache/plan.go
diff --git a/filter/dscache/plan.go b/filter/dscache/plan.go
new file mode 100644
index 0000000000000000000000000000000000000000..d5af688a3f63e5604496a292dc8adb532bead55d
--- /dev/null
+++ b/filter/dscache/plan.go
@@ -0,0 +1,92 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package dscache
+
+import (
+ "bytes"
+
+ ds "github.com/luci/gae/service/datastore"
+ mc "github.com/luci/gae/service/memcache"
+ "github.com/luci/luci-go/common/errors"
+ "github.com/luci/luci-go/common/logging"
+)
+
+type facts struct {
+ getKeys []ds.Key
+ getMeta ds.MultiMetaGetter
+ lockItems []mc.Item
+ nonce []byte
+}
+
+type plan struct {
+ keepMeta bool
+
+ idxMap []int
+ toGet []ds.Key
+ toGetMeta ds.MultiMetaGetter
+ toSave []mc.Item
+
+ decoded []ds.PropertyMap
+ lme errors.LazyMultiError
+}
+
+func (p *plan) add(idx int, get ds.Key, m ds.MetaGetter, save mc.Item) {
+ p.idxMap = append(p.idxMap, idx)
+ p.toGet = append(p.toGet, get)
+
+ p.toSave = append(p.toSave, save)
Vadim Sh. 2015/08/06 01:23:34 is it ok that you append 'nil's here?
iannucci 2015/08/06 02:37:33 yeah. nil means "don't save it" I added a bunch of
+
+ if p.keepMeta {
+ p.toGetMeta = append(p.toGetMeta, m)
+ }
+}
+
+func (p *plan) empty() bool {
+ return len(p.idxMap) == 0
+}
+
+func makePlan(aid, ns string, log logging.Logger, f *facts) *plan {
Vadim Sh. 2015/08/06 01:23:34 nit: makeFetchPlan or something
iannucci 2015/08/06 02:37:33 Done.
+ p := plan{
+ keepMeta: f.getMeta != nil,
+ decoded: make([]ds.PropertyMap, len(f.lockItems)),
+ lme: errors.LazyMultiError{Size: len(f.lockItems)},
+ }
+ for i, lockItm := range f.lockItems {
+ m := f.getMeta.GetSingle(i)
+ getKey := f.getKeys[i]
+
+ if lockItm == nil { // this item wasn't cacheable
Vadim Sh. 2015/08/06 01:23:34 typo: "wasn't cached" (unless I misunderstand some
iannucci 2015/08/06 02:37:33 fixed comment, it meant something different.
+ p.add(i, getKey, m, nil)
+ continue
+ }
+
+ flg := FlagValue(lockItm.Flags())
+ if flg == ItemHasLock {
dnj 2015/08/05 18:32:18 Consider "switch" on "flag", clearer what values/a
iannucci 2015/08/06 01:54:01 Done.
+ if !bytes.Equal(f.nonce, lockItm.Value()) {
dnj 2015/08/05 18:32:18 Invert this to make it more readable: if bytes.Equ
iannucci 2015/08/06 01:54:01 Done.
+ lockItm = nil // someone else has the lock, don't save
+ }
+ p.add(i, getKey, m, lockItm)
+ continue
+ }
+
+ // have some other sort of object, or our AddMulti failed to add this item.
+ if flg != ItemHasData {
+ p.add(i, getKey, m, nil)
+ continue
+ }
+
+ pmap, err := decodeItemValue(lockItm.Value(), aid, ns)
+ switch err {
+ case nil:
+ p.decoded[i] = pmap
+ case ds.ErrNoSuchEntity:
+ p.lme.Assign(i, ds.ErrNoSuchEntity)
+ default:
+ log.Errorf("dscache: error decoding %s, %s: %s", lockItm.Key(), getKey, err)
+ p.add(i, getKey, m, nil)
+ }
+ }
+ return &p
+}

Powered by Google App Engine
This is Rietveld 408576698