-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.go
592 lines (490 loc) · 12.4 KB
/
types.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
package ucache
import (
"fmt"
"reflect"
"sort"
"strconv"
"strings"
"time"
"github.com/dgryski/go-farm"
"github.com/kordax/basic-utils/uarray"
"github.com/vmihailenco/msgpack/v5"
)
type UIntKey uint64
type IntKey int64
type StringKey string
type keyContainer[K any] struct {
key K
updatedAt time.Time
}
/*
Comparable entity
*/
type Comparable interface {
Equals(other Comparable) bool
}
/*
Unique specifies an abstract key with an ability to provide hash.
*/
type Unique interface {
Comparable
Key() int64 // Key should return a unique item key. It can be a hash or just an index.
}
/*
CompositeKey specifies an abstract key with an ability to provide an ordered list of available keys.
*/
type CompositeKey interface {
Comparable
Keys() []Unique // Keys returns an ordered list of keys ordered by priority (ASC), so the first element has the most prio.
}
func (k IntKey) Equals(other Comparable) bool {
otherKey, ok := other.(IntKey)
return ok && k == otherKey
}
func (k IntKey) Key() int64 {
return int64(k)
}
func (k IntKey) Keys() []Unique {
return []Unique{k}
}
func (k IntKey) String() string {
return fmt.Sprintf("%d", k)
}
func (k StringKey) Equals(other Comparable) bool {
otherKey, ok := other.(StringKey)
return ok && k == otherKey
}
func (k StringKey) Key() int64 {
hash := int64(0)
for i := 0; i < len(k); i++ {
hash = 31*hash + int64(k[i])
}
return hash
}
func (k StringKey) Keys() []Unique {
return []Unique{IntKey(farm.Hash64([]byte(k)))}
}
func (k StringKey) String() string {
return string(k)
}
func (k UIntKey) Equals(other Comparable) bool {
otherKey, ok := other.(UIntKey)
return ok && k == otherKey
}
func (k UIntKey) Key() int64 {
return int64(k)
}
func (k UIntKey) Keys() []Unique {
return []Unique{k}
}
func (k UIntKey) String() string {
return fmt.Sprintf("%d", k)
}
type ComparableKey[T comparable] struct {
v T
}
func NewComparableKey[T comparable](v T) ComparableKey[T] {
return ComparableKey[T]{v: v}
}
func (k ComparableKey[T]) Key() int64 {
hash := int64(0)
switch value := any(k.v).(type) {
case int, int8, int16, int32, int64:
hash = 31*hash + reflect.ValueOf(value).Int()
case uint, uint8, uint16, uint32, uint64:
hash = 31*hash + int64(reflect.ValueOf(value).Uint())
case float32, float64:
hash = 31*hash + int64(reflect.ValueOf(value).Float())
case complex64, complex128:
c := reflect.ValueOf(value).Complex()
hash = 31*(31*hash+int64(real(c))) + int64(imag(c))
case string:
for i := 0; i < len(value); i++ {
hash = 31*hash + int64(value[i])
}
case bool:
if value {
hash = 31*hash + 1
} else {
hash = 31*hash + 0
}
default:
return hash
}
return hash
}
func (k ComparableKey[T]) String() string {
return convertToString(k.v)
}
func (k ComparableKey[T]) Equals(other Comparable) bool {
otherKey, ok := other.(ComparableKey[T])
return ok && k == otherKey
}
type ComparableSlice[T Comparable] struct {
Data []T
}
func (c ComparableSlice[T]) Equals(other Comparable) bool {
switch o := other.(type) {
case ComparableSlice[T]:
return c.eq(o)
default:
return false
}
}
func (c ComparableSlice[T]) eq(other ComparableSlice[T]) bool {
if len(c.Data) != len(other.Data) {
return false
}
for i, v := range c.Data {
if !v.Equals(other.Data[i]) {
return false
}
}
return true
}
type UIntCompositeKey struct {
keys []UIntKey
}
func NewUIntCompositeKey(keys ...uint64) UIntCompositeKey {
var conv []UIntKey
for _, key := range keys {
conv = append(conv, UIntKey(key))
}
return UIntCompositeKey{keys: conv}
}
func (k UIntCompositeKey) Equals(other Comparable) bool {
switch o := other.(type) {
case UIntCompositeKey:
return uarray.EqualsWithOrder(k.keys, o.keys)
default:
return false
}
}
func (k UIntCompositeKey) Keys() []int64 {
result := make([]int64, len(k.keys))
for i, key := range k.keys {
conv := IntKey(key)
result[i] = conv.Key()
}
return result
}
func (k UIntCompositeKey) String() string {
rep := make([]string, len(k.keys))
for i, key := range k.keys {
rep[i] = strconv.FormatUint(uint64(key), 10)
}
return strings.Join(rep, ", ")
}
type IntCompositeKey struct {
keys []IntKey
}
func NewIntCompositeKey(keys ...int64) IntCompositeKey {
var conv []IntKey
for _, key := range keys {
conv = append(conv, IntKey(key))
}
return IntCompositeKey{keys: conv}
}
func (k IntCompositeKey) Equals(other Comparable) bool {
switch o := other.(type) {
case IntCompositeKey:
return uarray.EqualsWithOrder(k.keys, o.keys)
default:
return false
}
}
func (k IntCompositeKey) Keys() []Unique {
result := make([]Unique, len(k.keys))
for i, key := range k.keys {
result[i] = IntKey(key.Key())
}
return result
}
func (k IntCompositeKey) String() string {
rep := make([]string, len(k.keys))
for i, key := range k.keys {
rep[i] = strconv.FormatInt(int64(key), 10)
}
return strings.Join(rep, ", ")
}
type StrCompositeKey struct {
keys []StringKey
}
//goland:noinspection GoUnusedExportedFunction
func NewStrCompositeKey(keys ...string) StrCompositeKey {
var conv []StringKey
for _, key := range keys {
conv = append(conv, StringKey(key))
}
return StrCompositeKey{keys: conv}
}
func (k StrCompositeKey) Equals(other Comparable) bool {
switch o := other.(type) {
case StrCompositeKey:
return uarray.EqualsWithOrder(k.keys, o.keys)
default:
return false
}
}
func (k StrCompositeKey) Keys() []Unique {
result := make([]Unique, len(k.keys))
for i, key := range k.keys {
result[i] = IntKey(key.Key())
}
return result
}
func (k StrCompositeKey) String() string {
return strings.Join(uarray.Map(k.keys, func(v *StringKey) string {
return string(*v)
}), ", ")
}
type GenericCompositeKey struct {
keys []ComparableKey[any]
}
// NewGenericCompositeKey creates a GenericCompositeKey that supports only 'comparable' keys
func NewGenericCompositeKey(keys ...any) GenericCompositeKey {
var conv []ComparableKey[any]
for _, key := range keys {
if !isComparable(key) {
panic(fmt.Errorf("unsupported key type passed to NewGenericCompositeKey: %s", reflect.TypeOf(key).Name()))
}
conv = append(conv, NewComparableKey(key))
}
return GenericCompositeKey{keys: any(conv).([]ComparableKey[any])}
}
func (k GenericCompositeKey) Equals(other Comparable) bool {
switch o := other.(type) {
case GenericCompositeKey:
return uarray.EqualsWithOrder(k.keys, o.keys)
default:
return false
}
}
func (k GenericCompositeKey) Keys() []Unique {
result := make([]Unique, len(k.keys))
for i, key := range k.keys {
result[i] = IntKey(key.Key())
}
return result
}
func (k GenericCompositeKey) String() string {
builder := strings.Builder{}
for _, key := range k.keys {
builder.WriteString(convertToString(key))
}
return builder.String()
}
type StringValue struct {
v string
}
func NewStringValue(v string) StringValue {
return StringValue{v: v}
}
func (s StringValue) Value() string {
return s.v
}
func (s StringValue) Equals(other Comparable) bool {
otherValuePtr, pok := other.(*StringValue)
if !pok {
otherValue, ok := other.(StringValue)
if !ok {
return false
}
return s.v == otherValue.v
}
return s.v == otherValuePtr.v
}
type StringSliceValue struct {
v []string
}
func NewStringSliceValue(v []string) StringSliceValue {
sort.Strings(v)
return StringSliceValue{v: v}
}
func (s StringSliceValue) Values() []string {
return s.v
}
func (s StringSliceValue) Equals(other Comparable) bool {
otherValuePtr, pok := other.(*StringSliceValue)
if !pok {
otherValue, ok := other.(StringSliceValue)
if !ok {
return false
}
return uarray.EqualValues(s.v, otherValue.v)
}
return uarray.EqualValues(s.v, otherValuePtr.v)
}
type Int64Value struct {
v int64
}
func NewInt64Value(v int64) Int64Value {
return Int64Value{v: v}
}
func (s Int64Value) Value() string {
return strconv.FormatInt(s.v, 10)
}
func (s Int64Value) Equals(other Comparable) bool {
otherValuePtr, pok := other.(*Int64Value)
if !pok {
otherValue, ok := other.(Int64Value)
if !ok {
return false
}
return s.v == otherValue.v
}
return s.v == otherValuePtr.v
}
/*
FarmHash64Entity wraps any object and provides a Unique implementation
using farm's 64-bit hash function to be used in cache.
This hashed entity uses internal hash to avoid redundant rehashing operations.
IMPORTANT: The object must have exported fields and only those fields will be considered for the hashing uniqueness operation.
IMPORTANT: If the object is a pointer, the hash will compare pointer values. If the object is not a pointer, the hash will compare contents.
- Equals method compares the hash values of the wrapped objects.
- Key method uses farm.Hash64 to generate a 64-bit hash of the object and
returns it as an int64 value for the key.
This can be used for uniquely identifying objects and comparing them
based on their content rather than their memory address.
*/
type FarmHash64Entity struct {
obj any
hashValue int64
hashReady bool
}
/*
Hashed is a constructor function that creates and returns a new instance
of FarmHash64Entity, wrapping the provided object. This instance provides
a Unique implementation using farm's 64-bit hash function.
Usage:
- To uniquely identify objects based on their content rather than their
memory address.
- To generate a unique key for any object by hashing its content.
Example:
obj := Hashed("example object")
fmt.Println("Key:", obj.Key())
// Outputs the unique key generated by farm.Hash64 for the string "example object".
*/
func Hashed(obj any) *FarmHash64Entity {
return &FarmHash64Entity{obj: obj}
}
func (e *FarmHash64Entity) calculateHash() int64 {
if e.hashReady {
return e.hashValue
}
val := reflect.ValueOf(e.obj)
if val.Kind() == reflect.Ptr {
e.hashValue = int64(val.Pointer())
} else {
b, err := msgpack.Marshal(e.obj)
if err != nil {
panic(err)
}
e.hashValue = int64(farm.Hash64(b))
}
e.hashReady = true
return e.hashValue
}
func (e *FarmHash64Entity) Equals(other Comparable) bool {
otherFH, ok := other.(*FarmHash64Entity)
if !ok {
return false
}
return e.calculateHash() == otherFH.calculateHash()
}
func (e *FarmHash64Entity) Key() int64 {
return e.calculateHash()
}
type FarmHash64CompositeKey struct {
keys []*FarmHash64Entity
}
// NewFarmHashCompositeKey creates a GenericCompositeKey with Farm64 support.
func NewFarmHashCompositeKey(keys ...any) FarmHash64CompositeKey {
var conv []*FarmHash64Entity
for _, key := range keys {
conv = append(conv, Hashed(key))
}
return FarmHash64CompositeKey{keys: conv}
}
func (k FarmHash64CompositeKey) Equals(other Comparable) bool {
switch o := other.(type) {
case FarmHash64CompositeKey:
derefThis := uarray.Map(k.keys, func(v **FarmHash64Entity) FarmHash64Entity {
return **v
})
derefOther := uarray.Map(o.keys, func(v **FarmHash64Entity) FarmHash64Entity {
return **v
})
return uarray.EqualsWithOrder(derefThis, derefOther)
default:
return false
}
}
func (k FarmHash64CompositeKey) Keys() []Unique {
result := make([]Unique, len(k.keys))
for i, key := range k.keys {
result[i] = key
}
return result
}
func convertToString[T comparable](input T) string {
switch value := any(input).(type) {
case string:
return value
case int:
return strconv.Itoa(value)
case int8:
return strconv.FormatInt(int64(value), 10)
case int16:
return strconv.FormatInt(int64(value), 10)
case int32:
return strconv.FormatInt(int64(value), 10)
case int64:
return strconv.FormatInt(value, 10)
case uint:
return strconv.FormatUint(uint64(value), 10)
case uint8:
return strconv.FormatUint(uint64(value), 10)
case uint16:
return strconv.FormatUint(uint64(value), 10)
case uint32:
return strconv.FormatUint(uint64(value), 10)
case uint64:
return strconv.FormatUint(value, 10)
case bool:
return strconv.FormatBool(value)
case float32:
return strconv.FormatFloat(float64(value), 'f', -1, 32)
case float64:
return strconv.FormatFloat(value, 'f', -1, 64)
case complex64:
return strconv.FormatComplex(complex128(value), 'f', -1, 64)
case complex128:
return strconv.FormatComplex(value, 'f', -1, 128)
default:
return ""
}
}
func isComparable(value interface{}) bool {
if value == nil {
return false
}
t := reflect.TypeOf(value)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
switch t.Kind() {
case reflect.Slice, reflect.Map, reflect.Func:
return false
case reflect.Struct:
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if !isComparable(reflect.Zero(field.Type).Interface()) {
return false
}
}
default:
return true
}
return true
}