findHashMeta finds the hash metadata by the given key.
(key string, dataType DataStructure)
| 1360 | |
| 1361 | // findHashMeta finds the hash metadata by the given key. |
| 1362 | func (hs *HashStructure) findHashMeta(key string, dataType DataStructure) (*HashMetadata, error) { |
| 1363 | // Convert the parameters to bytes |
| 1364 | k := stringToBytesWithKey(key) |
| 1365 | |
| 1366 | // Find the hash metadata by the given key |
| 1367 | meta, err := hs.db.Get(k) |
| 1368 | if err != nil && err != _const.ErrKeyNotFound { |
| 1369 | return nil, err |
| 1370 | } |
| 1371 | |
| 1372 | var hashMeta *HashMetadata |
| 1373 | var exist = true |
| 1374 | // If the hash metadata is not found, create a new one |
| 1375 | if err == _const.ErrKeyNotFound { |
| 1376 | exist = false |
| 1377 | } else { |
| 1378 | // Decode the hash metadata |
| 1379 | hashMeta = decodeHashMeta(meta) |
| 1380 | |
| 1381 | // Check the data type |
| 1382 | if hashMeta.dataType != dataType { |
| 1383 | return nil, ErrInvalidType |
| 1384 | } |
| 1385 | |
| 1386 | } |
| 1387 | |
| 1388 | // If the hash metadata is not found, create a new one |
| 1389 | if !exist { |
| 1390 | hashMeta = &HashMetadata{ |
| 1391 | dataType: dataType, |
| 1392 | dataSize: 0, |
| 1393 | expire: 0, |
| 1394 | version: time.Now().UnixNano(), |
| 1395 | counter: 0, |
| 1396 | createdTime: time.Now().UnixNano(), |
| 1397 | lastUpdatedTime: time.Now().UnixNano(), |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | return hashMeta, nil |
| 1402 | } |
| 1403 | |
| 1404 | func (hs *HashStructure) Stop() error { |
| 1405 | err := hs.db.Close() |