-
Notifications
You must be signed in to change notification settings - Fork 1
/
SingleFileStorage.java
598 lines (536 loc) · 12.9 KB
/
SingleFileStorage.java
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
import java.io.RandomAccessFile;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.ArrayList;
import javafx.util.Pair;
import java.io.File;
import java.io.UnsupportedEncodingException;
// Implementation for Accessing Physical Storage of Data as Single File
//
public class SingleFileStorage extends Storage {
// Constructor: initialize name of collection in storage
SingleFileStorage( String collection )
{
super( collection );
}
private RandomAccessFile fd = null; // file pointer for data storage file
// Implementation for opening (connecting) to storage
public void Open()
throws StorageException
{
String data = storageRoot + collection + ".dat";
try
{
fd = new RandomAccessFile( data, "rw" );
}
catch ( FileNotFoundException e ) {
throw new StorageException( "Storage File not Found: " + data );
}
}
// Implementation for closing (disconnecting) from storage
public void Close()
throws StorageException
{
try {
fd.close();
}
catch ( IOException e )
{
throw new StorageException( "Cannot close storage file" );
}
}
// Implementation for seeking to the begin of the storage
public void Begin()
throws StorageException
{
try {
fd.seek( 0 );
}
catch ( IOException e )
{
throw new StorageException( "Cannot seek in storage file" );
}
}
// Implementation for seeking to the end of the storage
public void End()
throws StorageException
{
try {
fd.seek( fd.length() );
}
catch ( IOException e )
{
throw new StorageException( "Cannot seek in storage file" );
}
}
// Implementation for returning current location in storage
public long Pos()
throws StorageException
{
try {
return fd.getFilePointer();
}
catch ( IOException e ) {
throw new StorageException( "Cannot get file location in storage file" );
}
}
// Implementation for moving to a location in storage
public void Move( long pos )
throws StorageException
{
try {
fd.seek( pos );
}
catch ( IOException e )
{
throw new StorageException( "Cannot seek in storage file" );
}
}
// Implementation for checking if at the end of the file in storage
public boolean Eof()
throws StorageException
{
try {
if ( fd.getFilePointer() >= fd.length() - 1 )
return true;
}
catch ( IOException e )
{
throw new StorageException( "Cannot get location in storage file" );
}
return false;
}
// Implementation for writing a fixed length string to storage
public void Write( String value, int length )
throws StorageException
{
try {
byte[] bytes = value.getBytes();
byte[] out = Arrays.copyOf( bytes, length );
fd.write( out, 0, length );
}
catch ( IOException e )
{
throw new StorageException( "Cannot write to storage file" );
}
}
// Implementation for writing a string to storage
public void Write( String value )
throws StorageException
{
try {
byte[] bytes = value.getBytes();
fd.write( bytes, 0, bytes.length );
}
catch ( IOException e )
{
throw new StorageException( "Cannot write to storage file" );
}
}
// Implementation for writing a byte to storage
public void Write( byte value )
throws StorageException
{
try {
fd.write( value );
}
catch ( IOException e )
{
throw new StorageException( "Cannot write to storage file" );
}
}
// Implementation for writing a short to storage
public void Write( Short value )
throws StorageException
{
try {
fd.writeShort( value );
}
catch ( IOException e )
{
throw new StorageException( "Cannot write to storage file" );
}
}
// Implementation for writing an integer to storage
public void Write( Integer value )
throws StorageException
{
try {
fd.writeInt( value );
}
catch ( IOException e )
{
throw new StorageException( "Cannot write to storage file" );
}
}
// Implementation for writing a long integer to storage
public void Write( Long value )
throws StorageException
{
try {
fd.writeLong( value );
}
catch ( IOException e )
{
throw new StorageException( "Cannot write to storage file" );
}
}
// Implementation for writing a float to storage
public void Write( Float value )
throws StorageException
{
try {
fd.writeFloat( value );
}
catch ( IOException e )
{
throw new StorageException( "Cannot write to storage file" );
}
}
// Implementation for writing a double to storage
public void Write( Double value )
throws StorageException
{
try {
fd.writeDouble( value );
}
catch ( IOException e )
{
throw new StorageException( "Cannot write to storage file" );
}
}
// Implementation for writing a boolean to storage
public void Write( Boolean value )
throws StorageException
{
try {
fd.writeBoolean( value );
}
catch ( IOException e )
{
throw new StorageException( "Cannot write to storage file" );
}
}
// Implementation for writing a line to storage
public void WriteLine( String value )
throws StorageException
{
Write( value + "\r\n" );
}
// Implementation for reading a string from storage
public String Read( int length )
throws StorageException
{
byte[] bytes = null;
try {
bytes = new byte[ length ];
fd.read( bytes );
}
catch ( IOException e ) {
throw new StorageException( "Cannot read from storage file" );
}
try
{
return new String( bytes, "UTF-8" );
}
catch ( UnsupportedEncodingException e ) {
return null;
}
}
// Implementation for reading a byte from storage
public byte ReadByte()
throws StorageException
{
try {
return fd.readByte();
}
catch ( IOException e ) {
throw new StorageException( "Cannot read from storage file" );
}
}
// Implementation for reading a Short from storage
public Short ReadShort()
throws StorageException
{
try {
return fd.readShort();
}
catch ( IOException e ) {
throw new StorageException( "Cannot read from storage file" );
}
}
// Implementation for reading an integer from storage
public Integer ReadInt()
throws StorageException
{
try {
return fd.readInt();
}
catch ( IOException e ) {
throw new StorageException( "Cannot read from storage file" );
}
}
// Implementation for reading a long from storage
public Long ReadLong()
throws StorageException
{
try {
return fd.readLong();
}
catch ( IOException e ) {
throw new StorageException( "Cannot read from storage file" );
}
}
// Implementation for reading a float from storage
public float ReadFloat()
throws StorageException
{
try {
return fd.readFloat();
}
catch ( IOException e ) {
throw new StorageException( "Cannot read from storage file" );
}
}
// Implementation for reading a double from storage
public double ReadDouble()
throws StorageException
{
try {
return fd.readDouble();
}
catch ( IOException e ) {
throw new StorageException( "Cannot read from storage file" );
}
}
// Implementation for reading a boolean from storage
public Boolean ReadBoolean()
throws StorageException
{
try {
return fd.readBoolean();
}
catch ( IOException e ) {
throw new StorageException( "Cannot read from storage file" );
}
}
// Implementation for reading a line from storage
public String ReadLine()
throws StorageException
{
try {
return fd.readLine();
}
catch ( IOException e ) {
throw new StorageException( "Cannot read from storage file" );
}
}
private RandomAccessFile ix = null; // file pointer for index storage file
// Implementation for writing out index to storage
public void Write( Index index )
throws StorageException
{
// construct path of index for this collection
String idx = storageRoot + collection + ".idx";
// Open the index file
try
{
ix = new RandomAccessFile( idx, "rw" );
}
catch ( FileNotFoundException e ) {
throw new StorageException( "Index File not Found: " + idx );
}
// Get the primary key(s)
String[] primary = index.Keys();
try
{
// No primary keys
if ( null == primary )
ix.writeShort( 0 );
else
{
// Calculate the length of the primary key string
int len = 0;
for ( String key : primary )
len += key.length() + 1;
// File starts with length of primary key string
ix.writeShort( len );
// Write the primary key list to the index
for ( String key : primary ) {
byte[] bytes = ( key + "," ).getBytes();
ix.write( bytes, 0, bytes.length );
}
}
}
catch ( IOException e )
{
throw new StorageException( "Cannot write to index file" );
}
// Write out the index
ArrayList<long[]> entries = (ArrayList<long[]>)index.Index();
for ( long[] entry : entries ) {
try
{
ix.writeLong( entry[ 0 ] );
ix.writeLong( entry[ 1 ] );
}
catch ( IOException e ) {
throw new StorageException( "Cannot write to index file" );
}
}
// Close the index file
try {
ix.close();
}
catch ( IOException e )
{
throw new StorageException( "Cannot close index file" );
}
}
// Implementation for reading in index from storage
public ArrayList<long[]> ReadIndex( Index index )
throws StorageException
{
String idx = storageRoot + collection + ".idx";
// check if index exists yet
File f = new File(idx);
if( false == f.exists() )
return null;
try
{
ix = new RandomAccessFile( idx, "r" );
}
catch ( FileNotFoundException e ) {
throw new StorageException( "Cannot open index file: " + idx );
}
// Get the file length
long filesize = 0;
try
{
filesize = ix.length();
}
catch ( IOException e ) {
throw new StorageException( "Cannot read from index file" );
}
// Integrity check
if ( filesize < 2 ) {
throw new StorageException( "Index file too small" );
}
filesize -= 2; // subtract from size the length of primary key(s) field
try
{
// Read in the primary key(s)
int len = ix.readShort();
if ( filesize < len )
throw new StorageException( "Index file too small" );
if ( len > 0 ) {
byte[] bytes = new byte[ len ];
ix.read( bytes );
String sKeys = new String( bytes, "UTF-8" );
filesize -= len; // subtract from size the length of the primary keys
// Load the keys
index.Keys( sKeys.substring( 0, sKeys.length() - 1 ).split( "," ) );
}
}
catch ( IOException e ) {
throw new StorageException( "Cannot read from index file" );
}
ArrayList<long[]> entries = new ArrayList<long[]>();
// read 16 bytes at a time
for ( long i = 0; i < filesize; i += 16 ) {
try {
long[] entry = { ix.readLong(), ix.readLong() };
entries.add( entry );
}
catch ( IOException e ) {
throw new StorageException( "Cannot read from index file" );
}
}
try {
ix.close();
}
catch ( IOException e ) {
throw new StorageException( "Cannot close index file" );
}
return entries;
}
private RandomAccessFile sc = null; // file pointer for schema storage file
// Implementation to Write out schema to storage
public void Write( Schema schema )
throws StorageException
{
// Construct path of Schema for this collection
String sch = storageRoot + collection + ".sch";
// Open the schema file
try
{
sc = new RandomAccessFile( sch, "rw" );
}
catch ( FileNotFoundException e ) {
throw new StorageException( "Schema File not Found: " + sch );
}
// Write schema in CSV format
ArrayList<Pair<String,String>> keys = schema.GetKeys();
if ( keys != null ) {
for ( Pair<String,String> key : keys ) {
try {
String value = key.getKey() + "," + key.getValue() + "\r\n";
byte[] bytes = value.getBytes();
sc.write( bytes, 0, bytes.length );
}
catch ( IOException e ) {
throw new StorageException( "Cannot write to schema file" );
}
}
}
// Close the schema file
try {
sc.close();
}
catch ( IOException e )
{
throw new StorageException( "Cannot close schema file" );
}
}
// Implementation to Read in schema from storage
public ArrayList<Pair<String,String>> ReadSchema()
throws StorageException
{
String sch = storageRoot + collection + ".sch";
// check if schema exists yet
File f = new File( sch );
if( false == f.exists() )
return null;
try
{
sc = new RandomAccessFile( sch, "r" );
}
catch ( FileNotFoundException e ) {
throw new StorageException( "Cannot open schema file: " + sch );
}
// Allocate list for key/type pairs
ArrayList<Pair<String,String>> keys = new ArrayList<Pair<String,String>>();
// Read in the pairs of key/type
try
{
String line;
while ( ( line = sc.readLine() ) != null ) {
String[] pair = line.split( "," );
keys.add( new Pair( pair[ 0 ], pair[ 1 ] ) );
}
}
catch ( IOException e ) {
throw new StorageException( "Cannot read from schema file: " + sch );
}
// Close the schema file
try {
sc.close();
}
catch ( IOException e )
{
throw new StorageException( "Cannot close schema file" );
}
return keys;
}
}