shrinkmap

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2025 License: MIT Imports: 6 Imported by: 0

README

ShrinkableMap

MIT License Go Reference Go Report Card Coverage Status

ShrinkableMap is a high-performance, generic, thread-safe map implementation for Go that automatically manages memory by shrinking its internal storage when items are deleted. It addresses the common issue where Go's built-in maps don't release memory after deleting elements.

🚀 Features

  • Type Safety

    • Generic type support for compile-time type checking
    • Type-safe operations for all map interactions
  • Performance

    • Optimized concurrent access with minimal locking
    • Efficient atomic operations for high throughput
    • Batch operations for improved performance
  • Memory Management

    • Automatic memory shrinking with configurable policies
    • Advanced concurrent shrinking behavior
    • Memory-efficient iterators
  • Reliability

    • Thread-safe implementation
    • Panic recovery and error tracking
    • Comprehensive metrics collection
  • Developer Experience

    • Safe iteration with snapshot support
    • Batch operations for bulk processing
    • Clear error reporting and metrics
    • Zero external dependencies
    • Production-ready with extensive tests

📦 Installation

go get github.com/jongyunha/shrinkmap

🔧 Quick Start

package main

import (
    "fmt"
    "github.com/jongyunha/shrinkmap"
)

func main() {
    // Create a new map with default configuration
    sm := shrinkmap.New[string, int](shrinkmap.DefaultConfig())
    defer sm.Stop()

    // Basic operations
    sm.Set("one", 1)
    sm.Set("two", 2)

    if value, exists := sm.Get("one"); exists {
        fmt.Printf("Value: %d\n", value)
    }

    // Delete value
    sm.Delete("one")
}

💡 Advanced Usage

Batch Operations

Efficiently process multiple operations atomically:

batch := shrinkmap.BatchOperations[string, int]{
    Operations: []shrinkmap.BatchOperation[string, int]{
        {Type: shrinkmap.BatchSet, Key: "one", Value: 1},
        {Type: shrinkmap.BatchSet, Key: "two", Value: 2},
        {Type: shrinkmap.BatchDelete, Key: "three"},
    },
}

// Apply all operations atomically
sm.ApplyBatch(batch)
Safe Iteration

Iterate over map contents safely using the iterator:

// Create an iterator
iter := sm.NewIterator()

// Iterate over all items
for iter.Next() {
    key, value := iter.Get()
    fmt.Printf("Key: %v, Value: %v\n", key, value)
}

// Or use snapshot for bulk processing
snapshot := sm.Snapshot()
for _, kv := range snapshot {
    fmt.Printf("Key: %v, Value: %v\n", kv.Key, kv.Value)
}
Performance Monitoring

Track performance metrics:

metrics := sm.GetMetrics()
fmt.Printf("Total operations: %d\n", metrics.TotalItemsProcessed())
fmt.Printf("Peak size: %d\n", metrics.PeakSize())
fmt.Printf("Total shrinks: %d\n", metrics.TotalShrinks())

🔍 Configuration Options

config := shrinkmap.Config{
    InitialCapacity:      1000,
    AutoShrinkEnabled:    true,
    ShrinkInterval:       time.Second,
    MinShrinkInterval:    time.Second,
    ShrinkRatio:         0.5,
    CapacityGrowthFactor: 1.5,
    MaxMapSize:           1000000,
}

🛡️ Thread Safety Guarantees

  • All map operations are atomic and thread-safe
  • Safe concurrent access from multiple goroutines
  • Thread-safe batch operations
  • Safe iteration with consistent snapshots
  • Coordinated shrinking operations
  • Thread-safe metrics collection

📊 Performance

Benchmark results on typical operations (Intel i7-9700K, 32GB RAM):

BenchmarkBasicOperations/Sequential_Set-8         5000000    234 ns/op
BenchmarkBasicOperations/Sequential_Get-8         10000000   112 ns/op
BenchmarkBatchOperations/BatchSize_100-8         100000     15234 ns/op
BenchmarkConcurrency/Parallel_8-8                1000000    1123 ns/op

📝 Best Practices

  1. Resource Management
sm := shrinkmap.New[string, int](config)
defer sm.Stop() // Always ensure proper cleanup
  1. Batch Processing
// Use batch operations for multiple updates
batch := prepareBatchOperations()
sm.ApplyBatch(batch)
  1. Safe Iteration
// Use iterator for safe enumeration
iter := sm.NewIterator()
for iter.Next() {
    // Process items safely
}

🗓️ Version History

v0.0.4 (Current)
  • Added batch operations support for atomic updates
  • Implemented safe iterator pattern
  • Enhanced performance for bulk operations
  • Added comprehensive benchmarking suite
  • Improved documentation and examples
history
v0.0.3
  • enhanced performance and memory management
v0.0.2
  • Added error tracking and panic recovery
  • Added state snapshot functionality
  • Added graceful shutdown
  • Enhanced metrics collection
  • Improved resource cleanup
v0.0.1
  • Initial release with core functionality
  • Thread-safe implementation
  • Automatic shrinking support
  • Generic type support

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.


Made with ❤️ by Jongyun Ha

Documentation

Overview

Package shrinkmap provides a thread-safe, generic map implementation with automatic memory management.

ShrinkableMap automatically shrinks its internal storage when items are deleted, addressing the common issue where Go's built-in maps don't release memory after deleting elements.

Key features:

  • Thread-safe operations with minimal locking overhead
  • Automatic memory shrinking with configurable policies
  • Generic type support for compile-time type safety
  • Comprehensive error handling with structured error types
  • Performance metrics and monitoring capabilities
  • Batch operations for improved performance
  • Safe iteration with snapshot support

Basic usage:

sm := shrinkmap.New[string, int](shrinkmap.DefaultConfig())
defer sm.Stop() // Always call Stop() to prevent goroutine leaks

// Basic operations
sm.Set("key1", 42)
if value, exists := sm.Get("key1"); exists {
	fmt.Printf("Value: %d\n", value)
}
sm.Delete("key1")

For production use, always call Stop() when the map is no longer needed to prevent goroutine leaks from the automatic shrinking background process.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMapStopped       = NewShrinkMapError(ErrCodeMapStopped, "operation", "map has been stopped")
	ErrCapacityExceeded = NewShrinkMapError(ErrCodeCapacityExceeded, "set", "maximum capacity exceeded")
	ErrInvalidConfig    = NewShrinkMapError(ErrCodeInvalidConfig, "config", "invalid configuration")
	ErrShrinkFailed     = NewShrinkMapError(ErrCodeShrinkFailed, "shrink", "shrink operation failed")
	ErrBatchFailed      = NewShrinkMapError(ErrCodeBatchFailed, "batch", "batch operation failed")
)

Common error instances

Functions

func IsCapacityExceeded added in v0.0.5

func IsCapacityExceeded(err error) bool

IsCapacityExceeded checks if the error indicates capacity exceeded

func IsMapStopped added in v0.0.5

func IsMapStopped(err error) bool

IsMapStopped checks if the error indicates a stopped map

Types

type BatchOpType added in v0.0.4

type BatchOpType int
const (
	BatchSet BatchOpType = iota
	BatchDelete
)

type BatchOperation added in v0.0.4

type BatchOperation[K comparable, V any] struct {
	Type  BatchOpType
	Key   K
	Value V
}

type BatchOperations added in v0.0.4

type BatchOperations[K comparable, V any] struct {
	Operations []BatchOperation[K, V]
}

BatchOperations provides batch operation capabilities

type Builder added in v0.0.5

type Builder[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Builder provides a fluent interface for creating and configuring ShrinkableMap instances. It allows method chaining for a more readable and convenient API.

func NewBuilder added in v0.0.5

func NewBuilder[K comparable, V any]() *Builder[K, V]

NewBuilder creates a new Builder with default configuration. This is the starting point for creating a ShrinkableMap with fluent syntax.

Example:

sm := shrinkmap.NewBuilder[string, int]().
	WithShrinkRatio(0.5).
	WithInitialCapacity(100).
	Build()
defer sm.Stop()

func (*Builder[K, V]) Build added in v0.0.5

func (b *Builder[K, V]) Build() *ShrinkableMap[K, V]

Build creates the ShrinkableMap with the configured settings. This is the final method in the builder chain that returns the actual map. The returned map is ready for use and will start background shrinking if enabled.

Important: Always call Stop() on the returned map when done to prevent goroutine leaks.

func (*Builder[K, V]) BuildWithValidation added in v0.0.5

func (b *Builder[K, V]) BuildWithValidation() (*ShrinkableMap[K, V], error)

BuildWithValidation creates the ShrinkableMap with validation of the configuration. Returns an error if the configuration is invalid.

func (*Builder[K, V]) WithAutoShrink added in v0.0.5

func (b *Builder[K, V]) WithAutoShrink(enabled bool) *Builder[K, V]

WithAutoShrink enables or disables automatic shrinking and returns the builder for chaining.

func (*Builder[K, V]) WithCapacityGrowthFactor added in v0.0.5

func (b *Builder[K, V]) WithCapacityGrowthFactor(factor float64) *Builder[K, V]

WithCapacityGrowthFactor sets the capacity growth factor and returns the builder for chaining. This controls how much extra space is allocated when shrinking.

func (*Builder[K, V]) WithInitialCapacity added in v0.0.5

func (b *Builder[K, V]) WithInitialCapacity(capacity int) *Builder[K, V]

WithInitialCapacity sets the initial capacity and returns the builder for chaining. This is the initial capacity of the underlying map.

func (*Builder[K, V]) WithMaxMapSize added in v0.0.5

func (b *Builder[K, V]) WithMaxMapSize(size int) *Builder[K, V]

WithMaxMapSize sets the maximum map size and returns the builder for chaining. Set to 0 for unlimited size.

func (*Builder[K, V]) WithMinShrinkInterval added in v0.0.5

func (b *Builder[K, V]) WithMinShrinkInterval(interval time.Duration) *Builder[K, V]

WithMinShrinkInterval sets the minimum shrink interval and returns the builder for chaining. This prevents shrinking too frequently even if conditions are met.

func (*Builder[K, V]) WithShrinkInterval added in v0.0.5

func (b *Builder[K, V]) WithShrinkInterval(interval time.Duration) *Builder[K, V]

WithShrinkInterval sets the shrink interval and returns the builder for chaining. This controls how often the map checks for shrinking opportunities.

func (*Builder[K, V]) WithShrinkRatio added in v0.0.5

func (b *Builder[K, V]) WithShrinkRatio(ratio float64) *Builder[K, V]

WithShrinkRatio sets the shrink ratio and returns the builder for chaining. The shrink ratio determines what percentage of deleted items triggers shrinking.

type Config

type Config struct {
	// Duration values (8 bytes each)
	ShrinkInterval    time.Duration // How often to check if the map needs shrinking
	MinShrinkInterval time.Duration // Minimum time between shrinks

	// Float64 values (8 bytes each)
	ShrinkRatio          float64 // Ratio of deleted items that triggers shrinking (0.0 to 1.0)
	CapacityGrowthFactor float64 // Extra capacity factor when creating new map (e.g., 1.2 for 20% extra space)

	// Int values (8 bytes on 64-bit)
	InitialCapacity int // Initial capacity of the map
	MaxMapSize      int // Maximum map size before forcing a shrink

	// Bool values (1 byte each)
	AutoShrinkEnabled bool // Enable/disable automatic shrinking
}

Config defines the configuration options for ShrinkableMap

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the default configuration for ShrinkableMap. These settings are optimized for general use cases with reasonable performance and memory management characteristics.

Default values:

  • ShrinkInterval: 5 minutes (how often to check for shrinking)
  • ShrinkRatio: 0.25 (shrink when 25% of items are deleted)
  • InitialCapacity: 16 (starting capacity)
  • AutoShrinkEnabled: true (automatic shrinking is enabled)
  • MinShrinkInterval: 30 seconds (minimum time between shrinks)
  • MaxMapSize: 1,000,000 (maximum items before forced shrink)
  • CapacityGrowthFactor: 1.2 (20% extra space when shrinking)

Example:

config := shrinkmap.DefaultConfig()
config.ShrinkRatio = 0.5 // Customize shrink ratio
sm := shrinkmap.New[string, int](config)

func (Config) Validate

func (c Config) Validate() error

Validate checks if the configuration is valid and returns an error if not. This method should be called before creating a new ShrinkableMap to ensure the configuration parameters are within acceptable ranges.

Validation rules:

  • ShrinkInterval must be positive
  • ShrinkRatio must be between 0 and 1 (exclusive)
  • InitialCapacity must be non-negative
  • MinShrinkInterval must be positive
  • MaxMapSize must be non-negative (0 means unlimited)
  • CapacityGrowthFactor must be greater than 1

Example:

config := shrinkmap.DefaultConfig()
config.ShrinkRatio = 0.5
if err := config.Validate(); err != nil {
	log.Fatal("Invalid configuration:", err)
}

func (Config) WithAutoShrinkEnabled

func (c Config) WithAutoShrinkEnabled(enabled bool) Config

WithAutoShrinkEnabled sets auto shrinking and returns the modified config

func (Config) WithCapacityGrowthFactor

func (c Config) WithCapacityGrowthFactor(factor float64) Config

WithCapacityGrowthFactor sets the capacity growth factor and returns the modified config

func (Config) WithInitialCapacity

func (c Config) WithInitialCapacity(capacity int) Config

WithInitialCapacity sets the initial capacity and returns the modified config

func (Config) WithMaxMapSize

func (c Config) WithMaxMapSize(size int) Config

WithMaxMapSize sets the maximum map size and returns the modified config

func (Config) WithMinShrinkInterval

func (c Config) WithMinShrinkInterval(d time.Duration) Config

WithMinShrinkInterval sets the minimum shrink interval and returns the modified config

func (Config) WithShrinkInterval

func (c Config) WithShrinkInterval(d time.Duration) Config

WithShrinkInterval sets the shrink interval and returns the modified config

func (Config) WithShrinkRatio

func (c Config) WithShrinkRatio(ratio float64) Config

WithShrinkRatio sets the shrink ratio and returns the modified config

type ErrCode added in v0.0.5

type ErrCode int

ErrCode represents different types of errors that can occur in ShrinkableMap

const (
	// ErrCodeInvalidConfig indicates configuration validation failed
	ErrCodeInvalidConfig ErrCode = iota
	// ErrCodeMapStopped indicates operation attempted on stopped map
	ErrCodeMapStopped
	// ErrCodeShrinkFailed indicates shrink operation failed
	ErrCodeShrinkFailed
	// ErrCodeBatchFailed indicates batch operation failed
	ErrCodeBatchFailed
	// ErrCodeCapacityExceeded indicates maximum capacity exceeded
	ErrCodeCapacityExceeded
)

func (ErrCode) String added in v0.0.5

func (e ErrCode) String() string

String returns the string representation of the error code

type ErrorRecord added in v0.0.2

type ErrorRecord struct {
	Timestamp time.Time
	Error     interface{} // panic 값이나 error 둘 다 저장 가능
	Stack     string      // 스택 트레이스 저장
}

ErrorRecord represents a single error or panic occurrence

type Iterator added in v0.0.4

type Iterator[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Iterator provides a safe way to iterate over map entries. The iterator works with a snapshot of the map taken at creation time, making it safe to use even if the map is modified during iteration.

func (*Iterator[K, V]) Get added in v0.0.4

func (it *Iterator[K, V]) Get() (K, V)

Get returns the current key-value pair and advances the iterator. Must be called only after Next() returns true. Panics if called when there are no more items.

func (*Iterator[K, V]) Next added in v0.0.4

func (it *Iterator[K, V]) Next() bool

Next advances the iterator to the next item. Returns true if there are more items to iterate over, false otherwise. Must be called before each Get() call.

type KeyValue added in v0.0.2

type KeyValue[K comparable, V any] struct {
	Key   K
	Value V
}

KeyValue represents a key-value pair for iteration purposes

type MapBuilder added in v0.0.5

type MapBuilder[K comparable, V any] struct {
	// contains filtered or unexported fields
}

MapBuilder provides additional fluent methods for map operations after creation.

func NewMapBuilder added in v0.0.5

func NewMapBuilder[K comparable, V any](sm *ShrinkableMap[K, V]) *MapBuilder[K, V]

NewMapBuilder creates a MapBuilder from an existing ShrinkableMap. This allows for fluent operations on the map.

func (*MapBuilder[K, V]) Delete added in v0.0.5

func (mb *MapBuilder[K, V]) Delete(key K) *MapBuilder[K, V]

Delete removes a key and returns the builder for chaining.

func (*MapBuilder[K, V]) Done added in v0.0.5

func (mb *MapBuilder[K, V]) Done() *ShrinkableMap[K, V]

Done is a convenience method that returns the underlying map. This is useful at the end of a builder chain.

func (*MapBuilder[K, V]) Map added in v0.0.5

func (mb *MapBuilder[K, V]) Map() *ShrinkableMap[K, V]

Map returns the underlying ShrinkableMap.

func (*MapBuilder[K, V]) Set added in v0.0.5

func (mb *MapBuilder[K, V]) Set(key K, value V) *MapBuilder[K, V]

Set stores a key-value pair and returns the builder for chaining. If an error occurs, it can be retrieved using the LastError method.

func (*MapBuilder[K, V]) SetIfAbsent added in v0.0.5

func (mb *MapBuilder[K, V]) SetIfAbsent(key K, value V) *MapBuilder[K, V]

SetIfAbsent sets a key-value pair only if the key doesn't exist and returns the builder for chaining.

type Metrics

type Metrics struct {
	// contains filtered or unexported fields
}

Metrics tracks performance and error metrics of the map

func (*Metrics) ErrorHistory added in v0.0.2

func (m *Metrics) ErrorHistory() []ErrorRecord

func (*Metrics) LastError added in v0.0.2

func (m *Metrics) LastError() *ErrorRecord

func (*Metrics) LastPanicTime added in v0.0.2

func (m *Metrics) LastPanicTime() time.Time

func (*Metrics) LastShrinkDuration

func (m *Metrics) LastShrinkDuration() time.Duration

func (*Metrics) PeakSize

func (m *Metrics) PeakSize() int32

func (*Metrics) RecordError added in v0.0.2

func (m *Metrics) RecordError(err error, stack string)

func (*Metrics) RecordPanic added in v0.0.2

func (m *Metrics) RecordPanic(r interface{}, stack string)

func (*Metrics) Reset added in v0.0.2

func (m *Metrics) Reset()

Reset resets all metrics

func (*Metrics) TotalErrors added in v0.0.2

func (m *Metrics) TotalErrors() int64

func (*Metrics) TotalItemsProcessed

func (m *Metrics) TotalItemsProcessed() int64

func (*Metrics) TotalPanics added in v0.0.2

func (m *Metrics) TotalPanics() int64

func (*Metrics) TotalShrinks

func (m *Metrics) TotalShrinks() int64

type ShrinkMapError added in v0.0.5

type ShrinkMapError struct {
	Code      ErrCode
	Message   string
	Operation string
	Timestamp time.Time
	Details   map[string]interface{}
}

ShrinkMapError represents a structured error from ShrinkableMap operations

func NewShrinkMapError added in v0.0.5

func NewShrinkMapError(code ErrCode, operation, message string) *ShrinkMapError

NewShrinkMapError creates a new ShrinkMapError

func (*ShrinkMapError) Error added in v0.0.5

func (e *ShrinkMapError) Error() string

Error implements the error interface

func (*ShrinkMapError) Is added in v0.0.5

func (e *ShrinkMapError) Is(target error) bool

Is allows error comparison using errors.Is

func (*ShrinkMapError) WithDetails added in v0.0.5

func (e *ShrinkMapError) WithDetails(key string, value interface{}) *ShrinkMapError

WithDetails adds details to the error

type ShrinkableMap

type ShrinkableMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

ShrinkableMap provides a generic map structure with automatic shrinking capabilities Note: Each ShrinkableMap instance creates its own goroutine for auto-shrinking when AutoShrinkEnabled is true. The goroutine will continue to run until Stop() is called, even if there are no more references to the map. For transient use cases, ensure to call Stop() when the map is no longer needed to prevent goroutine leaks.

func New

func New[K comparable, V any](config Config) *ShrinkableMap[K, V]

New creates a new ShrinkableMap with the given configuration. The returned map is ready for concurrent use and will automatically start background shrinking if AutoShrinkEnabled is true in the config.

Important: Always call Stop() when the map is no longer needed to prevent goroutine leaks from the automatic shrinking process.

Example:

config := shrinkmap.DefaultConfig()
config.ShrinkRatio = 0.5 // Shrink when 50% of items are deleted
sm := shrinkmap.New[string, int](config)
defer sm.Stop()

func (*ShrinkableMap[K, V]) ApplyBatch added in v0.0.4

func (sm *ShrinkableMap[K, V]) ApplyBatch(batch BatchOperations[K, V]) error

ApplyBatch applies multiple operations atomically. This method is more efficient than individual operations when processing multiple items as it acquires the lock only once. Returns an error if the map is stopped or capacity is exceeded.

Example:

batch := shrinkmap.BatchOperations[string, int]{
	Operations: []shrinkmap.BatchOperation[string, int]{
		{Type: shrinkmap.BatchSet, Key: "key1", Value: 1},
		{Type: shrinkmap.BatchSet, Key: "key2", Value: 2},
		{Type: shrinkmap.BatchDelete, Key: "key3"},
	},
}
if err := sm.ApplyBatch(batch); err != nil {
	log.Printf("Batch operation failed: %v", err)
}

func (*ShrinkableMap[K, V]) Contains added in v0.0.5

func (sm *ShrinkableMap[K, V]) Contains(key K) bool

Contains checks if the key exists in the map. This is a convenience method that returns only the existence boolean. For better performance when you also need the value, use Get instead.

Example:

sm := shrinkmap.New[string, int](shrinkmap.DefaultConfig())
sm.Set("key", 42)
if sm.Contains("key") {
	fmt.Println("Key exists")
}

func (*ShrinkableMap[K, V]) Delete

func (sm *ShrinkableMap[K, V]) Delete(key K) bool

Delete removes the entry for the given key. Returns true if the key existed and was deleted, false otherwise. This operation is safe for concurrent use and may trigger automatic shrinking.

Example:

sm := shrinkmap.New[string, int](shrinkmap.DefaultConfig())
sm.Set("key", 42)
if deleted := sm.Delete("key"); deleted {
	fmt.Println("Key was deleted")
}

func (*ShrinkableMap[K, V]) ForceShrink

func (sm *ShrinkableMap[K, V]) ForceShrink() bool

ForceShrink immediately shrinks the map regardless of conditions

func (*ShrinkableMap[K, V]) Get

func (sm *ShrinkableMap[K, V]) Get(key K) (V, bool)

Get retrieves the value associated with the given key. Returns the value and a boolean indicating whether the key exists. This operation is safe for concurrent use.

Example:

sm := shrinkmap.New[string, int](shrinkmap.DefaultConfig())
sm.Set("key", 42)
if value, exists := sm.Get("key"); exists {
	fmt.Printf("Value: %d\n", value)
}

func (*ShrinkableMap[K, V]) GetMetrics

func (sm *ShrinkableMap[K, V]) GetMetrics() Metrics

GetMetrics returns a copy of the current metrics

func (*ShrinkableMap[K, V]) GetOrSet added in v0.0.5

func (sm *ShrinkableMap[K, V]) GetOrSet(key K, value V) (V, bool, error)

GetOrSet returns the existing value for the key, or sets and returns the provided value if key doesn't exist

func (*ShrinkableMap[K, V]) Len

func (sm *ShrinkableMap[K, V]) Len() int64

Len returns the current number of items in the map. This operation is atomic and safe for concurrent use. The returned value reflects the actual number of accessible items, accounting for deletions that haven't been cleaned up yet.

Example:

sm := shrinkmap.New[string, int](shrinkmap.DefaultConfig())
sm.Set("key1", 1)
sm.Set("key2", 2)
fmt.Printf("Map contains %d items\n", sm.Len()) // Output: Map contains 2 items

func (*ShrinkableMap[K, V]) NewIterator added in v0.0.4

func (sm *ShrinkableMap[K, V]) NewIterator() *Iterator[K, V]

NewIterator creates a new iterator for the map. The iterator takes a snapshot of the current map state, making it safe to use even if the map is modified during iteration. The snapshot is taken with a read lock for consistency.

Example:

sm := shrinkmap.New[string, int](shrinkmap.DefaultConfig())
sm.Set("key1", 1)
sm.Set("key2", 2)

iter := sm.NewIterator()
for iter.Next() {
	key, value := iter.Get()
	fmt.Printf("Key: %s, Value: %d\n", key, value)
}

func (*ShrinkableMap[K, V]) Set

func (sm *ShrinkableMap[K, V]) Set(key K, value V) error

Set stores a key-value pair in the map

func (*ShrinkableMap[K, V]) SetIf added in v0.0.5

func (sm *ShrinkableMap[K, V]) SetIf(key K, value V, condition func(oldValue V, exists bool) bool) (bool, error)

SetIf sets the value for the key only if the condition is true

func (*ShrinkableMap[K, V]) SetIfAbsent added in v0.0.5

func (sm *ShrinkableMap[K, V]) SetIfAbsent(key K, value V) (bool, error)

SetIfAbsent sets the value for the key only if the key doesn't exist

func (*ShrinkableMap[K, V]) Snapshot added in v0.0.2

func (sm *ShrinkableMap[K, V]) Snapshot() []KeyValue[K, V]

Snapshot returns a slice of key-value pairs representing the current state of the map Note: This operation requires a full lock of the map and may be expensive for large maps

func (*ShrinkableMap[K, V]) Stop added in v0.0.2

func (sm *ShrinkableMap[K, V]) Stop()

Stop terminates the auto-shrink goroutine if it's running. This should be called when the map is no longer needed to prevent goroutine leaks. After calling Stop(), the map remains functional but automatic shrinking is disabled. It's safe to call Stop() multiple times.

Example:

sm := shrinkmap.New[string, int](shrinkmap.DefaultConfig())
defer sm.Stop() // Ensure cleanup

// Use the map...
sm.Set("key", 42)

func (*ShrinkableMap[K, V]) TryShrink

func (sm *ShrinkableMap[K, V]) TryShrink() bool

TryShrink attempts to shrink the map if conditions are met

Directories

Path Synopsis
examples
basic command

Jump to

Keyboard shortcuts

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