func()

in lib/store/base/file_map.go [209:263]


func (fm *lruFileMap) TryStore(name string, entry FileEntry, f func(string, FileEntry) bool) bool {
	// Lock on entry first, in case the lock is taken by other goroutine before f().
	e := &fileEntryWithAccessTime{
		fe: entry,
	}

	// After store, make sure size limit wasn't exceeded.
	// Also make sure this happens after e.RUnlock(), in case the new entry is to be deleted.
	defer fm.syncRemoveOldestIfNeeded()

	e.Lock()
	defer e.Unlock()

	fm.Lock()
	// Verify if it's already in the map.
	if _, ok := fm.get(name); ok {
		defer fm.Unlock()

		// Update last access time.
		t := fm.clk.Now()
		if t.Sub(e.lastAccessTime) >= fm.timeResolution {
			// Only update if new timestamp is <timeResolution> newer than
			// previous value.
			e.lastAccessTime = t
			e.fe.SetMetadata(metadata.NewLastAccessTime(t))
		}

		return false
	}

	// Add new entry to map.
	fm.add(name, e)

	lat := metadata.NewLastAccessTime(fm.clk.Now())
	if err := e.fe.GetMetadata(lat); err != nil {
		// Set LAT if it doesn't exist on disk or cannot be read.
		if !os.IsNotExist(err) {
			log.With("name", e.fe.GetName()).Errorf("Error reading LAT: %s", err)
		}
		if _, err := e.fe.SetMetadata(lat); err != nil {
			log.With("name", e.fe.GetName()).Errorf("Error setting LAT: %s", err)
		}
	}
	e.lastAccessTime = lat.Time

	fm.Unlock()

	if !f(name, e.fe) {
		// Remove from map while the entry lock is still being held.
		fm.syncRemove(name)
		return false
	}

	return true
}