- Fix deprecated getAllNodes() warnings by using getNodesWithPagination() in S3 adapter - Add getNounsWithPagination() method to S3CompatibleStorage for proper pagination support - Create optimizedS3Search module for efficient pagination and filtering - Update baseStorage to properly detect pagination support in adapters - Add comprehensive documentation for performance and logging fixes - Ensure backward compatibility with existing code This resolves the following warnings in dependent projects: - "getAllNodes() is deprecated and will be removed in a future version" - "Storage adapter does not support pagination, falling back to loading all nouns" - "Only returning the first 1000 nodes. There are more nodes available"
3.6 KiB
3.6 KiB
Performance and Logging Fixes for Brainy
Overview
This document outlines the fixes implemented to address pagination warnings and improve performance in the Brainy framework.
Issues Addressed
1. Pagination Warnings
The following warnings were appearing when using Brainy in dependent projects:
WARNING: Only returning the first 1000 nodes. There are more nodes available. Use getNodesWithPagination() for proper pagination.
Storage adapter does not support pagination, falling back to loading all nouns. This may cause performance issues with large datasets.
WARNING: getAllNodes() is deprecated and will be removed in a future version. Use getNodesWithPagination() instead.
2. Root Causes
- Missing Pagination Support: The S3 storage adapter didn't expose a
getNounsWithPagination()method, causing the base storage to fall back togetAllNouns_internal(). - Deprecation Warning Logic: The
getAllNouns_internal()method was calling the deprecatedgetAllNodes()method, which triggered deprecation warnings. - Inefficient Data Loading: The fallback approach could load all data into memory, causing performance issues with large datasets.
Solutions Implemented
1. Updated S3 Storage Adapter
File: src/storage/adapters/s3CompatibleStorage.ts
- Modified
getAllNouns_internal()to use the paginatedgetNodesWithPagination()method instead of the deprecatedgetAllNodes(). - Added
getNounsWithPagination()method to properly support pagination with filtering.
public async getNounsWithPagination(options: {
limit?: number
cursor?: string
filter?: {
nounType?: string | string[]
service?: string | string[]
metadata?: Record<string, any>
}
} = {}): Promise<{
items: HNSWNoun[]
totalCount?: number
hasMore: boolean
nextCursor?: string
}>
2. Created Optimized S3 Search Module
File: src/storage/adapters/optimizedS3Search.ts
A new module that provides:
- Efficient pagination using S3's ListObjectsV2 command
- Parallel batch loading for better performance
- Support for complex filtering without loading all data
- Proper cursor-based pagination for large datasets
3. Improved Base Storage Logic
File: src/storage/baseStorage.ts
- The base storage now properly detects when adapters support pagination
- Falls back gracefully when pagination isn't supported
- Limits the amount of data loaded to prevent memory issues
Benefits
- Eliminated Warnings: No more deprecation warnings in dependent projects.
- Better Performance: Pagination prevents loading entire datasets into memory.
- Improved Scalability: Can handle large datasets efficiently.
- Backward Compatibility: Existing code continues to work without changes.
Usage in Dependent Projects
Your code in github-package is already using the correct approach:
const response = await brainy.getNouns({
pagination: { limit: 10, offset: 0 },
filter: { nounType: 'Template' }
})
With these fixes, this will now:
- Use proper pagination without warnings
- Load only the requested data
- Support efficient filtering
Future Improvements
- Native S3 Filtering: Implement server-side filtering using S3 object tags or metadata.
- Index-based Search: Create indexes for common query patterns.
- Caching Layer: Add intelligent caching to reduce S3 API calls.
- Streaming Support: For very large datasets, implement streaming APIs.
Migration Notes
No changes are required in dependent projects. The fixes are backward compatible and will automatically improve performance and eliminate warnings.