nim-raft/tests/basic_state_machine.nim

37 lines
907 B
Nim
Raw Normal View History

2023-08-31 12:03:07 +00:00
# nim-raft
# Copyright (c) 2023 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
import ../raft/types
import std/tables
export tables
type
SmState* = Table[string, string]
SmCommands* = enum
scSet = 0,
scDel = 1
SmCommand* = object
cmd*: SmCommands
key*: string
val*: string
RaftBasicSm* = RaftNodeStateMachine[SmCommand, SmState]
proc RaftSmInit*(stateMachine: var RaftBasicSm) =
new(stateMachine)
new(stateMachine.state)
proc RaftSmApply*(stateMachine: RaftBasicSm, command: SmCommand) =
case command.cmd:
of scSet:
stateMachine.state[command.key] = command.val
of scDel:
2023-08-31 14:05:41 +00:00
stateMachine.state.del(command.key)