Example #1
0
// Contains returns true if the req.StartID is between the min and max
// There is a chance the request messages to be found in this range
func (entry *cacheEntry) Contains(req *store.FetchRequest) bool {
	if req.StartID == 0 {
		req.Direction = 1
		return true
	}
	if req.Direction >= 0 {
		return req.StartID >= entry.min && req.StartID <= entry.max
	}
	return req.StartID >= entry.min
}
Example #2
0
// calculateFetchList returns a list of fetchEntry records for all messages in the fetch request.
func (p *messagePartition) calculateFetchList(req *store.FetchRequest) (*indexList, error) {
	if req.Direction == 0 {
		req.Direction = 1
	}

	potentialEntries := newIndexList(0)

	// reading from IndexFiles
	// TODO: fix  prev when EndID logic will be done
	// prev specifies if we found anything in the previous list, in which case
	// it is possible the items to continue in the next list
	prev := false

	p.fileCache.RLock()

	for i, fce := range p.fileCache.entries {
		if fce.Contains(req) || (prev && potentialEntries.len() < req.Count) {
			prev = true

			l, err := p.loadIndexList(i)
			if err != nil {
				logger.WithError(err).Info("Error loading idx file in memory")
				return nil, err
			}

			potentialEntries.insert(l.extract(req).toSliceArray()...)
		} else {
			prev = false
		}
	}

	// Read from current cached value (the idx file which size is smaller than MESSAGE_PER_FILE
	if p.list.contains(req.StartID) || (prev && potentialEntries.len() < req.Count) {
		potentialEntries.insert(p.list.extract(req).toSliceArray()...)
	}

	// Currently potentialEntries contains a potentials IDs from any files and
	// from in memory. From this will select only Count.
	fetchList := potentialEntries.extract(req)

	p.fileCache.RUnlock()

	return fetchList, nil
}