Categorygithub.com/sirgallo/mmcmap
modulepackage
1.1.3
Repository: https://github.com/sirgallo/mmcmap.git
Documentation: pkg.go.dev

# README

MMCMap

Memory Mapped Concurrent Map

Overview

The mmcmap repository is an implementation of a concurrent hash array mapped trie that persists to disk using memory mapped files. Most chamts are implemented as in-memory only, which means that while the data structure itself is immutable, the data is volatile and the data structure will not persist on system restarts. This implementation looks to create a persistent version of a chamt so that the data structure is serialized and stored to disk, eliminating the need to rebuild the entire structure if a system restarts.

mmcmap is inspired by Boltdb, which was my introduction into how databases work at a more fundamental level. Boltdb utilizes a memory-mapped file as its mechanism to persist the data to disk while also maintaining both high read and write speeds. The difference lies in the data structure used, however. Boltdb utilizes a B+tree as its underlying datastructure, which excels at sequential reads. However, hash array mapped tries excel at random reads and writes, with amortized time complexity of O(1) for both single read and write operations.

This project is an exploration of memory mapped files and taking a different approach to storing and retrieving data within a database.

Usage

package main

import "os"
import "path/filepath"

import "github.com/sirgallo/mmcmap"


const FILENAME = "<your-file-name>"


func main() {
  homedir, homedirErr := os.UserHomeDir()
  if homedirErr != nil { panic(homedirErr.Error()) }

  // initialize the mmcmap filepath
  filepath := filepath.Join(homedir, FILENAME)
  opts := mmcmap.MMCMapOpts{ Filepath: filepath }

  // open the mmcmap
  mmcMap, openErr := mmcmap.Open(opts)
  if openErr != nil { panic(openErr.Error()) }

  key := []byte("hello")
  value := []byte("world")

  // put a value in the mmcmap
  _, putErr := mmcMap.Put(key, value)
  if putErr != nil { panic(putErr.Error()) }

  // get a value in the mmcmap
  fetched, getErr := mmcMap.Get(key)
  if getErr != nil { panic(getErr.Error()) }

  // delete a value in the mmcmap
  _, delErr := mmcMap.Delete(key)
  if delErr != nil { panic(delErr.Error()) }

  // get the mmcmap filesize
  fSize, sizeErr := mmcMap.FileSize()
  if sizeErr != nil { panic(sizeErr.Error()) }

  // close the mmcmap
  closeErr := mmcMap.Close()
  if closeErr != nil { panic(closeErr.Error()) }

  // close the mmcmap and remove the associated file
  removeErr := mmcMap.Remove()
  if removeErr != nil { panic(removeErr.Error()) }
}

Tests

mmcmap

go test -v ./tests

murmur

go test -v ./common/murmur/tests

mmap

go test -v ./common/mmap/tests

godoc

For in depth definitions of types and functions, godoc can generate documentation from the formatted function comments. If godoc is not installed, it can be installed with the following:

go install golang.org/x/tools/cmd/godoc

To run the godoc server and view definitions for the package:

godoc -http=:6060

Then, in your browser, navigate to:

http://localhost:6060/pkg/github.com/sirgallo/mmcmap/

Note

currently only tested on unix systems

The mmap function utilizes golang.org/x/sys/unix, so the mmap functionality will only work on unix based systems. Builds for other operating systems can be done but have not been explored or implemented yet.

Sources

CMap

MMCMap

Murmur

Tests

# Packages

No description provided by the author
No description provided by the author

# Functions

DeserializeMetaData Deserialize the byte representation of the meta data object in the memory mapped file.
GetIndex Gets the index at a particular level in the trie by shifting the hash over the chunk size t (5 for 32 bits).
IsBitSet Determines whether or not a bit is set in a bitmap by taking the bitmap and applying a mask with a 1 at the position in the bitmap to check.
NewMMCMapNodePool Creates a new node pool for recycling nodes instead of letting garbage collection handle them.
Open initializes a new mmcmap This will create the memory mapped file or read it in if it already exists.
SetBit Performs a logical xor operation on the current bitmap and the a 32 bit value where the value is all 0s except for at the position of the incoming index.

# Constants

Bitmap size in bytes since bitmap sis uint32.
Offset for the first version of root on mmcmap initialization.
1 GB MaxResize.
Index of Node Version in serialized node.
Index of Root Offset in serialized metadata.
Index of MMCMap Version in serialized metadata.
Size of a new empty internal not.
Index of Bitmap in serialized node.
Size of child pointers, where the pointers are uint64 offsets in the memory map.
Index of Children in serialized internal node.
Index of EndOffset in serialized node.
Index of IsLeaf in serialized node.
Index of Key in serialized leaf node node.
Index of Key Length in serialized node.
Index of StartOffset in serialized node.
The current node version index in serialized node.
OffsetSize for uint64 in serialized node.

# Variables

DefaultPageSize is the default page size set by the underlying OS.

# Structs

MMCMap contains the memory mapped buffer for the mmcmap, as well as all metadata for operations to occur.
MMCMapMetaData contains information related to where the root is located in the mem map and the version.
MMCMapNode represents a singular node within the hash array mapped trie data structure.
MMCMapNodePool contains pre-allocated mmcmap nodes to improve performance so go garbage collection doesn't handle allocating/deallocating nodes on every op.
MMCMapOpts initialize the MMCMap.