walmap

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2022 License: MIT Imports: 10 Imported by: 0

README

walmap

MIT License GoDoc Go Report Card Releases

walmap is a fast serializable concurrent Map implementation.
Map interface is the same as octu0/cmap. Key/Value is stored using Bitcask-like WAL.

Example

package main

import (
	"bytes"
	"github.com/octu0/walmap"
)

func main() {
	m := walmap.New()
	m.Set("foo", "bar")
	m.Set("hello", "world")

	if v, ok := m.Get("foo"); ok {
		println(v.(string))
	}

	m.Remove("hello")

	if 0 < m.ReclaimableSpace() {
		// Clean deleted values from memory
		if err := m.Compact(); err != nil {
			panic(err)
		}
	}

	// Snapshot / Restore
	out := bytes.NewBuffer(nil)
	if err := m.Snapshot(out); err != nil {
		panic(err)
	}
	m2, err := walmap.Restore(bytes.NewReader(out.Bytes()))
	if err != nil {
		panic(err)
	}

	if v, ok := m2.Get("foo"); ok {
		println(v.(string))
	}
}

Benchmark

5x to 9x faster than implementing Snapshot/Restore using octu0/cmap

goos: darwin
goarch: amd64
pkg: github.com/octu0/walmap
cpu: Intel(R) Core(TM) i5-8210Y CPU @ 1.60GHz
BenchmarkSnapshot
BenchmarkSnapshot/cmap/snapshot/10_000
BenchmarkSnapshot/cmap/snapshot/10_000-4         	     142	   8771184 ns/op
BenchmarkSnapshot/cmap/snapshot/500_000
BenchmarkSnapshot/cmap/snapshot/500_000-4        	       2	 566466707 ns/op
BenchmarkSnapshot/cmap/restore/10_000
BenchmarkSnapshot/cmap/restore/10_000-4          	      99	  10632182 ns/op
BenchmarkSnapshot/cmap/restore/500_000
BenchmarkSnapshot/cmap/restore/500_000-4         	       3	 429974238 ns/op
BenchmarkSnapshot/walmap/snapshot/10_000
BenchmarkSnapshot/walmap/snapshot/10_000-4       	     758	   1342532 ns/op
BenchmarkSnapshot/walmap/snapshot/500_000
BenchmarkSnapshot/walmap/snapshot/500_000-4      	      19	  57805311 ns/op
BenchmarkSnapshot/walmap/restore/10_000
BenchmarkSnapshot/walmap/restore/10_000-4        	      57	  19237020 ns/op
BenchmarkSnapshot/walmap/restore/500_000
BenchmarkSnapshot/walmap/restore/500_000-4       	       6	 200340997 ns/op
PASS

Documentation

Index

Constants

View Source
const (
	AppName string = "walmap"
	Version string = "1.0.1"
)

Variables

View Source
var (
	ErrCompactRunning = errors.New("compat already in progress")
)

Functions

func WithCacheCapacity

func WithCacheCapacity(size int) walmapOptFunc

func WithHashFunc

func WithHashFunc(hashFunc cmap.CMapHashFunc) walmapOptFunc

func WithInitialIndexSize

func WithInitialIndexSize(size int) walmapOptFunc

func WithInitialLogSize

func WithInitialLogSize(size int) walmapOptFunc

func WithShardSize

func WithShardSize(size int) walmapOptFunc

Types

type Log

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

func NewLog

func NewLog(logSize, indexSize int) *Log

func RestoreLog

func RestoreLog(r io.Reader, initialLogSize, initialIndexSize int) (*Log, error)

func (*Log) Compact

func (l *Log) Compact() error

func (*Log) Delete

func (l *Log) Delete(key string) ([]byte, bool, error)

func (*Log) Keys

func (l *Log) Keys() []string

func (*Log) Len

func (l *Log) Len() int

func (*Log) Read

func (l *Log) Read(key string) ([]byte, bool, error)

func (*Log) ReclaimableSpace

func (l *Log) ReclaimableSpace() uint64

func (*Log) Size added in v1.0.1

func (l *Log) Size() uint64

func (*Log) Snapshot

func (l *Log) Snapshot(w io.Writer) error

func (*Log) Write

func (l *Log) Write(key string, data []byte) error

type WMap

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

func New

func New(funcs ...walmapOptFunc) *WMap

func Restore

func Restore(r io.Reader, funcs ...walmapOptFunc) (*WMap, error)

func (*WMap) Compact

func (c *WMap) Compact() error

func (*WMap) Get

func (c *WMap) Get(key string) (interface{}, bool)

func (*WMap) Keys

func (c *WMap) Keys() []string

func (*WMap) Len

func (c *WMap) Len() int

func (*WMap) ReclaimableSpace

func (c *WMap) ReclaimableSpace() uint64

func (*WMap) Remove

func (c *WMap) Remove(key string) (interface{}, bool)

func (*WMap) RemoveIf

func (c *WMap) RemoveIf(key string, fn cmap.RemoveIfFunc) (removed bool)

func (*WMap) Set

func (c *WMap) Set(key string, value interface{})

func (*WMap) SetIfAbsent

func (c *WMap) SetIfAbsent(key string, value interface{}) (updated bool)

func (*WMap) Size added in v1.0.1

func (c *WMap) Size() uint64

func (*WMap) Snapshot

func (c *WMap) Snapshot(w io.Writer) error

func (*WMap) Upsert

func (c *WMap) Upsert(key string, fn cmap.UpsertFunc) (newValue interface{})

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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