zsync

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package zsync provides thread-safe concurrent data structures for Go applications.

This package offers high-performance, generic implementations of common data structures with built-in synchronization, eliminating the need for external locking when accessing shared data across multiple goroutines.

Data Structures

ZMap: A thread-safe generic map implementation using read-write locks for optimal performance. Supports all standard map operations with concurrent safety.

ZSet: A thread-safe generic set implementation built on top of ZMap, providing efficient operations for managing collections of unique values.

ZQueue: A thread-safe generic FIFO queue implementation with blocking operations for producer/consumer scenarios. Supports context cancellation and graceful shutdown.

Error Handling

All operations follow consistent error handling patterns:

  • ErrQueueClosed: Returned when attempting to operate on a closed queue
  • ErrQueueEmpty: Returned when attempting to pop from an empty queue
  • ErrCanceled: Returned when operations are canceled via context
  • Boolean returns: Indicate success/failure for operations like Get, Delete, Remove
  • No panics: All error conditions are handled gracefully

Performance Characteristics

- Read operations use RLock for concurrent access - Write operations use exclusive Lock for data integrity - Memory efficient: ZSet uses struct{} values to minimize overhead - Pre-allocated slices in bulk operations to reduce allocations

Usage Examples

Basic ZMap usage:

m := zsync.NewZMap[string, int]()
m.Set("key", 42)

value, ok := m.Get("key")
if !ok {
    // key not found
}

existed := m.Delete("key")

Basic ZSet usage:

s := zsync.NewZSet[string]()
s.Add("value")

if s.Contains("value") {
    s.Remove("value")
}

values := s.Values() // []string with all unique values

Basic ZQueue usage:

q := zsync.NewZQueue[string]()
q.Push("item1")

item, err := q.Pop() // blocks until item available
if err != nil {
    // handle ErrQueueClosed
}

q.Close() // signal shutdown to waiting consumers

Thread Safety

All data structures in this package are fully thread-safe and can be used concurrently from multiple goroutines without additional synchronization. The implementations use efficient read-write locks to maximize concurrent read performance while ensuring data consistency during writes.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrQueueClosed = errors.New("queue closed")
	ErrQueueEmpty  = errors.New("queue empty")
	ErrCanceled    = errors.New("operation canceled")
)

Package-level errors used across multiple data structures

Functions

func Ordered

func Ordered[T cmp.Ordered](s *ZSet[T]) []T

Ordered returns a slice containing all values in the set sorted in ascending order. Only works with types that implement cmp.Ordered (strings, numbers, etc).

Example:

values := Ordered(s) // automatically sorts strings, ints, etc.

Types

type ZMap

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

ZMap is a thread-safe generic map implementation. It provides concurrent access to a map with read-write locking for performance.

func NewZMap

func NewZMap[K comparable, V any]() *ZMap[K, V]

NewZMap creates a new thread-safe map with the specified key and value types.

func (*ZMap[K, V]) Clear

func (m *ZMap[K, V]) Clear()

Clear removes all key-value pairs from the map.

func (*ZMap[K, V]) Delete

func (m *ZMap[K, V]) Delete(key K) bool

Delete removes a key-value pair from the map. Returns true if the key existed and was deleted, false otherwise.

func (*ZMap[K, V]) Get

func (m *ZMap[K, V]) Get(key K) (V, bool)

Get retrieves the value associated with the given key. Returns the value and true if found, or the zero value and false if not.

func (*ZMap[K, V]) Keys

func (m *ZMap[K, V]) Keys() []K

Keys returns a slice containing all keys in the map. The order of keys is not guaranteed.

func (*ZMap[K, V]) Len

func (m *ZMap[K, V]) Len() int

Len returns the number of key-value pairs in the map.

func (*ZMap[K, V]) Set

func (m *ZMap[K, V]) Set(key K, value V)

Set stores a key-value pair in the map. If the key already exists, its value is updated.

type ZQueue

type ZQueue[T any] struct {
	// contains filtered or unexported fields
}

ZQueue is a thread-safe generic FIFO queue implementation. It provides blocking operations for producer/consumer scenarios with context cancellation support.

func NewZQueue

func NewZQueue[T any]() *ZQueue[T]

NewZQueue creates a new thread-safe FIFO queue.

func (*ZQueue[T]) Close

func (q *ZQueue[T]) Close() error

Close closes the queue, waking up any blocked Pop operations. After closing, Push operations will return ErrQueueClosed.

func (*ZQueue[T]) IsClosed

func (q *ZQueue[T]) IsClosed() bool

IsClosed returns true if the queue has been closed.

func (*ZQueue[T]) Len

func (q *ZQueue[T]) Len() int

Len returns the current number of items in the queue.

func (*ZQueue[T]) Pop

func (q *ZQueue[T]) Pop() (T, error)

Pop removes and returns an item from the front of the queue. Returns ErrQueueClosed if the queue is empty and closed.

func (*ZQueue[T]) PopContext

func (q *ZQueue[T]) PopContext(ctx context.Context) (T, error)

PopContext removes and returns an item from the front of the queue with context cancellation. Returns ErrQueueClosed if the queue is empty and closed, or ErrCanceled if the context is canceled.

func (*ZQueue[T]) Push

func (q *ZQueue[T]) Push(item T) error

Push adds an item to the back of the queue. Returns ErrQueueClosed if the queue has been closed.

func (*ZQueue[T]) TryPop

func (q *ZQueue[T]) TryPop() (T, error)

TryPop attempts to remove and return an item from the front of the queue without blocking. Returns ErrQueueEmpty if the queue is empty.

type ZSet

type ZSet[T comparable] struct {
	// contains filtered or unexported fields
}

ZSet is a thread-safe generic set implementation. It provides concurrent access to a set of unique values built on top of ZMap.

func NewZSet

func NewZSet[T comparable]() *ZSet[T]

NewZSet creates a new thread-safe set with the specified value type.

func (*ZSet[T]) Add

func (s *ZSet[T]) Add(value T)

Add inserts a value into the set. If the value already exists, this operation has no effect.

func (*ZSet[T]) Clear

func (s *ZSet[T]) Clear()

Clear removes all values from the set.

func (*ZSet[T]) Contains

func (s *ZSet[T]) Contains(value T) bool

Contains checks if a value exists in the set. Returns true if the value is present, false otherwise.

func (*ZSet[T]) Len

func (s *ZSet[T]) Len() int

Len returns the number of unique values in the set.

func (*ZSet[T]) Ordered

func (s *ZSet[T]) Ordered(compare func(a, b T) int) []T

Ordered returns a slice containing all values in the set sorted using the provided comparison function. Use the package-level Ordered for cmp.Ordered types.

func (*ZSet[T]) Remove

func (s *ZSet[T]) Remove(value T) bool

Remove deletes a value from the set. Returns true if the value existed and was removed, false otherwise.

func (*ZSet[T]) Values

func (s *ZSet[T]) Values() []T

Values returns a slice containing all values in the set. The order of values is not guaranteed.

Jump to

Keyboard shortcuts

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