idmap

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2015 License: MIT Imports: 2 Imported by: 0

README

Concurrent-safe string to id mapping. There's no internal state / consistency. If "leto" was initially assigned 4 and the application restarts, it'll likely get assigned a different value. Furthermore, if "leto" is removed and then re-added, it'll get a new id.

What's the point? The point is to be able to expose string ids ("leto" or guids) but interally use more efficient integers. If the internal integer changes, that's fine because it has no meaning on its own

// The parameter is the # of buckets to use. Buckets help shard write-locks.
// Write-locks are short-lived, so there should be no reason for this to be
// very large.
map := idmap.New(4)


// the 2nd parameter tells Get to create the mapping if it doesn't exist
// id1 will be equal to 1
id1 := map.Get("leto", true)

// id2 wil be equal to 2
id2 := map.Get("ghanima", true)

map.Remove("leto")

// id3 will be equal to 3
id3 := map.Get("paul", true)

// id4 will be equal to 0 (create is false)
id4 := map.Get("jessica", false)

uint32

The above map deals with uint64. idmap.New32 can be used when dealing with uint32. The API is the same.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket

type Bucket struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

type Bucket32

type Bucket32 struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

type Map

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

func New

func New(buckets int) *Map

Create a new id mapper. For greater throughput, values are internally sharded into X buckets, where buckets must be a power of 2

func (*Map) Get

func (m *Map) Get(s string, create bool) uint64

Get the id for the given string, optionally creating one if it doesn't exist

func (*Map) Remove

func (m *Map) Remove(s string)

Remove the value from the map

type Map32

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

func New32

func New32(buckets int) *Map32

Create a new id mapper. For greater throughput, values are internally sharded into X buckets, where buckets must be a power of 2

func (*Map32) Get

func (m *Map32) Get(s string, create bool) uint32

Get the id for the given string, optionally creating one if it doesn't exist

func (*Map32) Remove

func (m *Map32) Remove(s string)

Remove the value from the map

Jump to

Keyboard shortcuts

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