-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBinaryStore.java
283 lines (241 loc) · 8.06 KB
/
BinaryStore.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
import javafx.util.Pair;
import java.util.ArrayList;
import java.text.ParseException;
import java.util.Date;
// Implementation of DataStore as a Binary Record File
//
public class BinaryStore extends DataStore {
// Extended Constructor
public BinaryStore( String collection ) {
super( collection );
}
// Implementation of Insert() method
public void Insert( ArrayList<Pair<String,String>> keyVals )
throws IllegalArgumentException, StorageException
{
schema.checkKeyValArgs( keyVals );
// Seek to the end of the Storage
End();
// Index the entry for primary key(s)
long found = Index( keyVals );
if ( -1 != found ) {
// move to location in storage of found entry
Move( found );
// remove it (mark as dirty)
Write( (byte) 0x00 );
// Goto end of storage to add updated entry
End();
}
// Set dirty flag to clean
Write( (byte) 0x01 );
// find the column information (type) in schema
ArrayList<Pair<String,String>> keys = schema.GetKeys();
// Write each key value according to type
for ( Pair<String,String> keyVal : keyVals ) {
String key = keyVal.getKey();
// find the column in the schema
for ( Pair<String,String> col : keys ) {
if ( key.equals( col.getKey() ) ) {
// Convert string value into data type
Object value = null;
try {
value = schema.Convert( keyVal );
}
catch ( ParseException e ) {
throw new StorageException( e.getMessage() );
}
switch ( col.getValue() ) {
case "string16" : Write( (String) value, 16 ); break;
case "string32" : Write( (String) value, 32 ); break;
case "string64" : Write( (String) value, 64 ); break;
case "string128": Write( (String) value, 128 ); break;
case "short" : Write( (Short) value ); break;
case "char" : // Special case, write only lowest 8-bits (octet)
char v = (char) value; Write( (byte) v );
break;
case "integer" : Write( (Integer) value ); break;
case "long" : Write( (Long) value ); break;
case "float" : Write( (Float) value ); break;
case "double" : Write( (Double) value ); break;
case "date" : Write( ((Date)value).getTime() ); break;
case "time" : Write( ((Date)value).getTime() ); break;
}
break;
}
}
}
}
// Implementation of Update() method
public void Update( ArrayList<Pair<String,String>> keyVals, Where where )
throws UnsupportedOperationException, IllegalArgumentException
{
schema.checkKeyValArgs( keyVals );
throw new UnsupportedOperationException();
}
// Implementation of Select() method
public ArrayList<Data[]> Select( String[] cols, ArrayList<Where> whereList )
throws IllegalArgumentException, StorageException
{
int colOrder[];
Data[] row;
// Special case, match all columns
if ( 1 == cols.length && cols[ 0 ].equals( "*" ) )
{
ArrayList<Pair<String,String>> keys = schema.GetKeys();
// No Schema
if ( null == keys ) {
throw new IllegalArgumentException( "No Schema" );
}
// allocate array to hold column order
colOrder = new int[ keys.size() ];
// Allocate first row to hold column names
row = new Data[ colOrder.length ];
int i = 0;
for ( Pair<String,String> key : keys ) {
// Set column order
colOrder[ i ] = i;
// Set the column name in first row
row[ i ] = new DataString32(); row[ i++ ].Set( key.getKey() );
}
}
else {
schema.checkKeyArgs( cols );
// allocate array to hold column order
colOrder = new int[ cols.length ];
// Allocate first row to hold column names
row = new Data[ colOrder.length ];
// find the column order from the schema
for ( int i = 0; i < cols.length; i++ ) {
// Set column order
colOrder[ i ] = schema.GetColumnPosition ( cols[ i ] );
// Set the column name in first row
row[ i ] = new DataString32(); row[ i ].Set( cols[ i ] );
}
}
// Allocate space for returning result
ArrayList<Data[]> result = new ArrayList<Data[]>();
// Add first row of column names
result.add( row );
// Get the key/type schema definition
ArrayList<Pair<String,String>> keys = schema.GetKeys();
// Go to the beginning of the storage
Begin();
// Read each record in storage
//
int nth = 0;
while ( !Eof() ) {
row = new Data[ colOrder.length ];
int ncol = 0; // current column position
// First byte indicates if entry is dirty( is dirty == 0x0 )
byte dirty = ReadByte();
nth++; // increment the next sequential position of a record (row/document)
// entry is marked as dirty
if ( 0x0 == dirty ) {
// skip to the position of the next entry
Move( index.Pos( nth ) );
continue;
}
// Mismatch in data store type
else if ( 0x01 != dirty )
throw new StorageException( "Bad entry in storage or not a binary store" );
// Read each column according to data type
boolean skip = false;
for ( Pair<String,String> key : keys ) {
Data value = null;
switch ( key.getValue() )
{
case "string16" : value = new DataString16(); value.Set( ( ( String ) Read( 16 ) ).trim() ); break;
case "string32" : value = new DataString32(); value.Set( ( ( String ) Read( 32 ) ).trim() ); break;
case "string64" : value = new DataString64(); value.Set( ( ( String ) Read( 64 ) ).trim() ); break;
case "string128": value = new DataString128(); value.Set( ( ( String ) Read( 128 ) ).trim() ); break;
case "char" : value = new DataChar(); value.Set( ( char ) ReadByte() ); break;
case "short" : value = new DataShort(); value.Set( ReadShort() ); break;
case "integer" : value = new DataInteger(); value.Set( ReadInt() ); break;
case "long" : value = new DataLong(); value.Set( ReadLong() ); break;
case "float" : value = new DataFloat(); value.Set( ReadFloat() ); break;
case "double" : value = new DataDouble(); value.Set( ReadDouble() ); break;
case "date" : value = new DataDate(); value.Set( ReadLong() ); break;
case "time" : value = new DataTime(); value.Set( ReadLong() ); break;
}
// Check where
if ( null != whereList ) {
// Check each where condition (AND)
for ( Where where : whereList ) {
// matched key
if ( key.getKey().equals( where.key ) ) {
switch ( where.op ) {
case EQ:
if ( !value.EQ( where.value ) ) {
skip = true; // value not matched
break;
}
break;
case NE:
if ( !value.NE( where.value ) ) {
skip = true; // value not matched
break;
}
break;
case LT:
if ( !value.LT( where.value ) ) {
skip = true; // value not matched
break;
}
break;
case GT:
if ( !value.GT( where.value ) ) {
skip = true; // value not matched
break;
}
break;
case LE:
if ( !value.LE( where.value ) ) {
skip = true; // value not matched
break;
}
break;
case GE:
if ( !value.GE( where.value ) ) {
skip = true; // value not matched
break;
}
break;
}
}
// none match already found
if ( true == skip )
break;
}
// TODO: should jump to next row on skip (unmatched where), but needs an index always
}
// add to row result in correct order if part of result
for ( int i = 0; i < colOrder.length; i++ ) {
if ( colOrder[ i ] == ncol ) {
row[ i ] = value;
break;
}
}
ncol++; // increment the column position
}
// did not match where clause
if ( true == skip ) {
continue;
}
// Add the row to the result
result.add( row );
}
return result;
}
// Implementation of Delete() method
public void Delete( Where where )
throws UnsupportedOperationException
{
throw new UnsupportedOperationException();
}
// Implementation of Vacuum() method
public void Vacuum()
throws UnsupportedOperationException
{
throw new UnsupportedOperationException();
}
}