UNPKG

bigbasealpha

Version:

Enterprise-Grade NoSQL Database System with Modular Logger & Offline HSM Security - Complete database platform with professional text-based logging, encryption, caching, indexing, JWT authentication, auto-generated REST API, real-time dashboard, and maste

413 lines (334 loc) • 15.1 kB
#!/usr/bin/env node /** * BigBaseAlpha v1.4.0 - Collection System & Performance Demo * Demonstrates MongoDB-style collections and lazy write performance */ import BigBaseAlpha from '../src/alpha.js'; console.log('šŸ“š BigBaseAlpha v1.4.0 - Collection System & Performance Demo'); console.log('═'.repeat(65)); async function demonstrateCollections() { const db = new BigBaseAlpha({ path: './DEMO_COLLECTIONS_DATA', encryption: false, apiGateway: { enabled: false }, security: { paranoidLogging: false } }); try { await db.init(); console.log('āœ… Database initialized\n'); // ===================================================== // PART 1: MongoDB-Style Collections // ===================================================== console.log('šŸ“š PART 1: MongoDB-Style Collection System'); console.log('─'.repeat(50)); // Get collections (auto-created) const users = db.collection('users'); const logs = db.collection('logs'); const products = db.collection('products'); console.log('āœ… Collections created:', db.listCollections()); // Insert documents console.log('\nšŸ“ Inserting documents...'); const user1 = await users.insert({ id: 1, name: "Ahmet", email: "ahmet@example.com", age: 25, city: "Istanbul" }); const user2 = await users.insert({ id: 2, name: "Fatma", email: "fatma@example.com", age: 30, city: "Ankara" }); await logs.insert({ type: "info", message: "User logged in", date: "2025-08-03", userId: 1 }); await logs.insert({ type: "error", message: "Connection failed", date: "2025-08-02", userId: 2 }); await logs.insert({ type: "warning", message: "Disk space low", date: "2025-08-01" }); console.log('āœ… Documents inserted'); // ===================================================== // PART 2: Advanced MongoDB-Style Queries // ===================================================== console.log('\nšŸ” PART 2: Advanced Query System'); console.log('─'.repeat(50)); // Basic find console.log('\nšŸ”Ž Basic queries:'); const allUsers = users.find(); console.log(`šŸ“Š Total users: ${allUsers.length}`); const ahmet = users.findOne({ name: "Ahmet" }); console.log(`šŸ‘¤ Found user: ${ahmet.name} (${ahmet.email})`); // Advanced queries with operators console.log('\nšŸŽÆ Advanced queries with operators:'); // $gt (greater than) const olderUsers = users.find({ age: { $gt: 25 } }); console.log(`šŸ‘„ Users older than 25: ${olderUsers.length}`); // $in (in array) const cityUsers = users.find({ city: { $in: ["Istanbul", "Izmir"] } }); console.log(`šŸ™ļø Users in Istanbul/Izmir: ${cityUsers.length}`); // $regex (regular expression) const emailPattern = logs.find({ message: { $regex: "log" } }); console.log(`šŸ“§ Messages containing 'log': ${emailPattern.length}`); // $and (logical AND) const complexQuery = users.find({ $and: [ { age: { $gte: 25 } }, { city: "Istanbul" } ] }); console.log(`šŸŽ­ Complex query results: ${complexQuery.length}`); // Date range queries const recentLogs = logs.find({ date: { $gte: "2025-08-02" }, type: { $ne: "info" } }); console.log(`šŸ“… Recent non-info logs: ${recentLogs.length}`); // ===================================================== // PART 3: Sorting, Projection, Pagination // ===================================================== console.log('\nšŸ“‹ PART 3: Advanced Query Options'); console.log('─'.repeat(50)); // Sorting const sortedUsers = users.find({}, { sort: { age: -1 } // Descending by age }); console.log('šŸ‘„ Users sorted by age (desc):', sortedUsers.map(u => `${u.name}(${u.age})`)); // Projection (select specific fields) const userEmails = users.find({}, { projection: { name: 1, email: 1, _id: 0 } }); console.log('šŸ“§ User emails only:', userEmails); // Pagination const paginatedUsers = users.find({}, { skip: 0, limit: 1 }); console.log('šŸ“„ First page (limit 1):', paginatedUsers.length, 'users'); // ===================================================== // PART 4: Updates and Modifications // ===================================================== console.log('\nāœļø PART 4: Document Updates'); console.log('─'.repeat(50)); // $set operator const updateResult1 = await users.update( { name: "Ahmet" }, { $set: { age: 26, status: "active" } } ); console.log(`šŸ“ $set update: ${updateResult1.modifiedCount} documents`); // $inc operator (increment) await users.update( { name: "Fatma" }, { $inc: { age: 1 } } ); console.log('šŸ“ˆ $inc update: Fatma\'s age incremented'); // $push operator (add to array) await users.update( { name: "Ahmet" }, { $push: { hobbies: "reading" } } ); console.log('šŸ“š $push update: Added hobby to Ahmet'); // ===================================================== // PART 5: Indexing for Performance // ===================================================== console.log('\nšŸš€ PART 5: Indexing System'); console.log('─'.repeat(50)); // Create indexes users.createIndex({ email: 1 }, { name: 'email_index' }); users.createIndex({ city: 1, age: -1 }, { name: 'city_age_index' }); logs.createIndex({ type: 1 }, { name: 'type_index' }); console.log('šŸ“‡ Indexes created for better performance'); // Query execution plan const plan = db.explainQuery('users', { email: "ahmet@example.com" }); console.log('šŸ“Š Query execution plan:', { indexUsed: plan.executionStats.indexUsed, documentsExamined: plan.executionStats.documentsExamined }); // ===================================================== // PART 6: Collection Statistics // ===================================================== console.log('\nšŸ“Š PART 6: Collection Statistics'); console.log('─'.repeat(50)); const userStats = db.getCollectionStats('users'); const logStats = db.getCollectionStats('logs'); console.log('šŸ‘„ Users collection:', { documents: userStats.documentCount, indexes: userStats.indexes, memoryKB: Math.round(userStats.memoryUsage / 1024) }); console.log('šŸ“ Logs collection:', { documents: logStats.documentCount, indexes: logStats.indexes, memoryKB: Math.round(logStats.memoryUsage / 1024) }); await db.close(); console.log('\nšŸŽ‰ Collection System Demo Completed Successfully!'); console.log('āœ… MongoDB-style operations work perfectly'); console.log('āœ… Advanced queries with operators supported'); console.log('āœ… Indexing system provides performance optimization'); } catch (error) { console.error('āŒ Demo error:', error); await db.close(); } } async function demonstratePerformance() { console.log('\n\nšŸš€ PERFORMANCE ENGINE DEMO'); console.log('═'.repeat(65)); const db = new BigBaseAlpha({ path: './DEMO_PERFORMANCE_DATA', encryption: false, apiGateway: { enabled: false }, security: { paranoidLogging: false } }); try { await db.init(); console.log('āœ… Database initialized for performance testing\n'); // ===================================================== // PART 1: Enable Lazy Write Mode // ===================================================== console.log('šŸ’¾ PART 1: Lazy Write Performance Mode'); console.log('─'.repeat(50)); // Enable lazy write with custom settings db.enableLazyWrite({ delay: 3000, // 3 seconds batch delay batchSize: 50, // 50 operations per batch compressionEnabled: true }); console.log('šŸš€ Lazy Write enabled: 3s delay, 50 batch size'); // ===================================================== // PART 2: High-Volume Operations // ===================================================== console.log('\n⚔ PART 2: High-Volume Operations'); console.log('─'.repeat(50)); const testCollection = db.collection('performance_test'); const startTime = Date.now(); // Insert many documents quickly (queued, not written immediately) console.log('šŸ“ Inserting 100 documents (lazy mode)...'); for (let i = 1; i <= 100; i++) { await testCollection.insert({ id: i, name: `Test User ${i}`, score: Math.floor(Math.random() * 100), category: i % 5 === 0 ? 'premium' : 'standard', timestamp: new Date() }); } const insertTime = Date.now() - startTime; console.log(`⚔ 100 inserts completed in ${insertTime}ms (queued)`); // Show performance stats const perfStats = db.getPerformanceStats(); console.log('šŸ“Š Performance stats:', { pendingOperations: perfStats.pendingCount, totalOperations: perfStats.totalOperations, batchesWritten: perfStats.batchesWritten }); // ===================================================== // PART 3: Force Flush and Timing // ===================================================== console.log('\nšŸ’¾ PART 3: Force Flush Operations'); console.log('─'.repeat(50)); console.log('šŸ”„ Forcing flush of all pending operations...'); const flushStart = Date.now(); await db.flushOperations(); const flushTime = Date.now() - flushStart; console.log(`āœ… All operations flushed in ${flushTime}ms`); // Updated stats const newStats = db.getPerformanceStats(); console.log('šŸ“Š After flush:', { pendingOperations: newStats.pendingCount, batchesWritten: newStats.batchesWritten, compressionRatio: Math.round(newStats.compressionRatio * 100) + '%' }); // ===================================================== // PART 4: Performance Comparison // ===================================================== console.log('\nāš–ļø PART 4: Performance Comparison'); console.log('─'.repeat(50)); // Disable lazy write for comparison await db.disableLazyWrite(); console.log('šŸ›‘ Lazy write disabled'); const immediateCollection = db.collection('immediate_test'); const immediateStart = Date.now(); // Insert with immediate write console.log('šŸ“ Inserting 50 documents (immediate mode)...'); for (let i = 1; i <= 50; i++) { await immediateCollection.insert({ id: i, name: `Immediate User ${i}`, data: 'immediate_write_test' }); } const immediateTime = Date.now() - immediateStart; console.log(`ā±ļø 50 immediate inserts: ${immediateTime}ms`); // ===================================================== // PART 5: Query Performance // ===================================================== console.log('\nšŸ” PART 5: Query Performance'); console.log('─'.repeat(50)); // Create index for performance testCollection.createIndex({ category: 1, score: -1 }); const queryStart = Date.now(); // Complex query const results = testCollection.find({ $and: [ { category: 'premium' }, { score: { $gte: 50 } } ] }, { sort: { score: -1 }, limit: 10 }); const queryTime = Date.now() - queryStart; console.log(`šŸŽÆ Complex query found ${results.length} results in ${queryTime}ms`); // Show query plan const queryPlan = db.explainQuery('performance_test', { category: 'premium', score: { $gte: 50 } }); console.log('šŸ“‹ Query optimization:', { indexUsed: queryPlan.executionStats.indexUsed, documentsExamined: queryPlan.executionStats.documentsExamined, totalDocuments: queryPlan.totalDocuments }); await db.close(); console.log('\nšŸŽ‰ Performance Demo Completed Successfully!'); console.log('āœ… Lazy write provides significant performance boost'); console.log('āœ… Batch processing optimizes disk I/O'); console.log('āœ… Query optimization with indexes works perfectly'); } catch (error) { console.error('āŒ Performance demo error:', error); await db.close(); } } // Run both demos async function runAllDemos() { try { await demonstrateCollections(); await demonstratePerformance(); console.log('\nšŸš€ ALL DEMOS COMPLETED SUCCESSFULLY!'); console.log('═'.repeat(65)); console.log('šŸŽÆ BigBaseAlpha v1.4.0 New Features:'); console.log(' āœ… MongoDB-style collection system'); console.log(' āœ… Advanced query engine with operators'); console.log(' āœ… Performance optimization with lazy write'); console.log(' āœ… Indexing system for better performance'); console.log(' āœ… Comprehensive statistics and monitoring'); } catch (error) { console.error('āŒ Demo execution error:', error); process.exit(1); } } runAllDemos();