Documentation
ΒΆ
Overview ΒΆ
Package txmap provides a set of concurrent-safe data structures and utilities for managing mappings and collections with high-performance requirements. The package leverages Go's sync package and other advanced concurrency primitives such as lock-free techniques where applicable.
Core Features:
**Thread-safe Maps**: Flexible implementations of maps with read-write mutex synchronization for thread-safety in multi-threaded environments.
- **SyncedMap**: A generic concurrent-safe map with optional size limits. - **SwissMap**: A simple concurrent-safe map based on the swiss.Map library, designed to store transaction hashes or other key-value mappings efficiently. - **SwissMapUint64**: A variation of SwissMap for transaction hashes associated with `uint64` values. - **SwissLockFreeMapUint64**: A specialized lock-free map for `uint64` keys and values, offering better performance for certain scenarios.
**Synced Slice**: A thread-safe wrapper around slices, allowing for synchronized access and updates. Useful for managing shared lists in concurrent code.
**Split Bucket Maps**: Advanced sharding technique for reducing contention by splitting data into multiple buckets (e.g., SplitSwissMap). - Buckets minimize lock contention by distributing keys across multiple synchronized maps.
**Utilities for Map Conversion**: Helper functions to convert various map data structures into slices, making data extraction and iteration easier.
Design Considerations:
- **Locking Mechanisms**: Where necessary, the maps use read-write locks for consistent reads and writes, while minimizing the lock duration for better performance.
- **Limit Controls**: Maps such as SyncedMap optionally provide control over the maximum number of items stored, ensuring that memory usage is kept in check.
- **High Concurrency**: Certain implementations, like SwissLockFreeMapUint64, are lock-free for a subset of their operations, promoting better scalability in concurrent workloads.
- **Preallocation**: Many data structures accept initialization parameters for preallocating internal storage, reducing runtime overhead from frequent allocations.
Usage Scenarios: The package is suitable for tasks requiring efficient handling of: - Large-scale transaction mappings (e.g., blockchain transaction hash maps). - Concurrent key-value access and modifications under high contention. - Specialized locking or predictive space management for performance-critical applications.
Examples: - Managing transaction hash lookups and metadata in high-frequency trading systems. - Concurrent-safe configuration or cache management in distributed services. - Utility for parallel data aggregation or transformation of shared resources.
Dependencies: The package depends on the [`swiss`](https://github.com/dolthub/swiss) library and additionally uses the `chainhash` library (`github.com/bsv-blockchain/go-bt/v2/chainhash`) where applicable.
Package txmap provides alternative implementations using Go's native map (which uses Swiss Tables in Go 1.24+) for benchmarking purposes.
Default factories (NewDefault*) return native implementations, which are recommended for general-purpose use due to their speed. Use dolthub implementations (NewSwiss*, NewSplitSwiss*) when memory is constrained.
Index ΒΆ
- Variables
- func Bytes2Uint16Buckets(b chainhash.Hash, mod uint16) uint16
- func ConvertSyncMapToUint32Slice(syncMap *sync.Map) ([]uint32, bool)
- func ConvertSyncedMapToUint32Slice[K comparable](syncMap *SyncedMap[K, []uint32]) ([]uint32, bool)
- type LockFreeMapUint64
- type LockFreeMapUint64Iterable
- type Map
- type MapUint64
- type NativeLockFreeMapUint64
- func (s *NativeLockFreeMapUint64) Clear()
- func (s *NativeLockFreeMapUint64) Exists(hash uint64) bool
- func (s *NativeLockFreeMapUint64) Freeze()
- func (s *NativeLockFreeMapUint64) Get(hash uint64) (uint64, bool)
- func (s *NativeLockFreeMapUint64) Iter(f func(k, v uint64) (stop bool))
- func (s *NativeLockFreeMapUint64) Length() int
- func (s *NativeLockFreeMapUint64) Map() map[uint64]uint64
- func (s *NativeLockFreeMapUint64) Put(hash, n uint64) error
- type NativeMap
- func (s *NativeMap) Clear()
- func (s *NativeMap) Delete(hash chainhash.Hash) error
- func (s *NativeMap) Exists(hash chainhash.Hash) bool
- func (s *NativeMap) Freeze()
- func (s *NativeMap) Get(hash chainhash.Hash) (uint64, bool)
- func (s *NativeMap) Iter(f func(hash chainhash.Hash, value uint64) bool)
- func (s *NativeMap) Keys() []chainhash.Hash
- func (s *NativeMap) Length() int
- func (s *NativeMap) Map() TxHashMap
- func (s *NativeMap) Put(hash chainhash.Hash) error
- func (s *NativeMap) PutMulti(hashes []chainhash.Hash) error
- type NativeMapUint64
- func (s *NativeMapUint64) Clear()
- func (s *NativeMapUint64) Delete(hash chainhash.Hash) error
- func (s *NativeMapUint64) Exists(hash chainhash.Hash) bool
- func (s *NativeMapUint64) Freeze()
- func (s *NativeMapUint64) Get(hash chainhash.Hash) (uint64, bool)
- func (s *NativeMapUint64) Iter(f func(hash chainhash.Hash, value uint64) bool)
- func (s *NativeMapUint64) Keys() []chainhash.Hash
- func (s *NativeMapUint64) Length() int
- func (s *NativeMapUint64) Map() map[chainhash.Hash]uint64
- func (s *NativeMapUint64) Put(hash chainhash.Hash, n uint64) error
- func (s *NativeMapUint64) PutMulti(hashes []chainhash.Hash, n uint64) error
- func (s *NativeMapUint64) Set(hash chainhash.Hash, value uint64) error
- func (s *NativeMapUint64) SetIfExists(hash chainhash.Hash, value uint64) (bool, error)
- func (s *NativeMapUint64) SetIfNotExists(hash chainhash.Hash, value uint64) (bool, error)
- type NativeSplitLockFreeMapUint64
- func (g *NativeSplitLockFreeMapUint64) Clear()
- func (g *NativeSplitLockFreeMapUint64) DeleteBucket(h uint64)
- func (g *NativeSplitLockFreeMapUint64) Exists(hash uint64) bool
- func (g *NativeSplitLockFreeMapUint64) Freeze()
- func (g *NativeSplitLockFreeMapUint64) Get(hash uint64) (uint64, bool)
- func (g *NativeSplitLockFreeMapUint64) IterAll(f func(k, v uint64) (stop bool))
- func (g *NativeSplitLockFreeMapUint64) Length() int
- func (g *NativeSplitLockFreeMapUint64) Map() map[uint64]*NativeLockFreeMapUint64
- func (g *NativeSplitLockFreeMapUint64) Put(hash, n uint64) error
- type NativeSplitMap
- func (g *NativeSplitMap) Buckets() uint16
- func (g *NativeSplitMap) Clear()
- func (g *NativeSplitMap) Delete(hash chainhash.Hash) error
- func (g *NativeSplitMap) Exists(hash chainhash.Hash) bool
- func (g *NativeSplitMap) Freeze()
- func (g *NativeSplitMap) Get(hash chainhash.Hash) (uint64, bool)
- func (g *NativeSplitMap) Iter(f func(hash chainhash.Hash, value uint64) bool)
- func (g *NativeSplitMap) Keys() []chainhash.Hash
- func (g *NativeSplitMap) Length() int
- func (g *NativeSplitMap) Map() *NativeMapUint64
- func (g *NativeSplitMap) Put(hash chainhash.Hash, n uint64) error
- func (g *NativeSplitMap) PutMulti(hashes []chainhash.Hash, n uint64) (err error)
- func (g *NativeSplitMap) PutMultiBucket(bucket uint16, hashes []chainhash.Hash, n uint64) error
- func (g *NativeSplitMap) Set(hash chainhash.Hash, value uint64) error
- func (g *NativeSplitMap) SetIfExists(hash chainhash.Hash, value uint64) (bool, error)
- func (g *NativeSplitMap) SetIfNotExists(hash chainhash.Hash, value uint64) (bool, error)
- type NativeSplitMapUint64
- func (g *NativeSplitMapUint64) Clear()
- func (g *NativeSplitMapUint64) Delete(hash chainhash.Hash) error
- func (g *NativeSplitMapUint64) Exists(hash chainhash.Hash) bool
- func (g *NativeSplitMapUint64) Freeze()
- func (g *NativeSplitMapUint64) Get(hash chainhash.Hash) (uint64, bool)
- func (g *NativeSplitMapUint64) Iter(f func(hash chainhash.Hash, value uint64) bool)
- func (g *NativeSplitMapUint64) Keys() []chainhash.Hash
- func (g *NativeSplitMapUint64) Length() int
- func (g *NativeSplitMapUint64) Map() map[uint16]*NativeMapUint64
- func (g *NativeSplitMapUint64) Put(hash chainhash.Hash, n uint64) error
- func (g *NativeSplitMapUint64) PutMulti(hashes []chainhash.Hash, n uint64) error
- func (g *NativeSplitMapUint64) Set(hash chainhash.Hash, value uint64) error
- func (g *NativeSplitMapUint64) SetIfExists(hash chainhash.Hash, value uint64) (bool, error)
- func (g *NativeSplitMapUint64) SetIfNotExists(hash chainhash.Hash, value uint64) (bool, error)
- type SplitLockFreeMapUint64
- type SplitLockFreeMapUint64Like
- type SplitMap
- type SplitMapUint64
- type SplitSwissLockFreeMapUint64
- func (g *SplitSwissLockFreeMapUint64) Clear()
- func (g *SplitSwissLockFreeMapUint64) DeleteBucket(h uint64)
- func (g *SplitSwissLockFreeMapUint64) Exists(hash uint64) bool
- func (g *SplitSwissLockFreeMapUint64) Freeze()
- func (g *SplitSwissLockFreeMapUint64) Get(hash uint64) (uint64, bool)
- func (g *SplitSwissLockFreeMapUint64) IterAll(f func(k, v uint64) (stop bool))
- func (g *SplitSwissLockFreeMapUint64) Length() int
- func (g *SplitSwissLockFreeMapUint64) Map() map[uint64]*SwissLockFreeMapUint64
- func (g *SplitSwissLockFreeMapUint64) Put(hash, n uint64) error
- type SplitSwissMap
- func (g *SplitSwissMap) Buckets() uint16
- func (g *SplitSwissMap) Clear()
- func (g *SplitSwissMap) Delete(hash chainhash.Hash) error
- func (g *SplitSwissMap) Exists(hash chainhash.Hash) bool
- func (g *SplitSwissMap) Freeze()
- func (g *SplitSwissMap) Get(hash chainhash.Hash) (uint64, bool)
- func (g *SplitSwissMap) Iter(f func(hash chainhash.Hash, value uint64) bool)
- func (g *SplitSwissMap) Keys() []chainhash.Hash
- func (g *SplitSwissMap) Length() int
- func (g *SplitSwissMap) Map() *SwissMapUint64
- func (g *SplitSwissMap) Put(hash chainhash.Hash, n uint64) error
- func (g *SplitSwissMap) PutMulti(hashes []chainhash.Hash, n uint64) (err error)
- func (g *SplitSwissMap) PutMultiBucket(bucket uint16, hashes []chainhash.Hash, n uint64) error
- func (g *SplitSwissMap) Set(hash chainhash.Hash, value uint64) error
- func (g *SplitSwissMap) SetIfExists(hash chainhash.Hash, value uint64) (bool, error)
- func (g *SplitSwissMap) SetIfNotExists(hash chainhash.Hash, value uint64) (bool, error)
- type SplitSwissMapUint64
- func (g *SplitSwissMapUint64) Clear()
- func (g *SplitSwissMapUint64) Delete(hash chainhash.Hash) error
- func (g *SplitSwissMapUint64) Exists(hash chainhash.Hash) bool
- func (g *SplitSwissMapUint64) Freeze()
- func (g *SplitSwissMapUint64) Get(hash chainhash.Hash) (uint64, bool)
- func (g *SplitSwissMapUint64) Iter(f func(hash chainhash.Hash, value uint64) bool)
- func (g *SplitSwissMapUint64) Keys() []chainhash.Hash
- func (g *SplitSwissMapUint64) Length() int
- func (g *SplitSwissMapUint64) Map() map[uint16]*SwissMapUint64
- func (g *SplitSwissMapUint64) Put(hash chainhash.Hash, n uint64) error
- func (g *SplitSwissMapUint64) PutMulti(hashes []chainhash.Hash, n uint64) error
- func (g *SplitSwissMapUint64) Set(hash chainhash.Hash, value uint64) error
- func (g *SplitSwissMapUint64) SetIfExists(hash chainhash.Hash, value uint64) (bool, error)
- func (g *SplitSwissMapUint64) SetIfNotExists(hash chainhash.Hash, value uint64) (bool, error)
- type SwissLockFreeMapUint64
- func (s *SwissLockFreeMapUint64) Clear()
- func (s *SwissLockFreeMapUint64) Exists(hash uint64) bool
- func (s *SwissLockFreeMapUint64) Freeze()
- func (s *SwissLockFreeMapUint64) Get(hash uint64) (uint64, bool)
- func (s *SwissLockFreeMapUint64) Iter(f func(k, v uint64) (stop bool))
- func (s *SwissLockFreeMapUint64) Length() int
- func (s *SwissLockFreeMapUint64) Map() *swiss.Map[uint64, uint64]
- func (s *SwissLockFreeMapUint64) Put(hash, n uint64) error
- type SwissMap
- func (s *SwissMap) Clear()
- func (s *SwissMap) Delete(hash chainhash.Hash) error
- func (s *SwissMap) Exists(hash chainhash.Hash) bool
- func (s *SwissMap) Freeze()
- func (s *SwissMap) Get(hash chainhash.Hash) (uint64, bool)
- func (s *SwissMap) Iter(f func(hash chainhash.Hash, value uint64) bool)
- func (s *SwissMap) Keys() []chainhash.Hash
- func (s *SwissMap) Length() int
- func (s *SwissMap) Map() TxHashMap
- func (s *SwissMap) Put(hash chainhash.Hash) error
- func (s *SwissMap) PutMulti(hashes []chainhash.Hash) error
- type SwissMapUint64
- func (s *SwissMapUint64) Clear()
- func (s *SwissMapUint64) Delete(hash chainhash.Hash) error
- func (s *SwissMapUint64) Exists(hash chainhash.Hash) bool
- func (s *SwissMapUint64) Freeze()
- func (s *SwissMapUint64) Get(hash chainhash.Hash) (uint64, bool)
- func (s *SwissMapUint64) Iter(f func(hash chainhash.Hash, value uint64) bool)
- func (s *SwissMapUint64) Keys() []chainhash.Hash
- func (s *SwissMapUint64) Length() int
- func (s *SwissMapUint64) Map() *swiss.Map[chainhash.Hash, uint64]
- func (s *SwissMapUint64) Put(hash chainhash.Hash, n uint64) error
- func (s *SwissMapUint64) PutMulti(hashes []chainhash.Hash, n uint64) error
- func (s *SwissMapUint64) Set(hash chainhash.Hash, value uint64) error
- func (s *SwissMapUint64) SetIfExists(hash chainhash.Hash, value uint64) (bool, error)
- func (s *SwissMapUint64) SetIfNotExists(hash chainhash.Hash, value uint64) (bool, error)
- type SyncedMap
- func (m *SyncedMap[K, V]) Clear() bool
- func (m *SyncedMap[K, V]) Delete(key K) bool
- func (m *SyncedMap[K, V]) Exists(key K) bool
- func (m *SyncedMap[K, V]) Freeze()
- func (m *SyncedMap[K, V]) Get(key K) (V, bool)
- func (m *SyncedMap[K, V]) Iterate(f func(key K, value V) bool)
- func (m *SyncedMap[K, V]) Keys() []K
- func (m *SyncedMap[K, V]) Length() int
- func (m *SyncedMap[K, V]) Range() map[K]V
- func (m *SyncedMap[K, V]) Set(key K, value V)
- func (m *SyncedMap[K, V]) SetIfNotExists(key K, value V) (V, bool)
- func (m *SyncedMap[K, V]) SetIfNotExistsMulti(keys []K, values []V) []bool
- func (m *SyncedMap[K, V]) SetMulti(keys []K, value V)
- type SyncedSlice
- type SyncedSwissMap
- func (m *SyncedSwissMap[K, V]) Clear()
- func (m *SyncedSwissMap[K, V]) Delete(key K) bool
- func (m *SyncedSwissMap[K, V]) DeleteBatch(keys []K) bool
- func (m *SyncedSwissMap[K, V]) Freeze()
- func (m *SyncedSwissMap[K, V]) Get(key K) (V, bool)
- func (m *SyncedSwissMap[K, V]) Length() int
- func (m *SyncedSwissMap[K, V]) Range() map[K]V
- func (m *SyncedSwissMap[K, V]) Set(key K, value V)
- type TxHashMap
- type TxMap
- type Uint64
Constants ΒΆ
This section is empty.
Variables ΒΆ
var ( // ErrHashAlreadyExists is when hash already exists ErrHashAlreadyExists = errors.New("hash already exists in map") // ErrHashDoesNotExist is when hash doesn't exist ErrHashDoesNotExist = errors.New("hash does not exist in map") // ErrBucketDoesNotExist is when a bucket doesn't exist ErrBucketDoesNotExist = errors.New("bucket does not exist") // ErrMapFrozen is returned by write methods (Put, PutMulti, Set, // SetIfExists, SetIfNotExists, Delete) once Freeze has been called on the // map. Call Clear to un-freeze and reuse the map. ErrMapFrozen = errors.New("map is frozen and cannot be modified") )
Functions ΒΆ
func Bytes2Uint16Buckets ΒΆ
Bytes2Uint16Buckets converts the first two bytes of a chainhash.Hash to a uint16 value and returns the result modulo the specified value. This function is used to determine the bucket index for a given hash in a split map.
Params:
- b: The chainhash.Hash to convert.
- mod: The value to use for the modulo operation.
Returns:
- uint16: The resulting value after conversion and modulo operation.
func ConvertSyncMapToUint32Slice ΒΆ
ConvertSyncMapToUint32Slice returns a slice of uint32 keys from the provided *sync.Map.
Parameters:
- syncMap: A pointer to a sync.Map where keys are of type uint32.
Returns:
- []uint32: A slice containing all uint32 keys from the map.
- bool: True if the map contained any elements, false otherwise.
func ConvertSyncedMapToUint32Slice ΒΆ
func ConvertSyncedMapToUint32Slice[K comparable](syncMap *SyncedMap[K, []uint32]) ([]uint32, bool)
ConvertSyncedMapToUint32Slice returns a slice of all uint32 values from the provided SyncedMap.
Parameters:
- syncMap: A pointer to a SyncedMap with any comparable key type and []uint32 values.
Returns:
- []uint32: A slice containing all uint32 values from the map (flattened).
- bool: True if the map contained any elements, false otherwise.
Types ΒΆ
type LockFreeMapUint64 ΒΆ added in v1.5.2
type LockFreeMapUint64 = NativeLockFreeMapUint64
LockFreeMapUint64 is the default lock-free map type using native implementation. Use NewDefaultLockFreeMapUint64 to create instances.
type LockFreeMapUint64Iterable ΒΆ added in v1.5.2
LockFreeMapUint64Iterable has Iter for bucket iteration.
type MapUint64 ΒΆ added in v1.5.2
type MapUint64 = NativeMapUint64
MapUint64 is the default hash-to-uint64 map type using native implementation. Use NewDefaultMapUint64 to create instances.
type NativeLockFreeMapUint64 ΒΆ added in v1.5.2
type NativeLockFreeMapUint64 struct {
// contains filtered or unexported fields
}
NativeLockFreeMapUint64 is a lock-free map for uint64 keys and values
func NewDefaultLockFreeMapUint64 ΒΆ added in v1.5.2
func NewDefaultLockFreeMapUint64(length int) *NativeLockFreeMapUint64
NewDefaultLockFreeMapUint64 returns a native lock-free map. Use for general-purpose uint64-to-uint64 maps when speed matters. Use NewSwissLockFreeMapUint64 when memory is constrained.
func NewNativeLockFreeMapUint64 ΒΆ added in v1.5.2
func NewNativeLockFreeMapUint64(length int) *NativeLockFreeMapUint64
NewNativeLockFreeMapUint64 creates a new NativeLockFreeMapUint64 with the specified initial length. The length is used to preallocate the map size for better performance. It is not a hard limit, but a hint to the underlying map.
Params:
- length: The initial length of the map, used for preallocation.
Returns:
- *NativeLockFreeMapUint64: A pointer to the newly created NativeLockFreeMapUint64 instance.
func (*NativeLockFreeMapUint64) Clear ΒΆ added in v1.5.2
func (s *NativeLockFreeMapUint64) Clear()
Clear empties the map and un-freezes it for reuse. Not safe for concurrent use.
func (*NativeLockFreeMapUint64) Exists ΒΆ added in v1.5.2
func (s *NativeLockFreeMapUint64) Exists(hash uint64) bool
Exists checks if the given hash exists in the map.
Params:
- hash: The hash to check for existence in the map.
Returns:
- bool: True if the hash exists in the map, false otherwise.
Considerations: This method does not lock the map, so it is not suitable for concurrent access.
func (*NativeLockFreeMapUint64) Freeze ΒΆ added in v1.5.2
func (s *NativeLockFreeMapUint64) Freeze()
Freeze marks the map read-only; subsequent Put calls return ErrMapFrozen.
func (*NativeLockFreeMapUint64) Get ΒΆ added in v1.5.2
func (s *NativeLockFreeMapUint64) Get(hash uint64) (uint64, bool)
Get retrieves the uint64 value associated with the given hash from the map.
Params:
- hash: The hash to retrieve from the map.
Returns:
- uint64: The value associated with the hash, or 0 if the hash does not exist.
- bool: True if the hash was found in the map, false otherwise.
Considerations: This method does not lock the map, so it is not suitable for concurrent access.
func (*NativeLockFreeMapUint64) Iter ΒΆ added in v1.5.2
func (s *NativeLockFreeMapUint64) Iter(f func(k, v uint64) (stop bool))
Iter iterates over all key-value pairs in the map. Stops if f returns true. Compatible with the pattern used by SwissLockFreeMapUint64.Map().Iter for consumers that need a unified iteration API across dolthub and native backends.
func (*NativeLockFreeMapUint64) Length ΒΆ added in v1.5.2
func (s *NativeLockFreeMapUint64) Length() int
Length returns the current number of hashes in the map.
Returns:
- int: The number of hashes currently stored in the map.
Considerations: This method uses atomic operations to retrieve the length, making it safe for concurrent access.
func (*NativeLockFreeMapUint64) Map ΒΆ added in v1.5.2
func (s *NativeLockFreeMapUint64) Map() map[uint64]uint64
Map returns the underlying native map used by NativeLockFreeMapUint64. It provides access to the map for operations that do not require locking.
Returns:
- map[uint64]uint64: The underlying native map.
Considerations: This method does not lock the map, so it is not suitable for concurrent access.
func (*NativeLockFreeMapUint64) Put ΒΆ added in v1.5.2
func (s *NativeLockFreeMapUint64) Put(hash, n uint64) error
Put adds a new hash with an associated uint64 value to the map. It checks if the hash already exists in the map and returns an error if it does. If the hash does not exist, it adds the hash and increments the length of the map.
Params:
- hash: The hash to add to the map.
- n: The uint64 value to associate with the hash.
Returns:
- error: An error if the hash already exists in the map, nil otherwise.
Considerations: This method does not lock the map, so it is not suitable for concurrent access.
type NativeMap ΒΆ added in v1.5.2
type NativeMap struct {
// contains filtered or unexported fields
}
NativeMap is a simple concurrent-safe map that uses Go's native map
func NewDefaultMap ΒΆ added in v1.5.2
NewDefaultMap returns a native map.
func NewNativeMap ΒΆ added in v1.5.2
NewNativeMap creates a new NativeMap with the specified initial length. The length is used to preallocate the map size for better performance. It is not a hard limit, but a hint to the underlying map.
Params:
- length: The initial length of the map, used for preallocation.
Returns:
- *NativeMap: A pointer to the newly created NativeMap instance.
Considerations: The length is not enforced, and the map can grow beyond this size.
func (*NativeMap) Clear ΒΆ added in v1.5.2
func (s *NativeMap) Clear()
Clear empties the map (retaining its allocated capacity) and un-freezes it for reuse. Per the lifecycle contract above, Clear must not run concurrently with other operations on the map (a frozen reader skips the lock that Clear takes); the write lock only orders it against other locked, non-frozen ops.
func (*NativeMap) Delete ΒΆ added in v1.5.2
Delete removes a hash from the map. It decrements the length of the map.
Params:
- hash: The hash to remove from the map.
Returns:
- error: always returns nil, as this map does not have any constraints on deleting hashes.
func (*NativeMap) Exists ΒΆ added in v1.5.2
Exists checks if the given hash exists in the map. It returns true if the hash is found, false otherwise.
Params:
- hash: The hash to check for existence in the map.
Returns:
- bool: True if the hash exists in the map, false otherwise.
func (*NativeMap) Freeze ΒΆ added in v1.5.2
func (s *NativeMap) Freeze()
Freeze marks the map read-only. See the lifecycle notes at the top of this file.
func (*NativeMap) Get ΒΆ added in v1.5.2
Get retrieves the value associated with the given hash from the map. It always returns 0 and a boolean indicating whether the hash was found.
Params:
- hash: The hash to retrieve from the map.
Returns:
- uint64: Always returns 0, as this map does not store values.
- bool: True if the hash was found in the map, false otherwise.
func (*NativeMap) Iter ΒΆ added in v1.5.2
Iter iterates over all key-value pairs in the map and applies the provided function to each pair. Stops iterating if the function returns true.
Params:
- f: A function that takes a hash and its associated value (always 0 in this map).
func (*NativeMap) Keys ΒΆ added in v1.5.2
Keys returns a slice of all hashes currently stored in the map. It iterates over the map and collects the keys. The order of keys is not guaranteed.
Returns:
- []chainhash.Hash: A slice containing all the hashes in the map.
func (*NativeMap) Length ΒΆ added in v1.5.2
Length returns the current number of hashes in the map.
Returns:
- int: The number of hashes currently stored in the map.
func (*NativeMap) Put ΒΆ added in v1.5.2
Put adds a new hash to the map. It increments the length of the map.
Params:
- hash: The hash to add to the map.
Returns:
- error: always returns nil, as this map does not have any constraints on adding hashes.
type NativeMapUint64 ΒΆ added in v1.5.2
type NativeMapUint64 struct {
// contains filtered or unexported fields
}
NativeMapUint64 is a concurrent-safe map that uses Go's native map to store transaction hashes as keys and uint64 values.
func NewDefaultMapUint64 ΒΆ added in v1.5.2
func NewDefaultMapUint64(length uint32) *NativeMapUint64
NewDefaultMapUint64 returns a native map implementation. Use for general-purpose hash-to-uint64 maps when speed matters. Use NewSwissMapUint64 when memory is constrained.
func NewNativeMapUint64 ΒΆ added in v1.5.2
func NewNativeMapUint64(length uint32) *NativeMapUint64
NewNativeMapUint64 creates a new NativeMapUint64 with the specified initial length. The length is used to preallocate the map size for better performance. It is not a hard limit, but a hint to the underlying map.
Params:
- length: The initial length of the map, used for preallocation.
Returns:
- *NativeMapUint64: A pointer to the newly created NativeMapUint64 instance.
func (*NativeMapUint64) Clear ΒΆ added in v1.5.2
func (s *NativeMapUint64) Clear()
Clear empties the map (retaining its allocated capacity) and un-freezes it for reuse. Per the lifecycle contract above, Clear must not run concurrently with other operations on the map (a frozen reader skips the lock that Clear takes); the write lock only orders it against other locked, non-frozen ops.
func (*NativeMapUint64) Delete ΒΆ added in v1.5.2
func (s *NativeMapUint64) Delete(hash chainhash.Hash) error
Delete removes a hash from the map. It decrements the length of the map. It locks the map for writing, checks if the hash exists, and removes it if found. If the hash does not exist, it returns an error.
Params:
- hash: The hash to remove from the map.
Returns:
- error: An error if the hash does not exist in the map, nil otherwise.
func (*NativeMapUint64) Exists ΒΆ added in v1.5.2
func (s *NativeMapUint64) Exists(hash chainhash.Hash) bool
Exists checks if the given hash exists in the map. It returns true if the hash is found, false otherwise.
Params:
- hash: The hash to check for existence in the map.
Returns:
- bool: True if the hash exists in the map, false otherwise.
func (*NativeMapUint64) Freeze ΒΆ added in v1.5.2
func (s *NativeMapUint64) Freeze()
Freeze marks the map read-only. See the lifecycle notes at the top of this file.
func (*NativeMapUint64) Get ΒΆ added in v1.5.2
func (s *NativeMapUint64) Get(hash chainhash.Hash) (uint64, bool)
Get retrieves the uint64 value associated with the given hash from the map. It locks the map for reading, checks if the hash exists, and returns the value and a boolean indicating success. If the hash does not exist, it returns 0 and false.
Params:
- hash: The hash to retrieve from the map.
Returns:
- uint64: The value associated with the hash, or 0 if the hash does not exist.
- bool: True if the hash was found in the map, false otherwise.
func (*NativeMapUint64) Iter ΒΆ added in v1.5.2
func (s *NativeMapUint64) Iter(f func(hash chainhash.Hash, value uint64) bool)
Iter iterates over all key-value pairs in the map and applies the provided function to each pair. Stops iterating if the function returns true.
Params:
- f: A function that takes a hash and its associated uint64 value.
func (*NativeMapUint64) Keys ΒΆ added in v1.5.2
func (s *NativeMapUint64) Keys() []chainhash.Hash
Keys returns a slice of all hashes currently stored in the map. It locks the map for reading, iterates over the map, and collects the keys. The order of keys is not guaranteed.
Returns:
- []chainhash.Hash: A slice containing all the hashes in the map.
func (*NativeMapUint64) Length ΒΆ added in v1.5.2
func (s *NativeMapUint64) Length() int
Length returns the current number of hashes in the map. It locks the map for reading and returns the length.
Returns:
- int: The number of hashes currently stored in the map.
func (*NativeMapUint64) Map ΒΆ added in v1.5.2
func (s *NativeMapUint64) Map() map[chainhash.Hash]uint64
Map returns the underlying native map used by NativeMapUint64.
Returns:
- map[chainhash.Hash]uint64: The underlying native map.
func (*NativeMapUint64) Put ΒΆ added in v1.5.2
func (s *NativeMapUint64) Put(hash chainhash.Hash, n uint64) error
Put adds a new hash with an associated uint64 value to the map. It checks if the hash already exists in the map and returns an error if it does. If the hash does not exist, it adds the hash and increments the length of the map.
Params:
- hash: The hash to add to the map.
- n: The uint64 value to associate with the hash.
Returns:
- error: An error if the hash already exists in the map, nil otherwise.
func (*NativeMapUint64) PutMulti ΒΆ added in v1.5.2
func (s *NativeMapUint64) PutMulti(hashes []chainhash.Hash, n uint64) error
PutMulti adds multiple hashes with an associated uint64 value to the map. It checks if any of the hashes already exist in the map and returns an error if any do. If none of the hashes exist, it adds each hash with the value and increments the length of the map.
Params:
- hashes: A slice of hashes to add to the map.
- n: The uint64 value to associate with each hash.
Returns:
- error: An error if any of the hashes already exist in the map, nil otherwise.
func (*NativeMapUint64) Set ΒΆ added in v1.5.2
func (s *NativeMapUint64) Set(hash chainhash.Hash, value uint64) error
Set updates the value associated with the given hash in the map. It will error out if the hash does not exist.
Params:
- hash: The hash to update in the map.
- value: The value to associate with the hash (not used in this map).
Returns:
- error: An error if the hash does not exist in the map, nil otherwise.
func (*NativeMapUint64) SetIfExists ΒΆ added in v1.5.2
SetIfExists updates the value associated with the given hash in the map if it exists. It returns a boolean indicating whether the hash was found and updated. If the hash does not exist, it returns false and no error.
Params:
- hash: The hash to update in the map.
- value: The value to associate with the hash.
Returns:
- bool: True if the hash was found and updated, false otherwise.
- error: An error if there was an issue updating the hash, nil otherwise.
func (*NativeMapUint64) SetIfNotExists ΒΆ added in v1.5.2
SetIfNotExists adds the hash with the given value to the map only if the hash does not already exist. It returns a boolean indicating whether the hash was added. If the hash already exists, it returns false and no error.
Params:
- hash: The hash to add to the map.
- value: The value to associate with the hash.
Returns:
- bool: True if the hash was added, false if it already existed.
- error: An error if there was an issue adding the hash, nil otherwise.
type NativeSplitLockFreeMapUint64 ΒΆ added in v1.5.2
type NativeSplitLockFreeMapUint64 struct {
// contains filtered or unexported fields
}
NativeSplitLockFreeMapUint64 is a map that splits the data into multiple buckets to reduce contention. It uses NativeLockFreeMapUint64 for each bucket to store the hashes and their associated uint64 values.
func NewDefaultSplitLockFreeMapUint64 ΒΆ added in v1.5.2
func NewDefaultSplitLockFreeMapUint64(length int, buckets ...uint64) *NativeSplitLockFreeMapUint64
NewDefaultSplitLockFreeMapUint64 returns a native split lock-free map.
func NewNativeSplitLockFreeMapUint64 ΒΆ added in v1.5.2
func NewNativeSplitLockFreeMapUint64(length int, buckets ...uint64) *NativeSplitLockFreeMapUint64
NewNativeSplitLockFreeMapUint64 creates a new NativeSplitLockFreeMapUint64 with the specified initial length. The length is used to preallocate the size of each bucket. It divides the length by the number of buckets to determine the size of each bucket.
Params:
- length: The initial length of the map, used for preallocation.
Returns:
- *NativeSplitLockFreeMapUint64: A pointer to the newly created NativeSplitLockFreeMapUint64 instance.
func NewSplitLockFreeMapNativeUint64 ΒΆ added in v1.5.2
func NewSplitLockFreeMapNativeUint64(length int, buckets ...uint64) *NativeSplitLockFreeMapUint64
NewSplitLockFreeMapNativeUint64 creates a split lock-free map using Go's native map.
func (*NativeSplitLockFreeMapUint64) Clear ΒΆ added in v1.5.2
func (g *NativeSplitLockFreeMapUint64) Clear()
Clear empties and un-freezes every bucket, recycling the split map for reuse.
func (*NativeSplitLockFreeMapUint64) DeleteBucket ΒΆ added in v1.5.2
func (g *NativeSplitLockFreeMapUint64) DeleteBucket(h uint64)
DeleteBucket removes the bucket at index h from the map of buckets.
func (*NativeSplitLockFreeMapUint64) Exists ΒΆ added in v1.5.2
func (g *NativeSplitLockFreeMapUint64) Exists(hash uint64) bool
Exists checks if the given hash exists in the map. It calculates the bucket index using the modulo operation and checks the corresponding bucket.
Params:
- hash: The hash to check for existence in the map.
Returns:
- bool: True if the hash exists in the map, false otherwise.
Considerations: This method does not lock the map, so it is not suitable for concurrent access.
func (*NativeSplitLockFreeMapUint64) Freeze ΒΆ added in v1.5.2
func (g *NativeSplitLockFreeMapUint64) Freeze()
Freeze freezes every bucket; subsequent Put calls return ErrMapFrozen.
func (*NativeSplitLockFreeMapUint64) Get ΒΆ added in v1.5.2
func (g *NativeSplitLockFreeMapUint64) Get(hash uint64) (uint64, bool)
Get retrieves the uint64 value associated with the given hash from the map. It calculates the bucket index using the modulo operation and retrieves the value from the corresponding bucket.
Params:
- hash: The hash to retrieve from the map.
Returns:
- uint64: The value associated with the hash, or 0 if the hash does not exist.
- bool: True if the hash was found in the map, false otherwise.
Considerations: This method does not lock the map, so it is not suitable for concurrent access.
func (*NativeSplitLockFreeMapUint64) IterAll ΒΆ added in v1.5.2
func (g *NativeSplitLockFreeMapUint64) IterAll(f func(k, v uint64) (stop bool))
IterAll iterates over all key-value pairs across all buckets. Stops if f returns true.
func (*NativeSplitLockFreeMapUint64) Length ΒΆ added in v1.5.2
func (g *NativeSplitLockFreeMapUint64) Length() int
Length returns the current number of hashes in the map. It iterates over all buckets and sums their lengths to get the total count. It uses atomic operations to ensure thread safety.
Returns:
- int: The number of hashes currently stored in the map.
func (*NativeSplitLockFreeMapUint64) Map ΒΆ added in v1.5.2
func (g *NativeSplitLockFreeMapUint64) Map() map[uint64]*NativeLockFreeMapUint64
Map returns the underlying map of buckets used by NativeSplitLockFreeMapUint64. It provides access to the map for operations that do not require locking.
Returns:
- map[uint64]*NativeLockFreeMapUint64: A map where the keys are bucket indices and the values are pointers to NativeLockFreeMapUint64 instances.
Considerations: This method does not lock the map, so it is not suitable for concurrent access.
func (*NativeSplitLockFreeMapUint64) Put ΒΆ added in v1.5.2
func (g *NativeSplitLockFreeMapUint64) Put(hash, n uint64) error
Put adds a new hash with an associated uint64 value to the map. It calculates the bucket index using the modulo operation and adds the hash to the corresponding bucket. It checks if the hash already exists in the bucket and returns an error if it does.
Params:
- hash: The hash to add to the map.
- n: The uint64 value to associate with the hash.
Returns:
- error: An error if the hash already exists in the map, nil otherwise.
Considerations: This method does not lock the map, so it is not suitable for concurrent access.
type NativeSplitMap ΒΆ added in v1.5.2
type NativeSplitMap struct {
// contains filtered or unexported fields
}
NativeSplitMap splits the data into multiple buckets to reduce contention. It uses NativeMapUint64 for each bucket. NativeMapUint64 is concurrent-safe.
func NewDefaultSplitMap ΒΆ added in v1.5.2
func NewDefaultSplitMap(length int, buckets ...uint16) *NativeSplitMap
NewDefaultSplitMap returns a native split map implementation. Use for general-purpose hash-to-uint64 split maps when speed matters. Use NewSplitSwissMap when memory is constrained.
func NewNativeSplitMap ΒΆ added in v1.5.2
func NewNativeSplitMap(length int, buckets ...uint16) *NativeSplitMap
NewNativeSplitMap creates a new NativeSplitMap with the specified initial length. The length is used to preallocate the size of each bucket. It divides the length by the number of buckets to determine the size of each bucket.
Params:
- length: The initial length of the map, used for preallocation.
Returns:
- *NativeSplitMap: A pointer to the newly created NativeSplitMap instance.
Considerations: The number of buckets is fixed at 1024, and the length is divided by this number to determine the size of each bucket.
func (*NativeSplitMap) Buckets ΒΆ added in v1.5.2
func (g *NativeSplitMap) Buckets() uint16
Buckets returns the number of buckets in the NativeSplitMap.
func (*NativeSplitMap) Clear ΒΆ added in v1.5.2
func (g *NativeSplitMap) Clear()
Clear empties and un-freezes every bucket, recycling the split map for reuse.
func (*NativeSplitMap) Delete ΒΆ added in v1.5.2
func (g *NativeSplitMap) Delete(hash chainhash.Hash) error
Delete removes a hash from the map. It calculates the bucket index using the Bytes2Uint16Buckets function and checks the corresponding bucket for the hash.
Params:
- hash: The hash to remove from the map.
Returns:
- error: An error if the hash does not exist in the map or if the bucket does not exist, nil otherwise.
func (*NativeSplitMap) Exists ΒΆ added in v1.5.2
func (g *NativeSplitMap) Exists(hash chainhash.Hash) bool
Exists checks if the given hash exists in the map. It calculates the bucket index using the Bytes2Uint16Buckets function and checks the corresponding bucket.
Params:
- hash: The hash to check for existence in the map.
Returns:
- bool: True if the hash exists in the map, false otherwise.
func (*NativeSplitMap) Freeze ΒΆ added in v1.5.2
func (g *NativeSplitMap) Freeze()
Freeze freezes every bucket, so reads across the whole split map become lock-free. See the lifecycle notes at the top of this file.
func (*NativeSplitMap) Get ΒΆ added in v1.5.2
func (g *NativeSplitMap) Get(hash chainhash.Hash) (uint64, bool)
Get retrieves the uint64 value associated with the given hash from the map. It calculates the bucket index using the Bytes2Uint16Buckets function and retrieves the value from the corresponding bucket.
Params:
- hash: The hash to retrieve from the map.
Returns:
- uint64: The value associated with the hash, or 0 if the hash does not exist.
- bool: True if the hash was found in the map, false otherwise.
func (*NativeSplitMap) Iter ΒΆ added in v1.5.2
func (g *NativeSplitMap) Iter(f func(hash chainhash.Hash, value uint64) bool)
Iter iterates over all key-value pairs in the map and applies the provided function to each pair. Stops iterating if the function returns true.
Params:
- f: A function that takes a hash and its associated uint64 value.
func (*NativeSplitMap) Keys ΒΆ added in v1.5.2
func (g *NativeSplitMap) Keys() []chainhash.Hash
Keys returns a slice of all hashes currently stored in the map. It iterates over all buckets and collects the keys from each bucket. The order of keys is not guaranteed.
Returns:
- []chainhash.Hash: A slice containing all the hashes in the map.
func (*NativeSplitMap) Length ΒΆ added in v1.5.2
func (g *NativeSplitMap) Length() int
Length returns the current number of hashes in the map. It iterates over all buckets and sums their lengths to get the total count.
Returns:
- int: The number of hashes currently stored in the map.
func (*NativeSplitMap) Map ΒΆ added in v1.5.2
func (g *NativeSplitMap) Map() *NativeMapUint64
Map returns the underlying map of all buckets used by NativeSplitMap.
Returns:
- TxMap: A map where the keys are bucket indices and the values are pointers to NativeMapUint64 instances.
func (*NativeSplitMap) Put ΒΆ added in v1.5.2
func (g *NativeSplitMap) Put(hash chainhash.Hash, n uint64) error
Put adds a new hash with an associated uint64 value to the map. It calculates the bucket index using the Bytes2Uint16Buckets function and adds the hash to the corresponding bucket. It checks if the hash already exists in the bucket and returns an error if it does.
Params:
- hash: The hash to add to the map.
- n: The uint64 value to associate with the hash.
Returns:
- error: An error if the hash already exists in the map, nil otherwise.
func (*NativeSplitMap) PutMulti ΒΆ added in v1.5.2
func (g *NativeSplitMap) PutMulti(hashes []chainhash.Hash, n uint64) (err error)
PutMulti adds multiple hashes with an associated uint64 value to the map. It iterates over the hashes, calculates the bucket index for each hash using the Bytes2Uint16Buckets function, and adds each hash to the corresponding bucket. It checks if any of the hashes already exist in the bucket and returns an error if any do.
Params:
- hashes: A slice of hashes to add to the map.
- n: The uint64 value to associate with each hash.
Returns:
- error: An error if any of the hashes already exist in the map, nil otherwise.
func (*NativeSplitMap) PutMultiBucket ΒΆ added in v1.5.2
PutMultiBucket adds multiple hashes with an associated uint64 value to a specific bucket. It checks if the bucket exists and then adds the hashes directly to that bucket.
Params:
- bucket: The bucket index to add the hashes to.
- hashes: A slice of hashes to add to the specified bucket.
- n: The uint64 value to associate with each hash.
Returns:
- error: An error if the bucket does not exist or if there is an issue adding the hashes, nil otherwise.
func (*NativeSplitMap) Set ΒΆ added in v1.5.2
func (g *NativeSplitMap) Set(hash chainhash.Hash, value uint64) error
Set updates the value associated with the given hash in the map.
Params:
- hash: The hash to update in the map.
- value: The value to associate with the hash.
Returns:
- error: An error if the hash does not exist in the map, nil otherwise.
func (*NativeSplitMap) SetIfExists ΒΆ added in v1.5.2
SetIfExists updates the value associated with the given hash in the map if it exists. It returns a boolean indicating whether the hash was found and updated. If the hash does not exist, it returns false and no error.
Params:
- hash: The hash to update in the map.
- value: The value to associate with the hash.
Returns:
- bool: True if the hash was found and updated, false otherwise.
- error: An error if there was an issue updating the hash, nil otherwise.
func (*NativeSplitMap) SetIfNotExists ΒΆ added in v1.5.2
SetIfNotExists adds the hash with the given value to the map only if the hash does not already exist. It returns a boolean indicating whether the hash was added. If the hash already exists, it returns false and no error.
Params:
- hash: The hash to add to the map.
- value: The value to associate with the hash.
Returns:
- bool: True if the hash was added, false if it already existed.
- error: An error if there was an issue adding the hash, nil otherwise.
type NativeSplitMapUint64 ΒΆ added in v1.5.2
type NativeSplitMapUint64 struct {
// contains filtered or unexported fields
}
NativeSplitMapUint64 splits the data into multiple buckets to reduce contention. It uses NativeMapUint64 for each bucket. Buckets is fixed at 1024.
func NewDefaultSplitMapUint64 ΒΆ added in v1.5.2
func NewDefaultSplitMapUint64(length uint32, buckets ...uint16) *NativeSplitMapUint64
NewDefaultSplitMapUint64 returns a native split map implementation. Use for general-purpose hash-to-uint64 split maps when speed matters. Use NewSplitSwissMapUint64 when memory is constrained.
func NewNativeSplitMapUint64 ΒΆ added in v1.5.2
func NewNativeSplitMapUint64(length uint32, buckets ...uint16) *NativeSplitMapUint64
NewNativeSplitMapUint64 creates a new NativeSplitMapUint64 with the specified initial length. The length is used to preallocate the size of each bucket. It divides the length by the number of buckets to determine the size of each bucket.
Params:
- length: The initial length of the map, used for preallocation.
Returns:
- *NativeSplitMapUint64: A pointer to the newly created NativeSplitMapUint64 instance.
func (*NativeSplitMapUint64) Clear ΒΆ added in v1.5.2
func (g *NativeSplitMapUint64) Clear()
Clear empties and un-freezes every bucket, recycling the split map for reuse.
func (*NativeSplitMapUint64) Delete ΒΆ added in v1.5.2
func (g *NativeSplitMapUint64) Delete(hash chainhash.Hash) error
Delete removes a hash from the map. It calculates the bucket index using the Bytes2Uint16Buckets function and checks the corresponding bucket for the hash. If the hash does not exist, it returns an error.
Params:
- hash: The hash to remove from the map.
Returns:
- error: An error if the hash does not exist in the map or if the bucket does not exist, nil otherwise.
func (*NativeSplitMapUint64) Exists ΒΆ added in v1.5.2
func (g *NativeSplitMapUint64) Exists(hash chainhash.Hash) bool
Exists checks if the given hash exists in the map. It calculates the bucket index using the Bytes2Uint16Buckets function and checks the corresponding bucket.
Params:
- hash: The hash to check for existence in the map.
Returns:
- bool: True if the hash exists in the map, false otherwise.
func (*NativeSplitMapUint64) Freeze ΒΆ added in v1.5.2
func (g *NativeSplitMapUint64) Freeze()
Freeze freezes every bucket, so reads across the whole split map become lock-free. See the lifecycle notes at the top of this file.
func (*NativeSplitMapUint64) Get ΒΆ added in v1.5.2
func (g *NativeSplitMapUint64) Get(hash chainhash.Hash) (uint64, bool)
Get retrieves the uint64 value associated with the given hash from the map. It calculates the bucket index using the Bytes2Uint16Buckets function and retrieves the value from the corresponding bucket.
Params:
- hash: The hash to retrieve from the map.
Returns:
- uint64: The value associated with the hash, or 0 if the hash does not exist.
- bool: True if the hash was found in the map, false otherwise.
func (*NativeSplitMapUint64) Iter ΒΆ added in v1.5.2
func (g *NativeSplitMapUint64) Iter(f func(hash chainhash.Hash, value uint64) bool)
Iter iterates over all key-value pairs in the map and applies the provided function to each pair. Stops iterating if the function returns true.
Params:
- f: A function that takes a hash and its associated uint64 value.
func (*NativeSplitMapUint64) Keys ΒΆ added in v1.5.2
func (g *NativeSplitMapUint64) Keys() []chainhash.Hash
Keys returns a slice of all hashes currently stored in the map. It iterates over all buckets and collects the keys from each bucket. The order of keys is not guaranteed.
Returns:
- []chainhash.Hash: A slice containing all the hashes in the map.
func (*NativeSplitMapUint64) Length ΒΆ added in v1.5.2
func (g *NativeSplitMapUint64) Length() int
Length returns the current number of hashes in the map. It iterates over all buckets and sums their lengths to get the total count.
Returns:
- int: The number of hashes currently stored in the map.
func (*NativeSplitMapUint64) Map ΒΆ added in v1.5.2
func (g *NativeSplitMapUint64) Map() map[uint16]*NativeMapUint64
Map returns the underlying map of buckets used by NativeSplitMapUint64.
Returns:
- map[uint16]*NativeMapUint64: A map where the keys are bucket indices and the values are pointers to NativeMapUint64 instances.
func (*NativeSplitMapUint64) Put ΒΆ added in v1.5.2
func (g *NativeSplitMapUint64) Put(hash chainhash.Hash, n uint64) error
Put adds a new hash with an associated uint64 value to the map. It calculates the bucket index using the Bytes2Uint16Buckets function and adds the hash to the corresponding bucket. It checks if the hash already exists in the bucket and returns an error if it does.
Params:
- hash: The hash to add to the map.
- n: The uint64 value to associate with the hash.
Returns:
- error: An error if the hash already exists in the map, nil otherwise.
func (*NativeSplitMapUint64) PutMulti ΒΆ added in v1.5.2
func (g *NativeSplitMapUint64) PutMulti(hashes []chainhash.Hash, n uint64) error
PutMulti adds multiple hashes with an associated uint64 value to the map. It iterates over the hashes, calculates the bucket index for each hash using the Bytes2Uint16Buckets function, and adds each hash to the corresponding bucket. It checks if any of the hashes already exist in the bucket and returns an error if any do.
Params:
- hashes: A slice of hashes to add to the map.
- n: The uint64 value to associate with each hash.
Returns:
- error: An error if any of the hashes already exist in the map, nil otherwise.
func (*NativeSplitMapUint64) Set ΒΆ added in v1.5.2
func (g *NativeSplitMapUint64) Set(hash chainhash.Hash, value uint64) error
Set updates the value associated with the given hash in the map. It will error out if the hash does not exist.
Params:
- hash: The hash to update in the map.
- value: The value to associate with the hash.
Returns:
- error: An error if the hash does not exist in the map, nil otherwise.
func (*NativeSplitMapUint64) SetIfExists ΒΆ added in v1.5.2
SetIfExists updates the value associated with the given hash in the map if it exists. It returns a boolean indicating whether the hash was found and updated. If the hash does not exist, it returns false and no error.
Params:
- hash: The hash to update in the map.
- value: The value to associate with the hash.
Returns:
- bool: True if the hash was found and updated, false otherwise.
- error: An error if there was an issue updating the hash, nil otherwise.
func (*NativeSplitMapUint64) SetIfNotExists ΒΆ added in v1.5.2
SetIfNotExists adds the hash with the given value to the map only if the hash does not already exist. It returns a boolean indicating whether the hash was added. If the hash already exists, it returns false and no error.
Params:
- hash: The hash to add to the map.
- value: The value to associate with the hash.
Returns:
- bool: True if the hash was added, false if it already existed.
- error: An error if there was an issue adding the hash, nil otherwise.
type SplitLockFreeMapUint64 ΒΆ added in v1.5.2
type SplitLockFreeMapUint64 = NativeSplitLockFreeMapUint64
SplitLockFreeMapUint64 is the default split lock-free map type using native implementation. Use NewDefaultSplitLockFreeMapUint64 to create instances.
type SplitLockFreeMapUint64Like ΒΆ added in v1.5.2
type SplitLockFreeMapUint64Like interface {
Put(hash, value uint64) error
Get(hash uint64) (uint64, bool)
Length() int
Exists(hash uint64) bool
IterAll(f func(k, v uint64) (stop bool))
DeleteBucket(h uint64)
}
SplitLockFreeMapUint64Like is the interface for split lock-free maps (dolthub or native).
type SplitMap ΒΆ added in v1.5.2
type SplitMap = NativeSplitMap
SplitMap is the default split map type using native implementation. Use NewDefaultSplitMap to create instances.
type SplitMapUint64 ΒΆ added in v1.5.2
type SplitMapUint64 = NativeSplitMapUint64
SplitMapUint64 is the default split map uint64 type using native implementation. Use NewDefaultSplitMapUint64 to create instances.
type SplitSwissLockFreeMapUint64 ΒΆ
type SplitSwissLockFreeMapUint64 struct {
// contains filtered or unexported fields
}
SplitSwissLockFreeMapUint64 is a map that splits the data into multiple buckets to reduce contention. It uses SwissLockFreeMapUint64 for each bucket to store the hashes and their associated uint64 values.
func NewSplitLockFreeMapDolthubUint64 ΒΆ added in v1.5.2
func NewSplitLockFreeMapDolthubUint64(length int, buckets ...uint64) *SplitSwissLockFreeMapUint64
NewSplitLockFreeMapDolthubUint64 creates a split lock-free map using dolthub/swiss (~30% less memory at 100M entries).
func NewSplitSwissLockFreeMapUint64 ΒΆ
func NewSplitSwissLockFreeMapUint64(length int, buckets ...uint64) *SplitSwissLockFreeMapUint64
NewSplitSwissLockFreeMapUint64 creates a new SplitSwissLockFreeMapUint64 with the specified initial length.
func (*SplitSwissLockFreeMapUint64) Clear ΒΆ added in v1.5.2
func (g *SplitSwissLockFreeMapUint64) Clear()
Clear empties and un-freezes every bucket, recycling the split map for reuse.
func (*SplitSwissLockFreeMapUint64) DeleteBucket ΒΆ added in v1.5.2
func (g *SplitSwissLockFreeMapUint64) DeleteBucket(h uint64)
DeleteBucket removes the bucket at index h from the map of buckets.
func (*SplitSwissLockFreeMapUint64) Exists ΒΆ
func (g *SplitSwissLockFreeMapUint64) Exists(hash uint64) bool
Exists checks if the given hash exists in the map. It calculates the bucket index using the modulo operation and checks the corresponding bucket.
Params:
- hash: The hash to check for existence in the map.
Returns:
- bool: True if the hash exists in the map, false otherwise.
Considerations: This method does not lock the map, so it is not suitable for concurrent access.
func (*SplitSwissLockFreeMapUint64) Freeze ΒΆ added in v1.5.2
func (g *SplitSwissLockFreeMapUint64) Freeze()
Freeze freezes every bucket; subsequent Put calls return ErrMapFrozen.
func (*SplitSwissLockFreeMapUint64) Get ΒΆ
func (g *SplitSwissLockFreeMapUint64) Get(hash uint64) (uint64, bool)
Get retrieves the uint64 value associated with the given hash from the map. It calculates the bucket index using the modulo operation and retrieves the value from the corresponding bucket.
Params:
- hash: The hash to retrieve from the map.
Returns:
- uint64: The value associated with the hash, or 0 if the hash does not exist.
- bool: True if the hash was found in the map, false otherwise.
Considerations: This method does not lock the map, so it is not suitable for concurrent access.
func (*SplitSwissLockFreeMapUint64) IterAll ΒΆ added in v1.5.2
func (g *SplitSwissLockFreeMapUint64) IterAll(f func(k, v uint64) (stop bool))
IterAll iterates over all key-value pairs across all buckets. Stops if f returns true.
func (*SplitSwissLockFreeMapUint64) Length ΒΆ
func (g *SplitSwissLockFreeMapUint64) Length() int
Length returns the current number of hashes in the map. It iterates over all buckets and sums their lengths to get the total count. It uses atomic operations to ensure thread safety.
Returns:
- int: The number of hashes currently stored in the map.
func (*SplitSwissLockFreeMapUint64) Map ΒΆ
func (g *SplitSwissLockFreeMapUint64) Map() map[uint64]*SwissLockFreeMapUint64
Map returns the underlying map of buckets used by SplitSwissLockFreeMapUint64. It provides access to the map for operations that do not require locking.
Returns:
- map[uint64]*SwissLockFreeMapUint64: A map where the keys are bucket indices and the values are pointers to SwissLockFreeMapUint64 instances.
Considerations: This method does not lock the map, so it is not suitable for concurrent access.
func (*SplitSwissLockFreeMapUint64) Put ΒΆ
func (g *SplitSwissLockFreeMapUint64) Put(hash, n uint64) error
Put adds a new hash with an associated uint64 value to the map. It calculates the bucket index using the modulo operation and adds the hash to the corresponding bucket. It checks if the hash already exists in the bucket and returns an error if it does.
Params:
- hash: The hash to add to the map.
- n: The uint64 value to associate with the hash.
Returns:
- error: An error if the hash already exists in the map, nil otherwise.
Considerations: This method does not lock the map, so it is not suitable for concurrent access.
type SplitSwissMap ΒΆ
type SplitSwissMap struct {
// contains filtered or unexported fields
}
SplitSwissMap is a map that splits the data into multiple buckets to reduce contention. It uses SwissMapUint64 for each bucket to store the hashes and their associated uint64 values. Since SwissMapUint64 is concurrent-safe, SplitSwissMap can handle concurrent access without additional locks.
func NewSplitSwissMap ΒΆ
func NewSplitSwissMap(length int, buckets ...uint16) *SplitSwissMap
NewSplitSwissMap creates a new SplitSwissMap with the specified initial length. The length is used to preallocate the size of each bucket. It divides the length by the number of buckets to determine the size of each bucket.
Params:
- length: The initial length of the map, used for preallocation.
Returns:
- *SplitSwissMap: A pointer to the newly created SplitSwissMap instance.
Considerations: The number of buckets is fixed at 1024, and the length is divided by this number to determine the size of each bucket.
func (*SplitSwissMap) Buckets ΒΆ
func (g *SplitSwissMap) Buckets() uint16
Buckets returns the number of buckets in the SplitSwissMap.
func (*SplitSwissMap) Clear ΒΆ added in v1.5.2
func (g *SplitSwissMap) Clear()
Clear empties and un-freezes every bucket, recycling the split map for reuse.
func (*SplitSwissMap) Delete ΒΆ
func (g *SplitSwissMap) Delete(hash chainhash.Hash) error
Delete removes a hash from the map. It calculates the bucket index using the Bytes2Uint16Buckets function and checks the corresponding bucket for the hash.
Params:
- hash: The hash to remove from the map.
Returns:
- error: An error if the hash does not exist in the map or if the bucket does not exist, nil otherwise.
func (*SplitSwissMap) Exists ΒΆ
func (g *SplitSwissMap) Exists(hash chainhash.Hash) bool
Exists checks if the given hash exists in the map. It calculates the bucket index using the Bytes2Uint16Buckets function and checks the corresponding bucket.
Params:
- hash: The hash to check for existence in the map.
Returns:
- bool: True if the hash exists in the map, false otherwise.
func (*SplitSwissMap) Freeze ΒΆ added in v1.5.2
func (g *SplitSwissMap) Freeze()
Freeze freezes every bucket, so reads across the whole split map become lock-free. See the lifecycle notes at the top of this file.
func (*SplitSwissMap) Get ΒΆ
func (g *SplitSwissMap) Get(hash chainhash.Hash) (uint64, bool)
Get retrieves the uint64 value associated with the given hash from the map. It calculates the bucket index using the Bytes2Uint16Buckets function and retrieves the value from the corresponding bucket.
Params:
- hash: The hash to retrieve from the map.
Returns:
- uint64: The value associated with the hash, or 0 if the hash does not exist.
- bool: True if the hash was found in the map, false otherwise.
func (*SplitSwissMap) Iter ΒΆ added in v1.5.2
func (g *SplitSwissMap) Iter(f func(hash chainhash.Hash, value uint64) bool)
Iter iterates over all key-value pairs in the map and applies the provided function to each pair. Stops iterating if the function returns true.
Params:
- f: A function that takes a hash and its associated uint64 value.
func (*SplitSwissMap) Keys ΒΆ
func (g *SplitSwissMap) Keys() []chainhash.Hash
Keys returns a slice of all hashes currently stored in the map. It iterates over all buckets and collects the keys from each bucket. The order of keys is not guaranteed.
Returns:
- []chainhash.Hash: A slice containing all the hashes in the map.
func (*SplitSwissMap) Length ΒΆ
func (g *SplitSwissMap) Length() int
Length returns the current number of hashes in the map. It iterates over all buckets and sums their lengths to get the total count.
Returns:
- int: The number of hashes currently stored in the map.
func (*SplitSwissMap) Map ΒΆ
func (g *SplitSwissMap) Map() *SwissMapUint64
Map returns the underlying map of all buckets used by SplitSwissMap.
Returns:
- TxMap: A map where the keys are bucket indices and the values are pointers to SwissMapUint64 instances.
func (*SplitSwissMap) Put ΒΆ
func (g *SplitSwissMap) Put(hash chainhash.Hash, n uint64) error
Put adds a new hash with an associated uint64 value to the map. It calculates the bucket index using the Bytes2Uint16Buckets function and adds the hash to the corresponding bucket. It checks if the hash already exists in the bucket and returns an error if it does.
Params:
- hash: The hash to add to the map.
- n: The uint64 value to associate with the hash.
Returns:
- error: An error if the hash already exists in the map, nil otherwise.
func (*SplitSwissMap) PutMulti ΒΆ
func (g *SplitSwissMap) PutMulti(hashes []chainhash.Hash, n uint64) (err error)
PutMulti adds multiple hashes with an associated uint64 value to the map. It iterates over the hashes, calculates the bucket index for each hash using the Bytes2Uint16Buckets function, and adds each hash to the corresponding bucket. It checks if any of the hashes already exist in the bucket and returns an error if any do.
Params:
- hashes: A slice of hashes to add to the map.
- n: The uint64 value to associate with each hash.
Returns:
- error: An error if any of the hashes already exist in the map, nil otherwise.
func (*SplitSwissMap) PutMultiBucket ΒΆ
PutMultiBucket adds multiple hashes with an associated uint64 value to a specific bucket. It checks if the bucket exists and then adds the hashes directly to that bucket.
Params:
- bucket: The bucket index to add the hashes to.
- hashes: A slice of hashes to add to the specified bucket.
- n: The uint64 value to associate with each hash.
Returns:
- error: An error if the bucket does not exist or if there is an issue adding the hashes, nil otherwise.
func (*SplitSwissMap) Set ΒΆ added in v1.5.2
func (g *SplitSwissMap) Set(hash chainhash.Hash, value uint64) error
Set updates the value associated with the given hash in the map.
Params:
- hash: The hash to update in the map.
- value: The value to associate with the hash.
Returns:
- error: An error if the hash does not exist in the map, nil otherwise.
func (*SplitSwissMap) SetIfExists ΒΆ added in v1.5.2
SetIfExists updates the value associated with the given hash in the map if it exists. It returns a boolean indicating whether the hash was found and updated. If the hash does not exist, it returns false and no error.
Params:
- hash: The hash to update in the map.
- value: The value to associate with the hash.
Returns:
- bool: True if the hash was found and updated, false otherwise.
- error: An error if there was an issue updating the hash, nil otherwise.
func (*SplitSwissMap) SetIfNotExists ΒΆ added in v1.5.2
SetIfNotExists adds the hash with the given value to the map only if the hash does not already exist. It returns a boolean indicating whether the hash was added. If the hash already exists, it returns false and no error.
Params:
- hash: The hash to add to the map.
- value: The value to associate with the hash.
Returns:
- bool: True if the hash was added, false if it already existed.
- error: An error if there was an issue adding the hash, nil otherwise.
type SplitSwissMapUint64 ΒΆ
type SplitSwissMapUint64 struct {
// contains filtered or unexported fields
}
SplitSwissMapUint64 is a map that splits the data into multiple buckets to reduce contention. It uses SwissMapUint64 for each bucket to store the hashes and their associated uint64 values. The number of buckets is fixed at 1024, and the length is divided by this number to determine the size of each bucket.
func NewSplitSwissMapUint64 ΒΆ
func NewSplitSwissMapUint64(length uint32, buckets ...uint16) *SplitSwissMapUint64
NewSplitSwissMapUint64 creates a new SplitSwissMapUint64 with the specified initial length. The length is used to preallocate the size of each bucket. It divides the length by the number of buckets to determine the size of each bucket, and applies a 20% headroom so the underlying dolthub/swiss map does not rehash on natural Binomial(N, 1/B) hash variance.
Params:
- length: The initial length of the map, used for preallocation.
Returns:
- *SplitSwissMapUint64: A pointer to the newly created SplitSwissMapUint64 instance.
func (*SplitSwissMapUint64) Clear ΒΆ added in v1.5.2
func (g *SplitSwissMapUint64) Clear()
Clear empties every bucket without releasing the per-bucket group/ctrl backing storage. Intended for sync.Pool reuse: a 30 GB SplitSwissMapUint64 can be Clear()ed in a few milliseconds (zeroing ctrl bytes) and handed back to the pool without re-allocating any of its buckets.
Each underlying SwissMapUint64.Clear takes the write lock β concurrent readers will block until Clear completes. Callers should ensure no other goroutine is using the map before calling Clear.
func (*SplitSwissMapUint64) Delete ΒΆ
func (g *SplitSwissMapUint64) Delete(hash chainhash.Hash) error
Delete removes a hash from the map. It calculates the bucket index using the Bytes2Uint16Buckets function and checks the corresponding bucket for the hash. If the hash does not exist, it returns an error.
Params:
- hash: The hash to remove from the map.
Returns:
- error: An error if the hash does not exist in the map or if the bucket does not exist, nil otherwise.
func (*SplitSwissMapUint64) Exists ΒΆ
func (g *SplitSwissMapUint64) Exists(hash chainhash.Hash) bool
Exists checks if the given hash exists in the map. It calculates the bucket index using the Bytes2Uint16Buckets function and checks the corresponding bucket.
Params:
- hash: The hash to check for existence in the map.
Returns:
- bool: True if the hash exists in the map, false otherwise.
func (*SplitSwissMapUint64) Freeze ΒΆ added in v1.5.2
func (g *SplitSwissMapUint64) Freeze()
Freeze freezes every bucket, so reads across the whole split map become lock-free. See the lifecycle notes at the top of this file.
func (*SplitSwissMapUint64) Get ΒΆ
func (g *SplitSwissMapUint64) Get(hash chainhash.Hash) (uint64, bool)
Get retrieves the uint64 value associated with the given hash from the map. It calculates the bucket index using the Bytes2Uint16Buckets function and retrieves the value from the corresponding bucket.
Params:
- hash: The hash to retrieve from the map.
Returns:
- uint64: The value associated with the hash, or 0 if the hash does not exist.
- bool: True if the hash was found in the map, false otherwise.
func (*SplitSwissMapUint64) Iter ΒΆ added in v1.5.2
func (g *SplitSwissMapUint64) Iter(f func(hash chainhash.Hash, value uint64) bool)
Iter iterates over all key-value pairs in the map and applies the provided function to each pair. Stops iterating if the function returns true.
Params:
- f: A function that takes a hash and its associated uint64 value.
func (*SplitSwissMapUint64) Keys ΒΆ
func (g *SplitSwissMapUint64) Keys() []chainhash.Hash
Keys returns a slice of all hashes currently stored in the map. It iterates over all buckets and collects the keys from each bucket. The order of keys is not guaranteed.
Returns:
- []chainhash.Hash: A slice containing all the hashes in the map.
Considerations: This method does not lock the map, so it is not suitable for concurrent access.
func (*SplitSwissMapUint64) Length ΒΆ
func (g *SplitSwissMapUint64) Length() int
Length returns the current number of hashes in the map. It iterates over all buckets and sums their lengths to get the total count.
Returns:
- int: The number of hashes currently stored in the map.
func (*SplitSwissMapUint64) Map ΒΆ
func (g *SplitSwissMapUint64) Map() map[uint16]*SwissMapUint64
Map returns the underlying map of buckets used by SplitSwissMapUint64.
Returns:
- map[uint16]*SwissMapUint64: A map where the keys are bucket indices and the values are pointers to SwissMapUint64 instances.
func (*SplitSwissMapUint64) Put ΒΆ
func (g *SplitSwissMapUint64) Put(hash chainhash.Hash, n uint64) error
Put adds a new hash with an associated uint64 value to the map. It calculates the bucket index using the Bytes2Uint16Buckets function and adds the hash to the corresponding bucket. It checks if the hash already exists in the bucket and returns an error if it does.
Params:
- hash: The hash to add to the map.
- n: The uint64 value to associate with the hash.
Returns:
- error: An error if the hash already exists in the map, nil otherwise.
func (*SplitSwissMapUint64) PutMulti ΒΆ
func (g *SplitSwissMapUint64) PutMulti(hashes []chainhash.Hash, n uint64) error
PutMulti adds multiple hashes with an associated uint64 value to the map. It iterates over the hashes, calculates the bucket index for each hash using the Bytes2Uint16Buckets function, and adds each hash to the corresponding bucket. It checks if any of the hashes already exist in the bucket and returns an error if any do.
Params:
- hashes: A slice of hashes to add to the map.
- n: The uint64 value to associate with each hash.
Returns:
- error: An error if any of the hashes already exist in the map, nil otherwise.
func (*SplitSwissMapUint64) Set ΒΆ added in v1.5.2
func (g *SplitSwissMapUint64) Set(hash chainhash.Hash, value uint64) error
Set updates the value associated with the given hash in the map. It will error out if the hash does not exist.
Params:
- hash: The hash to update in the map.
- value: The value to associate with the hash.
Returns:
- error: An error if the hash does not exist in the map, nil otherwise.
func (*SplitSwissMapUint64) SetIfExists ΒΆ added in v1.5.2
SetIfExists updates the value associated with the given hash in the map if it exists. It returns a boolean indicating whether the hash was found and updated. If the hash does not exist, it returns false and no error.
Params:
- hash: The hash to update in the map.
- value: The value to associate with the hash.
Returns:
- bool: True if the hash was found and updated, false otherwise.
- error: An error if there was an issue updating the hash, nil otherwise.
func (*SplitSwissMapUint64) SetIfNotExists ΒΆ added in v1.5.2
SetIfNotExists adds the hash with the given value to the map only if the hash does not already exist. It returns a boolean indicating whether the hash was added. If the hash already exists, it returns false and no error.
Params:
- hash: The hash to add to the map.
- value: The value to associate with the hash.
Returns:
- bool: True if the hash was added, false if it already existed.
- error: An error if there was an issue adding the hash, nil otherwise.
type SwissLockFreeMapUint64 ΒΆ
type SwissLockFreeMapUint64 struct {
// contains filtered or unexported fields
}
SwissLockFreeMapUint64 is a lock-free map for uint64 keys and values
func NewSwissLockFreeMapUint64 ΒΆ
func NewSwissLockFreeMapUint64(length int) *SwissLockFreeMapUint64
NewSwissLockFreeMapUint64 creates a new SwissLockFreeMapUint64 with the specified initial length. The length is used to preallocate the map size for better performance. It is not a hard limit, but a hint to the underlying swiss map.
Params:
- length: The initial length of the map, used for preallocation.
Returns:
- *SwissLockFreeMapUint64: A pointer to the newly created SwissLockFreeMapUint64 instance.
func (*SwissLockFreeMapUint64) Clear ΒΆ added in v1.5.2
func (s *SwissLockFreeMapUint64) Clear()
Clear empties the map without releasing the underlying backing storage and un-freezes it for reuse. Not safe for concurrent use.
func (*SwissLockFreeMapUint64) Exists ΒΆ
func (s *SwissLockFreeMapUint64) Exists(hash uint64) bool
Exists checks if the given hash exists in the map.
Params:
- hash: The hash to check for existence in the map.
Returns:
- bool: True if the hash exists in the map, false otherwise.
Considerations: This method does not lock the map, so it is not suitable for concurrent access.
func (*SwissLockFreeMapUint64) Freeze ΒΆ added in v1.5.2
func (s *SwissLockFreeMapUint64) Freeze()
Freeze marks the map read-only; subsequent Put calls return ErrMapFrozen.
func (*SwissLockFreeMapUint64) Get ΒΆ
func (s *SwissLockFreeMapUint64) Get(hash uint64) (uint64, bool)
Get retrieves the uint64 value associated with the given hash from the map.
Params:
- hash: The hash to retrieve from the map.
Returns:
- uint64: The value associated with the hash, or 0 if the hash does not exist.
- bool: True if the hash was found in the map, false otherwise.
Considerations: This method does not lock the map, so it is not suitable for concurrent access.
func (*SwissLockFreeMapUint64) Iter ΒΆ added in v1.5.2
func (s *SwissLockFreeMapUint64) Iter(f func(k, v uint64) (stop bool))
Iter iterates over all key-value pairs in the map. Stops if f returns true. Enables unified iteration API with NativeLockFreeMapUint64 for consumers that support both dolthub and native backends.
func (*SwissLockFreeMapUint64) Length ΒΆ
func (s *SwissLockFreeMapUint64) Length() int
Length returns the current number of hashes in the map.
Returns:
- int: The number of hashes currently stored in the map.
Considerations: This method uses atomic operations to retrieve the length, making it safe for concurrent access.
func (*SwissLockFreeMapUint64) Map ΒΆ
func (s *SwissLockFreeMapUint64) Map() *swiss.Map[uint64, uint64]
Map returns the underlying swiss map used by SwissLockFreeMapUint64. It provides access to the map for operations that do not require locking.
Returns:
- *swiss.Map[uint64, uint64]: A pointer to the underlying swiss map.
Considerations: This method does not lock the map, so it is not suitable for concurrent access.
func (*SwissLockFreeMapUint64) Put ΒΆ
func (s *SwissLockFreeMapUint64) Put(hash, n uint64) error
Put adds a new hash with an associated uint64 value to the map. It checks if the hash already exists in the map and returns an error if it does. If the hash does not exist, it adds the hash and increments the length of the map.
Params:
- hash: The hash to add to the map.
- n: The uint64 value to associate with the hash.
Returns:
- error: An error if the hash already exists in the map, nil otherwise.
Considerations: This method does not lock the map, so it is not suitable for concurrent access.
type SwissMap ΒΆ
type SwissMap struct {
// contains filtered or unexported fields
}
SwissMap is a simple concurrent-safe map that uses the swiss package
func NewSwissMap ΒΆ
NewSwissMap creates a new SwissMap with the specified initial length. The length is used to preallocate the map size for better performance. It is not a hard limit, but a hint to the underlying swiss map.
Params:
- length: The initial length of the map, used for preallocation.
Returns:
- *SwissMap: A pointer to the newly created SwissMap instance.
Considerations: The length is not enforced, and the map can grow beyond this size.
func (*SwissMap) Clear ΒΆ added in v1.5.2
func (s *SwissMap) Clear()
Clear empties the map without releasing the underlying group/ctrl backing storage. Intended for sync.Pool reuse: Clear β Put β next Get hands a zero-length map with the same preallocation intact.
func (*SwissMap) Delete ΒΆ
Delete removes a hash from the map. It decrements the length of the map.
Params:
- hash: The hash to remove from the map.
Returns:
- error: always returns nil, as this map does not have any constraints on deleting hashes.
func (*SwissMap) Exists ΒΆ
Exists checks if the given hash exists in the map. It returns true if the hash is found, false otherwise.
Params:
- hash: The hash to check for existence in the map.
Returns:
- bool: True if the hash exists in the map, false otherwise.
func (*SwissMap) Freeze ΒΆ added in v1.5.2
func (s *SwissMap) Freeze()
Freeze marks the map read-only. See the lifecycle notes at the top of this file.
func (*SwissMap) Get ΒΆ
Get retrieves the value associated with the given hash from the map. It always returns 0 and a boolean indicating whether the hash was found.
Params:
- hash: The hash to retrieve from the map.
Returns:
- uint64: Always returns 0, as this map does not store values.
- bool: True if the hash was found in the map, false otherwise.
func (*SwissMap) Iter ΒΆ added in v1.5.2
Iter iterates over all key-value pairs in the map and applies the provided function to each pair. Stops iterating if the function returns true.
Params:
- f: A function that takes a hash and its associated value (always 0 in this map).
func (*SwissMap) Keys ΒΆ
Keys returns a slice of all hashes currently stored in the map. It iterates over the map and collects the keys. The order of keys is not guaranteed.
Returns:
- []chainhash.Hash: A slice containing all the hashes in the map.
func (*SwissMap) Length ΒΆ
Length returns the current number of hashes in the map.
Returns:
- int: The number of hashes currently stored in the map.
func (*SwissMap) Put ΒΆ
Put adds a new hash to the map. It increments the length of the map.
Params:
- hash: The hash to add to the map.
Returns:
- error: always returns nil, as this map does not have any constraints on adding hashes.
type SwissMapUint64 ΒΆ
type SwissMapUint64 struct {
// contains filtered or unexported fields
}
SwissMapUint64 is a concurrent-safe map that uses the swiss package to store transaction hashes as keys and uint64 values.
func NewSwissMapUint64 ΒΆ
func NewSwissMapUint64(length uint32) *SwissMapUint64
NewSwissMapUint64 creates a new SwissMapUint64 with the specified initial length. The length is used to preallocate the map size for better performance. It is not a hard limit, but a hint to the underlying swiss map.
Params:
- length: The initial length of the map, used for preallocation.
Returns:
- *SwissMapUint64: A pointer to the newly created SwissMapUint64 instance.
func (*SwissMapUint64) Clear ΒΆ added in v1.5.2
func (s *SwissMapUint64) Clear()
Clear empties the map without releasing the underlying group/ctrl backing storage, and un-freezes it. This is the operation a pool wants: reset state for the next user while keeping the (often multi-GB) preallocation intact.
Per the Freeze/Clear contract (see freeze.go), Clear must not run concurrently with any other operation on the map: after Freeze, readers skip the RWMutex, so a concurrent Clear would race their lock-free reads of the underlying map. The write lock only orders Clear against other locked, non-frozen operations. Callers returning a SwissMapUint64 to a sync.Pool should call Clear immediately before Put so the next Get receives a zero-length map.
func (*SwissMapUint64) Delete ΒΆ
func (s *SwissMapUint64) Delete(hash chainhash.Hash) error
Delete removes a hash from the map. It decrements the length of the map. It locks the map for writing, checks if the hash exists, and removes it if found. If the hash does not exist, it returns an error.
Params:
- hash: The hash to remove from the map.
Returns:
- error: An error if the hash does not exist in the map, nil otherwise.
func (*SwissMapUint64) Exists ΒΆ
func (s *SwissMapUint64) Exists(hash chainhash.Hash) bool
Exists checks if the given hash exists in the map. It returns true if the hash is found, false otherwise.
Params:
- hash: The hash to check for existence in the map.
Returns:
- bool: True if the hash exists in the map, false otherwise.
func (*SwissMapUint64) Freeze ΒΆ added in v1.5.2
func (s *SwissMapUint64) Freeze()
Freeze marks the map read-only. See the lifecycle notes at the top of this file.
func (*SwissMapUint64) Get ΒΆ
func (s *SwissMapUint64) Get(hash chainhash.Hash) (uint64, bool)
Get retrieves the uint64 value associated with the given hash from the map. It locks the map for reading, checks if the hash exists, and returns the value and a boolean indicating success. If the hash does not exist, it returns 0 and false.
Params:
- hash: The hash to retrieve from the map.
Returns:
- uint64: The value associated with the hash, or 0 if the hash does not exist.
- bool: True if the hash was found in the map, false otherwise.
func (*SwissMapUint64) Iter ΒΆ added in v1.5.2
func (s *SwissMapUint64) Iter(f func(hash chainhash.Hash, value uint64) bool)
Iter iterates over all key-value pairs in the map and applies the provided function to each pair. Stops iterating if the function returns true.
Params:
- f: A function that takes a hash and its associated uint64 value.
func (*SwissMapUint64) Keys ΒΆ
func (s *SwissMapUint64) Keys() []chainhash.Hash
Keys returns a slice of all hashes currently stored in the map. It locks the map for reading, iterates over the map, and collects the keys. The order of keys is not guaranteed.
Returns:
- []chainhash.Hash: A slice containing all the hashes in the map.
func (*SwissMapUint64) Length ΒΆ
func (s *SwissMapUint64) Length() int
Length returns the current number of hashes in the map. It locks the map for reading and returns the length.
Returns:
- int: The number of hashes currently stored in the map.
func (*SwissMapUint64) Map ΒΆ
Map returns the underlying swiss map used by SwissMapUint64.
Returns:
- *swiss.Map[chainhash.Hash, uint64]: A pointer to the underlying swiss map.
func (*SwissMapUint64) Put ΒΆ
func (s *SwissMapUint64) Put(hash chainhash.Hash, n uint64) error
Put adds a new hash with an associated uint64 value to the map. It checks if the hash already exists in the map and returns an error if it does. If the hash does not exist, it adds the hash and increments the length of the map.
Params:
- hash: The hash to add to the map.
- n: The uint64 value to associate with the hash.
Returns:
- error: An error if the hash already exists in the map, nil otherwise.
func (*SwissMapUint64) PutMulti ΒΆ
func (s *SwissMapUint64) PutMulti(hashes []chainhash.Hash, n uint64) error
PutMulti adds multiple hashes with an associated uint64 value to the map. It checks if any of the hashes already exist in the map and returns an error if any do. If none of the hashes exist, it adds each hash with the value and increments the length of the map.
Params:
- hashes: A slice of hashes to add to the map.
- n: The uint64 value to associate with each hash.
Returns:
- error: An error if any of the hashes already exist in the map, nil otherwise.
func (*SwissMapUint64) Set ΒΆ added in v1.5.2
func (s *SwissMapUint64) Set(hash chainhash.Hash, value uint64) error
Set updates the value associated with the given hash in the map. It will error out if the hash does not exist.
Params:
- hash: The hash to update in the map.
- value: The value to associate with the hash (not used in this map).
Returns:
- error: An error if the hash does not exist in the map, nil otherwise.
func (*SwissMapUint64) SetIfExists ΒΆ added in v1.5.2
SetIfExists updates the value associated with the given hash in the map if it exists. It returns a boolean indicating whether the hash was found and updated. If the hash does not exist, it returns false and no error.
Params:
- hash: The hash to update in the map.
- value: The value to associate with the hash.
Returns:
- bool: True if the hash was found and updated, false otherwise.
- error: An error if there was an issue updating the hash, nil otherwise.
func (*SwissMapUint64) SetIfNotExists ΒΆ added in v1.5.2
SetIfNotExists adds the hash with the given value to the map only if the hash does not already exist. It returns a boolean indicating whether the hash was added. If the hash already exists, it returns false and no error.
Params:
- hash: The hash to add to the map.
- value: The value to associate with the hash.
Returns:
- bool: True if the hash was added, false if it already existed.
- error: An error if there was an issue adding the hash, nil otherwise.
type SyncedMap ΒΆ
type SyncedMap[K comparable, V any] struct { // contains filtered or unexported fields }
SyncedMap is a thread-safe generic map with read-write mutex synchronization. It supports concurrent access and provides an optional item limit for constrained storage.
func NewSyncedMap ΒΆ
func NewSyncedMap[K comparable, V any](l ...int) *SyncedMap[K, V]
NewSyncedMap creates and returns a new SyncedMap with an optional item limit.
Parameters:
- l (optional): An integer specifying the maximum number of items allowed in the map. If omitted or zero, the map has no limit.
Returns:
- *SyncedMap[K, V]: A pointer to a new, empty SyncedMap instance.
If a limit is set and the map reaches its capacity, a random item will be deleted to make room for new entries.
func (*SyncedMap[K, V]) Clear ΒΆ
Clear removes all key-value pairs from the SyncedMap and un-freezes it for reuse. Per the lifecycle contract (see freeze.go), Clear must not run concurrently with any other operation on the map: after Freeze, readers skip the RWMutex, so a concurrent Clear would race their lock-free reads. The write lock only orders Clear against other locked, non-frozen operations.
Returns:
- bool: True if the map was cleared successfully.
func (*SyncedMap[K, V]) Delete ΒΆ
Delete removes the key and its value from the SyncedMap. Panics if the map is frozen.
Parameters:
- key: The key to delete.
Returns:
- bool: True if the key was deleted, false otherwise.
func (*SyncedMap[K, V]) Exists ΒΆ
Exists checks if a key exists in the SyncedMap.
Parameters:
- key: The key to check for existence.
Returns:
- bool: True if the key exists, false otherwise.
func (*SyncedMap[K, V]) Freeze ΒΆ added in v1.5.2
func (m *SyncedMap[K, V]) Freeze()
Freeze marks the map read-only. After Freeze, read methods skip the RWMutex (eliminating reader-counter cache-line contention under heavy concurrent reads). Write methods (Set, SetIfNotExists, SetMulti, SetIfNotExistsMulti, Delete) panic rather than silently mutating a map that concurrent readers assume is immutable. Call Clear to empty the map and un-freeze it for reuse. See the lifecycle notes at the top of freeze.go.
func (*SyncedMap[K, V]) Get ΒΆ
Get returns the value associated with the given key in the SyncedMap.
Parameters:
- key: The key to retrieve the value for.
Returns:
- V: The value associated with the key (zero value if not found).
- bool: True if the key exists, false otherwise.
func (*SyncedMap[K, V]) Iterate ΒΆ
Iterate calls the provided function for each key-value pair in the SyncedMap. The iteration stops if the function returns false: the opposite of the TxMap Iter methods, where true means stop.
Parameters:
- f: A function that takes a key and value, and returns a bool indicating whether to continue iteration.
func (*SyncedMap[K, V]) Keys ΒΆ
func (m *SyncedMap[K, V]) Keys() []K
Keys returns a slice of all keys in the SyncedMap.
Returns:
- []K: A slice containing all keys in the map.
func (*SyncedMap[K, V]) Length ΒΆ
Length returns the number of key-value pairs currently stored in the SyncedMap.
Returns:
- int: The number of items in the map.
func (*SyncedMap[K, V]) Range ΒΆ
func (m *SyncedMap[K, V]) Range() map[K]V
Range returns a copy of the SyncedMap as a standard Go map.
Returns:
- map[K]V: A copy of the map's key-value pairs.
func (*SyncedMap[K, V]) Set ΒΆ
func (m *SyncedMap[K, V]) Set(key K, value V)
Set sets the value for the given key in the SyncedMap. Panics if the map is frozen.
Parameters:
- key: The key to set.
- value: The value to associate with the key.
func (*SyncedMap[K, V]) SetIfNotExists ΒΆ
SetIfNotExists sets the value for the key if it does not already exist in the SyncedMap. Panics if the map is frozen.
Parameters:
- key: The key to set the value for.
- value: The value to set for the key.
Returns:
- V: The value that was set or already existed.
- bool: True if the value was set, false if the key already existed.
func (*SyncedMap[K, V]) SetIfNotExistsMulti ΒΆ added in v1.5.2
SetIfNotExistsMulti inserts each keys[i] -> values[i] pair under a single write-lock acquisition. Matches SetIfNotExists semantics per element: if a key already exists in the map its value is left unchanged. Panics if the map is frozen.
Parameters:
- keys: A slice of keys to insert.
- values: A slice of values to associate with each key. If shorter than keys, only the first len(values) pairs are considered.
Returns:
- []bool: A slice of length min(len(keys), len(values)). wasInserted[i] is true if keys[i] was newly added, false if the key already existed.
Designed for bucket-affinity bulk inserts where the caller has pre- partitioned its workload so a single bucket's map is written by a single goroutine; eliminates the per-element Lock/Unlock overhead of calling SetIfNotExists in a loop.
type SyncedSlice ΒΆ
type SyncedSlice[V any] struct { // contains filtered or unexported fields }
SyncedSlice is a thread-safe wrapper around a slice of pointers, providing synchronized access and modification operations.
func NewSyncedSlice ΒΆ
func NewSyncedSlice[V any](length ...int) *SyncedSlice[V]
NewSyncedSlice creates and returns a new SyncedSlice with an optional initial capacity.
Parameters:
- length (optional): The initial capacity of the slice. If omitted or zero, the slice has no preallocated capacity.
Returns:
- *SyncedSlice[V]: A pointer to a new, empty SyncedSlice instance.
func (*SyncedSlice[V]) Append ΒΆ
func (s *SyncedSlice[V]) Append(item *V)
Append adds an item to the end of the SyncedSlice.
Parameters:
- item: A pointer to the item to append to the slice.
func (*SyncedSlice[V]) Get ΒΆ
func (s *SyncedSlice[V]) Get(index int) (*V, bool)
Get returns the item at the specified index in the SyncedSlice.
Parameters:
- index: The index of the item to retrieve.
Returns:
- *V: A pointer to the item at the specified index, or nil if out of range.
- bool: True if the item exists at the index, false otherwise.
func (*SyncedSlice[V]) Length ΒΆ
func (s *SyncedSlice[V]) Length() int
Length returns the number of items currently stored in the SyncedSlice.
Returns:
- int: The number of items in the slice.
func (*SyncedSlice[V]) Pop ΒΆ
func (s *SyncedSlice[V]) Pop() (*V, bool)
Pop removes and returns the last item in the SyncedSlice.
Returns:
- *V: A pointer to the last item, or nil if the slice is empty.
- bool: True if an item was returned, false if the slice was empty.
func (*SyncedSlice[V]) Shift ΒΆ
func (s *SyncedSlice[V]) Shift() (*V, bool)
Shift removes and returns the first item in the SyncedSlice.
Returns:
- *V: A pointer to the first item, or nil if the slice is empty.
- bool: True if an item was returned, false if the slice was empty.
func (*SyncedSlice[V]) Size ΒΆ
func (s *SyncedSlice[V]) Size() int
Size returns the capacity of the SyncedSlice.
Returns:
- int: The capacity of the slice.
type SyncedSwissMap ΒΆ
type SyncedSwissMap[K comparable, V any] struct { // contains filtered or unexported fields }
SyncedSwissMap is a concurrent-safe wrapper around swiss.Map, providing locking mechanisms for thread-safety.
func NewSyncedSwissMap ΒΆ
func NewSyncedSwissMap[K comparable, V any](length uint32) *SyncedSwissMap[K, V]
NewSyncedSwissMap creates and returns a new SyncedSwissMap with the specified initial capacity.
Parameters:
- length: The initial capacity of the underlying swiss.Map.
Returns:
- *SyncedSwissMap[K, V]: A pointer to a new, empty SyncedSwissMap instance.
func (*SyncedSwissMap[K, V]) Clear ΒΆ added in v1.5.2
func (m *SyncedSwissMap[K, V]) Clear()
Clear empties the map without releasing the underlying backing storage and un-freezes it for reuse. Per the lifecycle contract (see freeze.go), Clear must not run concurrently with any other operation on the map: after Freeze, readers skip the RWMutex, so a concurrent Clear would race their lock-free reads. The write lock only orders Clear against other locked, non-frozen operations.
func (*SyncedSwissMap[K, V]) Delete ΒΆ
func (m *SyncedSwissMap[K, V]) Delete(key K) bool
Delete removes the key and its value from the SyncedSwissMap. Panics if the map is frozen.
Parameters:
- key: The key to delete.
Returns:
- bool: True if the key was deleted, false otherwise.
func (*SyncedSwissMap[K, V]) DeleteBatch ΒΆ
func (m *SyncedSwissMap[K, V]) DeleteBatch(keys []K) bool
DeleteBatch removes multiple keys and their values from the SyncedSwissMap. Panics if the map is frozen.
Parameters:
- keys: A slice of keys to delete.
Returns:
- bool: True if at least one key was deleted, false otherwise.
func (*SyncedSwissMap[K, V]) Freeze ΒΆ added in v1.5.2
func (m *SyncedSwissMap[K, V]) Freeze()
Freeze marks the map read-only. After Freeze, read methods skip the RWMutex (eliminating reader-counter cache-line contention under heavy concurrent reads). Write methods (Set, Delete, DeleteBatch) panic rather than silently mutating a map that concurrent readers assume is immutable. Call Clear to empty the map and un-freeze it for reuse. See the lifecycle notes at the top of freeze.go.
func (*SyncedSwissMap[K, V]) Get ΒΆ
func (m *SyncedSwissMap[K, V]) Get(key K) (V, bool)
Get returns the value associated with the given key in the SyncedSwissMap.
Parameters:
- key: The key to retrieve the value for.
Returns:
- V: The value associated with the key (zero value if not found).
- bool: True if the key exists, false otherwise.
func (*SyncedSwissMap[K, V]) Length ΒΆ
func (m *SyncedSwissMap[K, V]) Length() int
Length returns the number of key-value pairs currently stored in the SyncedSwissMap.
Returns:
- int: The number of items in the map.
func (*SyncedSwissMap[K, V]) Range ΒΆ
func (m *SyncedSwissMap[K, V]) Range() map[K]V
Range returns a copy of the SyncedSwissMap as a standard Go map.
Returns:
- map[K]V: A copy of the map's key-value pairs.
func (*SyncedSwissMap[K, V]) Set ΒΆ
func (m *SyncedSwissMap[K, V]) Set(key K, value V)
Set sets the value for the given key in the SyncedSwissMap. Panics if the map is frozen.
Parameters:
- key: The key to set.
- value: The value to associate with the key.
type TxHashMap ΒΆ
type TxHashMap interface {
Delete(hash chainhash.Hash) error
Exists(hash chainhash.Hash) bool
Get(hash chainhash.Hash) (uint64, bool)
Keys() []chainhash.Hash
Length() int
Put(hash chainhash.Hash) error
PutMulti(hashes []chainhash.Hash) error
// Iter calls f for every entry until f returns true (stop), as TxMap.Iter.
Iter(f func(hash chainhash.Hash, value uint64) bool)
// Freeze marks the map read-only; subsequent writes return ErrMapFrozen.
Freeze()
// Clear empties the map and un-freezes it for reuse.
Clear()
}
TxHashMap is a map that stores transaction hashes without any associated value.
type TxMap ΒΆ
type TxMap interface {
Delete(hash chainhash.Hash) error
Exists(hash chainhash.Hash) bool
Get(hash chainhash.Hash) (uint64, bool)
Keys() []chainhash.Hash
Length() int
Put(hash chainhash.Hash, value uint64) error
PutMulti(hashes []chainhash.Hash, value uint64) error
Set(hash chainhash.Hash, value uint64) error
SetIfExists(hash chainhash.Hash, value uint64) (bool, error)
SetIfNotExists(hash chainhash.Hash, value uint64) (bool, error)
// Iter calls f for every entry until f returns true (stop). This is the
// opposite of SyncedMap.Iterate, where true means continue.
Iter(f func(hash chainhash.Hash, value uint64) bool)
// Freeze marks the map read-only. After Freeze, read methods skip locking
// (on lock-based implementations) and every write method returns ErrMapFrozen.
// Clear un-freezes. See the package-level Freeze documentation in freeze.go.
Freeze()
// Clear empties the map in place (retaining preallocated capacity) and
// un-freezes it, so a pooled map can be recycled for the next use.
Clear()
}
TxMap is a map that stores transaction hashes and associated uint64 values.
type Uint64 ΒΆ
type Uint64 interface {
Exists(hash uint64) bool
Get(hash uint64) (uint64, bool)
Length() int
Put(hash, value uint64) error
// Freeze marks the map read-only; subsequent Put calls return ErrMapFrozen.
Freeze()
// Clear empties the map and un-freezes it for reuse.
Clear()
}
Uint64 is a map that stores uint64's and associated uint64 value.


