liveBook · Manning

Event store

As explained in Build and Orchestrator in Go (From Scratch)

An event store is a persistent datastore that records and retrieves task lifecycle events.

An event store persistently records and retrieves task lifecycle events. It gives the manager a dedicated place to save events without exposing the underlying map or datastore. section 11.10 section 11.3

A small event-store implementation exposes a consistent set of responsibilities:

  • Create and close the backing store, and create its bucket. section 11.10
  • Put an event under a string key, retrieve it with Get, enumerate events with List, and count them with Count. section 11.10
  • Use task.TaskEvent values rather than task.Task values, while presenting the same basic interface as the task store. section 11.4 section 11.10

The implementation can wrap a Go map for simple in-memory operation or BoltDB for file-backed persistence. The persistent version serializes events as JSON inside a bucket and restores them across manager or worker restarts. section 11.4 section 11.12

Switching storage implementations

A program can select either memory or persistent storage through the store.Store interface. The manager calls Put, Get, List, and Count without knowing whether events live in a map or an on-disk BoltDB file. section 11.12 section 11.3

type EventStore struct {
    DbFile   string
    FileMode os.FileMode
    Db       *bolt.DB
    Bucket   string
}
 
func NewEventStore(file string, mode os.FileMode, bucket string)
   (*EventStore, error) {
    db, err := bolt.Open(file, mode, nil)
    if err != nil {
        return nil, fmt.Errorf("unable to open %v", file)
    }
    e := EventStore{
        DbFile:   file,
        FileMode: mode,
        Db:       db,
        Bucket:   bucket,
    }
 
    err = e.CreateBucket()
    if err != nil {
        log.Printf("bucket already exists, will use it instead
     of creating new one")
    }
 
    return &e, nil
}

Inspect the EventStore structure and constructor to see how a BoltDB file, file mode, database handle, and bucket are assembled. section 11.10

How Build and Orchestrator in Go (From Scratch) covers it

How deep it goes
Implements constructors, buckets, serialization, CRUD-like access, listing, and counting, while treating the in-memory variant as nearly identical.
Examples
Uses Go map storage, BoltDB buckets, JSON event values, and Put, Get, List, and Count operations as implementation examples.
What it assumes
Assumes task and event types plus Go maps, interfaces, and datastore calls, while showing the storage abstraction incrementally.
What you can do after
Operate/configure interchangeable in-memory and BoltDB-backed event stores through a common store interface.
How it connects
Connects task events with the store interface, task store, manager, worker, BoltDB, and embedded key-value storage.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build and Orchestrator in Go (From Scratch). The section links point to the parts of the book it draws on.