mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-16 13:00:40 +08:00

* Convert hash to composite type. Fixed broken Hash commands from Hash refactor. Coverage and fixed broken test - @osteensco
49 lines
833 B
Go
49 lines
833 B
Go
package hash
|
|
|
|
import (
|
|
"time"
|
|
"unsafe"
|
|
|
|
"github.com/echovault/sugardb/internal/constants"
|
|
)
|
|
|
|
type HashValue struct {
|
|
Value interface{}
|
|
ExpireAt time.Time
|
|
}
|
|
|
|
type Hash map[string]HashValue
|
|
|
|
func (h Hash) GetMem() int64 {
|
|
|
|
var size int64
|
|
// Map headers
|
|
size += int64(unsafe.Sizeof(h))
|
|
|
|
for key, val := range h {
|
|
|
|
size += int64(unsafe.Sizeof(key))
|
|
size += int64(len(key))
|
|
|
|
size += int64(unsafe.Sizeof(val))
|
|
size += int64(unsafe.Sizeof(val.ExpireAt))
|
|
|
|
switch vt := val.Value.(type) {
|
|
|
|
// AdaptType() will always ensure data type is of string, float64 or int.
|
|
case nil:
|
|
size += 0
|
|
case int:
|
|
size += int64(unsafe.Sizeof(vt))
|
|
case float64, int64:
|
|
size += 8
|
|
case string:
|
|
size += int64(unsafe.Sizeof(vt))
|
|
size += int64(len(vt))
|
|
}
|
|
}
|
|
return size
|
|
}
|
|
|
|
var _ constants.CompositeType = (*Hash)(nil)
|