@phroun/paged-buffer
Version:
High-performance buffer system for editing massive files with intelligent memory management and undo/redo capabilities
283 lines • 8.83 kB
TypeScript
/**
* Enhanced Virtual Page Manager with Line Tracking, Marks Integration, and Page Merging
*/
export class VirtualPageManager {
constructor(buffer: any, pageSize?: number);
buffer: any;
pageSize: number;
nextPageId: number;
addressIndex: PageAddressIndex;
pageCache: Map<any, any>;
loadedPages: Set<any>;
sourceFile: string | null;
sourceSize: number;
sourceChecksum: string | null;
maxLoadedPages: number;
lruOrder: any[];
minPageSize: number;
maxPageSize: number;
lineAndMarksManager: any;
/**
* Set the line and marks manager (called by PagedBuffer)
* @param {LineAndMarksManager} manager - Line and marks manager instance
*/
setLineAndMarksManager(manager: LineAndMarksManager): void;
/**
* Initialize from a file
* @param {string} filename - Source file path
* @param {number} fileSize - File size
* @param {string} checksum - File checksum
*/
initializeFromFile(filename: string, fileSize: number, checksum: string): void;
/**
* Initialize from string content
* @param {Buffer} content - Content buffer
*/
initializeFromContent(content: Buffer): void;
/**
* Apply memory limit by evicting excess pages
* @private
*/
private _applyMemoryLimit;
/**
* Translate virtual address to page and relative position
* @param {number} virtualPos - Virtual buffer position
* @returns {Promise<{page: PageInfo, relativePos: number, descriptor: PageDescriptor}>}
*/
translateAddress(virtualPos: number): Promise<{
page: PageInfo;
relativePos: number;
descriptor: PageDescriptor;
}>;
/**
* Insert data at a virtual position with line and marks tracking
* @param {number} virtualPos - Position to insert at
* @param {Buffer} data - Data to insert
*/
insertAt(virtualPos: number, data: Buffer): Promise<number>;
/**
* Delete data from a virtual range with line and marks tracking
* @param {number} startPos - Start position
* @param {number} endPos - End position
* @returns {Promise<Buffer>} - Deleted data
*/
deleteRange(startPos: number, endPos: number): Promise<Buffer>;
/**
* Read data from a virtual range
* @param {number} startPos - Start position
* @param {number} endPos - End position
* @returns {Promise<Buffer>} - Read data
*/
readRange(startPos: number, endPos: number): Promise<Buffer>;
/**
* Get total virtual size
*/
getTotalSize(): number;
/**
* Split a page that has grown too large
* @private
*/
private _splitPage;
/**
* Check for page merging opportunities
* @private
*/
private _checkForMergeOpportunities;
/**
* Merge two adjacent pages
* @param {PageDescriptor} firstPage - First page to merge
* @param {PageDescriptor} secondPage - Second page to merge (will be absorbed)
* @private
*/
private _mergePages;
/**
* Clean up empty pages and merge small ones
* @private
*/
private _cleanupAndMergePages;
/**
* Clean up pages that have become empty
* @private
*/
private _cleanupEmptyPages;
/**
* Find the next page after the given page
* @param {PageDescriptor} currentPage - Current page descriptor
* @returns {PageDescriptor|null} - Next page or null
* @private
*/
private _findNextPage;
/**
* Get memory statistics
*/
getMemoryStats(): {
totalPages: number;
loadedPages: number;
dirtyPages: number;
cachedLineInfoPages: number;
memoryUsed: number;
linesMemory: number;
marksMemory: number;
persistentLineMemory: number;
virtualSize: number;
sourceSize: number;
};
/**
* Create initial page descriptors for a file
* @private
*/
private _createInitialPages;
/**
* Enhanced page loading with detachment detection
* @private
*/
private _ensurePageLoaded;
/**
* Enhanced corruption handling that properly triggers detachment
*/
_handleCorruption(descriptor: any, error: any): void;
/**
* Determine the specific reason for corruption based on error
*/
_determineCorruptionReason(error: any): "file_deleted" | "file_truncated" | "permission_denied" | "storage_failure" | "data_corruption";
/**
* Enhanced file loading with better corruption detection
*/
_loadFromOriginalFile(descriptor: any): Promise<Buffer<ArrayBuffer>>;
/**
* Enhanced storage loading with better error handling
*/
_loadFromStorage(descriptor: any): Promise<any>;
/**
* Create PageInfo with enhanced line and marks support
*/
_createPageInfo(descriptor: any, data: any): PageInfo;
/**
* Update LRU order
* @private
*/
private _updateLRU;
/**
* Evict pages if over memory limit
* @private
*/
private _evictIfNeeded;
/**
* Evict a specific page with marks preservation and line info caching
* @private
*/
private _evictPage;
/**
* Generate unique page ID
* @private
*/
private _generatePageId;
}
/**
* Represents a page's metadata for address translation
*/
export class PageDescriptor {
constructor(pageId: any, virtualStart: any, virtualSize: any, sourceType: any, sourceInfo: any);
pageId: any;
virtualStart: any;
virtualSize: any;
sourceType: any;
sourceInfo: any;
isDirty: boolean;
isLoaded: boolean;
lastAccess: number;
generation: number;
parentId: any;
newlineCount: number;
lineInfoCached: boolean;
/**
* Get the virtual end position of this page
*/
get virtualEnd(): any;
/**
* Check if a virtual position falls within this page
*/
contains(virtualPos: any): boolean;
/**
* Convert virtual position to relative position within this page
*/
toRelativePosition(virtualPos: any): number;
/**
* Cache line information from a loaded page
* @param {PageInfo} pageInfo - Loaded page info
*/
cacheLineInfo(pageInfo: PageInfo): void;
}
/**
* Efficient B-tree-like structure for fast address lookups
* Uses binary search for O(log n) lookups even with thousands of pages
* Hash map for O(1) pageId lookups
*/
export class PageAddressIndex {
pages: any[];
pageIdIndex: Map<any, any>;
totalVirtualSize: number;
/**
* Find the page containing a virtual address
* @param {number} virtualPos - Virtual position to look up
* @returns {PageDescriptor|null} - Page containing this position
*/
findPageAt(virtualPos: number): PageDescriptor | null;
/**
* Find page by pageId
* @param {string} pageId - Page ID to find
* @returns {PageDescriptor|null} - Page descriptor or null
*/
findPageById(pageId: string): PageDescriptor | null;
/**
* Insert a new page, maintaining sorted order
* @param {PageDescriptor} pageDesc - Page to insert
*/
insertPage(pageDesc: PageDescriptor): void;
/**
* Remove a page from the index
* @param {string} pageId - ID of page to remove
*/
removePage(pageId: string): void;
/**
* Update virtual addresses after a size change
* @param {string} pageId - Page that changed size
* @param {number} sizeDelta - Change in size (positive or negative)
*/
updatePageSize(pageId: string, sizeDelta: number): void;
/**
* Split a page into two pages
* @param {string} pageId - Page to split
* @param {number} splitPoint - Relative position within page to split at
* @param {string} newPageId - ID for the new second page
* @returns {PageDescriptor} - The new second page descriptor
*/
splitPage(pageId: string, splitPoint: number, newPageId: string): PageDescriptor;
/**
* Get all pages in virtual address order
*/
getAllPages(): any[];
/**
* Get pages that intersect with a virtual range
* @param {number} startPos - Start of range
* @param {number} endPos - End of range
* @returns {PageDescriptor[]} - Pages that intersect the range
*/
getPagesInRange(startPos: number, endPos: number): PageDescriptor[];
/**
* Recalculate total virtual size
* @private
*/
private _updateVirtualSizes;
/**
* Validate the index consistency (for debugging)
*/
validate(): void;
/**
* Validate that hash map is synchronized with pages array
* @throws {Error} If synchronization is broken
*/
validateHashMapSync(): void;
}
import { PageInfo } from "./utils/page-info";
//# sourceMappingURL=virtual-page-manager.d.ts.map