|
2 | 2 | package namer |
3 | 3 |
|
4 | 4 | import ( |
5 | | - "github.com/tarantool/go-storage/kv" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "iter" |
| 8 | + "strings" |
6 | 9 | ) |
7 | 10 |
|
8 | | -// KeyType represents key types. |
9 | | -type KeyType int |
10 | | - |
11 | 11 | const ( |
12 | | - // KeyTypeValue represents data type. |
13 | | - KeyTypeValue KeyType = iota + 1 |
14 | | - // KeyTypeHash represents hash of the data type. |
15 | | - KeyTypeHash |
16 | | - // KeyTypeSignature represents signature of the data type. |
17 | | - KeyTypeSignature |
| 12 | + hashName = "hash" |
| 13 | + signatureName = "sig" |
| 14 | + keysPerName = 3 |
| 15 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + // ErrInvalidKey is returned when missing key, hash or signature. |
| 19 | + ErrInvalidKey = errors.New("missing key, hash or signature") |
| 20 | + // ErrInvalidInput is returned when input data is invalid. |
| 21 | + ErrInvalidInput = errors.New("failed to generate: invalid input data") |
| 22 | + // ErrInvalidPrefix is returned when prefix didn't match. |
| 23 | + ErrInvalidPrefix = errors.New("invalid prefix") |
18 | 24 | ) |
19 | 25 |
|
20 | | -// Key implements internal realization. |
21 | | -type Key struct { |
22 | | - Name string // Object identificator. |
23 | | - Type KeyType // Type of the object. |
24 | | - Property string // Additional information (version/algorithm). |
| 26 | +// Results represents Namer working result. |
| 27 | +type Results struct { |
| 28 | + IsSingle bool // True if result contains only one object name. |
| 29 | + IsSingleName string // Cached name when isSingle=true. |
| 30 | + Result map[string][]Key // Grouped keys: object name -> key list. |
25 | 31 | } |
26 | 32 |
|
27 | | -// Namer represents keys naming strategy. |
28 | | -type Namer interface { |
29 | | - GenerateNames(name string) []string // Object's keys generation. |
30 | | - ParseNames(names []string) []Key // Convert names into keys. |
| 33 | +// SelectSingle gets keys for single-name case (if applicable). |
| 34 | +func (r *Results) SelectSingle() ([]Key, bool) { |
| 35 | + if r.IsSingle { |
| 36 | + return r.Result[r.IsSingleName], true |
| 37 | + } |
| 38 | + |
| 39 | + return nil, false |
| 40 | +} |
| 41 | + |
| 42 | +// Items return iterator over all name->keys groups. |
| 43 | +func (r *Results) Items() iter.Seq2[string, []Key] { |
| 44 | + return func(yield func(str string, res []Key) bool) { |
| 45 | + for i, v := range r.Result { |
| 46 | + if !yield(i, v) { |
| 47 | + return |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// Select gets keys for a specific object name. |
| 54 | +func (r *Results) Select(name string) ([]Key, bool) { |
| 55 | + if i, ok := r.Result[name]; ok { |
| 56 | + return i, true |
| 57 | + } |
| 58 | + |
| 59 | + return nil, false |
31 | 60 | } |
32 | 61 |
|
33 | | -// Generator generates signer K/V pairs. |
34 | | -// Implementation should use `generic` and will used for strong typing of the solution. |
35 | | -type Generator[T any] interface { |
36 | | - Generate(name string, value T) ([]kv.KeyValue, error) |
| 62 | +// Len returns the number of unique object names. |
| 63 | +func (r *Results) Len() int { |
| 64 | + return len(r.Result) |
| 65 | +} |
| 66 | + |
| 67 | +// DefaultNamer represents default namer. |
| 68 | +type DefaultNamer struct { |
| 69 | + prefix string // Key prefix (e.g., "storage/"). |
| 70 | + hashNames []string |
| 71 | + sigNames []string |
| 72 | +} |
| 73 | + |
| 74 | +// NewDefaultNamer returns new DefaultNamer object with hash/signature names configuration. |
| 75 | +func NewDefaultNamer(prefix string, hashNames []string, sigNames []string) *DefaultNamer { |
| 76 | + prefix = strings.TrimPrefix(prefix, "/") |
| 77 | + prefix = strings.TrimSuffix(prefix, "/") |
| 78 | + |
| 79 | + return &DefaultNamer{ |
| 80 | + prefix: prefix, // "storage/". |
| 81 | + hashNames: hashNames, // "sha256". |
| 82 | + sigNames: sigNames, // "RSAPSS". |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// GenerateNames all keys for an object name. |
| 87 | +func (n *DefaultNamer) GenerateNames(name string) []Key { |
| 88 | + name = strings.TrimPrefix(name, "/") |
| 89 | + |
| 90 | + out := make([]Key, 0, len(n.hashNames)+len(n.sigNames)+1) |
| 91 | + |
| 92 | + out = append(out, |
| 93 | + NewDefaultKey( |
| 94 | + fmt.Sprintf("/%s/%s", n.prefix, name), |
| 95 | + KeyTypeValue, |
| 96 | + "", |
| 97 | + []byte{}, |
| 98 | + )) |
| 99 | + |
| 100 | + for _, hash := range n.hashNames { |
| 101 | + out = append(out, |
| 102 | + NewDefaultKey( |
| 103 | + fmt.Sprintf("/%s/%s/%s/%s", n.prefix, hashName, hash, name), |
| 104 | + KeyTypeHash, |
| 105 | + hash, |
| 106 | + []byte{}, |
| 107 | + ), |
| 108 | + ) |
| 109 | + } |
| 110 | + |
| 111 | + for _, sig := range n.sigNames { |
| 112 | + out = append(out, |
| 113 | + NewDefaultKey( |
| 114 | + fmt.Sprintf("/%s/%s/%s/%s", n.prefix, signatureName, sig, name), |
| 115 | + KeyTypeSignature, |
| 116 | + sig, |
| 117 | + []byte{}, |
| 118 | + ), |
| 119 | + ) |
| 120 | + } |
| 121 | + |
| 122 | + return out |
37 | 123 | } |
38 | 124 |
|
39 | | -// Validator validates and build the object from K/V. |
40 | | -type Validator[T any] interface { |
41 | | - Validate(pairs []kv.KeyValue) (T, error) |
| 125 | +// ParseKeys combine multiple raw keys into grouped results. |
| 126 | +func (n *DefaultNamer) ParseKeys(names []string) (Results, error) { |
| 127 | + var out Results |
| 128 | + |
| 129 | + out.Result = map[string][]Key{} |
| 130 | + |
| 131 | + if len(names) == 0 { |
| 132 | + return out, ErrInvalidInput |
| 133 | + } |
| 134 | + |
| 135 | + if len(names) == 1 { |
| 136 | + out.IsSingle = true |
| 137 | + out.IsSingleName = names[0] |
| 138 | + } |
| 139 | + |
| 140 | + for _, name := range names { |
| 141 | + originName := name |
| 142 | + |
| 143 | + name, found := strings.CutPrefix(name, n.prefix) |
| 144 | + if !found { |
| 145 | + return out, ErrInvalidInput |
| 146 | + } |
| 147 | + |
| 148 | + name = strings.TrimPrefix(name, "/") |
| 149 | + name = strings.TrimSuffix(name, "/") |
| 150 | + |
| 151 | + parts := strings.Split(name, "/") |
| 152 | + partslen := len(parts) |
| 153 | + |
| 154 | + var keyType KeyType |
| 155 | + |
| 156 | + var keyProp string |
| 157 | + |
| 158 | + switch parts[0] { |
| 159 | + case hashName: |
| 160 | + keyType = KeyTypeHash |
| 161 | + keyProp = parts[1] |
| 162 | + case signatureName: |
| 163 | + keyType = KeyTypeSignature |
| 164 | + keyProp = parts[1] |
| 165 | + default: |
| 166 | + keyType = KeyTypeValue |
| 167 | + keyProp = "" |
| 168 | + } |
| 169 | + |
| 170 | + key := NewDefaultKey( |
| 171 | + originName, |
| 172 | + keyType, |
| 173 | + keyProp, |
| 174 | + []byte{}, |
| 175 | + ) |
| 176 | + |
| 177 | + out.Result[parts[partslen-1]] = append(out.Result[parts[partslen-1]], key) |
| 178 | + } |
| 179 | + |
| 180 | + return out, nil |
42 | 181 | } |
0 commit comments