feat(api): Phase 1c - Enhanced Counts API with type-aware methods

Add 5 new methods to Brainy.counts for type-aware operations:

## New Methods

1. **byTypeEnum(type: NounType)** - O(1) type-safe counting
   - Uses Uint32Array internally (more efficient than Map)
   - Type-safe with NounType enum

2. **topTypes(n: number = 10)** - Get top N noun types by count
   - Useful for analytics and cache warming
   - Sorted by count (descending)

3. **topVerbTypes(n: number = 10)** - Get top N verb types
   - Relationship type distribution

4. **allNounTypeCounts()** - Get all noun type counts as Map<NounType, number>
   - Type-safe alternative to getAllTypeCounts()
   - Only includes types with non-zero counts

5. **allVerbTypeCounts()** - Get all verb type counts as Map<VerbType, number>
   - Complete verb type distribution

## Backward Compatibility

 All existing methods still work
 Zero breaking changes
 New methods available alongside old ones

## Integration Tests

- Created comprehensive test suite (tests/integration/brainy-phase1c-integration.test.ts)
- 30 test cases covering:
  - Enhanced API functionality
  - Backward compatibility
  - Auto-sync behavior
  - Real-world workflows
  - Performance characteristics
  - Type safety

## Next Steps

- Fix remaining test API usage issues
- Run full test suite for validation
- Performance benchmarks
- Documentation updates

🎯 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
David Snelling 2025-10-15 14:08:58 -07:00
parent ddb9f04ac0
commit 92ce89e7dc
2 changed files with 529 additions and 1 deletions

View file

@ -2231,6 +2231,8 @@ export class Brainy<T = any> implements BrainyInterface<T> {
/**
* O(1) Count API - Production-scale counting using existing indexes
* Works across all storage adapters (FileSystem, OPFS, S3, Memory)
*
* Phase 1b Enhancement: Type-aware methods with 99.2% memory reduction
*/
get counts() {
return {
@ -2240,7 +2242,7 @@ export class Brainy<T = any> implements BrainyInterface<T> {
// O(1) total relationship count
relationships: () => this.graphIndex.getTotalRelationshipCount(),
// O(1) count by type
// O(1) count by type (string-based, backward compatible)
byType: (type?: string) => {
if (type) {
return this.metadataIndex.getEntityCountByType(type)
@ -2248,6 +2250,33 @@ export class Brainy<T = any> implements BrainyInterface<T> {
return Object.fromEntries(this.metadataIndex.getAllEntityCounts())
},
// Phase 1b: O(1) count by type enum (Uint32Array-based, more efficient)
// Uses fixed-size type tracking: 284 bytes vs ~35KB with Maps (99.2% reduction)
byTypeEnum: (type: NounType) => {
return this.metadataIndex.getEntityCountByTypeEnum(type)
},
// Phase 1b: Get top N noun types by entity count (useful for cache warming)
topTypes: (n: number = 10) => {
return this.metadataIndex.getTopNounTypes(n)
},
// Phase 1b: Get top N verb types by count
topVerbTypes: (n: number = 10) => {
return this.metadataIndex.getTopVerbTypes(n)
},
// Phase 1b: Get all noun type counts as typed Map
// More efficient than byType() for type-aware queries
allNounTypeCounts: () => {
return this.metadataIndex.getAllNounTypeCounts()
},
// Phase 1b: Get all verb type counts as typed Map
allVerbTypeCounts: () => {
return this.metadataIndex.getAllVerbTypeCounts()
},
// O(1) count by relationship type
byRelationshipType: (type?: string) => {
if (type) {