-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSchema.java
174 lines (156 loc) · 5.02 KB
/
Schema.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
import javafx.util.Pair;
import java.util.ArrayList;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.text.ParseException;
// Implementation Layer for Dynamic Schemas
//
public class Schema {
// Constructor
public Schema( String _collectionName ) {
collectionName = _collectionName;
}
private ArrayList<Pair<String,String>> keys = null; // dynamic schema
private String collectionName = null; // name of the collection
// Method for dynamically specifying the schema
// Pair =
// L = Field Name
// R = Field Type ( String, Integer, Long, Float, Double, Date, Time )
public void Schema( ArrayList<Pair<String,String>> _keys )
throws IllegalArgumentException
{
if ( null == _keys )
throw new IllegalArgumentException( "Schema: Keys is null" );
keys = _keys;
// Check that keys data types are valid
int len = keys.size();
for ( int i = 0; i < len; i++ ) {
// Look for duplicate keys in schema
for ( int j = i + 1; j < len; j++ ) {
if ( keys.get( i ).getKey().equals( keys.get( j ).getKey() ) ) {
throw new IllegalArgumentException( "Schema: key is defined more than once: " + keys.get( i ).getKey() );
}
}
switch ( keys.get( i ).getValue() ) {
case "string16" :
case "string32" :
case "string64" :
case "string128":
case "char" :
case "short" :
case "integer" :
case "long" :
case "float" :
case "double" :
case "date" :
case "time" : break;
default: throw new IllegalArgumentException( "Schema: key has invalid data type (-S): " + keys.get( i ).getValue() );
}
}
}
// Accessor function to get the key/type pairs from the schema
public ArrayList<Pair<String,String>> GetKeys() {
return keys;
}
// Method to check that keys arguments are valid
public void checkKeyArgs( String[] mykeys )
throws IllegalArgumentException
{
if ( null == mykeys )
throw new IllegalArgumentException( "Keys is null" );
if ( 0 == mykeys.length )
throw new IllegalArgumentException( "Keys is empty" );
if ( null == keys )
throw new IllegalArgumentException( "No schema" );
// Check key name is in the schema
for ( int i = 0; i < mykeys.length; i++ ) {
boolean matched = false;
for ( Pair<String,String> key : keys ) {
if ( mykeys[ i ].equals( key.getKey() ) ) {
matched = true;
break;
}
}
if ( false == matched )
throw new IllegalArgumentException( "Key not found: " + mykeys[ i ] );
}
}
// Method to check that keyVal arguments are valid
public void checkKeyValArgs( ArrayList<Pair<String,String>> keyVals )
throws IllegalArgumentException
{
if ( null == keyVals )
throw new IllegalArgumentException( "KeyVals is null" );
if ( null == keys )
throw new IllegalArgumentException( "No schema" );
// Check key name is in the schema
for ( Pair<String,String> keyVal : keyVals ) {
boolean matched = false;
for ( Pair<String,String> key : keys ) {
if ( keyVal.getKey().equals( key.getKey() ) ) {
matched = true;
break;
}
}
if ( false == matched )
throw new IllegalArgumentException( "Key not found: " + keyVal.getKey() );
}
}
// Method for converting a value of a key-value from String
// representation to internal datastore data type
protected Object Convert( Pair<String,String> keyVal )
throws NumberFormatException, ParseException
{
for ( Pair<String,String> key : keys ) {
// Look for field name in keyval in the schema (keys)
if ( key.getKey().equals( keyVal.getKey() ) ) {
// Cast the value in keyval according to the type
// in the matching schema
String value = keyVal.getValue();
switch ( key.getValue().toLowerCase() ) {
case "string16" :
case "string32" :
case "string64" :
case "string128": return value;
case "char" : return value.charAt( 0 );
case "short" : return Short.parseShort( value );
case "integer" : return Integer.parseInt( value );
case "long" : return Long.parseLong( value );
case "float" : return Float.parseFloat( value );
case "double" : return Double.parseDouble( value );
case "date" : DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.parse( value );
case "time" : format = new SimpleDateFormat("HH:ss");
return format.parse( value );
}
}
}
return null; // should never happen
}
// Get data type of a string
public String GetType( String col )
throws IllegalArgumentException
{
if ( null == keys )
throw new IllegalArgumentException( "No Schema" );
for ( Pair<String,String> key : keys ) {
// Look for key in the schema (keys)
if ( key.getKey().equals( col ) ) {
return key.getValue();
}
}
return null; // should never happen
}
// Get the column position in the schema
protected Integer GetColumnPosition( String col ) {
int ix = 0;
for ( Pair<String,String> key : keys ) {
// Look for key in the schema (keys)
if ( key.getKey().equals( col ) )
return ix;
ix++;
}
return -1; // not found
}
}