Skip to content

ramstorage (r9e) is a Golang Memory storage library

License

Notifications You must be signed in to change notification settings

slashdevops/r9e

Repository files navigation

RamStorage (r9e)

CodeQL Analysis Gosec Unit Test Release GitHub go.mod Go version license codecov release Go Reference

RamStorage (r9e) is a Thread-Safe Golang library used for memory storage with convenient methods to store and retrieve data.

This is focused on usability and simplicity rather than performance, but it doesn't mean that it's not fast. Discover it for yourself

Overview

Taking advantage of the Golang Generics and internal golang data structures, RamStorage (r9e) provides a simple way to store and retrieve data.

The goal is to provide an easy way to use a library to store and retrieve data from memory using an API and data structures simples and.

This package doesn't have any dependencies, so it's easy to use and maintain, only golang standard library is required.

Available Containers

Documentation

Official documentation is available on pkg.go.dev -> slashdevops/r9e

Installing

Latest release:

go get -u github.com/slashdevops/r9e@latest

Specific release:

go get -u github.com/slashdevops/[email protected]

Adding it to your project:

import "github.com/slashdevops/r9e"

Example

package main

import (
  "fmt"

  "github.com/slashdevops/r9e"
)

func main() {
  type MathematicalConstants struct {
    Name  string
    Value float64
  }

  // With Capacity allocated
  // kv := r9e.NewMapKeyValue[string, MathematicalConstants](r9e.WithCapacity(5))
  kv := r9e.NewMapKeyValue[string, MathematicalConstants]()

  kv.Set("pi", MathematicalConstants{"Archimedes' constant", 3.141592})
  kv.Set("e", MathematicalConstants{"Euler number, Napier's constant", 2.718281})
  kv.Set("γ", MathematicalConstants{"Euler number, Napier's constant", 0.577215})
  kv.Set("Φ", MathematicalConstants{"Golden ratio constant", 1.618033})
  kv.Set("ρ", MathematicalConstants{"Plastic number ρ (or silver constant)", 2.414213})

  kvFilteredValues := kv.FilterValue(func(value MathematicalConstants) bool {
    return value.Value > 2.0
  })

  fmt.Println("Mathematical Constants:")
  kvFilteredValues.ForEach(func(key string, value MathematicalConstants) {
    fmt.Printf("Key: %v, Name: %v, Value: %v\n", key, value.Name, value.Value)
  })

  fmt.Printf("\n")
  fmt.Printf("The most famous mathematical constant:\n")
  fmt.Printf("Name: %v, Value: %v\n", kv.Get("pi").Name, kv.Get("pi").Value)

  lst := kv.SortValues(func(value1, value2 MathematicalConstants) bool {
    return value1.Value > value2.Value
  })

  fmt.Printf("\n")
  fmt.Printf("The most famous mathematical constant sorted by value:\n")
  for i, value := range lst {
    fmt.Printf("i: %v, Name: %v, Value: %v\n", i, value.Name, value.Value)
  }

  kvHigh, kvLow := kv.Partition(func(key string, value MathematicalConstants) bool {
    return value.Value > 2.5
  })

  fmt.Printf("\n")
  fmt.Printf("Mathematical constants which value is greater than 2.5:\n")
  kvHigh.ForEach(func(key string, value MathematicalConstants) {
    fmt.Printf("Key: %v, Name: %v, Value: %v\n", key, value.Name, value.Value)
  })

  fmt.Printf("\n")
  fmt.Printf("Mathematical constants which value is less than 2.5:\n")
  kvLow.ForEach(func(key string, value MathematicalConstants) {
    fmt.Printf("Key: %v, Name: %v, Value: %v\n", key, value.Name, value.Value)
  })
}

Output:

Mathematical Constants:
Key: ρ, Name: Plastic number ρ (or silver constant), Value: 2.414213
Key: e, Name: Euler number, Napier's constant, Value: 2.718281
Key: pi, Name: Archimedes' constant, Value: 3.141592

The most famous mathematical constant:
Name: Archimedes' constant, Value: 3.141592

The most famous mathematical constants sorted by value:
i: 0, Name: Archimedes' constant, Value: 3.141592
i: 1, Name: Euler number, Napier's constant, Value: 2.718281
i: 2, Name: Plastic number ρ (or silver constant), Value: 2.414213
i: 3, Name: Golden ratio constant, Value: 1.618033
i: 4, Name: Euler number, Napier's constant, Value: 0.577215

Mathematical constants which value is greater than 2.5:
Key: pi, Name: Archimedes' constant, Value: 3.141592
Key: e, Name: Euler number, Napier's constant, Value: 2.718281

Mathematical constants which value is less than 2.5:
Key: Φ, Name: Golden ratio constant, Value: 1.618033
Key: ρ, Name: Plastic number ρ (or silver constant), Value: 2.414213
Key: γ, Name: Euler number, Napier's constant, Value: 0.577215

How Fast?

Discover it for yourself:

git clone [email protected]:slashdevops/r9e.git
cd r9e/
make bench

License

RamStorage (r9e) is released under the Apache License Version 2.0: