This file is indexed.

/usr/share/gocode/src/github.com/jacobsa/gcloud/gcs/gcscaching/stat_cache.go is in golang-github-jacobsa-gcloud-dev 0.0~git20150709-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package gcscaching

import (
	"time"

	"github.com/jacobsa/gcloud/gcs"
	"github.com/jacobsa/util/lrucache"
)

// A cache mapping from name to most recent known record for the object of that
// name. External synchronization must be provided.
type StatCache interface {
	// Insert an entry for the given object record.
	//
	// In order to help cope with caching of arbitrarily out of date (i.e.
	// inconsistent) object listings, entry will not replace any positive entry
	// with a newer generation number, or with an equivalent generation number
	// but newer metadata generation number. We have no choice, however, but to
	// replace negative entries.
	//
	// The entry will expire after the supplied time.
	Insert(o *gcs.Object, expiration time.Time)

	// Set up a negative entry for the given name, indicating that the name
	// doesn't exist. Overwrite any existing entry for the name, positive or
	// negative.
	AddNegativeEntry(name string, expiration time.Time)

	// Erase the entry for the given object name, if any.
	Erase(name string)

	// Return the current entry for the given name, or nil if there is a negative
	// entry. Return hit == false when there is neither a positive nor a negative
	// entry, or the entry has expired according to the supplied current time.
	LookUp(name string, now time.Time) (hit bool, o *gcs.Object)

	// Panic if any internal invariants have been violated. The careful user can
	// arrange to call this at crucial moments.
	CheckInvariants()
}

// Create a new stat cache that holds the given number of entries, which must
// be positive.
func NewStatCache(capacity int) (sc StatCache) {
	sc = &statCache{
		c: lrucache.New(capacity),
	}

	return
}

type statCache struct {
	c lrucache.Cache
}

// An entry in the cache, pairing an object with the expiration time for the
// entry. Nil object means negative entry.
type entry struct {
	o          *gcs.Object
	expiration time.Time
}

// Should the supplied object for a new positive entry replace the given
// existing entry?
func shouldReplace(o *gcs.Object, existing entry) bool {
	// Negative entries should always be replaced with positive entries.
	if existing.o == nil {
		return true
	}

	// Compare first on generation.
	if o.Generation != existing.o.Generation {
		return o.Generation > existing.o.Generation
	}

	// Break ties on metadata generation.
	if o.MetaGeneration != existing.o.MetaGeneration {
		return o.MetaGeneration > existing.o.MetaGeneration
	}

	// Break ties by preferring fresher entries.
	return true
}

func (sc *statCache) Insert(o *gcs.Object, expiration time.Time) {
	// Is there already a better entry?
	if existing := sc.c.LookUp(o.Name); existing != nil {
		if !shouldReplace(o, existing.(entry)) {
			return
		}
	}

	// Insert an entry.
	e := entry{
		o:          o,
		expiration: expiration,
	}

	sc.c.Insert(o.Name, e)
}

func (sc *statCache) AddNegativeEntry(name string, expiration time.Time) {
	// Insert a negative entry.
	e := entry{
		o:          nil,
		expiration: expiration,
	}

	sc.c.Insert(name, e)
}

func (sc *statCache) Erase(name string) {
	sc.c.Erase(name)
}

func (sc *statCache) LookUp(
	name string,
	now time.Time) (hit bool, o *gcs.Object) {
	// Look up in the LRU cache.
	value := sc.c.LookUp(name)
	if value == nil {
		return
	}

	e := value.(entry)

	// Has this entry expired?
	if e.expiration.Before(now) {
		sc.Erase(name)
		return
	}

	hit = true
	o = e.o

	return
}

func (sc *statCache) CheckInvariants() {
	sc.c.CheckInvariants()
}