func()

in v1storage/series.go [577:617]


func (it *memorySeriesIterator) ValueAtOrBeforeTime(t model.Time) model.SamplePair {
	// The most common case. We are iterating through a chunk.
	if it.chunkIt != nil {
		containsT, err := it.chunkIt.Contains(t)
		if err != nil {
			it.quarantine(err)
			return model.ZeroSamplePair
		}
		if containsT {
			if it.chunkIt.FindAtOrBefore(t) {
				return it.chunkIt.Value()
			}
			if it.chunkIt.Err() != nil {
				it.quarantine(it.chunkIt.Err())
			}
			return model.ZeroSamplePair
		}
	}

	if len(it.chunks) == 0 {
		return model.ZeroSamplePair
	}

	// Find the last chunk where FirstTime() is before or equal to t.
	l := len(it.chunks) - 1
	i := sort.Search(len(it.chunks), func(i int) bool {
		return !it.chunks[l-i].FirstTime().After(t)
	})
	if i == len(it.chunks) {
		// Even the first chunk starts after t.
		return model.ZeroSamplePair
	}
	it.chunkIt = it.chunkIterator(l - i)
	if it.chunkIt.FindAtOrBefore(t) {
		return it.chunkIt.Value()
	}
	if it.chunkIt.Err() != nil {
		it.quarantine(it.chunkIt.Err())
	}
	return model.ZeroSamplePair
}