Skip to content

Commit cabe22f

Browse files
committed
initial commit, including transforms, validation & compaction
0 parents  commit cabe22f

24 files changed

+1906
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*.DS_Store*
2+
bin
3+
target
4+
.classpath
5+
.project
6+
.settings
7+
test-output

pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>org.sunnycode.schema</groupId>
4+
<artifactId>schema-java</artifactId>
5+
<version>0.0.0-SNAPSHOT</version>
6+
7+
<scm>
8+
<connection>scm:git:[email protected]:sunnycode/schema-java.git</connection>
9+
<developerConnection>scm:git:[email protected]:sunnycode/schema-java.git</developerConnection>
10+
</scm>
11+
12+
<dependencies>
13+
<dependency>
14+
<groupId>com.google.guava</groupId>
15+
<artifactId>guava</artifactId>
16+
<version>13.0.1</version>
17+
</dependency>
18+
<dependency>
19+
<groupId>com.fasterxml.jackson.core</groupId>
20+
<artifactId>jackson-core</artifactId>
21+
<version>2.0.6</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>com.fasterxml.jackson.core</groupId>
25+
<artifactId>jackson-databind</artifactId>
26+
<version>2.0.6</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.fasterxml.jackson.dataformat</groupId>
30+
<artifactId>jackson-dataformat-smile</artifactId>
31+
<version>2.0.6</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>joda-time</groupId>
35+
<artifactId>joda-time</artifactId>
36+
<version>1.6.2</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.testng</groupId>
40+
<artifactId>testng</artifactId>
41+
<version>5.11</version>
42+
<classifier>jdk15</classifier>
43+
<scope>test</scope>
44+
</dependency>
45+
</dependencies>
46+
47+
<build>
48+
<plugins>
49+
<plugin>
50+
<artifactId>maven-compiler-plugin</artifactId>
51+
<configuration>
52+
<source>1.6</source>
53+
<target>1.6</target>
54+
</configuration>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
59+
</project>
60+
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.sunnycode.schema;
19+
20+
import java.util.ArrayList;
21+
import java.util.Arrays;
22+
import java.util.Collections;
23+
import java.util.List;
24+
25+
import org.sunnycode.schema.Schema.Builder;
26+
27+
import com.fasterxml.jackson.annotation.JsonCreator;
28+
import com.fasterxml.jackson.annotation.JsonProperty;
29+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
30+
31+
/**
32+
* Attribute class for properties of a user-defined Object.
33+
*/
34+
public class Attribute {
35+
public static Schema ATTRIBUTE_SCHEMA;
36+
static {
37+
Builder schema = new Builder();
38+
39+
schema.addAttribute("name", Type.UTF8_SMALLSTRING, false);
40+
schema.addAttribute("type", Type.ENUM,
41+
Arrays.asList((Object[]) Type.values()), false);
42+
schema.addAttribute("nullable", Type.BOOLEAN, true);
43+
schema.addAttribute("values", Type.ARRAY, true);
44+
45+
ATTRIBUTE_SCHEMA = schema.build();
46+
}
47+
48+
public enum Type {
49+
// structured types
50+
ANY, MAP, ARRAY,
51+
// compact types
52+
BOOLEAN, CHAR_ONE,
53+
// enumeration types
54+
ENUM,
55+
// unsigned integer types
56+
U8, U16, U32, U64,
57+
// signed integer types
58+
I8, I16, I32, I64,
59+
// date types
60+
UTC_DATE_SECS,
61+
// string types
62+
UTF8_SMALLSTRING, UTF8_TEXT;
63+
}
64+
65+
private final String name;
66+
private final Type type;
67+
private final List<String> values;
68+
private final boolean nullable;
69+
70+
@JsonCreator
71+
public Attribute(@JsonProperty("name") String name,
72+
@JsonProperty("type") Type type,
73+
@JsonProperty("values") List<Object> values,
74+
@JsonProperty("nullable") Boolean nullable) {
75+
if (name == null) {
76+
throw new IllegalArgumentException(
77+
"Attribute 'name' must not be null");
78+
}
79+
80+
if (type == null) {
81+
throw new IllegalArgumentException(
82+
"Attribute 'type' must be specified");
83+
}
84+
85+
this.name = name;
86+
this.type = type;
87+
this.nullable = (nullable == null) || nullable;
88+
89+
if (values != null) {
90+
List<String> newVals = new ArrayList<String>();
91+
for (Object object : values) {
92+
newVals.add(object.toString());
93+
}
94+
95+
this.values = Collections.unmodifiableList(newVals);
96+
} else {
97+
this.values = null;
98+
}
99+
}
100+
101+
public String getName() {
102+
return name;
103+
}
104+
105+
public Type getType() {
106+
return type;
107+
}
108+
109+
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
110+
public List<String> getValues() {
111+
return values;
112+
}
113+
114+
public boolean isNullable() {
115+
return nullable;
116+
}
117+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.sunnycode.schema;
19+
20+
import java.util.ArrayList;
21+
import java.util.Collections;
22+
import java.util.LinkedHashMap;
23+
import java.util.List;
24+
import java.util.Map;
25+
26+
import org.sunnycode.schema.Attribute.Type;
27+
28+
import com.fasterxml.jackson.annotation.JsonCreator;
29+
import com.fasterxml.jackson.annotation.JsonIgnore;
30+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
31+
import com.fasterxml.jackson.annotation.JsonProperty;
32+
33+
/**
34+
* Schema definition class - the main entry point for schema creation.
35+
*/
36+
@JsonIgnoreProperties(ignoreUnknown = true)
37+
public class Schema {
38+
public static Schema SCHEMA_SCHEMA = (new Builder()).addAttribute(
39+
"attributes", Type.ARRAY, false).build();
40+
41+
private final List<Attribute> attributes;
42+
private final Map<String, Attribute> attributeMap;
43+
44+
@JsonCreator
45+
public Schema(@JsonProperty("attributes") List<Attribute> attributes) {
46+
if (attributes == null) {
47+
throw new IllegalArgumentException("'attributes' must be present");
48+
}
49+
50+
this.attributes = Collections.unmodifiableList(attributes);
51+
52+
Map<String, Attribute> newAttributes = new LinkedHashMap<String, Attribute>();
53+
for (Attribute attr : attributes) {
54+
newAttributes.put(attr.getName(), attr);
55+
}
56+
57+
this.attributeMap = Collections.unmodifiableMap(newAttributes);
58+
}
59+
60+
public List<Attribute> getAttributes() {
61+
return attributes;
62+
}
63+
64+
@JsonIgnore
65+
public Attribute getAttribute(String name) {
66+
return attributeMap.get(name);
67+
}
68+
69+
public static class Builder {
70+
private List<Attribute> attributes = new ArrayList<Attribute>();
71+
72+
public Builder addAttribute(String name, Type type, boolean nullable) {
73+
this.attributes.add(new Attribute(name, type, null, nullable));
74+
75+
return this;
76+
}
77+
78+
public Builder addAttribute(String name, Type type,
79+
List<Object> values, boolean nullable) {
80+
this.attributes.add(new Attribute(name, type, values, nullable));
81+
82+
return this;
83+
}
84+
85+
public Schema build() {
86+
return new Schema(attributes);
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)