-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListDataMapper.cs
438 lines (394 loc) · 13.9 KB
/
ListDataMapper.cs
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
using System;
using System.Collections.Generic;
using System.Data;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Text;
namespace DNQ.RandomSuffler
{
/// <summary>
/// A data reader that implementation for parse lists of 32bit integers,
/// previously saved by the shuffle operation (via memory mapped files).
///
/// This class functions as a specialized adapter that reads the numbers
/// from the file and presents them in the form that can be consumed via
/// the IDataReader interface.
/// </summary>
public class ListDataMapper
: IDataReader
{
private System.IO.FileInfo fileInfo;
private MemoryMappedFile memMap;
private MemoryMappedViewAccessor memView;
private long _offset = -1;
private long _index = 0;
/// <summary>
/// Instantiates a new ListDataMapper from a given file name.
/// Users should call Dispose when finished with the mapper.
/// </summary>
/// <param name="file">A file that contains the shuffled numbers in binary form.</param>
public ListDataMapper(string file)
{
fileInfo = new System.IO.FileInfo(file);
memMap = MemoryMappedFile.CreateFromFile(file, System.IO.FileMode.Open, "ListMap", fileInfo.Length, MemoryMappedFileAccess.ReadWrite);
memView = memMap.CreateViewAccessor();
_offset = 0;
}
/// <summary>
/// Hardcoded to always return 0. This value does not matter for our implementation.
/// </summary>
public int Depth
{
get { return 0; }
}
/// <summary>
/// Returns meta-data about the data provided by this reader.
/// </summary>
/// <returns>A DataTable object that represents the schema for the data provided by this reader.</returns>
public DataTable GetSchemaTable()
{
throw new NotImplementedException();
}
/// <summary>
/// Always return false because this implementation only supports a single-resultset.
/// </summary>
/// <returns></returns>
public bool NextResult()
{
return false;
}
/// <summary>
/// Call this method to advance the reader to the next record (number in the suffled list).
/// </summary>
/// <returns></returns>
public bool Read()
{
if (IsClosed)
throw new ObjectDisposedException("Cannot read from a disposed mapper");
if (_offset == -1)
_offset = 0;
else
{
_offset += 4; _index++;
}
return (_offset < fileInfo.Length);
}
/// <summary>
/// Hard-coded to always return 1. For our purposes the actual value does not matter much.
/// </summary>
public int RecordsAffected
{
get { return 1; }
}
/// <summary>
/// Number of fields presented by the IDataReader interface.
/// </summary>
public int FieldCount
{
get { return 4; }
}
/// <summary>
/// Returns the value, as a boolean, for a given field. In this implementation
/// the only field that can be read as a boolean is field 2, the code type.
/// </summary>
/// <param name="i">The field index.</param>
/// <returns>A boolean value.</returns>
public bool GetBoolean(int i)
{
if (i == 2)
return false;
throw new InvalidCastException("Invalid data type for column " + i.ToString());
}
/// <summary>
/// Returns the value, as a byte, for a given field. In this implementation
/// the only field that can be read as a byte is field 2, the code type.
/// </summary>
/// <param name="i">The field index.</param>
/// <returns>A byte value (0 means the Code is less then 2^31 and 1 means it greater than)</returns>
public byte GetByte(int i)
{
if (i == 2)
return (byte)(((memView.ReadUInt32(_offset) & (uint)0x80000000) != 0) ? 1 : 0);
throw new InvalidCastException("Invalid data type for column " + i.ToString());
}
/// <summary>
/// Returns the name of teh data type of a given field.
/// </summary>
/// <param name="i">The index of the field.</param>
/// <returns></returns>
public string GetDataTypeName(int i)
{
switch (i)
{
case 0:
return "Int64";
case 1:
return "Int32";
case 2:
return "Byte";
case 3:
return "Int32";
default:
throw new IndexOutOfRangeException();
}
}
/// <summary>
/// Returns the the data type of a given field.
/// </summary>
/// <param name="i">The index of the field.</param>
/// <returns></returns>
public Type GetFieldType(int i)
{
switch (i)
{
case 0:
return typeof(Int64);
case 1:
return typeof(Int32);
case 2:
return typeof(Byte);
case 3:
return typeof(Int32);
default:
throw new IndexOutOfRangeException();
}
}
/// <summary>
/// Returns the value as a 32-bit integer, for a given field. In this implementation
/// the only field that can be read as a 32-bit integer is field 1, the shuffled number.
/// </summary>
/// <param name="i">The field index.</param>
/// <returns>A 32-bit integer - the value of the current number in the list of shuffled numbers.</returns>
public int GetInt32(int i)
{
if (i == 1)
return memView.ReadInt32(_offset);
throw new InvalidCastException("Invalid data type for column " + i.ToString());
}
/// <summary>
/// Returns the value as a 64-bit integer, for a given field. In this implementation
/// the only field that can be read as a 64-bit integer is field 0, the index position.
/// </summary>
/// <param name="i">The field index.</param>
/// <returns>A 64-bit integer - the index of the current number in the list of shuffled numbers.</returns>
public long GetInt64(int i)
{
if (i == 0)
return _index;
throw new InvalidCastException("Invalid data type for column " + i.ToString());
}
/// <summary>
/// Returns the name of a given field.
/// </summary>
/// <param name="i">The index of the field to get the name of</param>
/// <returns>A string, name of the field.</returns>
public string GetName(int i)
{
switch (i)
{
case 0:
return "CodeIndex";
case 1:
return "Code";
case 2:
return "CodeType";
case 3:
return "Assigned";
default:
throw new IndexOutOfRangeException();
}
}
/// <summary>
/// Returns the index for a given field.
/// </summary>
/// <param name="name">The name of the field to get the index for.</param>
/// <returns>The index (order) or the field.</returns>
public int GetOrdinal(string name)
{
switch (name)
{
case "CodeIndex":
return 0;
case "Code":
return 1;
case "CodeType":
return 2;
case "Assigned":
return 3;
default:
throw new IndexOutOfRangeException();
}
}
/// <summary>
/// Returns the value for a given field, for the current record.
/// </summary>
/// <param name="i">The index of the field to get the value for.</param>
/// <returns>The value of the field, for the current record.</returns>
public object GetValue(int i)
{
switch (i)
{
case 0:
return GetInt64(0);
case 1:
return GetInt32(1);
case 2:
return GetByte(2);
case 3:
return DBNull.Value;
default:
throw new IndexOutOfRangeException();
}
}
/// <summary>
/// Tests whether a field is null (does not have a value) for the current record.
/// </summary>
/// <param name="i">The index of the field to test.</param>
/// <returns>True if the field is null (does not have a value), false otherwise.</returns>
public bool IsDBNull(int i)
{
if (i == 3) return true;
return false;
}
/// <summary>
/// Convenience indexed accessor (for retrieving field values by field name)
/// </summary>
/// <param name="name">Name of the filed to retreive</param>
/// <returns>The value that corresponds to the given field.</returns>
public object this[string name]
{
get
{
switch (name)
{
case "CodeIndex":
return GetInt64(0);
case "Code":
return GetInt32(1);
case "CodeType":
return GetByte(2);
case "Assigned":
return DBNull.Value;
default:
throw new IndexOutOfRangeException();
}
}
}
/// <summary>
/// Convenience indexed accessor (for retrieving field values by field index)
/// </summary>
/// <param name="name">Index of the filed to retreive</param>
/// <returns>The value that corresponds to the given field.</returns>
public object this[int i]
{
get
{
switch (i)
{
case 0:
return GetInt64(0);
case 1:
return GetInt32(1);
case 2:
return GetByte(2);
case 3:
return DBNull.Value;
default:
throw new IndexOutOfRangeException();
}
}
}
#region Closing and Disposing related methods
/// <summary>
/// Read-only boolean property, returns True if the data reader is closed (disposed).
/// </summary>
public bool IsClosed
{
get;
private set;
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (memView != null)
{
memView.Dispose();
memView = null;
}
if (memMap != null)
{
memMap.Dispose();
memMap = null;
}
}
IsClosed = true;
}
/// <summary>
/// Disposes this instance
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Closes this instance of list mapper (ivokes Dispose)
/// </summary>
public void Close()
{
Dispose();
}
#endregion
#region Extra IDataReader methods that are not used/implemented
public float GetFloat(int i)
{
throw new InvalidCastException("Invalid data type for column " + i.ToString());
}
public Guid GetGuid(int i)
{
throw new InvalidCastException("Invalid data type for column " + i.ToString());
}
public short GetInt16(int i)
{
throw new InvalidCastException("Invalid data type for column " + i.ToString());
}
public int GetValues(object[] values)
{
throw new NotImplementedException();
}
public string GetString(int i)
{
throw new InvalidCastException("Invalid data type for column " + i.ToString());
}
public DateTime GetDateTime(int i)
{
throw new InvalidCastException("Invalid data type for column " + i.ToString());
}
public decimal GetDecimal(int i)
{
throw new InvalidCastException("Invalid data type for column " + i.ToString());
}
public double GetDouble(int i)
{
throw new InvalidCastException("Invalid data type for column " + i.ToString());
}
public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
{
throw new InvalidCastException("Invalid data type for column " + i.ToString());
}
public char GetChar(int i)
{
throw new InvalidCastException("Invalid data type for column " + i.ToString());
}
public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
{
throw new InvalidCastException("Invalid data type for column " + i.ToString());
}
public IDataReader GetData(int i)
{
throw new InvalidCastException("Invalid data type for column " + i.ToString());
}
#endregion
}
}