util

package
v1.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 8, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package util provides shared utility functions used across NornicDB packages.

Index

Constants

View Source
const (
	MaxPreallocHintCap = 1 << 16
)

Variables

This section is empty.

Functions

func ClampInt64ToInt added in v1.2.1

func ClampInt64ToInt(v int64) int

ClampInt64ToInt converts int64 to int and saturates on overflow.

func DecodeMsgpackBytes

func DecodeMsgpackBytes(data []byte, dst any) error

DecodeMsgpackBytes safely decodes msgpack from an in-memory payload with size validation.

func DecodeMsgpackFile

func DecodeMsgpackFile(file *os.File, dst any) error

DecodeMsgpackFile safely decodes msgpack from a file, rejecting oversized payloads.

func HashStringToInt64

func HashStringToInt64(s string) int64

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

func SafeFloat64ToInt(v float64) (int, bool)

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

func SafeFloat64ToInt64(v float64) (int64, bool)

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

func SafeInt32ToInt(v int32) (int, bool)

SafeInt32ToInt converts int32 to int.

func SafeInt64ToInt added in v1.2.1

func SafeInt64ToInt(v int64) (int, bool)

SafeInt64ToInt converts int64 to int when the value fits.

func SafeIntAdd added in v1.2.1

func SafeIntAdd(a, b int) (int, bool)

SafeIntAdd adds two non-negative ints and reports whether the result fits in int.

func SafeIntProduct added in v1.2.1

func SafeIntProduct(a, b int) (int, bool)

SafeIntProduct multiplies two positive ints and reports whether the result fits in int.

func SafeIntSum added in v1.2.1

func SafeIntSum(values ...int) (int, bool)

SafeIntSum adds non-negative ints and reports whether the result fits in int.

func SafeIntToInt32 added in v1.2.1

func SafeIntToInt32(v int) (int32, bool)

SafeIntToInt32 converts int to int32 when the value fits.

func SafeIntToUint16 added in v1.2.1

func SafeIntToUint16(v int) (uint16, bool)

SafeIntToUint16 converts a non-negative int to uint16 when it fits.

func SafeIntToUint32 added in v1.2.1

func SafeIntToUint32(v int) (uint32, bool)

SafeIntToUint32 converts a non-negative int to uint32 when it fits.

func SafePreallocCap added in v1.2.1

func SafePreallocCap(requested int, bounds ...int) int

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

func SafePreallocProduct(a, b int, bounds ...int) int

SafePreallocProduct multiplies two non-negative ints for a capacity hint and clamps the result.

func SafePreallocSum added in v1.2.1

func SafePreallocSum(a, b int, bounds ...int) int

SafePreallocSum adds two non-negative ints for a capacity hint and clamps the result.

func ValidateMsgpackPayloadSize

func ValidateMsgpackPayloadSize(size int64) error

ValidateMsgpackPayloadSize rejects payloads larger than the configured cap.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL