Size returns the size of a field in the hash as a formatted string. It takes a string key 'k' and one or more fields 'f' (optional). It returns a formatted string indicating the size of the field and any possible error. Parameters: k: The key of the hash table. f: The field(s) whose size needs t
(key string)
| 1325 | // string: A formatted string indicating the size of the field. |
| 1326 | // error: An error if occurred during the operation, or nil on success. |
| 1327 | func (hs *HashStructure) Size(key string) (string, error) { |
| 1328 | // Get all fields and values |
| 1329 | keysAndValues, err := hs.HGetAllFieldAndValue(key) |
| 1330 | if err != nil { |
| 1331 | return "", err |
| 1332 | } |
| 1333 | |
| 1334 | var sizeInBytes int |
| 1335 | |
| 1336 | // Calculate the size |
| 1337 | for f, v := range keysAndValues { |
| 1338 | sizeInBytes += len(f) + len(v.(string)) |
| 1339 | } |
| 1340 | |
| 1341 | // Convert bytes to corresponding units (KB, MB...) |
| 1342 | const ( |
| 1343 | KB = 1 << 10 |
| 1344 | MB = 1 << 20 |
| 1345 | GB = 1 << 30 |
| 1346 | ) |
| 1347 | |
| 1348 | var size string |
| 1349 | switch { |
| 1350 | case sizeInBytes < KB: |
| 1351 | size = fmt.Sprintf("%dB", sizeInBytes) |
| 1352 | case sizeInBytes < MB: |
| 1353 | size = fmt.Sprintf("%.2fKB", float64(sizeInBytes)/KB) |
| 1354 | case sizeInBytes < GB: |
| 1355 | size = fmt.Sprintf("%.2fMB", float64(sizeInBytes)/MB) |
| 1356 | } |
| 1357 | |
| 1358 | return size, nil |
| 1359 | } |
| 1360 | |
| 1361 | // findHashMeta finds the hash metadata by the given key. |
| 1362 | func (hs *HashStructure) findHashMeta(key string, dataType DataStructure) (*HashMetadata, error) { |
nothing calls this directly
no test coverage detected