Log Block Model - Individual log data block management within streaming log systems.

This model represents a single block of log data within a LogStreamModel, providing efficient memory management and organized access to log lines. Log blocks are the fundamental storage units that enable high-performance log streaming by breaking large log streams into manageable, sequential segments with precise line numbering and timing information.

Key Features

  • Block-based Storage - Efficient memory usage through segmented log storage
  • Line Management - Precise line numbering with start/end tracking
  • Time-based Organization - Temporal ordering with start and end timestamps
  • Dynamic Loading - On-demand synchronization of log lines from source
  • Incremental Updates - Add new lines without full reload
  • Stream Integration - Seamless integration with LogStreamModel containers

Architecture Relationship

LogBlock works as a component within the broader log streaming architecture:

  • LogStreamModel contains multiple LogBlockModels via LogBlockContainerModel
  • LogBlockContainerModel manages and sorts blocks by block number
  • LogBlockModel stores the actual log lines and metadata for a specific segment
  • This design enables efficient memory usage and fast access to recent log data

Common Use Cases

  • Memory-efficient Log Storage - Break large log streams into manageable blocks
  • Sequential Log Access - Navigate through log history by block
  • Selective Loading - Load only needed blocks for performance
  • Log Line Management - Add, retrieve, and manage individual log entries
  • Time-based Log Analysis - Filter and analyze logs by time ranges
  • Stream Synchronization - Keep log blocks synchronized with source streams

Example: Basic Usage

// Typically accessed through LogStreamModel, not created directly
const logStream = LogStream.instance('app-logs')
.options({ name: 'application-logs', nodeId: 'primary' })
.build();

await logStream.load();

// Access blocks within the stream
const blocks = logStream.blocks;
const currentBlock = logStream.currentBlock;

if (currentBlock) {
console.log(`Block ${currentBlock.blockNum}`);
console.log(`Lines: ${currentBlock.startLineNum}-${currentBlock.endLineNum}`);
console.log(`Time range: ${currentBlock.startTime} to ${currentBlock.endTime}`);
console.log(`Line count: ${currentBlock.lineCount}`);
}

Example: Block Line Management

// Access through log stream
const logStream = LogStream.instance('system-logs').build();
const block = logStream.currentBlock;

if (block) {
// Get current lines
console.log(`Block has ${block.lines.length} loaded lines`);

// Add new lines (typically done automatically by stream)
block.addLines(['New log entry 1', 'New log entry 2']);

// Sync with remote source
await block.syncLines();

// Process lines
block.lines.forEach((line, index) => {
const globalLineNum = block.startLineNum + index;
console.log(`Line ${globalLineNum}: ${line}`);
});
}

Example: Block Analysis and Filtering

// Analyze blocks within a stream
const logStream = LogStream.instance('error-logs').build();
await logStream.load();

// Find blocks within a time range
const startTime = new Date('2024-01-01T00:00:00Z');
const endTime = new Date('2024-01-01T23:59:59Z');

const blocksInRange = logStream.blocks.filter(block =>
block.startTime >= startTime && block.endTime <= endTime
);

console.log(`Found ${blocksInRange.length} blocks in date range`);

// Process error logs from specific blocks
for (const block of blocksInRange) {
await block.syncLines(); // Ensure lines are loaded

const errorLines = block.lines
.map((line, index) => ({ line, lineNum: block.startLineNum + index }))
.filter(({ line }) => line.includes('ERROR'));

if (errorLines.length > 0) {
console.log(`Block ${block.blockNum} has ${errorLines.length} errors`);
errorLines.forEach(({ line, lineNum }) => {
console.log(`Line ${lineNum}: ${line}`);
});
}
}

Example: Block Container Integration

// Access blocks through their container
const logStream = LogStream.instance('debug-logs').build();
const blockContainer = logStream.blocks; // This is a LogBlockContainerModel

// Blocks are automatically sorted by block number
const sortedBlocks = blockContainer.data;
console.log(`Stream has ${sortedBlocks.length} blocks`);

// Get specific block by ID
const blockId = 'debug-logs-block-5';
const specificBlock = blockContainer.getModel(blockId);

if (specificBlock) {
console.log(`Block ${specificBlock.blockNum} loaded`);
await specificBlock.syncLines();
}

// Add new block to container (typically done automatically)
const newBlockOptions: LogBlockOptions = {
nodeId: 'primary',
stream: 'debug-logs',
blockNum: 10,
startLineNum: 1000,
endLineNum: 1099,
startTime: new Date(),
endTime: new Date(Date.now() + 60000),
lineCount: 100
};

const newBlock = LogBlock.instance('debug-logs-block-10')
.options(newBlockOptions)
.build();

blockContainer.addModel(newBlock);

Example: Block Update and Synchronization

const block = logStream.currentBlock;

if (block) {
// Update block metadata (typically done by stream management)
const updatedOptions: LogBlockOptions = {
...block,
endLineNum: block.endLineNum + 50,
endTime: new Date(),
lineCount: block.lineCount + 50
};

block.updateModel(updatedOptions);

// Sync to get new lines
await block.syncLines();

console.log(`Block now has ${block.lines.length} lines loaded`);
console.log(`Updated end time: ${block.endTime}`);
}

Use Declared Type

See

interface LogBlockModel {
    init(): Promise<void>;
}

Methods

Methods

  • -------------------LIFECYCLE----------------------------

    Returns Promise<void>