Home Download Docs Code Community
     1	/*
     2	Copyright 2011 The Perkeep Authors
     3	
     4	Licensed under the Apache License, Version 2.0 (the "License");
     5	you may not use this file except in compliance with the License.
     6	You may obtain a copy of the License at
     7	
     8	     http://www.apache.org/licenses/LICENSE-2.0
     9	
    10	Unless required by applicable law or agreed to in writing, software
    11	distributed under the License is distributed on an "AS IS" BASIS,
    12	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13	See the License for the specific language governing permissions and
    14	limitations under the License.
    15	*/
    16	
    17	package index
    18	
    19	import (
    20		"net/url"
    21		"time"
    22	
    23		"perkeep.org/pkg/schema"
    24		"perkeep.org/pkg/types/camtypes"
    25	)
    26	
    27	var urle = url.QueryEscape
    28	
    29	func urld(s string) string {
    30		d, _ := url.QueryUnescape(s)
    31		return d
    32	}
    33	
    34	type dupSkipper struct {
    35		m map[string]bool
    36	}
    37	
    38	// not thread safe.
    39	func (s *dupSkipper) Dup(v string) bool {
    40		if s.m == nil {
    41			s.m = make(map[string]bool)
    42		}
    43		if s.m[v] {
    44			return true
    45		}
    46		s.m[v] = true
    47		return false
    48	}
    49	
    50	// claimPtrsAttrValue returns the value of attr from claims,
    51	// or the empty string if not found.
    52	// Claims should be sorted by claim.Date.
    53	func claimPtrsAttrValue(claims []*camtypes.Claim, attr string, at time.Time, signerFilter SignerRefSet) string {
    54		return claimsIntfAttrValue(claimPtrSlice(claims), attr, at, signerFilter)
    55	}
    56	
    57	type claimsIntf interface {
    58		Len() int
    59		Claim(i int) *camtypes.Claim
    60	}
    61	
    62	type claimSlice []camtypes.Claim
    63	
    64	func (s claimSlice) Len() int                    { return len(s) }
    65	func (s claimSlice) Claim(i int) *camtypes.Claim { return &s[i] }
    66	
    67	type claimPtrSlice []*camtypes.Claim
    68	
    69	func (s claimPtrSlice) Len() int                    { return len(s) }
    70	func (s claimPtrSlice) Claim(i int) *camtypes.Claim { return s[i] }
    71	
    72	// claimsIntfAttrValue finds the value of an attribute in a list of claims
    73	// or empty string if not found. claims must be non-nil.
    74	// If signerFilter contains any refs, a claim is only taken into account if it
    75	// has been signed by one of the given signer refs.
    76	func claimsIntfAttrValue(claims claimsIntf, attr string, at time.Time, signerFilter SignerRefSet) string {
    77		if claims == nil {
    78			panic("nil claims argument in claimsIntfAttrValue")
    79		}
    80	
    81		if at.IsZero() {
    82			at = time.Now()
    83		}
    84	
    85		// use a small static buffer as it speeds up
    86		// search.BenchmarkQueryPermanodeLocation by 6-7%
    87		// with go 1.7.1
    88		var buf [8]string
    89		v := buf[:][:0]
    90	
    91		for i := 0; i < claims.Len(); i++ {
    92			cl := claims.Claim(i)
    93			if cl.Attr != attr || cl.Date.After(at) {
    94				continue
    95			}
    96	
    97			if len(signerFilter) > 0 {
    98				if !signerFilter.blobMatches(cl.Signer) {
    99					continue
   100				}
   101			}
   102	
   103			switch cl.Type {
   104			case string(schema.DelAttributeClaim):
   105				if cl.Value == "" {
   106					v = v[:0]
   107				} else {
   108					i := 0
   109					for _, w := range v {
   110						if w != cl.Value {
   111							v[i] = w
   112							i++
   113						}
   114					}
   115					v = v[:i]
   116				}
   117			case string(schema.SetAttributeClaim):
   118				v = append(v[:0], cl.Value)
   119			case string(schema.AddAttributeClaim):
   120				v = append(v, cl.Value)
   121			}
   122		}
   123		if len(v) != 0 {
   124			return v[0]
   125		}
   126		return ""
   127	}
Website layout inspired by memcached.
Content by the authors.