github.com/cilium/statedb@v0.3.2/cell.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package statedb
     5  
     6  import (
     7  	"github.com/cilium/hive/cell"
     8  )
     9  
    10  // This module provides an in-memory database built on top of immutable radix trees
    11  // As the database is based on an immutable data structure, the objects inserted into
    12  // the database MUST NOT be mutated, but rather copied first!
    13  var Cell = cell.Module(
    14  	"statedb",
    15  	"In-memory transactional database",
    16  
    17  	cell.Provide(
    18  		newHiveDB,
    19  		ScriptCommands,
    20  	),
    21  )
    22  
    23  type params struct {
    24  	cell.In
    25  
    26  	Lifecycle cell.Lifecycle
    27  	Metrics   Metrics `optional:"true"`
    28  }
    29  
    30  func newHiveDB(p params) *DB {
    31  	db := New(WithMetrics(p.Metrics))
    32  	p.Lifecycle.Append(
    33  		cell.Hook{
    34  			OnStart: func(cell.HookContext) error {
    35  				return db.Start()
    36  			},
    37  			OnStop: func(cell.HookContext) error {
    38  				return db.Stop()
    39  			},
    40  		})
    41  	return db
    42  }