Files
SugarDB/internal/modules/hash/hash.go
osteensco 09640082c4 Update Hash to allow for expirey commands (#146)
* Convert hash to composite type. Fixed broken Hash commands from Hash refactor. Coverage and fixed broken test - @osteensco
2024-11-04 03:24:31 +08:00

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)