@Override public boolean hasNext() { return documentDataIterator != null && documentDataIterator.hasNext(); } }
@Override public void close() { if (dbc != null) dbc.close(); dbc = null; }
private void checkTableDefinition(String annotationTableName, String schemaAnnotation) throws ResourceInitializationException { try { dbc.checkTableDefinition(annotationTableName, schemaAnnotation); } catch (TableSchemaMismatchException e) { throw new ResourceInitializationException(e); } }
private void createNamespaceTable(DataBaseConnector dbc) { if (!dbc.tableExists(dbc.getActiveDataPGSchema() + "." + XMI_NS_TABLE)) { try (CoStoSysConnection conn = dbc.obtainOrReserveConnection()){ conn.setAutoCommit(true); Statement stmt = conn.createStatement(); String sql = String.format("CREATE TABLE %s (%s text PRIMARY KEY, %s text)", dbc.getActiveDataPGSchema() + "." + XMI_NS_TABLE, PREFIX, NS_URI); stmt.execute(sql); } catch (SQLException e) { e.printStackTrace(); SQLException ne = e.getNextException(); if (null != ne) ne.printStackTrace(); } } }
@Override public void initialize(UimaContext context) throws ResourceInitializationException { super.initialize(context); // Check whether a subset table name or a data table name was given. if (readDataTable) { log.debug("Reading from data table {}", tableName); dataTableDocumentIds = dbc.query(tableName, Arrays.asList(dbc.getFieldConfiguration(dbc.getActiveTableSchema()).getPrimaryKey())); hasNext = dataTableDocumentIds.hasNext(); } else { log.debug("Reading from subset table {}", tableName); hasNext = dbc.withConnectionQueryBoolean(c -> c.hasUnfetchedRows(tableName)); } }
/** * Determines the data table. This may be the specified table itself or, if it is a subset, the first referenced data table. * * @throws ResourceInitializationException If an SQL exception occurs. */ private void determineDataTable() throws ResourceInitializationException { try { readDataTable = dbc.withConnectionQueryBoolean(c -> c.isDataTable(tableName)); dataTable = dbc.withConnectionQueryString(c -> c.getNextOrThisDataTable(tableName)); if (readDataTable) log.info("The table \"{}\" is a data table, documents will not be marked to be in process and no " + "synchronization of multiple DB readers will happen.", tableName); } catch (CoStoSysSQLRuntimeException e) { throw new ResourceInitializationException(e); } }
private byte[][] getNextFromDataTable() { byte[][] next; // Must be set to true again if the iterator has more elements. hasNext = false; next = xmlBytes.next(); // totalDocumentCount could be set to the Limit parameter. Thus we // should stop when we reach the limit. and not set hasNext back to // true. if (processedDocuments < totalDocumentCount - 1) { hasNext = xmlBytes.hasNext(); if (!hasNext) close(); } return next; }
private void checkTableExists() throws ResourceInitializationException { // Check whether the table we are supposed to read from actually exists. if (!dbc.withConnectionQueryBoolean(c -> c.tableExists(tableName))) { throw new ResourceInitializationException( new IllegalArgumentException("The configured table \"" + tableName + "\" does not exist.")); } }
@Override public void initialize(UimaContext context) throws ResourceInitializationException { adaptReaderConfigurationForXmiData(); super.initialize(context); dbc.reserveConnection(); initializer = new Initializer(this, dbc, additionalTableNames, joinTables); initializer.initialize(context); casPopulator = new CasPopulator(dataTable, initializer, readDataTable, tableName); dbc.releaseConnections(); }
/** * If <code>documentTableParameter</code> is not schema qualified, prependns the active data postgres schema. * * @param documentTableParameter The document table, as given in the component parameter. * @return The effective document table name. */ String getEffectiveDocumentTableName(String documentTableParameter) { // If the table is already schema qualified, accept it. if (documentTableParameter.contains(".")) return documentTableParameter; return dbc.getActiveDataPGSchema() + "." + documentTableParameter; }
private DataBaseConnector getDataBaseConnector(String costosysConfig) throws AnalysisEngineProcessException { DataBaseConnector dbc; try { dbc = new DataBaseConnector(costosysConfig); } catch (FileNotFoundException e) { throw new AnalysisEngineProcessException(e); } return dbc; }
private void populateCas(JCas jCas) throws AnalysisEngineProcessException { try { final byte[][] data = documentDataIterator.next(); if (data != null) casPopulator.populateCas(data, jCas); } catch (CasPopulationException e) { throw new AnalysisEngineProcessException(e); } }
private String getPkStringFromData(byte[][] data) { List<Integer> pkIndices = dbc.getPrimaryKeyIndices(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < pkIndices.size(); ++i) { Integer index = pkIndices.get(i); byte[] pkElementValue = data[index]; String elementString = new String(pkElementValue); sb.append(elementString); if (i < pkIndices.size() - 1) sb.append("-"); } return sb.toString(); }
@Override public void initialize(UimaContext context) throws ResourceInitializationException { super.initialize(context); dataTimestamp = (String) getConfigParameterValue(PARAM_DATA_TIMESTAMP); if (readDataTable && hasNext) { log.debug("Querying data table {} with schema {} and where condition {}", tableName, dbc.getActiveTableSchema(), whereCondition); xmlBytes = dbc.queryDataTable(tableName, whereCondition); } }
/** * Convenience method for extending classes. * * @return */ @SuppressWarnings("unchecked") protected List<Map<String, Object>> getAllRetrievedColumns() { List<Map<String, Object>> fields = new ArrayList<Map<String, Object>>(); Pair<Integer, List<Map<String, String>>> numColumnsAndFields = dbc.getNumColumnsAndFields(joinTables, schemas); return numColumnsAndFields.getRight().stream().map(HashMap<String, Object>::new).collect(Collectors.toList()); }
private void createAnnotationListTable() { if (!dbc.tableExists(dbc.getActiveDataPGSchema() + "." + ANNOTATION_LIST_TABLE)) { try (CoStoSysConnection conn = dbc.obtainOrReserveConnection()){ conn.setAutoCommit(true); Statement stmt = conn.createStatement(); String sql = String.format("CREATE TABLE %s (%s text PRIMARY KEY)", dbc.getActiveDataPGSchema() + "." + ANNOTATION_LIST_TABLE, TABLE_NAME); stmt.execute(sql); } catch (SQLException e) { e.printStackTrace(); SQLException ne = e.getNextException(); if (null != ne) ne.printStackTrace(); } } } }
private List<Object[]> getNextFromDataTable() { List<Object[]> next = new ArrayList<>(batchSize); // Must be set to true again if the iterator has more elements. hasNext = false; log.trace("Filling document ID list with the next batch of documents."); while (dataTableDocumentIds.hasNext() && next.size() < batchSize) next.add(dataTableDocumentIds.next()); // totalDocumentCount could be set to the Limit parameter. Thus we // should stop when we reach the limit. and not set hasNext back to // true. if (processedDocuments < totalDocumentCount - 1) { log.trace("Checking if there are more documents to read from the data table."); hasNext = dataTableDocumentIds.hasNext(); } return next; }
@Override public AbstractCas next() throws AnalysisEngineProcessException { JCas jCas = getEmptyJCas(); if (documentDataIterator.hasNext()) { try { initializer.initializeAnnotationTableNames(jCas); } catch (ResourceInitializationException e) { throw new AnalysisEngineProcessException(e); } populateCas(jCas); } return jCas; }
/** * Adds the extract string <tt>tablename</tt> to the list of annotation * table names. * * @param tablename */ void addAnnotationTableToList(String tablename) { try (CoStoSysConnection conn = dbc.obtainOrReserveConnection()){ conn.setAutoCommit(true); Statement stmt = conn.createStatement(); String template = "INSERT INTO %s VALUES('%s')"; String sql = String.format(template, dbc.getActiveDataPGSchema() + "." + ANNOTATION_LIST_TABLE, tablename); stmt.execute(sql); } catch (PSQLException e) { log.debug("Tried to add already existing annotation table to annotation list: \"{}\", ignoring.", tablename); } catch (SQLException e) { e.printStackTrace(); SQLException ne = e.getNextException(); if (null != ne) ne.printStackTrace(); } }