1   package eu.fbk.knowledgestore.datastore.hbase.utils;
2   
3   import com.google.common.collect.ImmutableList;
4   
5   import org.apache.avro.Schema;
6   import org.apache.avro.Schema.Field;
7   import org.apache.avro.Schema.Type;
8   
9   /**
10   * Definition of AVRO schemas for nodes of the data model.
11   */
12  public final class AvroSchemas {
13  
14      /** The namespace for KS-specific AVRO schemas. */
15      public static final String NAMESPACE = "eu.fbk.knowledgestore";
16  
17      /** AVRO schema for NULL. */
18      public static final Schema NULL = Schema.create(Type.NULL);
19  
20      /** AVRO schema for boolean literals. */
21      public static final Schema BOOLEAN = Schema.create(Type.BOOLEAN);
22  
23      /** AVRO schema for string literals. */
24      public static final Schema STRING = Schema.create(Type.STRING);
25  
26      /** AVRO schema for string literals with a language. */
27      public static final Schema STRING_LANG = Schema.createRecord("stringlang", null,
28              AvroSchemas.NAMESPACE, false);
29  
30      /** AVRO schema for long literals. */
31      public static final Schema LONG = Schema.create(Type.LONG);
32  
33      /** AVRO schema for int literals. */
34      public static final Schema INT = Schema.create(Type.INT);
35  
36      /** AVRO schema for short literals. */
37      public static final Schema SHORT = Schema.createRecord("short", null, AvroSchemas.NAMESPACE,
38              false);
39  
40      /** AVRO schema for byte literals. */
41      public static final Schema BYTE = Schema.createRecord("byte", null, AvroSchemas.NAMESPACE,
42              false);
43  
44      /** AVRO schema for double literals. */
45      public static final Schema DOUBLE = Schema.create(Type.DOUBLE);
46  
47      /** AVRO schema for float literals. */
48      public static final Schema FLOAT = Schema.create(Type.FLOAT);
49  
50      /** AVRO schema for big integer literals. */
51      public static final Schema BIGINTEGER = Schema.createRecord("biginteger", null,
52              AvroSchemas.NAMESPACE, false);
53  
54      /** AVRO schema for big decimal literals. */
55      public static final Schema BIGDECIMAL = Schema.createRecord("bigdecimal", null,
56              AvroSchemas.NAMESPACE, false);
57  
58      /** AVRO schema for non-compressed IDs (URIs, BNodes). */
59      public static final Schema PLAIN_IDENTIFIER = Schema //
60              .createRecord("plainidentifier", null, AvroSchemas.NAMESPACE, false);
61  
62      /** AVRO schema for compressed ID (URIs, BNodes). */
63      public static final Schema COMPRESSED_IDENTIFIER = Schema //
64              .createRecord("compressedidentifier", null, AvroSchemas.NAMESPACE, false);
65  
66      /** AVRO schema for any ID (URIs, BNodes). */
67      public static final Schema IDENTIFIER = Schema.createUnion(ImmutableList.<Schema>of(
68              PLAIN_IDENTIFIER, COMPRESSED_IDENTIFIER));
69  
70      /** AVRO schema for calendar literals. */
71      public static final Schema CALENDAR = Schema.createRecord("calendar", null,
72              AvroSchemas.NAMESPACE, false);
73  
74      /** AVRO schema for RDF statements. */
75      public static final Schema STATEMENT = Schema.createRecord("statement", null,
76              AvroSchemas.NAMESPACE, false);
77  
78      /** AVRO schema for record nodes ({@code Record}). */
79      public static final Schema RECORD = Schema.createRecord("struct", null, AvroSchemas.NAMESPACE,
80              false);
81  
82      /** AVRO schema for generic data model nodes. */
83      public static final Schema NODE = Schema.createUnion(ImmutableList.<Schema>of(
84              AvroSchemas.BOOLEAN, AvroSchemas.STRING, AvroSchemas.STRING_LANG, AvroSchemas.LONG,
85              AvroSchemas.INT, AvroSchemas.SHORT, AvroSchemas.BYTE, AvroSchemas.DOUBLE,
86              AvroSchemas.FLOAT, AvroSchemas.BIGINTEGER, AvroSchemas.BIGDECIMAL,
87              AvroSchemas.PLAIN_IDENTIFIER, AvroSchemas.COMPRESSED_IDENTIFIER, AvroSchemas.CALENDAR,
88              AvroSchemas.STATEMENT, AvroSchemas.RECORD));
89  
90      /** AVRO schema for lists of nodes. */
91      public static final Schema LIST = Schema.createArray(AvroSchemas.NODE);
92  
93      /** AVRO schema for properties of a record node. */
94      public static final Schema PROPERTY = Schema.createRecord("property", null,
95              AvroSchemas.NAMESPACE, false);
96  
97      private AvroSchemas() {
98      }
99  
100     static {
101         AvroSchemas.STRING_LANG.setFields(ImmutableList.<Field>of(new Field("label",
102                 AvroSchemas.STRING, null, null), new Field("language", AvroSchemas.STRING, null,
103                 null)));
104         AvroSchemas.SHORT.setFields(ImmutableList.<Field>of(new Field("short", AvroSchemas.INT,
105                 null, null)));
106         AvroSchemas.BYTE.setFields(ImmutableList.<Field>of(new Field("byte", AvroSchemas.INT,
107                 null, null)));
108         AvroSchemas.BIGINTEGER.setFields(ImmutableList.<Field>of(new Field("biginteger",
109                 AvroSchemas.STRING, null, null)));
110         AvroSchemas.BIGDECIMAL.setFields(ImmutableList.<Field>of(new Field("bigdecimal",
111                 AvroSchemas.STRING, null, null)));
112         AvroSchemas.PLAIN_IDENTIFIER.setFields(ImmutableList.<Field>of(new Field("identifier",
113                 AvroSchemas.STRING, null, null)));
114         AvroSchemas.COMPRESSED_IDENTIFIER.setFields(ImmutableList.<Field>of(new Field(
115                 "identifier", AvroSchemas.INT, null, null)));
116         AvroSchemas.CALENDAR.setFields(ImmutableList
117                 .<Field>of(new Field("timezone", AvroSchemas.INT, null, null), new Field(
118                         "timestamp", AvroSchemas.LONG, null, null)));
119 
120         AvroSchemas.STATEMENT.setFields(ImmutableList.<Field>of(
121                 new Field("subject", AvroSchemas.IDENTIFIER, null, null),
122                 new Field("predicate", AvroSchemas.IDENTIFIER, null, null),
123                 new Field("object", Schema.createUnion(ImmutableList.<Schema>of(
124                         AvroSchemas.BOOLEAN, AvroSchemas.STRING, AvroSchemas.STRING_LANG,
125                         AvroSchemas.LONG, AvroSchemas.INT, AvroSchemas.SHORT, AvroSchemas.BYTE,
126                         AvroSchemas.DOUBLE, AvroSchemas.FLOAT, AvroSchemas.BIGINTEGER,
127                         AvroSchemas.BIGDECIMAL, AvroSchemas.CALENDAR,
128                         AvroSchemas.PLAIN_IDENTIFIER, AvroSchemas.COMPRESSED_IDENTIFIER)), null,
129                         null), //
130                 new Field("context", AvroSchemas.IDENTIFIER, null, null)));
131 
132         AvroSchemas.PROPERTY
133                 .setFields(ImmutableList.<Field>of(
134                         new Field("propertyURI", AvroSchemas.COMPRESSED_IDENTIFIER, null, null),
135                         new Field("propertyValue", Schema.createUnion(ImmutableList.<Schema>of(
136                                 AvroSchemas.BOOLEAN, AvroSchemas.STRING, AvroSchemas.STRING_LANG,
137                                 AvroSchemas.LONG, AvroSchemas.INT, AvroSchemas.SHORT,
138                                 AvroSchemas.BYTE, AvroSchemas.DOUBLE, AvroSchemas.FLOAT,
139                                 AvroSchemas.BIGINTEGER, AvroSchemas.BIGDECIMAL,
140                                 AvroSchemas.CALENDAR, AvroSchemas.PLAIN_IDENTIFIER,
141                                 AvroSchemas.COMPRESSED_IDENTIFIER, AvroSchemas.STATEMENT,
142                                 AvroSchemas.RECORD, AvroSchemas.LIST)), null, null)));
143 
144         AvroSchemas.RECORD.setFields(ImmutableList.<Field>of(
145                 new Field("id", Schema.createUnion(ImmutableList.<Schema>of(AvroSchemas.NULL,
146                         AvroSchemas.PLAIN_IDENTIFIER, AvroSchemas.COMPRESSED_IDENTIFIER)), null,
147                         null), //
148                 new Field("properties", Schema.createArray(AvroSchemas.PROPERTY), null, null)));
149     }
150 
151 }