-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
630 lines (541 loc) · 26.5 KB
/
Program.cs
File metadata and controls
630 lines (541 loc) · 26.5 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.IO.MemoryMappedFiles;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
///
/// RandomShuffler is a .NET 4 tool that can generate & shuffle a list of
/// 32bit numbers, up to a specified maximum (up to 2^32)
///
/// Author: M. Dinescu <mdinescu@donaq.com>
///
/// DESCRIPTION:
/// The tool can also upload the numbers from the list to a table in a SQL
/// database, using the bulk copy mechanism to speed up the upload.
///
/// The tool is released as open source software and as such there are no
/// express or implied warranty claims.
///
/// All interaction happens via an interactive console.
///
/// By default, the list is generated in a file named: numbers.lst
///
/// The table schema is: CodeIndex (INT64, Code INT32, CodeType BIT, Assigned INT32)
///
/// The CodeIndex is a numeric index that is incremented for each value added to the list.
///
/// The Code is the the actual randomized numbers
///
/// The CodeType will be set to 0 for numbers less than 2^31, and 1 otherwise.
///
/// The Assigned field will alsways be null (to be used later)
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace DNQ.RandomSuffler
{
class Program
{
static uint maxNum = 0;
static string serverName = ".";
static string databaseName = "";
static string username = "sa";
static string password = "";
static string tableName = "";
static string inputFile = "numbers.lst";
static System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
/// <summary>
/// Prompts user to enter the number of elements in the list, then proceeds to generate a randomized
/// list of that many numbers. The list of numbers generated will be saved in a file (by default
/// called numbers.lst) in binary form. The endianness of the list will be the same as the endianness
/// of the microprocessor where the code is executed.
/// </summary>
static void GenerateList()
{
DisplayHeader();
Console.WriteLine("[List Generator]");
Console.WriteLine();
do
{
ClearPosition(6);
Console.SetCursorPosition(0, 5);
Console.WriteLine("Enter # of values to generate 1 - [4294967296]: ");
string val = Console.ReadLine();
if (val == "")
maxNum = UInt32.MaxValue;
else
UInt32.TryParse(val, out maxNum);
} while (maxNum == 0);
if (maxNum > 100000000)
{
ClearPosition(6);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Warning! The list will require approx. {0:0.000}GB of disk space.. \r\n[Press Any Key To Continue or ESC to cancel]", ((double)maxNum / 250000000));
Console.ForegroundColor = ConsoleColor.Gray;
if (Console.ReadKey().Key == ConsoleKey.Escape)
return;
}
ClearPosition(6);
Console.WriteLine("Shuffling a list of {0} integers ", maxNum);
sw.Start();
using (var memMap = MemoryMappedFile.CreateFromFile(inputFile, System.IO.FileMode.Create, "ListMap", (long)maxNum * 4, MemoryMappedFileAccess.ReadWrite))
{
using (var memView = memMap.CreateViewAccessor())
{
sw.Restart();
for (uint idx = 0; idx < maxNum; idx++)
{
memView.Write((long)idx * 4, idx);
}
Console.WriteLine("Generated initial list. {0}", sw.Elapsed);
Console.WriteLine("Shuffling..");
var rng = System.Security.Cryptography.RNGCryptoServiceProvider.Create();
int milCnt = 0;
for (uint idx = maxNum - 1; idx > 0; idx--)
{
byte[] b = new byte[4];
rng.GetBytes(b);
uint rndNumber = BitConverter.ToUInt32(b, 0);
uint rndPosition = (uint)(rndNumber % idx);
long srcPos = (long)idx * 4;
long destPos = (long)rndPosition * 4;
uint tmp = memView.ReadUInt32(srcPos);
memView.Write(srcPos, memView.ReadUInt32(destPos));
memView.Write(destPos, tmp);
if (idx % 1000000 == 0)
{
milCnt++;
Console.SetCursorPosition(0, 11);
Console.WriteLine("{0} million numbers shuffled -> time elapsed {1}", milCnt, sw.Elapsed);
}
}
Console.SetCursorPosition(0, 11);
Console.WriteLine("{0} million numbers shuffled -> time elapsed {1}", milCnt, sw.Elapsed);
sw.Stop();
}
}
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Done.. Total time elapsed {0} [Press Any Key]", sw.Elapsed);
Console.ForegroundColor = ConsoleColor.Gray;
Console.ReadKey();
}
/// <summary>
/// Displays the sub-menu for configuring a database connection and uploading a pre-generated list of
/// numbers to a SQL database.
/// </summary>
static void UploadToServer()
{
do
{
DisplayHeader();
Console.WriteLine("[Upload to Server]");
Console.WriteLine();
Console.WriteLine("Please select:");
Console.WriteLine("1. Create table");
Console.WriteLine("2. Drop table");
Console.WriteLine("3. Upload numbers");
Console.WriteLine("4. Change configuration");
Console.WriteLine("0. Return to main menu");
var key = Console.ReadKey();
if (key.Key == ConsoleKey.Escape || key.Key == ConsoleKey.D0) return;
if (key.Key == ConsoleKey.D1) CreateTable();
if (key.Key == ConsoleKey.D2) DropTable();
if (key.Key == ConsoleKey.D3) UploadNumbers();
if (key.Key == ConsoleKey.D4) ConfigUpload();
} while (true);
}
/// <summary>
/// Helper method that clears the screen and displays a header. The header is always the same.
/// </summary>
static void DisplayHeader()
{
Console.Clear();
Console.WriteLine("Random List Generator\r\n==============================================================================");
Console.WriteLine(" M. Dinescu <mdinescu@donaq.com>");
Console.WriteLine();
}
/// <summary>
/// Helper method that clears a line of text, given the line's vertical position.
/// </summary>
/// <param name="position">Vertical position of the line to clear.</param>
static void ClearPosition(int position)
{
Console.ForegroundColor = ConsoleColor.Gray;
Console.SetCursorPosition(0, position);
Console.Write(" "
}
/// <summary>
/// Helper method that can be used to read & validate user input, with an optional header message.
/// </summary>
/// <param name="position">Vertical position of the line where the input will be requested</param>
/// <param name="input">The name of the input being requested (ie. username)</param>
/// <param name="output">A reference to a string variable that will hold the user input.</param>
/// <param name="displayPrompt">Optional, a delegate that would render some text in preparation for the input.</param>
/// <param name="validator">Optional, a validator function that can check the user input for validity.</param>
static void ReadString(int position, string input, ref string output, Action displayPrompt, Func<string, bool> validator)
{
do
{
if (displayPrompt != null)
displayPrompt();
ClearPosition(position + 1);
Console.SetCursorPosition(0, position);
Console.WriteLine("Enter {0} [{1}]: ", input, output);
var val = Console.ReadLine();
if (val != "") output = val;
if (validator != null && !validator(val))
output = "";
} while (string.IsNullOrWhiteSpace(output));
}
/// <summary>
/// Helper method that can be used to read & validate user input, with an optional header message. The difference
/// between this method and <see cref="ReadString"/> is that this method does not display the previous user input.
/// It should be used for password inputs.
/// </summary>
/// <param name="position">Vertical position of the line where the input will be requested</param>
/// <param name="input">The name of the input being requested (ie. username)</param>
/// <param name="output">A reference to a string variable that will hold the user input.</param>
/// <param name="displayPrompt">Optional, a delegate that would render some text in preparation for the input.</param>
/// <param name="validator">Optional, a validator function that can check the user input for validity.</param>
static void ReadMaskedString(int position, string input, ref string output, Action displayPrompt, Func<string, bool> validator)
{
do
{
if (displayPrompt != null)
displayPrompt();
ClearPosition(position + 1);
Console.SetCursorPosition(0, position);
Console.WriteLine("Enter {0} [***********]: ", input);
var val = Console.ReadLine();
if (val != "") output = val;
if (validator != null && !validator(val))
output = "";
} while (string.IsNullOrWhiteSpace(output));
}
/// <summary>
/// Helper method that displays the current configuration for uploads.
/// </summary>
/// <param name="position">Vertical line where the settings should be displayed on the screen.</param>
static void DisplayCurrentSettings(int position)
{
Console.SetCursorPosition(0, position);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("\r\nCurrent Settings: ");
Console.Write(" Server: "); Console.ForegroundColor = ConsoleColor.White; Console.Write(serverName + "\r\n"); Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" Database: "); Console.ForegroundColor = ConsoleColor.White; Console.Write(databaseName + "\r\n"); Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" Username: "); Console.ForegroundColor = ConsoleColor.White; Console.Write(username + "\r\n"); Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" Password: "); Console.ForegroundColor = ConsoleColor.White; Console.Write("***********\r\n"); Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" Table: "); Console.ForegroundColor = ConsoleColor.White; Console.Write(tableName + "\r\n"); Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" Input File: "); Console.ForegroundColor = ConsoleColor.White; Console.Write(inputFile + "\r\n"); Console.ForegroundColor = ConsoleColor.Gray;
}
/// <summary>
/// Interactively requests configuration input for uploading to a database.
/// </summary>
static void ConfigUpload()
{
do
{
DisplayHeader();
Console.WriteLine("[Upload to Server]");
Console.WriteLine("");
ReadString(14, "server name", ref serverName, delegate { DisplayCurrentSettings(5); }, null);
ReadString(14, "database name", ref databaseName, delegate { DisplayCurrentSettings(5); }, null);
ReadString(14, "username", ref username, delegate { DisplayCurrentSettings(5); }, null);
ReadMaskedString(14, "password", ref password, delegate { DisplayCurrentSettings(5); }, null);
ReadString(14, "table name", ref tableName, delegate { DisplayCurrentSettings(5); }, null);
ReadString(14, "input file", ref inputFile, delegate { DisplayCurrentSettings(5); }, fileName =>
{
if (!System.IO.File.Exists(inputFile))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.SetCursorPosition(0, 15);
Console.WriteLine(" ERROR: " + inputFile + " does not exist!");
Console.ForegroundColor = ConsoleColor.Gray;
Console.ReadKey();
return false;
}
return true;
});
DisplayCurrentSettings(5);
ClearPosition(14);
ClearPosition(15);
Console.SetCursorPosition(0, 15);
Console.Write("Are these values correct [Y]/n: ");
var key = Console.ReadKey();
if (key.Key == ConsoleKey.Enter || key.Key == ConsoleKey.Y) break;
if (key.Key == ConsoleKey.Escape) return;
} while (true);
}
/// <summary>
/// Convenience Method.
/// Creates a table with correct schema in the configured database, on the configured server, that
/// can hold a shuffled list.
/// </summary>
static void CreateTable()
{
DisplayHeader();
Console.WriteLine("[Upload to Server]");
Console.WriteLine(" - Create Table -");
// if configuraton is not available - or incorrect, display error message and go back..
if (databaseName == "" || password == "" || tableName == "")
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\r\nInvalid configuration! Please go back and update configuration..");
Console.ForegroundColor = ConsoleColor.Gray;
Console.ReadKey();
return;
}
Console.SetCursorPosition(0, 5);
Console.Write(" Will create table ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(tableName);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" in database ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(databaseName);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" on ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(serverName);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("\r\nPress any key to begin..");
Console.ReadKey();
SqlConnectionStringBuilder scBuilder = new SqlConnectionStringBuilder();
scBuilder.ApplicationName = "ListGenerator";
scBuilder.DataSource = serverName;
scBuilder.InitialCatalog = databaseName;
scBuilder.UserID = username;
scBuilder.Password = password;
scBuilder.ConnectTimeout = 5;
try
{
using (var sqCon = new SqlConnection(scBuilder.ToString()))
{
sqCon.Open();
using (var sqCmd = sqCon.CreateCommand())
{
sqCmd.CommandType = CommandType.Text;
sqCmd.CommandText = "CREATE TABLE " + tableName + " (CodeIndex INTEGER PRIMARY KEY, Code INTEGER NOT NULL, CodeType TINYINT, Assigned INTEGER NULL)";
sqCmd.ExecuteNonQuery();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("\r\nSUCCESS! The table " + tableName + " was created successfully");
Console.ForegroundColor = ConsoleColor.Gray;
Console.ReadKey();
}
}
}
catch (Exception exc)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\r\n" + exc.Message);
Console.WriteLine("\r\nError creating table! Please go back and update configuration..");
Console.ForegroundColor = ConsoleColor.Gray;
Console.ReadKey();
return;
}
}
/// <summary>
/// Convenience method.
/// Drops a table from the configured database (and server). The table name can also be configured.
/// </summary>
static void DropTable()
{
DisplayHeader();
Console.WriteLine("[Upload to Server]");
Console.WriteLine(" - Drop Table -");
if (databaseName == "" || password == "" || tableName == "")
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\r\nInvalid configuration! Please go back and update configuration..");
Console.ForegroundColor = ConsoleColor.Gray;
Console.ReadKey();
return;
}
Console.SetCursorPosition(0, 5);
Console.Write(" Will drop table ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(tableName);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" from database ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(databaseName);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" on ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(serverName);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("\r\nPress any key to begin..");
Console.ReadKey();
SqlConnectionStringBuilder scBuilder = new SqlConnectionStringBuilder();
scBuilder.ApplicationName = "ListGenerator";
scBuilder.DataSource = serverName;
scBuilder.InitialCatalog = databaseName;
scBuilder.UserID = username;
scBuilder.Password = password;
scBuilder.ConnectTimeout = 5;
try
{
using (var sqCon = new SqlConnection(scBuilder.ToString()))
{
sqCon.Open();
using (var sqCmd = sqCon.CreateCommand())
{
sqCmd.CommandType = CommandType.Text;
sqCmd.CommandText = "DROP TABLE " + tableName;
sqCmd.ExecuteNonQuery();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("\r\nSUCCESS! The table " + tableName + " was created successfully");
Console.ForegroundColor = ConsoleColor.Gray;
Console.ReadKey();
}
}
}
catch (Exception exc)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\r\n" + exc.Message);
Console.WriteLine("\r\nError dropping table! Please go back and update configuration..");
Console.ForegroundColor = ConsoleColor.Gray;
Console.ReadKey();
return;
}
}
/// <summary>
/// Interactively uploads numbers from the shuffled list into a SQL database table.
///
/// The database and the table must exist (see CreateTable for a helper method that creates the table)
///
/// </summary>
static void UploadNumbers()
{
DisplayHeader();
Console.WriteLine("[Upload to Server]");
Console.WriteLine(" - Upload Numbers (" + maxNum + ")-");
if (databaseName == "" || password == "" || tableName == "" || !System.IO.File.Exists(inputFile))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\r\nInvalid configuration! Please go back and update configuration..");
Console.ForegroundColor = ConsoleColor.Gray;
Console.ReadKey();
return;
}
do
{
Console.SetCursorPosition(0, 5);
Console.Write(" ");
Console.SetCursorPosition(0, 5);
Console.Write("How many numbers to upload (" + maxNum + ")? ");
Console.ForegroundColor = ConsoleColor.White;
var val = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.Gray;
if (val != "")
UInt32.TryParse(val, out maxNum);
} while (maxNum == 0);
Console.SetCursorPosition(0, 5);
Console.Write(" Will upload ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(maxNum);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" numbers into database ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(databaseName);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" on ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write(serverName);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("\r\nPress any key to begin..");
Console.ReadKey();
SqlConnectionStringBuilder scBuilder = new SqlConnectionStringBuilder();
scBuilder.ApplicationName = "ListGenerator";
scBuilder.DataSource = serverName;
scBuilder.InitialCatalog = databaseName;
scBuilder.UserID = username;
scBuilder.Password = password;
scBuilder.ConnectTimeout = 5;
try
{
using (var sqCon = new SqlConnection(scBuilder.ToString()))
{
sqCon.Open();
using (var sqCmd = sqCon.CreateCommand())
{
sqCmd.CommandType = CommandType.Text;
sqCmd.CommandText = "SELECT CodeIndex,Code,CodeType,Assigned FROM " + tableName;
sqCmd.ExecuteNonQuery();
}
}
}
catch (Exception exc)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error: " + exc.Message);
Console.WriteLine("\r\nError connecting to database! Please go back and update configuration..");
Console.ForegroundColor = ConsoleColor.Gray;
Console.ReadKey();
return;
}
sw.Restart();
try
{
using (var bc = new SqlBulkCopy(scBuilder.ToString()))
{
bc.BatchSize = 100000;
bc.BulkCopyTimeout = 20;
bc.DestinationTableName = tableName;
bc.NotifyAfter = 100000;
bc.SqlRowsCopied += bc_SqlRowsCopied;
using (var listMapper = new ListDataMapper(inputFile))
{
bc.WriteToServer(listMapper);
}
}
}
catch (Exception exc)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error: " + exc.Message);
Console.ForegroundColor = ConsoleColor.Gray;
Console.ReadKey();
}
sw.Stop();
Console.WriteLine("\r\nPress any key to return to upload menu.");
Console.ReadKey();
}
/// <summary>
/// notifies on the console as records are uploaded to the SQL database via bulk copy
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void bc_SqlRowsCopied(object sender, SqlRowsCopiedEventArgs e)
{
Console.SetCursorPosition(0, 15);
Console.Write(" Copied {0} records in {1}", e.RowsCopied, sw.Elapsed);
}
/// <summary>
/// Application entry point - displays an interactive menu
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
do
{
Console.Clear();
Console.WriteLine("Random List Generator\r\n==============================================================================");
Console.WriteLine(" M. Dinescu <mdinescu@donaq.com>");
Console.WriteLine();
Console.WriteLine("Please Select:");
Console.WriteLine("1. Generate New List");
Console.WriteLine("2. Upload List to Database");
Console.WriteLine("0. Exit [ESC]");
var key = Console.ReadKey();
if (key.Key == ConsoleKey.Escape || key.Key == ConsoleKey.D0) break;
if (key.Key == ConsoleKey.D1) GenerateList();
if (key.Key == ConsoleKey.D2) UploadToServer();
} while (true);
}
}
}