index.gno
2.75 Kb ยท 100 lines
1package datastore
2
3import (
4 "gno.land/p/moul/collection"
5)
6
7// DefaultIndexOptions defines the default options for new indexes.
8const DefaultIndexOptions = collection.DefaultIndex | collection.SparseIndex
9
10type (
11 // IndexFn defines a type for single value indexing functions.
12 // This type of function extracts a single string value from
13 // a record that is then used to index it.
14 IndexFn func(Record) string
15
16 // IndexMultiValueFn defines a type for multi value indexing functions.
17 // This type of function extracts multiple string values from a
18 // record that are then used to index it.
19 IndexMultiValueFn func(Record) []string
20
21 // Index defines a type for custom user defined storage indexes.
22 // Storages are by default indexed by the auto geneated record ID
23 // but can additionally be indexed by other custom record fields.
24 Index struct {
25 name string
26 options collection.IndexOption
27 fn interface{}
28 }
29)
30
31// NewIndex creates a new single value index.
32//
33// Usage example:
34//
35// // Index a User record by email
36// idx := NewIndex("email", func(r Record) string {
37// return r.MustGet("email").(string)
38// })
39func NewIndex(name string, fn IndexFn) Index {
40 return Index{
41 name: name,
42 options: DefaultIndexOptions,
43 fn: func(v interface{}) string {
44 return fn(v.(Record))
45 },
46 }
47}
48
49// NewMultiValueIndex creates a new multi value index.
50//
51// Usage example:
52//
53// // Index a Post record by tag
54// idx := NewMultiValueIndex("tag", func(r Record) []string {
55// return r.MustGet("tags").([]string)
56// })
57func NewMultiValueIndex(name string, fn IndexMultiValueFn) Index {
58 return Index{
59 name: name,
60 options: DefaultIndexOptions,
61 fn: func(v interface{}) []string {
62 return fn(v.(Record))
63 },
64 }
65}
66
67// Name returns index's name.
68func (idx Index) Name() string {
69 return idx.name
70}
71
72// Options returns current index options.
73// These options define the index behavior regarding case sensitivity and uniquenes.
74func (idx Index) Options() collection.IndexOption {
75 return idx.options
76}
77
78// Func returns the function that storage collections apply
79// to each record to get the value to use for indexing it.
80func (idx Index) Func() interface{} {
81 return idx.fn
82}
83
84// Unique returns a copy of the index that indexes record values as unique values.
85// Returned index contains previous options plus the unique one.
86func (idx Index) Unique() Index {
87 if idx.options&collection.UniqueIndex == 0 {
88 idx.options |= collection.UniqueIndex
89 }
90 return idx
91}
92
93// CaseInsensitive returns a copy of the index that indexes record values ignoring casing.
94// Returned index contains previous options plus the case insensitivity one.
95func (idx Index) CaseInsensitive() Index {
96 if idx.options&collection.CaseInsensitiveIndex == 0 {
97 idx.options |= collection.CaseInsensitiveIndex
98 }
99 return idx
100}