001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2008.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.sail.rdbms.schema;
007:
008: import static java.sql.Types.BIGINT;
009: import static java.sql.Types.DOUBLE;
010: import static java.sql.Types.LONGVARCHAR;
011: import static java.sql.Types.VARCHAR;
012:
013: import java.sql.Connection;
014: import java.sql.SQLException;
015: import java.util.concurrent.BlockingQueue;
016:
017: /**
018: * Factory class used to create or load the database tables.
019: *
020: * @author James Leigh
021: *
022: */
023: public class ValueTableFactory {
024: private static final int VCS = 127;
025: private static final int VCL = 255;
026: public static final boolean INDEX_VALUES = false;
027: protected static final String LANGUAGES = "LANGUAGES";
028: protected static final String NAMESPACES = "NAMESPACE_PREFIXES";
029: protected static final String RESOURCES = "RESOURCES";
030: protected static final String BNODE_VALUES = "BNODE_VALUES";
031: protected static final String URI_VALUES = "URI_VALUES";
032: protected static final String LURI_VALUES = "LONG_URI_VALUES";
033: protected static final String LBS = "LABEL_VALUES";
034: protected static final String LLBS = "LONG_LABEL_VALUES";
035: protected static final String LANGS = "LANGUAGE_VALUES";
036: protected static final String DTS = "DATATYPE_VALUES";
037: protected static final String NUM_VALUES = "NUMERIC_VALUES";
038: protected static final String TIMES = "DATETIME_VALUES";
039: protected static final String HASH_TABLE = "HASH_VALUES";
040: private TableFactory factory;
041: private IdSequence ids;
042: private boolean sequenced;
043:
044: public ValueTableFactory(TableFactory factory) {
045: super ();
046: this .factory = factory;
047: }
048:
049: public void setIdSequence(IdSequence ids) {
050: this .ids = ids;
051: }
052:
053: public void setSequenced(boolean sequenced) {
054: this .sequenced = sequenced;
055: }
056:
057: public HashTable createHashTable(Connection conn,
058: BlockingQueue<Batch> queue) throws SQLException {
059: ValueTable table = newValueTable();
060: table.setRdbmsTable(createTable(conn, HASH_TABLE));
061: //table.setTemporaryTable(factory.createTemporaryTable(conn, "INSERT_" + HASH_TABLE));
062: initValueTable(table, queue, BIGINT, -1, true);
063: HashTable hashTable = newHashtable(table);
064: hashTable.init();
065: return hashTable;
066: }
067:
068: public NamespacesTable createNamespacesTable(Connection conn) {
069: return new NamespacesTable(createTable(conn, NAMESPACES));
070: }
071:
072: public BNodeTable createBNodeTable(Connection conn,
073: BlockingQueue<Batch> queue) throws SQLException {
074: ValueTable table = createValueTable(conn, queue, BNODE_VALUES,
075: VARCHAR, VCS);
076: return new BNodeTable(table);
077: }
078:
079: public URITable createURITable(Connection conn,
080: BlockingQueue<Batch> queue) throws SQLException {
081: ValueTable shorter = createValueTable(conn, queue, URI_VALUES,
082: VARCHAR, VCL);
083: ValueTable longer = createValueTable(conn, queue, LURI_VALUES,
084: LONGVARCHAR);
085: return new URITable(shorter, longer);
086: }
087:
088: public LiteralTable createLiteralTable(Connection conn,
089: BlockingQueue<Batch> queue) throws SQLException {
090: ValueTable lbs = createValueTable(conn, queue, LBS, VARCHAR,
091: VCL);
092: ValueTable llbs = createValueTable(conn, queue, LLBS,
093: LONGVARCHAR);
094: ValueTable lgs = createValueTable(conn, queue, LANGS, VARCHAR,
095: VCS);
096: ValueTable dt = createValueTable(conn, queue, DTS, VARCHAR, VCL);
097: ValueTable num = createValueTable(conn, queue, NUM_VALUES,
098: DOUBLE);
099: ValueTable dateTime = createValueTable(conn, queue, TIMES,
100: BIGINT);
101: LiteralTable literals = new LiteralTable();
102: literals.setLabelTable(lbs);
103: literals.setLongLabelTable(llbs);
104: literals.setLanguageTable(lgs);
105: literals.setDatatypeTable(dt);
106: literals.setNumericTable(num);
107: literals.setDateTimeTable(dateTime);
108: return literals;
109: }
110:
111: public TripleTable createTripleTable(Connection conn,
112: String tableName) {
113: RdbmsTable table = createTable(conn, tableName);
114: return new TripleTable(table);
115: }
116:
117: protected RdbmsTable createTable(Connection conn, String name) {
118: return factory.createTable(conn, name);
119: }
120:
121: protected ValueTable createValueTable(Connection conn,
122: BlockingQueue<Batch> queue, String name, int sqlType)
123: throws SQLException {
124: return createValueTable(conn, queue, name, sqlType, -1);
125: }
126:
127: protected ValueTable createValueTable(Connection conn,
128: BlockingQueue<Batch> queue, String name, int sqlType,
129: int length) throws SQLException {
130: ValueTable table = newValueTable();
131: table.setRdbmsTable(createTable(conn, name));
132: if (!sequenced) {
133: table.setTemporaryTable(factory.createTemporaryTable(conn,
134: "INSERT_" + name));
135: }
136: initValueTable(table, queue, sqlType, length, INDEX_VALUES);
137: return table;
138: }
139:
140: protected HashTable newHashtable(ValueTable table) {
141: return new HashTable(table);
142: }
143:
144: protected ValueTable newValueTable() {
145: return new ValueTable();
146: }
147:
148: private void initValueTable(ValueTable table,
149: BlockingQueue<Batch> queue, int sqlType, int length,
150: boolean indexValues) throws SQLException {
151: table.setQueue(queue);
152: table.setSqlType(sqlType);
153: table.setIdType(ids.getJdbcIdType());
154: table.setLength(length);
155: table.setIndexingValues(indexValues);
156: table.initialize();
157: }
158: }
|