util

package
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 13 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. Small successful reads reuse a private cursor and decoder. Failed decodes discard their state; large payloads still decode without a size restriction.

func DecodeMsgpackFile

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

DecodeMsgpackFile decodes database-owned MessagePack state from a file.

func ErrorIsOnly

func ErrorIsOnly(err, target error) bool

ErrorIsOnly reports that every non-nil leaf is the expected status. Testing errors.Is on an aggregate only establishes that one branch matches; it must not suppress a different cleanup outcome joined beside that branch.

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 ObserveListenerClose

func ObserveListenerClose(listener net.Listener, record func(error)) net.Listener

ObserveListenerClose preserves the outcome of a directly owned listener before handing its closure to a protocol server. HTTP may replace a listener error with its drain deadline, and gRPC's stop API has no error result. This adapter observes the existing physical close; it neither schedules nor repeats disposal. Accepted connections retain their concrete transport type.

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 ShutdownHTTPServer

func ShutdownHTTPServer(ctx context.Context, server *http.Server) error

ShutdownHTTPServer drains HTTP requests until ctx ends, then closes remaining connections. Both standalone MCP and the database HTTP server use this owner.

Types

This section is empty.

Directories

Path Synopsis
Package eventchannel owns buffered, nonblocking event delivery and closure.
Package eventchannel owns buffered, nonblocking event delivery and closure.

Jump to

Keyboard shortcuts

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