File: utils.go

package info (click to toggle)
deck 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,080 kB
  • sloc: makefile: 17; sh: 3
file content (55 lines) | stat: -rw-r--r-- 1,208 bytes parent folder | download | duplicates (2)
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
package state

import (
	memdb "github.com/hashicorp/go-memdb"
	"github.com/pkg/errors"
)

const (
	all = "all"
)

// ErrNotFound is an error type that is
// returned when an entity is not found in the state.
var ErrNotFound = errors.New("entity not found")

// ErrAlreadyExists represents an entity is already present in the state.
var ErrAlreadyExists = errors.New("entity already exists")

// internal errors
var errIDRequired = errors.New("ID is required")

// error annotation messages
const (
	unexpectedType = "unexpected type found"
)

var allIndex = &memdb.IndexSchema{
	Name: all,
	Indexer: &memdb.ConditionalIndex{
		Conditional: func(v interface{}) (bool, error) {
			return true, nil
		},
	},
}

// multiIndexLookupUsingTxn can be used to search for an entity
// based on search on multiple indexes with same key.
func multiIndexLookupUsingTxn(txn *memdb.Txn, tableName string,
	indices []string,
	args ...interface{}) (interface{}, error) {

	for _, indexName := range indices {
		res, err := txn.First(tableName, indexName, args...)
		if res == nil && err == nil {
			continue
		}
		if err != nil {
			return nil, err
		}
		if res != nil {
			return res, nil
		}
	}
	return nil, ErrNotFound
}