MCPcopy Create free account
hub / github.com/ByteStorage/FlyDB / Size

Method Size

structure/string.go:403–434  ·  view source on GitHub ↗

Size returns the size of a value

(key string)

Source from the content-addressed store, hash-verified

401
402// Size returns the size of a value
403func (s *StringStructure) Size(key string) (string, error) {
404 value, err := s.Get(key)
405 if err != nil {
406 return "", err
407 }
408
409 toString, err := interfaceToString(value)
410 if err != nil {
411 return "", err
412 }
413
414 sizeInBytes := len(toString)
415
416 // Convert bytes to corresponding units (KB, MB...)
417 const (
418 KB = 1 << 10
419 MB = 1 << 20
420 GB = 1 << 30
421 )
422
423 var size string
424 switch {
425 case sizeInBytes < KB:
426 size = fmt.Sprintf("%dB", sizeInBytes)
427 case sizeInBytes < MB:
428 size = fmt.Sprintf("%.2fKB", float64(sizeInBytes)/KB)
429 case sizeInBytes < GB:
430 size = fmt.Sprintf("%.2fMB", float64(sizeInBytes)/MB)
431 }
432
433 return size, nil
434}
435
436func (s *StringStructure) MGet(keys ...string) ([]interface{}, error) {
437 // Create a slice to store the values

Callers

nothing calls this directly

Calls 2

GetMethod · 0.95
interfaceToStringFunction · 0.85

Tested by

no test coverage detected