From bfeb7458827e219fa040cd1f310425096ad18ca0 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 13 Mar 2014 18:47:41 -0700 Subject: [PATCH] corpus: add PermanodeHasAttrValueLocked convenience accessor Change-Id: Ie73270ce54c3125a76987daa9f50895c5acf42db --- pkg/index/corpus.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/pkg/index/corpus.go b/pkg/index/corpus.go index 19bbcca03..a28f8915e 100644 --- a/pkg/index/corpus.go +++ b/pkg/index/corpus.go @@ -1001,6 +1001,41 @@ func (c *Corpus) ForeachClaimBackLocked(value blob.Ref, at time.Time, fn func(*c } } +// PermanodeHasAttrValueLocked reports whether the permanode pn at +// time at (zero means now) has the given attribute with the given +// value. If the attribute is multi-valued, any may match. +func (c *Corpus) PermanodeHasAttrValueLocked(pn blob.Ref, at time.Time, attr, val string) bool { + pm, ok := c.permanodes[pn] + if !ok { + return false + } + if at.IsZero() { + at = time.Now() + } + ret := false + for _, cl := range pm.Claims { + if cl.Attr != attr { + continue + } + if cl.Date.After(at) { + break + } + switch cl.Type { + case string(schema.DelAttributeClaim): + if cl.Value == "" || cl.Value == val { + ret = false + } + case string(schema.SetAttributeClaim): + ret = (cl.Value == val) + case string(schema.AddAttributeClaim): + if cl.Value == val { + return true + } + } + } + return ret +} + // SetVerboseCorpusLogging controls corpus setup verbosity. It's on by default // but used to disable verbose logging in tests. func SetVerboseCorpusLogging(v bool) {