|
1 | 1 | import mongoose from "mongoose"; |
2 | 2 |
|
3 | | -// Database indexes to prevent race conditions and improve performance |
| 3 | +// Drop and recreate all performance indexes |
4 | 4 | export const createOptimalIndexes = async () => { |
5 | 5 | try { |
6 | | - console.log('Creating database indexes for race condition prevention...'); |
7 | | - |
8 | | - // User model indexes |
9 | | - await mongoose.connection.db.collection('users').createIndexes([ |
10 | | - // Unique constraints to prevent duplicate registrations |
11 | | - { key: { email: 1 }, unique: true, partialFilterExpression: { isVerified: true } }, |
12 | | - { key: { username: 1 }, unique: true, partialFilterExpression: { isVerified: true } }, |
13 | | - { key: { RegistrationNumber: 1 }, unique: true, partialFilterExpression: { isVerified: true } }, |
14 | | - |
15 | | - // Performance indexes |
16 | | - { key: { points: -1, rank: 1 }, name: 'leaderboard_sort' }, |
17 | | - { key: { isVerified: 1, points: -1 }, name: 'leaderboard_filter' }, |
18 | | - { key: { 'solveChallenges.easy.timestamp': -1 }, name: 'streak_easy' }, |
19 | | - { key: { 'solveChallenges.medium.timestamp': -1 }, name: 'streak_medium' }, |
20 | | - { key: { 'solveChallenges.hard.timestamp': -1 }, name: 'streak_hard' }, |
21 | | - |
22 | | - // Platform data indexes |
23 | | - { key: { 'leetCode.username': 1 }, sparse: true }, |
24 | | - { key: { 'gfg.username': 1 }, sparse: true }, |
25 | | - { key: { 'codeforces.username': 1 }, sparse: true }, |
26 | | - { key: { 'codechef.username': 1 }, sparse: true } |
27 | | - ]); |
28 | | - |
29 | | - // Challenge model indexes |
30 | | - await mongoose.connection.db.collection('challenges').createIndexes([ |
31 | | - // Query performance indexes |
32 | | - { key: { category: 1, difficulty: 1, createdAt: -1 }, name: 'challenge_filter' }, |
33 | | - { key: { createdAt: -1 }, name: 'daily_challenge' }, |
34 | | - { key: { title: 'text', category: 'text' }, name: 'challenge_search' }, |
| 6 | + console.log('Setting up database indexes...'); |
| 7 | + |
| 8 | + const usersCollection = mongoose.connection.db.collection('users'); |
| 9 | + const challengesCollection = mongoose.connection.db.collection('challenges'); |
| 10 | + |
| 11 | + // Drop problematic indexes individually |
| 12 | + try { |
| 13 | + // Drop known problematic indexes |
| 14 | + const problematicIndexes = [ |
| 15 | + 'username_1', 'email_1', 'UserTextIndex', 'ChallengeTextIndex', |
| 16 | + 'email_unique_idx', 'username_unique_idx', 'regno_unique_idx', |
| 17 | + 'challenge_search_idx', 'leaderboard_sort_idx' |
| 18 | + ]; |
35 | 19 |
|
36 | | - // Solved users index |
37 | | - { key: { solvedUsers: 1 }, sparse: true } |
38 | | - ]); |
39 | | - |
| 20 | + for (const indexName of problematicIndexes) { |
| 21 | + try { |
| 22 | + await usersCollection.dropIndex(indexName); |
| 23 | + } catch (e) { /* Index doesn't exist */ } |
| 24 | + try { |
| 25 | + await challengesCollection.dropIndex(indexName); |
| 26 | + } catch (e) { /* Index doesn't exist */ } |
| 27 | + } |
| 28 | + } catch (error) { |
| 29 | + // Continue anyway |
| 30 | + } |
| 31 | + |
| 32 | + // Wait for drops to complete |
| 33 | + await new Promise(resolve => setTimeout(resolve, 1000)); |
| 34 | + |
| 35 | + // Create essential user indexes with explicit names |
| 36 | + await usersCollection.createIndex({ email: 1 }, { unique: true, sparse: true, name: 'email_unique_idx' }); |
| 37 | + await usersCollection.createIndex({ username: 1 }, { unique: true, sparse: true, name: 'username_unique_idx' }); |
| 38 | + // Skip RegistrationNumber for now - has duplicates |
| 39 | + |
| 40 | + // Performance indexes for leaderboard |
| 41 | + await usersCollection.createIndex({ points: -1, rank: 1 }, { name: 'leaderboard_sort_idx' }); |
| 42 | + await usersCollection.createIndex({ isVerified: 1, points: -1 }, { name: 'verified_points_idx' }); |
| 43 | + |
| 44 | + // Streak tracking indexes |
| 45 | + await usersCollection.createIndex({ 'solveChallenges.easy.timestamp': -1 }, { name: 'easy_streak_idx' }); |
| 46 | + await usersCollection.createIndex({ 'solveChallenges.medium.timestamp': -1 }, { name: 'medium_streak_idx' }); |
| 47 | + await usersCollection.createIndex({ 'solveChallenges.hard.timestamp': -1 }, { name: 'hard_streak_idx' }); |
| 48 | + |
| 49 | + // Platform data indexes |
| 50 | + await usersCollection.createIndex({ 'leetCode.username': 1 }, { sparse: true, name: 'leetcode_username_idx' }); |
| 51 | + await usersCollection.createIndex({ 'gfg.username': 1 }, { sparse: true, name: 'gfg_username_idx' }); |
| 52 | + await usersCollection.createIndex({ 'codeforces.username': 1 }, { sparse: true, name: 'cf_username_idx' }); |
| 53 | + await usersCollection.createIndex({ 'codechef.username': 1 }, { sparse: true, name: 'cc_username_idx' }); |
| 54 | + |
| 55 | + // Challenge indexes |
| 56 | + await challengesCollection.createIndex({ category: 1, difficulty: 1, createdAt: -1 }, { name: 'challenge_filter_idx' }); |
| 57 | + await challengesCollection.createIndex({ createdAt: -1 }, { name: 'challenge_date_idx' }); |
| 58 | + await challengesCollection.createIndex({ title: 'text', category: 'text' }, { name: 'challenge_search_idx' }); |
| 59 | + await challengesCollection.createIndex({ solvedUsers: 1 }, { sparse: true, name: 'solved_users_idx' }); |
| 60 | + |
40 | 61 | console.log('Database indexes created successfully'); |
41 | 62 |
|
42 | 63 | } catch (error) { |
43 | | - console.error('Error creating database indexes:', error); |
| 64 | + console.error('Error setting up database indexes:', error); |
44 | 65 | } |
45 | 66 | }; |
46 | 67 |
|
|
0 commit comments