2022-11-11 11:18:38 +00:00
|
|
|
package result
|
|
|
|
|
|
|
|
import (
|
2023-01-06 10:16:09 +00:00
|
|
|
"fmt"
|
2022-11-11 11:18:38 +00:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
|
|
|
|
|
|
|
"codeberg.org/gruf/go-byteutil"
|
|
|
|
"codeberg.org/gruf/go-mangler"
|
|
|
|
)
|
|
|
|
|
|
|
|
// structKeys provides convience methods for a list
|
2022-11-13 13:02:07 +00:00
|
|
|
// of structKey field combinations used for cache keys.
|
|
|
|
type structKeys []structKey
|
2022-11-11 11:18:38 +00:00
|
|
|
|
2022-11-13 13:02:07 +00:00
|
|
|
// get fetches the structKey info for given lookup name (else, panics).
|
|
|
|
func (sk structKeys) get(name string) *structKey {
|
2022-11-11 11:18:38 +00:00
|
|
|
for i := range sk {
|
2022-11-13 13:02:07 +00:00
|
|
|
if sk[i].name == name {
|
2022-11-11 11:18:38 +00:00
|
|
|
return &sk[i]
|
|
|
|
}
|
|
|
|
}
|
2022-11-13 13:02:07 +00:00
|
|
|
panic("unknown lookup: \"" + name + "\"")
|
2022-11-11 11:18:38 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 13:02:07 +00:00
|
|
|
// generate will calculate and produce a slice of cache keys the given value
|
|
|
|
// can be stored under in the, as determined by receiving struct keys.
|
2022-12-16 22:36:52 +00:00
|
|
|
func (sk structKeys) generate(a any) []cacheKey {
|
|
|
|
var keys []cacheKey
|
2022-11-13 13:02:07 +00:00
|
|
|
|
2022-11-11 11:18:38 +00:00
|
|
|
// Get reflected value in order
|
|
|
|
// to access the struct fields
|
|
|
|
v := reflect.ValueOf(a)
|
|
|
|
|
|
|
|
// Iteratively deref pointer value
|
|
|
|
for v.Kind() == reflect.Pointer {
|
|
|
|
if v.IsNil() {
|
|
|
|
panic("nil ptr")
|
|
|
|
}
|
|
|
|
v = v.Elem()
|
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Acquire buffer
|
2022-12-16 22:36:52 +00:00
|
|
|
buf := getBuf()
|
2022-11-11 11:18:38 +00:00
|
|
|
|
2023-08-04 11:28:33 +00:00
|
|
|
outer:
|
2022-11-11 11:18:38 +00:00
|
|
|
for i := range sk {
|
|
|
|
// Reset buffer
|
2023-08-04 11:28:33 +00:00
|
|
|
buf.Reset()
|
2022-11-11 11:18:38 +00:00
|
|
|
|
2022-11-13 13:02:07 +00:00
|
|
|
// Append each field value to buffer.
|
2023-01-06 10:16:09 +00:00
|
|
|
for _, field := range sk[i].fields {
|
|
|
|
fv := v.Field(field.index)
|
2022-11-13 13:02:07 +00:00
|
|
|
fi := fv.Interface()
|
2023-08-04 11:28:33 +00:00
|
|
|
|
|
|
|
// Mangle this key part into buffer.
|
|
|
|
ok := field.manglePart(buf, fi)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
// don't generate keys
|
|
|
|
// for zero value parts.
|
|
|
|
continue outer
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append part separator.
|
2022-11-13 13:02:07 +00:00
|
|
|
buf.B = append(buf.B, '.')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop last '.'
|
|
|
|
buf.Truncate(1)
|
2022-11-11 11:18:38 +00:00
|
|
|
|
2022-11-13 13:02:07 +00:00
|
|
|
// Append new cached key to slice
|
2022-12-16 22:36:52 +00:00
|
|
|
keys = append(keys, cacheKey{
|
|
|
|
info: &sk[i],
|
|
|
|
key: string(buf.B), // copy
|
2022-11-13 13:02:07 +00:00
|
|
|
})
|
2022-11-11 11:18:38 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Release buf
|
|
|
|
putBuf(buf)
|
|
|
|
|
2022-11-11 11:18:38 +00:00
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
2022-12-16 22:36:52 +00:00
|
|
|
type cacheKeys []cacheKey
|
2022-11-14 09:14:34 +00:00
|
|
|
|
|
|
|
// drop will drop the cachedKey with lookup name from receiving cacheKeys slice.
|
|
|
|
func (ck *cacheKeys) drop(name string) {
|
|
|
|
_ = *ck // move out of loop
|
|
|
|
for i := range *ck {
|
2022-12-16 22:36:52 +00:00
|
|
|
if (*ck)[i].info.name == name {
|
2022-11-14 09:14:34 +00:00
|
|
|
(*ck) = append((*ck)[:i], (*ck)[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-16 22:36:52 +00:00
|
|
|
// cacheKey represents an actual cached key.
|
|
|
|
type cacheKey struct {
|
|
|
|
// info is a reference to the structKey this
|
2022-11-13 13:02:07 +00:00
|
|
|
// cacheKey is representing. This is a shared
|
|
|
|
// reference and as such only the structKey.pkeys
|
|
|
|
// lookup map is expecting to be modified.
|
2022-12-16 22:36:52 +00:00
|
|
|
info *structKey
|
2022-11-13 13:02:07 +00:00
|
|
|
|
2022-11-11 11:18:38 +00:00
|
|
|
// value is the actual string representing
|
|
|
|
// this cache key for hashmap lookups.
|
2022-12-16 22:36:52 +00:00
|
|
|
key string
|
2022-11-11 11:18:38 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 13:02:07 +00:00
|
|
|
// structKey represents a list of struct fields
|
|
|
|
// encompassing a single cache key, the string name
|
|
|
|
// of the lookup, the lookup map to primary cache
|
|
|
|
// keys, and the key's possible zero value string.
|
|
|
|
type structKey struct {
|
|
|
|
// name is the provided cache lookup name for
|
|
|
|
// this particular struct key, consisting of
|
|
|
|
// period ('.') separated struct field names.
|
|
|
|
name string
|
|
|
|
|
2023-05-09 14:17:43 +00:00
|
|
|
// unique determines whether this structKey supports
|
|
|
|
// multiple or just the singular unique result.
|
|
|
|
unique bool
|
|
|
|
|
2022-11-11 11:18:38 +00:00
|
|
|
// fields is a slice of runtime struct field
|
2023-11-30 16:22:34 +00:00
|
|
|
// indices, of fields encompassed by this key.
|
2023-01-06 10:16:09 +00:00
|
|
|
fields []structField
|
2022-11-11 11:18:38 +00:00
|
|
|
|
|
|
|
// pkeys is a lookup of stored struct key values
|
2023-11-30 16:22:34 +00:00
|
|
|
// to the primary cache lookup key (int64). this
|
|
|
|
// is protected by the main cache mutex.
|
2023-05-09 14:17:43 +00:00
|
|
|
pkeys map[string][]int64
|
2022-11-11 11:18:38 +00:00
|
|
|
}
|
|
|
|
|
2023-01-06 10:16:09 +00:00
|
|
|
// newStructKey will generate a structKey{} information object for user-given lookup
|
2022-11-13 13:02:07 +00:00
|
|
|
// key information, and the receiving generic paramter's type information. Panics on error.
|
2023-01-06 10:16:09 +00:00
|
|
|
func newStructKey(lk Lookup, t reflect.Type) structKey {
|
2023-08-04 11:28:33 +00:00
|
|
|
var sk structKey
|
2023-01-06 10:16:09 +00:00
|
|
|
|
|
|
|
// Set the lookup name
|
|
|
|
sk.name = lk.Name
|
2022-11-13 13:02:07 +00:00
|
|
|
|
2022-11-11 11:18:38 +00:00
|
|
|
// Split dot-separated lookup to get
|
|
|
|
// the individual struct field names
|
2022-11-13 13:02:07 +00:00
|
|
|
names := strings.Split(lk.Name, ".")
|
2022-11-11 11:18:38 +00:00
|
|
|
|
2023-01-06 10:16:09 +00:00
|
|
|
// Allocate the mangler and field indices slice.
|
|
|
|
sk.fields = make([]structField, len(names))
|
2022-11-11 11:18:38 +00:00
|
|
|
|
|
|
|
for i, name := range names {
|
|
|
|
// Get field info for given name
|
|
|
|
ft, ok := t.FieldByName(name)
|
|
|
|
if !ok {
|
|
|
|
panic("no field found for name: \"" + name + "\"")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check field is usable
|
|
|
|
if !isExported(name) {
|
|
|
|
panic("field must be exported")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the runtime field index
|
2023-01-06 10:16:09 +00:00
|
|
|
sk.fields[i].index = ft.Index[0]
|
2022-11-13 13:02:07 +00:00
|
|
|
|
|
|
|
// Allocate new instance of field
|
|
|
|
v := reflect.New(ft.Type)
|
|
|
|
v = v.Elem()
|
|
|
|
|
2023-01-06 10:16:09 +00:00
|
|
|
// Fetch mangler for field type.
|
|
|
|
sk.fields[i].mangle = mangler.Get(ft.Type)
|
|
|
|
|
2022-11-13 13:02:07 +00:00
|
|
|
if !lk.AllowZero {
|
2023-08-04 11:28:33 +00:00
|
|
|
// Append the mangled zero value interface
|
|
|
|
zero := sk.fields[i].mangle(nil, v.Interface())
|
|
|
|
sk.fields[i].zero = string(zero)
|
2022-11-13 13:02:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-09 14:17:43 +00:00
|
|
|
// Set unique lookup flag.
|
|
|
|
sk.unique = !lk.Multi
|
|
|
|
|
2023-01-06 10:16:09 +00:00
|
|
|
// Allocate primary lookup map
|
2023-05-09 14:17:43 +00:00
|
|
|
sk.pkeys = make(map[string][]int64)
|
2022-11-11 11:18:38 +00:00
|
|
|
|
2023-01-06 10:16:09 +00:00
|
|
|
return sk
|
2022-11-11 11:18:38 +00:00
|
|
|
}
|
|
|
|
|
2023-08-04 11:28:33 +00:00
|
|
|
// genKey generates a cache key string for given key parts (i.e. serializes them using "go-mangler").
|
|
|
|
func (sk *structKey) genKey(parts []any) string {
|
|
|
|
// Check this expected no. key parts.
|
|
|
|
if len(parts) != len(sk.fields) {
|
|
|
|
panic(fmt.Sprintf("incorrect no. key parts provided: want=%d received=%d", len(parts), len(sk.fields)))
|
|
|
|
}
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Acquire buffer
|
2023-08-04 11:28:33 +00:00
|
|
|
buf := getBuf()
|
|
|
|
buf.Reset()
|
|
|
|
|
|
|
|
for i, part := range parts {
|
|
|
|
// Mangle this key part into buffer.
|
|
|
|
// specifically ignoring whether this
|
|
|
|
// is returning a zero value key part.
|
|
|
|
_ = sk.fields[i].manglePart(buf, part)
|
|
|
|
|
|
|
|
// Append part separator.
|
|
|
|
buf.B = append(buf.B, '.')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop last '.'
|
|
|
|
buf.Truncate(1)
|
|
|
|
|
2023-11-30 16:22:34 +00:00
|
|
|
// Create str copy
|
|
|
|
str := string(buf.B)
|
|
|
|
|
|
|
|
// Release buf
|
|
|
|
putBuf(buf)
|
|
|
|
|
|
|
|
return str
|
2023-08-04 11:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type structField struct {
|
|
|
|
// index is the reflect index of this struct field.
|
|
|
|
index int
|
|
|
|
|
|
|
|
// zero is the possible zero value for this
|
|
|
|
// key part. if set, this will _always_ be
|
|
|
|
// non-empty due to how the mangler works.
|
|
|
|
//
|
|
|
|
// i.e. zero = "" --> allow zero value keys
|
|
|
|
// zero != "" --> don't allow zero value keys
|
|
|
|
zero string
|
|
|
|
|
|
|
|
// mangle is the mangler function for
|
|
|
|
// serializing values of this struct field.
|
|
|
|
mangle mangler.Mangler
|
|
|
|
}
|
|
|
|
|
|
|
|
// manglePart ...
|
|
|
|
func (field *structField) manglePart(buf *byteutil.Buffer, part any) bool {
|
|
|
|
// Start of part bytes.
|
|
|
|
start := len(buf.B)
|
|
|
|
|
|
|
|
// Mangle this key part into buffer.
|
|
|
|
buf.B = field.mangle(buf.B, part)
|
|
|
|
|
|
|
|
// End of part bytes.
|
|
|
|
end := len(buf.B)
|
|
|
|
|
|
|
|
// Return whether this is zero value.
|
|
|
|
return (field.zero == "" ||
|
|
|
|
string(buf.B[start:end]) != field.zero)
|
|
|
|
}
|
|
|
|
|
2022-11-11 11:18:38 +00:00
|
|
|
// isExported checks whether function name is exported.
|
|
|
|
func isExported(fnName string) bool {
|
|
|
|
r, _ := utf8.DecodeRuneInString(fnName)
|
|
|
|
return unicode.IsUpper(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// bufpool provides a memory pool of byte
|
|
|
|
// buffers use when encoding key types.
|
2022-12-16 22:36:52 +00:00
|
|
|
var bufPool = sync.Pool{
|
2022-11-11 11:18:38 +00:00
|
|
|
New: func() any {
|
|
|
|
return &byteutil.Buffer{B: make([]byte, 0, 512)}
|
|
|
|
},
|
|
|
|
}
|
2022-12-16 22:36:52 +00:00
|
|
|
|
2023-08-04 11:28:33 +00:00
|
|
|
// getBuf acquires a byte buffer from memory pool.
|
2022-12-16 22:36:52 +00:00
|
|
|
func getBuf() *byteutil.Buffer {
|
|
|
|
return bufPool.Get().(*byteutil.Buffer)
|
|
|
|
}
|
|
|
|
|
2023-08-04 11:28:33 +00:00
|
|
|
// putBuf replaces a byte buffer back in memory pool.
|
2022-12-16 22:36:52 +00:00
|
|
|
func putBuf(buf *byteutil.Buffer) {
|
|
|
|
if buf.Cap() > int(^uint16(0)) {
|
|
|
|
return // drop large bufs
|
|
|
|
}
|
|
|
|
bufPool.Put(buf)
|
|
|
|
}
|