Beispiel #1
0
func (d *templateKV) List(pattern string, offset, limit int) ([]Template, error) {
	// Templates are indexed via their ID only.
	// While templates are sorted in the data section by their ID anyway
	// this allows us to do offset/limits and filtering without having to read in all template data.

	// List all template ids sorted by ID
	ids, err := d.store.List(templateIndexesPrefix + idIndex)
	if err != nil {
		return nil, err
	}

	var match func([]byte) bool
	if pattern != "" {
		match = func(value []byte) bool {
			id := string(value)
			matched, _ := path.Match(pattern, id)
			return matched
		}
	} else {
		match = func([]byte) bool { return true }
	}
	matches := storage.DoListFunc(ids, match, offset, limit)

	templates := make([]Template, len(matches))
	for i, id := range matches {
		data, err := d.store.Get(d.templateDataKey(string(id)))
		if err != nil {
			return nil, err
		}
		t, err := d.decodeTemplate(data.Value)
		templates[i] = t
	}
	return templates, nil
}
Beispiel #2
0
func (d *replayKV) List(pattern string, offset, limit int) ([]Replay, error) {
	// Replays are indexed by their Date.
	// This allows us to do offset/limits and filtering without having to read in all replay data.

	// List all replay ids sorted by Date
	ids, err := d.store.List(replayIndexesPrefix + replayDateIndex)
	if err != nil {
		return nil, err
	}
	// Reverse to sort by newest first
	for i, j := 0, len(ids)-1; i < j; i, j = i+1, j-1 {
		ids[i], ids[j] = ids[j], ids[i]
	}

	var match func([]byte) bool
	if pattern != "" {
		match = func(value []byte) bool {
			id := string(value)
			matched, _ := path.Match(pattern, id)
			return matched
		}
	} else {
		match = func([]byte) bool { return true }
	}
	matches := storage.DoListFunc(ids, match, offset, limit)

	replays := make([]Replay, len(matches))
	for i, id := range matches {
		data, err := d.store.Get(d.replayDataKey(string(id)))
		if err != nil {
			return nil, err
		}
		t, err := d.decodeReplay(data.Value)
		replays[i] = t
	}
	return replays, nil
}