shm

package module
v0.0.0-...-287e184 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2020 License: MIT Imports: 8 Imported by: 3

README

shm

A hash map implemented in a shared memory mapping.

Example:

package main

import (
	"github.com/fengyoulin/shm"
	"log"
	"time"
)

func main() {
	m, err := shm.Create("map.db", 4096, 40, 32, 20, time.Second)
	if err != nil {
		log.Fatalln(err)
	}

	defer func() {
		err = m.Close()
		if err != nil {
			log.Fatalln(err)
		}
	}()

	// get or add a key
	b, err := m.Get("1a2b3c4d5e6f", true)
	if err != nil {
		log.Fatalln(err)
	}

	// do something with b
	log.Println(cap(b))

	// iterate over the map
	m.Foreach(func(key string, value []byte) bool {
		log.Printf("key: %s\n", key)
		return true
	})
	// m.Delete("key")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMapCap on param validate
	ErrMapCap = errors.New("map cap too large or too small")
	// ErrKeyLen on param validate
	ErrKeyLen = errors.New("key too long or too short")
	// ErrValLen on param validate
	ErrValLen = errors.New("value too large or too small")
	// ErrKeyNot on get and not add
	ErrKeyNot = errors.New("key not found in map")
	// ErrDbSize on open an exist db
	ErrDbSize = errors.New("database size mismatch")
	// ErrDbFull on add a new key
	ErrDbFull = errors.New("no more space in map")
	// ErrTryEnd on add or delete
	ErrTryEnd = errors.New("cannot add after too many tries")
)

Functions

This section is empty.

Types

type Map

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

Map is a shared map

func Create

func Create(path string, mapCap, keyLen, valueLen, maxTry int, wait time.Duration) (m *Map, err error)

Create or open a shared map database

func (*Map) Cap

func (m *Map) Cap() int

Cap return map capacity, cannot grow

func (*Map) Close

func (m *Map) Close() error

Close the shared map database

func (*Map) Delete

func (m *Map) Delete(key string) bool

Delete a key return false on failure, maybe because of: too many tries on a highly parallel situation, or hash func failed

func (*Map) Foreach

func (m *Map) Foreach(fn func(key string, value []byte) bool)

Foreach key/value pair in the map call fn stop on fn return false or finished

func (*Map) Get

func (m *Map) Get(key string, add bool) (b []byte, err error)

Get or add an key return the value in a byte slice on success return error on failure if !add, maybe because of: too many tries on a highly parallel situation, or no more space in the database, or hash func failed

func (*Map) Len

func (m *Map) Len() int

Len return item count in map

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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