artemia

package module
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2024 License: MIT Imports: 5 Imported by: 1

README

🦐 Artemia: A Delightful Golang Prevalence Layer

Welcome to Artemia, the Go library that makes data persistence as easy as keeping sea monkeys alive! (Actually, it's much easier than that.)

What is Artemia?

Artemia is a lightweight, in-memory object prevalence layer for Go applications. It's inspired by the concept of prevalence layers (check out Prevayler and the Prevalence Layer pattern for more info).

Think of Artemia as a magical aquarium where your data objects live and thrive, with the added bonus of being able to survive power outages (unlike real sea monkeys).

🌟 Features

  • In-memory data storage for lightning-fast operations
  • Transparent persistence to disk
  • Support for transactions
  • Indexing for speedy queries
  • No complex setup or external dependencies

🚀 Quick Start

First, install Artemia:

go get github.com/sea-monkeys/artemia

Now, let's dive into some code!

🏊‍♂️ Example 1: Adding a Human to Our Data Aquarium
package main

import (
	"encoding/gob"
	"log"

	"github.com/sea-monkeys/artemia"
)

type Human struct {
	FirstName string
	LastName  string
	Age       int
	City      string
}

func init() {
	gob.Register(Human{})
}

func main() {
	pl, err := artemia.NewPrevalenceLayer("data.gob")
	if err != nil {
		log.Fatal(err)
	}

	bob := Human{
		FirstName: "Bob",
		LastName:  "Morane",
		Age:       42,
		City:      "Lyon",
	}
	if err := pl.Set("bob", bob); err != nil {
		log.Fatal(err)
	}

	// Bob is now swimming happily in our data aquarium!
}
🎣 Example 2: Fishing Out Our Human
package main

import (
	"encoding/gob"
	"fmt"
	"log"

	"github.com/sea-monkeys/artemia"
)

type Human struct {
	FirstName string
	LastName  string
	Age       int
	City      string
}

func init() {
	gob.Register(Human{})
}

func main() {
	pl, err := artemia.NewPrevalenceLayer("data.gob")
	if err != nil {
		log.Fatal(err)
	}

	if val, ok := pl.Get("bob"); ok {
		if human, ok := val.(Human); ok {
			fmt.Printf("Caught a wild human: %+v\n", human)
			// Output: Caught a wild human: {FirstName:Bob LastName:Morane Age:42 City:Lyon}
		}
	}
}

🧜‍♀️ Advanced Features

Indexing: Finding Nemo (or Bob) Faster
pl.CreateIndex(reflect.TypeOf(Human{}), "City")
lyonnais := pl.QueryByIndex(reflect.TypeOf(Human{}), "City", "Lyon")
fmt.Printf("Humans in Lyon: %+v\n", lyonnais)
Transactions: Atomic Operations for Your Data Aquarium
tx := pl.BeginTransaction()
tx.Set(pl, "alice", Human{FirstName: "Alice", LastName: "Wonder", Age: 28, City: "Paris"})
tx.Set(pl, "charlie", Human{FirstName: "Charlie", LastName: "Brown", Age: 8, City: "New York"})
err := pl.Commit(tx)
// If err is nil, both Alice and Charlie are now part of our data ecosystem!

🐠 Why Artemia?

  1. Simple as Sea Monkeys: Easy to set up and use, just add water... err, data!
  2. Fast as a Shark: In-memory operations for high-speed data access.
  3. Persistent as a Barnacle: Your data sticks around, even when the power goes out.
  4. Flexible as an Octopus: Store any Go struct, create indexes, run transactions.

🦀 Contributing

We welcome contributions! Feel free to open issues, submit pull requests, or just send us your best fish puns.

🐙 License

Artemia is released under the MIT License. See the LICENSE file for details.

Remember: Always practice safe data persistence, and never flush your sea monkeys down the toilet!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateFieldFilter

func CreateFieldFilter(fieldName string, expectedValue interface{}) func(interface{}) bool

Types

type Index

type Index struct {
	FieldName string
	Data      map[interface{}][]string
}

type PrevalenceLayer

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

func NewPrevalenceLayer

func NewPrevalenceLayer(filename string) (*PrevalenceLayer, error)

func (*PrevalenceLayer) BeginTransaction

func (pl *PrevalenceLayer) BeginTransaction() *Transaction

func (*PrevalenceLayer) Commit

func (pl *PrevalenceLayer) Commit(tx *Transaction) error

func (*PrevalenceLayer) CreateIndex

func (pl *PrevalenceLayer) CreateIndex(structType reflect.Type, field string)

func (*PrevalenceLayer) Delete

func (pl *PrevalenceLayer) Delete(key string) error

func (*PrevalenceLayer) Get

func (pl *PrevalenceLayer) Get(key string) (interface{}, bool)

func (*PrevalenceLayer) Query

func (pl *PrevalenceLayer) Query(filter func(interface{}) bool) []interface{}

func (*PrevalenceLayer) QueryByIndex

func (pl *PrevalenceLayer) QueryByIndex(structType reflect.Type, field string, value interface{}) []interface{}

func (*PrevalenceLayer) Set

func (pl *PrevalenceLayer) Set(key string, value interface{}) error

type Transaction

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

func (*Transaction) Delete

func (tx *Transaction) Delete(pl *PrevalenceLayer, key string) *Transaction

func (*Transaction) Set

func (tx *Transaction) Set(pl *PrevalenceLayer, key string, value interface{}) *Transaction

type TypeInfo

type TypeInfo struct {
	PkgPath string
	Name    string
}

Jump to

Keyboard shortcuts

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