Skip to content

shomali11/maps

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

maps Build Status Go Report Card GoDoc License: MIT

A collection of various maps with concurrent versions that supports concurrent reads and writes.

Features

  • Thread safe
  • Available types:
    • Maps:
      • Multi Map
    • Concurrent Maps:
      • Concurrent Map
      • Concurrent Multi Map
    • Sharded Concurrent Maps:
      • Sharded Concurrent Map
      • Sharded Concurrent Multi Map

Note: Sharded versions provides improved performance by reducing the number of write locks

Dependencies

Examples

Example 1

Using NewConcurrentMap to create a concurrent map

package main

import (
	"fmt"
	"github.com/shomali11/maps"
)

func main() {
	concurrentMap := maps.NewConcurrentMap()
	concurrentMap.Set("name", "Raed Shomali")

	fmt.Println(concurrentMap.ContainsKey("name")) // true
	fmt.Println(concurrentMap.Get("name"))      // "Raed Shomali" true
	fmt.Println(concurrentMap.Size())           // 1
	fmt.Println(concurrentMap.IsEmpty())        // false
	fmt.Println(concurrentMap.Keys())           // ["name"]

	concurrentMap.Remove("name")

	fmt.Println(concurrentMap.ContainsKey("name")) // false
	fmt.Println(concurrentMap.Get("name"))      // <nil> false
	fmt.Println(concurrentMap.Size())           // 0
	fmt.Println(concurrentMap.IsEmpty())        // true
	fmt.Println(concurrentMap.Keys())           // []

	concurrentMap.Set("name", "Raed Shomali")
	concurrentMap.Clear()

	fmt.Println(concurrentMap.ContainsKey("name")) // false
	fmt.Println(concurrentMap.Get("name"))      // <nil> false
	fmt.Println(concurrentMap.Size())           // 0
	fmt.Println(concurrentMap.IsEmpty())        // true
	fmt.Println(concurrentMap.Keys())           // []
}

Example 2

Using NewShardedConcurrentMap to create a sharded concurrent map. Default shards are 16

package main

import (
	"fmt"
	"github.com/shomali11/maps"
)

func main() {
	concurrentMap := maps.NewShardedConcurrentMap()
	concurrentMap.Set("name", "Raed Shomali")

	fmt.Println(concurrentMap.ContainsKey("name")) // true
	fmt.Println(concurrentMap.Get("name"))      // "Raed Shomali" true
	fmt.Println(concurrentMap.Size())           // 1
	fmt.Println(concurrentMap.IsEmpty())        // false
	fmt.Println(concurrentMap.Keys())           // ["name"]

	concurrentMap.Remove("name")

	fmt.Println(concurrentMap.ContainsKey("name")) // false
	fmt.Println(concurrentMap.Get("name"))      // <nil> false
	fmt.Println(concurrentMap.Size())           // 0
	fmt.Println(concurrentMap.IsEmpty())        // true
	fmt.Println(concurrentMap.Keys())           // []

	concurrentMap.Set("name", "Raed Shomali")
	concurrentMap.Clear()

	fmt.Println(concurrentMap.ContainsKey("name")) // false
	fmt.Println(concurrentMap.Get("name"))      // <nil> false
	fmt.Println(concurrentMap.Size())           // 0
	fmt.Println(concurrentMap.IsEmpty())        // true
	fmt.Println(concurrentMap.Keys())           // []
}

Example 3

Using WithNumberOfShards to override default number of shards

package main

import (
	"fmt"
	"github.com/shomali11/maps"
)

func main() {
	concurrentMap := maps.NewShardedConcurrentMap(maps.WithNumberOfShards(100))
	concurrentMap.Set("name", "Raed Shomali")

	fmt.Println(concurrentMap.ContainsKey("name")) // true
	fmt.Println(concurrentMap.Get("name"))      // "Raed Shomali" true
	fmt.Println(concurrentMap.Size())           // 1
	fmt.Println(concurrentMap.IsEmpty())        // false
	fmt.Println(concurrentMap.Keys())           // ["name"]

	concurrentMap.Remove("name")

	fmt.Println(concurrentMap.ContainsKey("name")) // false
	fmt.Println(concurrentMap.Get("name"))      // <nil> false
	fmt.Println(concurrentMap.Size())           // 0
	fmt.Println(concurrentMap.IsEmpty())        // true
	fmt.Println(concurrentMap.Keys())           // []

	concurrentMap.Set("name", "Raed Shomali")
	concurrentMap.Clear()

	fmt.Println(concurrentMap.ContainsKey("name")) // false
	fmt.Println(concurrentMap.Get("name"))      // <nil> false
	fmt.Println(concurrentMap.Size())           // 0
	fmt.Println(concurrentMap.IsEmpty())        // true
	fmt.Println(concurrentMap.Keys())           // []
}

Example 4

Using NewConcurrentMultiMap to create a concurrent multi map

package main

import (
	"fmt"
	"github.com/shomali11/maps"
)

func main() {
	concurrentMap := maps.NewConcurrentMultiMap()
	concurrentMap.Set("names", []interface{}{"Raed Shomali"})
	concurrentMap.Append("names", "Dwayne Johnson")

	fmt.Println(concurrentMap.ContainsKey("names")) // true
	fmt.Println(concurrentMap.Get("names"))      // ["Raed Shomali" "Dwayne Johnson"] true
	fmt.Println(concurrentMap.Size())            // 1
	fmt.Println(concurrentMap.IsEmpty())         // false
	fmt.Println(concurrentMap.Keys())            // ["name"]

	concurrentMap.Remove("names")

	fmt.Println(concurrentMap.ContainsKey("names")) // false
	fmt.Println(concurrentMap.Get("names"))      // [] false
	fmt.Println(concurrentMap.Size())            // 0
	fmt.Println(concurrentMap.IsEmpty())         // true
	fmt.Println(concurrentMap.Keys())            // []

	concurrentMap.Append("names", "Raed Shomali")
	concurrentMap.Clear()

	fmt.Println(concurrentMap.ContainsKey("names")) // false
	fmt.Println(concurrentMap.Get("names"))      // [] false
	fmt.Println(concurrentMap.Size())            // 0
	fmt.Println(concurrentMap.IsEmpty())         // true
	fmt.Println(concurrentMap.Keys())            // []
}

Example 5

Using NewShardedConcurrentMultiMap to create a sharded concurrent multi map. Default shards are 16

package main

import (
	"fmt"
	"github.com/shomali11/maps"
)

func main() {
	concurrentMap := maps.NewShardedConcurrentMultiMap()
	concurrentMap.Set("names", []interface{}{"Raed Shomali"})
	concurrentMap.Append("names", "Dwayne Johnson")

	fmt.Println(concurrentMap.ContainsKey("names")) // true
	fmt.Println(concurrentMap.Get("names"))      // ["Raed Shomali" "Dwayne Johnson"] true
	fmt.Println(concurrentMap.Size())            // 1
	fmt.Println(concurrentMap.IsEmpty())         // false
	fmt.Println(concurrentMap.Keys())            // ["name"]

	concurrentMap.Remove("names")

	fmt.Println(concurrentMap.ContainsKey("names")) // false
	fmt.Println(concurrentMap.Get("names"))      // [] false
	fmt.Println(concurrentMap.Size())            // 0
	fmt.Println(concurrentMap.IsEmpty())         // true
	fmt.Println(concurrentMap.Keys())            // []

	concurrentMap.Append("names", "Raed Shomali")
	concurrentMap.Clear()

	fmt.Println(concurrentMap.ContainsKey("names")) // false
	fmt.Println(concurrentMap.Get("names"))      // [] false
	fmt.Println(concurrentMap.Size())            // 0
	fmt.Println(concurrentMap.IsEmpty())         // true
	fmt.Println(concurrentMap.Keys())            // []
}

Example 6

Using WithNumberOfShards to override default number of shards

package main

import (
	"fmt"
	"github.com/shomali11/maps"
)

func main() {
	concurrentMap := maps.NewShardedConcurrentMultiMap(maps.WithNumberOfShards(100))
	concurrentMap.Set("names", []interface{}{"Raed Shomali"})
	concurrentMap.Append("names", "Dwayne Johnson")

	fmt.Println(concurrentMap.ContainsKey("names")) // true
	fmt.Println(concurrentMap.Get("names"))      // ["Raed Shomali" "Dwayne Johnson"] true
	fmt.Println(concurrentMap.Size())            // 1
	fmt.Println(concurrentMap.IsEmpty())         // false
	fmt.Println(concurrentMap.Keys())            // ["name"]

	concurrentMap.Remove("names")

	fmt.Println(concurrentMap.ContainsKey("names")) // false
	fmt.Println(concurrentMap.Get("names"))      // [] false
	fmt.Println(concurrentMap.Size())            // 0
	fmt.Println(concurrentMap.IsEmpty())         // true
	fmt.Println(concurrentMap.Keys())            // []

	concurrentMap.Append("names", "Raed Shomali")
	concurrentMap.Clear()

	fmt.Println(concurrentMap.ContainsKey("names")) // false
	fmt.Println(concurrentMap.Get("names"))      // [] false
	fmt.Println(concurrentMap.Size())            // 0
	fmt.Println(concurrentMap.IsEmpty())         // true
	fmt.Println(concurrentMap.Keys())            // []
}

Releases

No releases published

Packages

No packages published

Languages