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 ErrorIsOnly(err, target error) bool
- func EstimateValueBytes(value any) int64
- func HashStringToInt64(s string) int64
- func ObserveListenerClose(listener net.Listener, record func(error)) net.Listener
- 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 ShutdownHTTPServer(ctx context.Context, server *http.Server) 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 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 ¶
DecodeMsgpackFile decodes database-owned MessagePack state from a file.
func ErrorIsOnly ¶
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 ¶
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 ¶
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 ¶
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
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.
Types ¶
This section is empty.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package eventchannel owns buffered, nonblocking event delivery and closure.
|
Package eventchannel owns buffered, nonblocking event delivery and closure. |