Documentation
¶
Overview ¶
Package util provides shared utility functions used across NornicDB packages.
Index ¶
- Constants
- func ClampInt64ToInt(v int64) int
- func DecodeMsgpackBytes(data []byte, dst any) error
- func DecodeMsgpackFile(file *os.File, dst any) error
- func HashStringToInt64(s string) int64
- func MaxMsgpackDecodeBytes() int64
- func SafeFloat64ToInt(v float64) (int, bool)
- func SafeFloat64ToInt64(v float64) (int64, bool)
- func SafeInt32ToInt(v int32) (int, bool)
- func SafeInt64ToInt(v int64) (int, bool)
- func SafeIntAdd(a, b int) (int, bool)
- func SafeIntProduct(a, b int) (int, bool)
- func SafeIntSum(values ...int) (int, bool)
- func SafeIntToInt32(v int) (int32, bool)
- func SafeIntToUint16(v int) (uint16, bool)
- func SafeIntToUint32(v int) (uint32, bool)
- func SafePreallocCap(requested int, bounds ...int) int
- func SafePreallocProduct(a, b int, bounds ...int) int
- func SafePreallocSum(a, b int, bounds ...int) int
- func ValidateMsgpackPayloadSize(size int64) error
Constants ¶
const (
MaxPreallocHintCap = 1 << 16
)
Variables ¶
This section is empty.
Functions ¶
func ClampInt64ToInt ¶ added in v1.2.1
ClampInt64ToInt converts int64 to int and saturates on overflow.
func DecodeMsgpackBytes ¶
DecodeMsgpackBytes safely decodes msgpack from an in-memory payload with size validation.
func DecodeMsgpackFile ¶
DecodeMsgpackFile safely decodes msgpack from a file, rejecting oversized payloads.
func HashStringToInt64 ¶
HashStringToInt64 converts a string ID to an int64 for Neo4j Bolt protocol compatibility. NornicDB uses string IDs (UUIDs or custom strings), but Bolt protocol expects int64 IDs. This function uses FNV-1a hash algorithm for deterministic conversion with good distribution.
FNV-1a (Fowler-Noll-Vo) is a fast, non-cryptographic hash function that:
- Produces deterministic results (same input = same output)
- Has good distribution (fewer collisions than simple multiplicative hashing)
- Is fast and suitable for high-frequency operations
The hash uses FNV-1a 64-bit variant with offset basis 14695981039346656037.
Example:
HashStringToInt64("user-123") => 1234567890123456789
HashStringToInt64("user-123") => 1234567890123456789 (same result)
func MaxMsgpackDecodeBytes ¶
func MaxMsgpackDecodeBytes() int64
MaxMsgpackDecodeBytes returns the configured maximum number of bytes accepted for msgpack decode operations.
func SafeFloat64ToInt ¶ added in v1.2.1
SafeFloat64ToInt converts float64 to int using truncation toward zero when the truncated value is finite and fits in int.
func SafeFloat64ToInt64 ¶ added in v1.2.1
SafeFloat64ToInt64 converts float64 to int64 using truncation toward zero when the truncated value is finite and fits in int64.
func SafeInt32ToInt ¶ added in v1.2.1
SafeInt32ToInt converts int32 to int.
func SafeInt64ToInt ¶ added in v1.2.1
SafeInt64ToInt converts int64 to int when the value fits.
func SafeIntAdd ¶ added in v1.2.1
SafeIntAdd adds two non-negative ints and reports whether the result fits in int.
func SafeIntProduct ¶ added in v1.2.1
SafeIntProduct multiplies two positive ints and reports whether the result fits in int.
func SafeIntSum ¶ added in v1.2.1
SafeIntSum adds non-negative ints and reports whether the result fits in int.
func SafeIntToInt32 ¶ added in v1.2.1
SafeIntToInt32 converts int to int32 when the value fits.
func SafeIntToUint16 ¶ added in v1.2.1
SafeIntToUint16 converts a non-negative int to uint16 when it fits.
func SafeIntToUint32 ¶ added in v1.2.1
SafeIntToUint32 converts a non-negative int to uint32 when it fits.
func SafePreallocCap ¶ added in v1.2.1
SafePreallocCap bounds slice capacity hints by caller-provided limits and a hard ceiling. It is intended for make(..., 0, cap) sites where correctness does not depend on the exact hint.
func SafePreallocProduct ¶ added in v1.2.1
SafePreallocProduct multiplies two non-negative ints for a capacity hint and clamps the result.
func SafePreallocSum ¶ added in v1.2.1
SafePreallocSum adds two non-negative ints for a capacity hint and clamps the result.
func ValidateMsgpackPayloadSize ¶
ValidateMsgpackPayloadSize rejects payloads larger than the configured cap.
Types ¶
This section is empty.