This is a Go package that implements a partitioned data structure for storing key/value pairs. Key features include:
- Using a partitioning strategy with CRC32 hashing to distribute keys across multiple partitions,
- thread-safe implementation with mutex locks for concurrent data access,
- generic implementation supporting any key and value types,
- designed for efficient concurrent access by reducing lock contention,
- implements standard map operations (
Get,Put,Delete, etc.).
The package is designed to provide better performance than a standard map when used in highly concurrent environments by distributing the data across multiple partitions.
You can use Go to install this package for you:
go get -u github.com/mwat56/partitionmap
package main
import (
"fmt"
"github.com/mwat56/partitionmap"
)
func main() {
// Create a new partition map with string keys and string values
pm := partitionmap.New[string, string]()
// Store key-value pairs
pm.Put("user1", "John Doe")
pm.Put("user2", "Jane Smith")
// Retrieve values
value, exists := pm.Get("user1")
if exists {
fmt.Printf("Found user1: %s\n", value)
}
// Delete a key
pm.Delete("user2")
// Check map size
fmt.Printf("Map contains %d entries\n", pm.Len())
// Iterate over all entries
pm.ForEach(func(aKey string, aValue string) {
fmt.Printf("%s: %s\n", aKey, aValue)
})
// Get all keys
keys := pm.Keys()
fmt.Printf("Keys: %v\n", keys)
// Clear all entries
pm.Clear()
} // main()
-
Generic Implementation: Works with any key and value types using
Gogenerics.intMap := partitionmap.New[string, int]() structMap := partitionmap.New[int, MyStruct]() -
Thread-Safety: All operations are thread-safe, making it suitable for concurrent access.
// Can be safely used from multiple goroutines go func() { pm.Put("key1", "value1") }() go func() { pm.Get("key2") }() -
Method Chaining: Most methods return the map itself for chaining operations.
pm.Put("key1", "value1").Put("key2", "value2").Delete("key3") -
Efficient Partitioning: Uses CRC32 hashing to distribute keys across 64 partitions, reducing lock contention.
-
Lazy Partition Creation: Partitions are created lazily when needed, saving memory in a sparse map.
- The map uses partitioning to reduce lock contention in concurrent scenarios.
- Partitions are created lazily when needed.
- For high-concurrency applications, this implementation can offer better performance than a standard map with a global lock.
The package is ideal for scenarios where you need a thread-safe map with good performance under concurrent access patterns.
The following external libraries were used building partitionmap:
- No external libraries are used by this module.
Copyright © 2024, 2025 M.Watermann, 10247 Berlin, Germany
All rights reserved
EMail : <support@mwat.de>
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
You should have received a copy of the GNU General Public License along with this program. If not, see the GNU General Public License for details.
