util

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 6 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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 decodes database-owned MessagePack state from memory.

func DecodeMsgpackFile

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

DecodeMsgpackFile decodes database-owned MessagePack state from a file.

func EstimateValueBytes

func EstimateValueBytes(value any) int64

EstimateValueBytes returns an approximate in-memory size of a decoded property value (string, []byte, []string, []any, map[string]any; any other type counts as one 16-byte word pair). It is used for byte-bounded caches, where a cheap, deterministic estimate is preferable to reflection.

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 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.

Types

This section is empty.

Jump to

Keyboard shortcuts

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