Skip to content

Add BadgerDBInsert method for DriverMessageModel #260

Description

@jim-junior

Description

Extend the DriverMessageModel struct by implementing a new standalone method called BadgerDBInsert. To prevent backward compatibility problems across the orchestration plane, do not refactor, rename, or drop the current etcd-based Insert function. This method introduces standalone support for serializing and storing an event or task payload directly inside BadgerDB.

Requirements

  • File Location: internal/models/messages.go
  • Method Signature Strategy: Introduce a new method named BadgerDBInsert(message craneTypes.DriverMessage) error on the DriverMessageModel struct.
  • Transactionality: Execute both the data serialization checking and database storage updates within a single BadgerDB Update (write) transaction to handle execution safely.
  • Serialization & Key Generation: * Generate the targeted storage key combining the existing runtime prefix struct member and the unique object key field: m.Prefix + message.ID (yielding driver-messages/{message.ID}).
    • Marshal the incoming craneTypes.DriverMessage payload struct into valid JSON bytes using the standard json.Marshal library.
  • Overwrite Safety / Duplicate Constraint: Prior to writing the new bytes, check if the calculated storage key already exists in BadgerDB. If it exists, return a descriptive custom error indicating that a message with that ID is already registered, preventing accidental overwrites.
  • Testing: Add a dedicated unit test suite inside internal/models/messages_test.go to validate successful data insertion paths, JSON payload storage structures, and key collision validation constraints utilizing an in-memory database instance.

Success Criteria

  • The BadgerDBInsert method is added cleanly alongside legacy functions without altering public method schemas.
  • Stored item writes occur atomically within an exclusive write transaction block.
  • Submitting a message name or ID that is already allocated correctly triggers a duplicate error boundary instead of overwriting the previous entry.
  • Test Case implementation: A comprehensive unit test is provided that:
    • Sets up an isolated, in-memory BadgerDB instance.
    • Successfully calls BadgerDBInsert using a mock driver message.
    • Confirms that the output is accurately stored as unmarshaled JSON data matching the input metrics.
    • Asserts that submitting an identical object data payload twice accurately drops out with a key collision error response.

Metadata

Metadata

Assignees

No one assigned

    Labels

    goPull requests that update go code

    Type

    Fields

    No fields configured for Task.

    Projects

    Status
    No status

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions