FoldDB Atom & AtomRef System
What is the Atom & AtomRef System?
The Atom & AtomRef system provides the foundation for FoldDB's immutable data storage model. This system enables version tracking, immutable data storage, and efficient data access through a combination of Atoms (immutable data containers) and AtomRefs (version pointers).
Key Components
The system consists of three primary components that work together to provide immutable data storage with version control:
Component | Description | Primary Responsibilities |
---|---|---|
Atom | Immutable data container | • Stores content and metadata • Links to previous versions • Maintains schema information • Tracks creation timestamp • Stores creator's public key |
AtomRef | Version tracking pointer | • Points to latest version of data • Enables version history tracking • Maintains update history • Tracks reference status • Records update timestamps |
AtomRefCollection | Collection of references | • Manages multiple atom references • Organizes related atoms with keys • Maintains update history • Tracks collection status • Provides key-based access |
How the Atom & AtomRef System Works
Data Flow
When data is created or updated in FoldDB, it follows this sequence:
- A new Atom is created with content and metadata
- The Atom is assigned a unique UUID
- An AtomRef is created to point to the Atom
- When data is updated, a new Atom is created with a reference to the previous version
- The AtomRef is updated to point to the new Atom
- Previous versions remain accessible through the version chain
flowchart LR
subgraph Creation
C[Create Request] --> A1[Atom v1]
A1 --> AR[AtomRef]
end
subgraph Update
U[Update Request] --> A2[Atom v2]
A2 --> |prev_atom_uuid| A1
AR --> |update pointer| A2
end
subgraph Access
AR --> |get_atom_uuid| A2
A2 --> |prev_atom_uuid| A1
end
Component Architecture
The components interact in this structure:
classDiagram
class Atom {
+uuid: String
+source_schema_name: String
+source_pub_key: String
+created_at: DateTime
+prev_atom_uuid: Option<String>
+content: Value
+status: AtomStatus
+new()
+with_prev_version()
+content()
+uuid()
}
class AtomRef {
+uuid: String
+atom_uuid: String
+updated_at: DateTime
+status: AtomRefStatus
+update_history: Vec<AtomRefUpdate>
+new()
+set_atom_uuid()
+get_atom_uuid()
}
class AtomRefCollection {
+uuid: String
+atom_uuids: HashMap<String, String>
+updated_at: DateTime
+status: AtomRefStatus
+update_history: Vec<AtomRefUpdate>
+new()
+set_atom_uuid()
+get_atom_uuid()
+remove_atom_uuid()
}
class AtomRefBehavior {
<<interface>>
+uuid()
+updated_at()
+status()
+set_status()
+update_history()
}
AtomRef ..|> AtomRefBehavior
AtomRefCollection ..|> AtomRefBehavior
AtomRef --> Atom
AtomRefCollection --> Atom
Implementation Features
1. Immutable Data Storage
The system stores data with these features:
- Immutable Atoms - Once created, Atom content cannot be modified
- Unique identifiers - Each Atom has a UUID for unique identification
- Metadata tracking - Stores schema name, creator, and timestamp
- Status tracking - Maintains Active or Deleted status
- Content storage - Stores JSON data in the content field
2. Version Control
The system provides version control through:
- Version chaining - Each Atom can reference a previous version
- AtomRef pointers - Point to the latest version of data
- Update history - Tracks all changes to references
- Timestamp recording - Maintains creation and update times
- Status tracking - Records Active or Deleted status
3. Reference Collections
The system supports organizing references through:
- Key-based access - References stored with string keys
- Collection management - Add, update, or remove references
- Shared behavior - Common interface through AtomRefBehavior
- Update tracking - Records all collection changes
- Status management - Maintains collection status
Data Structures
Atom Structure
The Atom structure contains these fields:
- uuid - Unique identifier for this Atom
- source_schema_name - Name of the schema that defines this Atom
- source_pub_key - Public key of the creator
- created_at - Timestamp when this Atom was created
- prev_atom_uuid - Optional reference to previous version
- content - The actual data content
- status - Current status (Active or Deleted)
AtomRef Structure
The AtomRef structure contains these fields:
- uuid - Unique identifier for this reference
- atom_uuid - UUID of the referenced Atom
- updated_at - Timestamp of the last update
- status - Current status (Active or Deleted)
- update_history - Record of all updates with timestamps
AtomRefCollection Structure
The AtomRefCollection structure contains these fields:
- uuid - Unique identifier for this collection
- atom_uuids - Map of keys to Atom UUIDs
- updated_at - Timestamp of the last update
- status - Current status (Active or Deleted)
- update_history - Record of all updates with timestamps
Common Operations
Creating Data
To create new data in the system:
- Create a new Atom with content and metadata
- Create an AtomRef pointing to the Atom
- Store both the Atom and AtomRef
Updating Data
To update existing data in the system:
- Create a new Atom with updated content
- Set the prev_atom_uuid to reference the previous version
- Update the AtomRef to point to the new Atom
Accessing Data
To access data in the system:
- Retrieve the AtomRef for the desired data
- Get the atom_uuid from the AtomRef
- Retrieve the Atom using the atom_uuid
- Access the content from the Atom
Managing Collections
To manage collections of references:
- Create an AtomRefCollection
- Add references using set_atom_uuid with keys
- Retrieve references using get_atom_uuid with keys
- Remove references using remove_atom_uuid with keys
Best Practices
Data Management
Follow these practices for data management:
- Always use AtomRefs to access the latest version of data
- Create new Atoms for each data change to maintain immutability
- Link versions properly using prev_atom_uuid
- Use appropriate status values (Active or Deleted)
- Maintain proper update history
Version Control
Follow these practices for version control:
- Always create a new Atom for changes instead of modifying existing ones
- Update AtomRefs atomically to point to the latest version
- Use the version chain to access historical versions when needed
- Track status changes in update history
- Record the source of each update in the history
Performance
Follow these practices for performance:
- Use AtomRefs to access the latest version directly
- Create AtomRefCollections for related data
- Use appropriate keys in collections for efficient access
- Implement caching for frequently accessed Atoms
- Consider version pruning for very long chains