-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpostgresql-queue-processor.js
More file actions
324 lines (280 loc) · 8.27 KB
/
Copy pathpostgresql-queue-processor.js
File metadata and controls
324 lines (280 loc) · 8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
* PostgreSQL Queue Processor with Batch Insert Operations
*
* Demonstrates efficient queue processing with PostgreSQL batch inserts:
* - Batch consumption from queue
* - Batch and COPY operations
* - Connection pooling
* - Performance monitoring
*/
const { Pool } = require('pg');
class PostgreSQLQueueProcessor {
/**
* Initialize PostgreSQL queue processor
* @param {Object} options - Configuration options
* @param {string} options.connectionString - PostgreSQL connection string
* @param {string} options.tableName - Target table name
*/
constructor({
connectionString = 'postgresql://postgres:password@localhost:5432/market_data',
tableName = 'orders'
} = {}) {
this.connectionString = connectionString;
this.tableName = tableName;
this.queue = [];
this.processedCount = 0;
this.errorCount = 0;
this.totalTime = 0;
// Create connection pool
this.pool = new Pool({
connectionString: this.connectionString,
max: 20,
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 2000
});
this.initialized = false;
}
/**
* Initialize database (create table and indexes)
*/
async initDatabase() {
if (this.initialized) return;
const client = await this.pool.connect();
try {
await client.query(`
CREATE TABLE IF NOT EXISTS ${this.tableName} (
id SERIAL PRIMARY KEY,
order_id VARCHAR(50) UNIQUE NOT NULL,
symbol VARCHAR(10) NOT NULL,
quantity INTEGER NOT NULL,
price NUMERIC(10, 2) NOT NULL,
side VARCHAR(4) NOT NULL,
order_type VARCHAR(20) NOT NULL,
timestamp DOUBLE PRECISION NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
`);
// Create indexes for better performance
await client.query(`
CREATE INDEX IF NOT EXISTS idx_${this.tableName}_symbol
ON ${this.tableName}(symbol)
`);
await client.query(`
CREATE INDEX IF NOT EXISTS idx_${this.tableName}_timestamp
ON ${this.tableName}(timestamp)
`);
this.initialized = true;
} finally {
client.release();
}
}
/**
* Add item to processing queue
* @param {Object} item - Item to enqueue
*/
enqueue(item) {
this.queue.push(item);
}
/**
* Add multiple items to queue
* @param {Array} items - Items to enqueue
*/
enqueueBatch(items) {
this.queue.push(...items);
}
/**
* Batch insert using parameterized query
* @param {Array} documents - Documents to insert
* @returns {number} Number of documents inserted
*/
async batchInsert(documents) {
if (!documents || documents.length === 0) {
return 0;
}
await this.initDatabase();
const client = await this.pool.connect();
try {
const startTime = Date.now();
// Build values array
const values = [];
const placeholders = [];
let paramIndex = 1;
documents.forEach((doc, i) => {
const rowPlaceholders = [];
values.push(
doc.order_id,
doc.symbol,
doc.quantity,
doc.price,
doc.side,
doc.order_type,
doc.timestamp
);
for (let j = 0; j < 7; j++) {
rowPlaceholders.push(`$${paramIndex++}`);
}
placeholders.push(`(${rowPlaceholders.join(', ')})`);
});
const query = `
INSERT INTO ${this.tableName}
(order_id, symbol, quantity, price, side, order_type, timestamp)
VALUES ${placeholders.join(', ')}
ON CONFLICT (order_id) DO NOTHING
`;
const result = await client.query(query, values);
const elapsed = (Date.now() - startTime) / 1000;
this.totalTime += elapsed;
this.processedCount += result.rowCount;
return result.rowCount;
} catch (error) {
this.errorCount += documents.length;
console.error('Batch insert error:', error.message);
return 0;
} finally {
client.release();
}
}
/**
* Insert single document (for small queues)
* @param {Object} document - Document to insert
* @returns {boolean} True if successful
*/
async singleInsert(document) {
await this.initDatabase();
const client = await this.pool.connect();
try {
const startTime = Date.now();
await client.query(
`
INSERT INTO ${this.tableName}
(order_id, symbol, quantity, price, side, order_type, timestamp)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (order_id) DO NOTHING
`,
[
document.order_id,
document.symbol,
document.quantity,
document.price,
document.side,
document.order_type,
document.timestamp
]
);
this.totalTime += (Date.now() - startTime) / 1000;
this.processedCount++;
return true;
} catch (error) {
this.errorCount++;
console.error('Insert error:', error.message);
return false;
} finally {
client.release();
}
}
/**
* Process queue with optional adaptive batching
* @param {Object} options - Processing options
* @param {number} options.batchSize - Maximum batch size
* @param {number} options.workers - Number of worker threads (simplified)
* @param {boolean} options.adaptive - Enable adaptive batching
*/
async processQueue({
batchSize = 1000,
workers = 1,
adaptive = true
} = {}) {
while (this.queue.length > 0) {
// Determine batch size based on queue depth
let currentBatchSize;
if (adaptive) {
const queueSize = this.queue.length;
if (queueSize < 100) {
currentBatchSize = 1;
} else if (queueSize < 1000) {
currentBatchSize = 100;
} else {
currentBatchSize = batchSize;
}
} else {
currentBatchSize = batchSize;
}
// Extract batch from queue
const batch = this.queue.splice(0, currentBatchSize);
if (batch.length === 0) {
break;
}
// Process batch
if (batch.length === 1) {
await this.singleInsert(batch[0]);
} else {
await this.batchInsert(batch);
}
}
}
/**
* Get processing statistics
* @returns {Object} Performance metrics
*/
getStats() {
const throughput = this.totalTime > 0
? this.processedCount / this.totalTime
: 0;
return {
processedCount: this.processedCount,
errorCount: this.errorCount,
totalTimeSeconds: parseFloat(this.totalTime.toFixed(2)),
throughputPerSecond: parseFloat(throughput.toFixed(2)),
queueSize: this.queue.length
};
}
/**
* Close database connection pool
*/
async close() {
await this.pool.end();
}
}
// Example usage and demonstration
async function main() {
console.log('=== PostgreSQL Queue Processor Demo ===\n');
// Initialize processor
const processor = new PostgreSQLQueueProcessor({
connectionString: 'postgresql://postgres:password@localhost:5432/market_data',
tableName: 'orders'
});
// Generate sample market orders
console.log('Generating sample market orders...');
const symbols = ['AAPL', 'GOOGL', 'MSFT', 'AMZN', 'TSLA'];
for (let i = 0; i < 5000; i++) {
const order = {
order_id: `PG_ORD${String(i).padStart(6, '0')}`,
symbol: symbols[Math.floor(Math.random() * symbols.length)],
quantity: Math.floor(Math.random() * 1000) + 1,
price: parseFloat((Math.random() * 400 + 100).toFixed(2)),
side: Math.random() > 0.5 ? 'BUY' : 'SELL',
order_type: 'MARKET',
timestamp: Date.now() / 1000
};
processor.enqueue(order);
}
console.log(`Enqueued ${processor.queue.length} orders`);
// Process with adaptive batching
console.log('\nProcessing queue with adaptive batching...');
const start = Date.now();
await processor.processQueue({
batchSize: 1000,
workers: 4,
adaptive: true
});
// Display stats
const elapsed = (Date.now() - start) / 1000;
console.log(`\nProcessing completed in ${elapsed.toFixed(2)} seconds`);
console.log('Statistics:', processor.getStats());
await processor.close();
console.log('\nDemo completed!');
}
if (require.main === module) {
main().catch(console.error);
}
module.exports = { PostgreSQLQueueProcessor };