Documentation
¶
Overview ¶
Example (MultiTypeStorage) ¶
Custom wrapper for a map that stores multiple value types
package main import ( "context" "fmt" "github.com/thisisdevelopment/mightymap" "github.com/thisisdevelopment/mightymap/storage" ) func main() { // Create a struct that contains multiple typed maps type MultiTypeStore struct { strings *mightymap.Map[string, string] ints *mightymap.Map[string, int] bools *mightymap.Map[string, bool] } // Constructor newMultiTypeStore := func() *MultiTypeStore { strStore := storage.NewMightyMapBadgerStorage[string, string]( storage.WithMemoryStorage(true), ) intStore := storage.NewMightyMapBadgerStorage[string, int]( storage.WithMemoryStorage(true), ) boolStore := storage.NewMightyMapBadgerStorage[string, bool]( storage.WithMemoryStorage(true), ) return &MultiTypeStore{ strings: mightymap.New[string, string](true, strStore), ints: mightymap.New[string, int](true, intStore), bools: mightymap.New[string, bool](true, boolStore), } } // Create a new store ctx := context.Background() store := newMultiTypeStore() defer func() { store.strings.Close(ctx) store.ints.Close(ctx) store.bools.Close(ctx) }() // Store different types of values store.strings.Store(ctx, "name", "John") store.ints.Store(ctx, "age", 30) store.bools.Store(ctx, "active", true) // Retrieve values with correct types name, _ := store.strings.Load(ctx, "name") age, _ := store.ints.Load(ctx, "age") active, _ := store.bools.Load(ctx, "active") fmt.Printf("Name: %s, Age: %d, Active: %v\n", name, age, active) }
Output: Name: John, Age: 30, Active: true
Example (WorkingWithStringAndAnyValues) ¶
A solution for applications that need to store string values via 'any'
package main import ( "context" "fmt" "github.com/thisisdevelopment/mightymap" "github.com/thisisdevelopment/mightymap/storage" ) func main() { // Create a type-safe wrapper for storing string values type StringValueMap struct { storage mightymap.Map[string, string] } // Constructor function for the wrapper newStringValueMap := func() *StringValueMap { store := storage.NewMightyMapBadgerStorage[string, string]( storage.WithMemoryStorage(true), ) mm := mightymap.New[string, string](true, store) return &StringValueMap{storage: *mm} } // Create a new instance ctx := context.Background() valueMap := newStringValueMap() defer valueMap.storage.Close(ctx) // Store string values valueMap.storage.Store(ctx, "key1", "value1") // Retrieve without type assertion value, ok := valueMap.storage.Load(ctx, "key1") if ok { fmt.Printf("String value: %s\n", value) } }
Output: String value: value1
Index ¶
- func RegisterMsgpackType(value interface{})
- type IMightyMapStorage
- func NewMightyMapBadgerStorage[K comparable, V any](optfuncs ...OptionFuncBadger) IMightyMapStorage[K, V]
- func NewMightyMapDefaultStorage[K comparable, V any]() IMightyMapStorage[K, V]
- func NewMightyMapRedisStorage[K comparable, V any](optfuncs ...OptionFuncRedis) IMightyMapStorage[K, V]
- func NewMightyMapSQLiteStorage[K comparable, V any](optfuncs ...OptionFuncSQLite) IMightyMapStorage[K, V]
- func NewMightyMapSwissStorage[K comparable, V any](optfuncs ...OptionFuncSwiss) IMightyMapStorage[K, V]
- type OptionFuncBadger
- func WithBlockCacheSize(blockCacheSize int64) OptionFuncBadger
- func WithBlockSize(blockSize int) OptionFuncBadger
- func WithCompression(compression bool) OptionFuncBadger
- func WithDetectConflicts(detectConflicts bool) OptionFuncBadger
- func WithEncryptionKey(encryptionKey string) OptionFuncBadger
- func WithEncryptionKeyRotationDuration(encryptionKeyRotation time.Duration) OptionFuncBadger
- func WithGcInterval(gcInterval time.Duration) OptionFuncBadger
- func WithGcPercentage(gcPercentage float64) OptionFuncBadger
- func WithIndexCacheSize(indexCacheSize int64) OptionFuncBadger
- func WithLoggingLevel(loggingLevel int) OptionFuncBadger
- func WithMemTableSize(memTableSize int64) OptionFuncBadger
- func WithMemoryStorage(memoryStorage bool) OptionFuncBadger
- func WithMetricsEnabled(metricsEnabled bool) OptionFuncBadger
- func WithNumCompactors(numCompactors int) OptionFuncBadger
- func WithNumVersionsToKeep(numVersionsToKeep int) OptionFuncBadger
- func WithSyncWrites(syncWrites bool) OptionFuncBadger
- func WithTempDir(dir string) OptionFuncBadger
- func WithValueThreshold(valueThreshold int64) OptionFuncBadger
- type OptionFuncRedis
- func WithRedisAddr(addr string) OptionFuncRedis
- func WithRedisDB(db int) OptionFuncRedis
- func WithRedisExpire(expire time.Duration) OptionFuncRedis
- func WithRedisMaxRetries(maxRetries int) OptionFuncRedis
- func WithRedisMock(t *testing.T) OptionFuncRedis
- func WithRedisPassword(password string) OptionFuncRedis
- func WithRedisPoolSize(poolSize int) OptionFuncRedis
- func WithRedisPrefix(prefix string) OptionFuncRedis
- func WithRedisTLS(tls bool) OptionFuncRedis
- func WithRedisTLSConfig(tlsConfig *tls.Config) OptionFuncRedis
- func WithRedisTimeout(timeout time.Duration) OptionFuncRedis
- func WithRedisUsername(username string) OptionFuncRedis
- type OptionFuncSQLite
- func WithSQLiteCountCacheDuration(duration time.Duration) OptionFuncSQLite
- func WithSQLiteDBPath(path string) OptionFuncSQLite
- func WithSQLiteInMemory() OptionFuncSQLite
- func WithSQLiteJournalMode(mode string) OptionFuncSQLite
- func WithSQLiteMaxIdleConns(count int) OptionFuncSQLite
- func WithSQLiteMaxOpenConns(count int) OptionFuncSQLite
- func WithSQLitePragma(pragma, value string) OptionFuncSQLite
- func WithSQLiteSyncMode(mode string) OptionFuncSQLite
- func WithSQLiteTableName(tableName string) OptionFuncSQLite
- type OptionFuncSwiss
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RegisterMsgpackType ¶ added in v0.5.0
func RegisterMsgpackType(value interface{})
RegisterMsgpackType registers a type with the MessagePack encoder. Unlike Gob, MessagePack doesn't require explicit registration, but we use this to maintain a type registry for proper type conversion.
Types ¶
type IMightyMapStorage ¶ added in v0.1.2
type IMightyMapStorage[K comparable, V any] interface { // Load retrieves a value from storage for the given key. // Returns the value and true if found, zero value and false if not present. Load(ctx context.Context, key K) (value V, ok bool) // Store adds or updates a key-value pair in storage. // The operation is atomic and thread-safe. Store(ctx context.Context, key K, value V) // Delete removes one or more keys and their associated values from storage. // Non-existent keys are silently ignored. Delete(ctx context.Context, keys ...K) // Range iterates over all key-value pairs in storage in an unspecified order, // calling the provided function for each pair. // If the function returns false, iteration stops early. Range(ctx context.Context, f func(key K, value V) bool) // Keys returns all keys in storage in an unspecified order. Keys(ctx context.Context) []K // Next returns and removes the next key-value pair from storage. // The iteration order is not specified and may vary between implementations. // Returns zero values and false when storage is empty. // This operation is atomic - the key-value pair is removed as part of retrieval. Next(ctx context.Context) (key K, value V, ok bool) // Len returns the current number of key-value pairs in storage. Len(ctx context.Context) int // Clear removes all key-value pairs from storage. // This operation is atomic and thread-safe. Clear(ctx context.Context) // Close releases any resources held by the storage implementation. // After calling Close, the storage should not be used for further operations. Close(ctx context.Context) error }
IMightyMapStorage defines the interface for all storage implementations used by MightyMap. This interface provides thread-safe operations for storing, retrieving, and managing key-value pairs. All implementations must support concurrent access and provide context-aware operations.
Type parameters:
- K: the key type, must be comparable
- V: the value type, can be any type
func NewMightyMapBadgerStorage ¶ added in v0.1.2
func NewMightyMapBadgerStorage[K comparable, V any](optfuncs ...OptionFuncBadger) IMightyMapStorage[K, V]
NewMightyMapBadgerStorage creates a new thread-safe storage implementation using BadgerDB. It accepts optional configuration through OptionFuncBadger functions to customize the BadgerDB instance. Values are automatically encoded using MessagePack encoding.
Parameters:
- optfuncs: Optional configuration functions that modify badgerOpts settings
The function:
- Starts with default options and applies any provided option functions
- Configures BadgerDB options including compression, logging level, and performance settings
- Opens a BadgerDB instance with the configured options
- Starts a background goroutine for value log garbage collection
Returns:
- IMightyMapStorage[K, V]: A new BadgerDB-backed storage implementation
Panics if BadgerDB fails to open with the provided configuration.
func NewMightyMapDefaultStorage ¶ added in v0.1.2
func NewMightyMapDefaultStorage[K comparable, V any]() IMightyMapStorage[K, V]
NewMightyMapDefaultStorage creates a new default storage implementation with the specified key and value types. This function returns a direct in-memory storage without encoding for optimal performance. The storage uses a standard Go map protected by a read-write mutex for thread safety.
This is the recommended storage for most use cases where persistence is not required and maximum performance is desired.
Type parameters:
- K: the key type, must be comparable
- V: the value type, can be any type
Returns a new IMightyMapStorage instance ready for use.
func NewMightyMapRedisStorage ¶ added in v0.3.0
func NewMightyMapRedisStorage[K comparable, V any](optfuncs ...OptionFuncRedis) IMightyMapStorage[K, V]
func NewMightyMapSQLiteStorage ¶ added in v0.5.0
func NewMightyMapSQLiteStorage[K comparable, V any](optfuncs ...OptionFuncSQLite) IMightyMapStorage[K, V]
NewMightyMapSQLiteStorage creates a new thread-safe storage implementation using SQLite. It accepts optional configuration through OptionFuncSQLite functions. Values are automatically encoded using MessagePack encoding.
Parameters:
- optfuncs: Optional configuration functions that modify sqliteOpts settings
Returns:
- IMightyMapStorage[K, V]: A new SQLite-backed storage implementation
Panics if SQLite fails to open/initialize with the provided configuration.
func NewMightyMapSwissStorage ¶ added in v0.1.2
func NewMightyMapSwissStorage[K comparable, V any](optfuncs ...OptionFuncSwiss) IMightyMapStorage[K, V]
NewMightyMapSwissStorage creates a new thread-safe map storage implementation using swiss.Map with optional configuration through OptionFuncSwiss functions. Values are automatically encoded using MessagePack encoding.
NOTE: If you're using Go 1.24 or later, consider using the default storage implementation instead, as Go 1.24+ already uses SwissMap internally for its map implementation.
type OptionFuncBadger ¶
type OptionFuncBadger func(*badgerOpts)
OptionFuncBadger is a function type that modifies badgerOpts configuration. It allows customizing the behavior of the BadgerDB storage implementation through functional options pattern. WithXXX...
func WithBlockCacheSize ¶
func WithBlockCacheSize(blockCacheSize int64) OptionFuncBadger
WithBlockCacheSize sets the size of the block cache in bytes. **Default value**: `512 << 20` (512 MB)
func WithBlockSize ¶
func WithBlockSize(blockSize int) OptionFuncBadger
WithBlockSize sets the size of each block in the LSM tree in bytes. **Default value**: `16 * 1024` (16 KB)
func WithCompression ¶
func WithCompression(compression bool) OptionFuncBadger
WithCompression enables or disables data compression using ZSTD in Badger. **Default value**: `false`
func WithDetectConflicts ¶
func WithDetectConflicts(detectConflicts bool) OptionFuncBadger
WithDetectConflicts enables or disables conflict detection in Badger. **Default value**: `true`
func WithEncryptionKey ¶ added in v0.1.4
func WithEncryptionKey(encryptionKey string) OptionFuncBadger
WithEncryptionKey sets the encryption key for the Badger database.
func WithEncryptionKeyRotationDuration ¶ added in v0.1.4
func WithEncryptionKeyRotationDuration(encryptionKeyRotation time.Duration) OptionFuncBadger
WithEncryptionKeyRotationDuration sets the rotation duration for the encryption key in Badger.
func WithGcInterval ¶ added in v0.1.2
func WithGcInterval(gcInterval time.Duration) OptionFuncBadger
WithGcInterval sets the interval for garbage collection in Badger. **Default value**: `10 * time.Second`
func WithGcPercentage ¶ added in v0.1.2
func WithGcPercentage(gcPercentage float64) OptionFuncBadger
WithGcPercentage sets the percentage of value log space to be collected during garbage collection. **Default value**: `0.5`
func WithIndexCacheSize ¶
func WithIndexCacheSize(indexCacheSize int64) OptionFuncBadger
WithIndexCacheSize sets the size of the LSM tree cache in bytes. **Default value**: `128 << 20` (128 MB)
func WithLoggingLevel ¶
func WithLoggingLevel(loggingLevel int) OptionFuncBadger
WithLoggingLevel sets the logging level for Badger. **Default value**: `int(badger.ERROR)` Logging levels: - `0`: `DEBUG` - `1`: `INFO` - `2`: `WARNING` - `3`: `ERROR`
func WithMemTableSize ¶ added in v0.1.4
func WithMemTableSize(memTableSize int64) OptionFuncBadger
WithMemTableSize sets the size of the memtable in bytes. **Default value**: `128 << 20` (128 MB)
func WithMemoryStorage ¶
func WithMemoryStorage(memoryStorage bool) OptionFuncBadger
WithMemoryStorage enables or disables in-memory storage. If set to `true`, the database will be stored in memory. **Default value**: `true`
func WithMetricsEnabled ¶
func WithMetricsEnabled(metricsEnabled bool) OptionFuncBadger
WithMetricsEnabled enables or disables metrics collection in Badger. **Default value**: `false`
func WithNumCompactors ¶
func WithNumCompactors(numCompactors int) OptionFuncBadger
WithNumCompactors sets the number of compaction workers in Badger. **Default value**: `8`
func WithNumVersionsToKeep ¶
func WithNumVersionsToKeep(numVersionsToKeep int) OptionFuncBadger
WithNumVersionsToKeep specifies the number of versions to keep per key. **Default value**: `2`
func WithSyncWrites ¶ added in v0.1.5
func WithSyncWrites(syncWrites bool) OptionFuncBadger
WithSyncWrites enables or disables synchronous writes in Badger. **Default value**: `false`
func WithTempDir ¶
func WithTempDir(dir string) OptionFuncBadger
WithTempDir sets the directory for storing the Badger database files. **Default value**: `os.TempDir() + "/badger-{timestamp}"`
func WithValueThreshold ¶ added in v0.1.4
func WithValueThreshold(valueThreshold int64) OptionFuncBadger
WithValueThreshold sets the threshold for value storage in Badger. **Default value**: `4 << 20` (4 MB)
type OptionFuncRedis ¶ added in v0.3.0
type OptionFuncRedis func(*redisOpts)
func WithRedisAddr ¶ added in v0.3.0
func WithRedisAddr(addr string) OptionFuncRedis
WithRedisAddr sets the Redis server address (host:port) for the client connection. Example: "localhost:6379" or "redis.example.com:6379"
func WithRedisDB ¶ added in v0.3.0
func WithRedisDB(db int) OptionFuncRedis
WithRedisDB selects the Redis logical database to use. Redis servers support multiple logical databases indexed by numbers (0-15 by default). The default database is 0.
func WithRedisExpire ¶ added in v0.3.0
func WithRedisExpire(expire time.Duration) OptionFuncRedis
WithRedisExpire sets the expiration time for the Redis key-value pairs. The expire parameter specifies the duration after which keys will expire. If expire is 0, keys will not expire (persist indefinitely).
func WithRedisMaxRetries ¶ added in v0.3.0
func WithRedisMaxRetries(maxRetries int) OptionFuncRedis
WithRedisMaxRetries sets the maximum number of retries for failed Redis operations. The client will retry operations if they fail due to network issues or other recoverable errors.
func WithRedisMock ¶ added in v0.3.0
func WithRedisMock(t *testing.T) OptionFuncRedis
WithRedisMock (not implemented) sets the Redis client to use a mock implementation. This is useful for testing and development environments where a real Redis server is not available.
func WithRedisPassword ¶ added in v0.3.0
func WithRedisPassword(password string) OptionFuncRedis
WithRedisPassword sets the password for Redis authentication. If Redis server requires authentication, this password will be used. For servers without authentication, pass an empty string.
func WithRedisPoolSize ¶ added in v0.3.0
func WithRedisPoolSize(poolSize int) OptionFuncRedis
WithRedisPoolSize sets the maximum number of socket connections in the Redis connection pool. Default is 10 connections per every available CPU as reported by runtime.NumCPU.
func WithRedisPrefix ¶ added in v0.3.0
func WithRedisPrefix(prefix string) OptionFuncRedis
WithRedisPrefix sets a global key prefix for all Redis operations. All keys will be automatically prefixed with this string. Useful for namespacing keys in a shared Redis instance.
func WithRedisTLS ¶ added in v0.3.0
func WithRedisTLS(tls bool) OptionFuncRedis
WithRedisTLS enables or disables TLS/SSL encryption for Redis connections. When enabled, the client will attempt to establish secure connections to the Redis server.
func WithRedisTLSConfig ¶ added in v0.3.0
func WithRedisTLSConfig(tlsConfig *tls.Config) OptionFuncRedis
WithRedisTLSConfig sets custom TLS configuration for Redis connections. This allows fine-grained control over TLS settings such as certificates, cipher suites, etc. Only used when TLS is enabled.
func WithRedisTimeout ¶ added in v0.3.0
func WithRedisTimeout(timeout time.Duration) OptionFuncRedis
WithRedisTimeout sets the timeout duration for Redis client operations. This timeout value is used to create a context with timeout for Redis operations. It helps prevent operations from hanging indefinitely.
func WithRedisUsername ¶ added in v0.5.1
func WithRedisUsername(username string) OptionFuncRedis
WithRedisUsername sets the username for Redis authentication. This is used for Redis ACL (Access Control List) authentication in Redis 6.0+. For Redis servers that support username-based authentication, this username will be used along with the password for authentication.
type OptionFuncSQLite ¶ added in v0.5.0
type OptionFuncSQLite func(*sqliteOpts)
OptionFuncSQLite is a function type that modifies sqliteOpts configuration. It allows customizing the behavior of the SQLite storage implementation through functional options pattern.
func WithSQLiteCountCacheDuration ¶ added in v0.5.0
func WithSQLiteCountCacheDuration(duration time.Duration) OptionFuncSQLite
WithSQLiteCountCacheDuration sets the duration for which the count result is cached.
func WithSQLiteDBPath ¶ added in v0.5.0
func WithSQLiteDBPath(path string) OptionFuncSQLite
WithSQLiteDBPath specifies the file path for the SQLite database. If not specified, an in-memory database will be used.
func WithSQLiteInMemory ¶ added in v0.5.0
func WithSQLiteInMemory() OptionFuncSQLite
WithSQLiteInMemory specifies that an in-memory database should be used. This is faster but data will be lost when the application exits.
func WithSQLiteJournalMode ¶ added in v0.5.0
func WithSQLiteJournalMode(mode string) OptionFuncSQLite
WithSQLiteJournalMode sets the journal mode for the SQLite database. Common values: DELETE, TRUNCATE, PERSIST, MEMORY, WAL, OFF
func WithSQLiteMaxIdleConns ¶ added in v0.5.0
func WithSQLiteMaxIdleConns(count int) OptionFuncSQLite
WithSQLiteMaxIdleConns sets the maximum number of idle connections in the connection pool.
func WithSQLiteMaxOpenConns ¶ added in v0.5.0
func WithSQLiteMaxOpenConns(count int) OptionFuncSQLite
WithSQLiteMaxOpenConns sets the maximum number of open connections to the database.
func WithSQLitePragma ¶ added in v0.5.0
func WithSQLitePragma(pragma, value string) OptionFuncSQLite
WithSQLitePragma sets a custom PRAGMA option for the SQLite database.
func WithSQLiteSyncMode ¶ added in v0.5.0
func WithSQLiteSyncMode(mode string) OptionFuncSQLite
WithSQLiteSyncMode sets the synchronous mode for the SQLite database. Common values: OFF, NORMAL, FULL, EXTRA
func WithSQLiteTableName ¶ added in v0.5.0
func WithSQLiteTableName(tableName string) OptionFuncSQLite
WithSQLiteTableName sets a custom table name for the key-value store.
type OptionFuncSwiss ¶
type OptionFuncSwiss func(*swissOpts)
OptionFuncSwiss is a function type that modifies swissOpts configuration. It allows customizing the behavior of the swiss.Map storage implementation through functional options pattern.
func WithDefaultCapacity ¶
func WithDefaultCapacity(capacity uint32) OptionFuncSwiss
WithDefaultCapacity returns an OptionFuncSwiss that sets the initial capacity of the swiss.Map. The capacity should be set based on the expected number of items to optimize memory usage.