You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@metamodel.apache.org by ka...@apache.org on 2013/07/22 10:10:29 UTC
[16/64] [partial] Hard rename of all 'org/eobjects' folders to
'org/apache'.
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/csv/src/main/java/org/eobjects/metamodel/csv/CsvDataContext.java
----------------------------------------------------------------------
diff --git a/csv/src/main/java/org/eobjects/metamodel/csv/CsvDataContext.java b/csv/src/main/java/org/eobjects/metamodel/csv/CsvDataContext.java
deleted file mode 100644
index 6409596..0000000
--- a/csv/src/main/java/org/eobjects/metamodel/csv/CsvDataContext.java
+++ /dev/null
@@ -1,393 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.eobjects.metamodel.csv;
-
-import java.io.BufferedReader;
-import java.io.BufferedWriter;
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
-import java.io.UnsupportedEncodingException;
-import java.net.URL;
-import java.util.List;
-
-import org.eobjects.metamodel.MetaModelException;
-import org.eobjects.metamodel.QueryPostprocessDataContext;
-import org.eobjects.metamodel.UpdateScript;
-import org.eobjects.metamodel.UpdateableDataContext;
-import org.eobjects.metamodel.data.DataSet;
-import org.eobjects.metamodel.query.FilterItem;
-import org.eobjects.metamodel.schema.Column;
-import org.eobjects.metamodel.schema.Table;
-import org.eobjects.metamodel.util.FileHelper;
-import org.eobjects.metamodel.util.FileResource;
-import org.eobjects.metamodel.util.Func;
-import org.eobjects.metamodel.util.Resource;
-import org.eobjects.metamodel.util.UrlResource;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import au.com.bytecode.opencsv.CSVReader;
-
-/**
- * DataContext implementation for reading CSV files.
- */
-public final class CsvDataContext extends QueryPostprocessDataContext implements UpdateableDataContext {
-
- private static final Logger logger = LoggerFactory.getLogger(CsvDataContext.class);
-
- private final Object WRITE_LOCK = new Object();
-
- private final Resource _resource;
- private final CsvConfiguration _configuration;
- private final boolean _writable;
-
- /**
- * Constructs a CSV DataContext based on a file
- *
- * @param file
- * @param configuration
- */
- public CsvDataContext(File file, CsvConfiguration configuration) {
- if (file == null) {
- throw new IllegalArgumentException("File cannot be null");
- }
- if (configuration == null) {
- throw new IllegalArgumentException("CsvConfiguration cannot be null");
- }
- _resource = new FileResource(file);
- _configuration = configuration;
- _writable = true;
- }
-
- public CsvDataContext(Resource resource, CsvConfiguration configuration) {
- if (resource == null) {
- throw new IllegalArgumentException("File cannot be null");
- }
- if (configuration == null) {
- throw new IllegalArgumentException("CsvConfiguration cannot be null");
- }
- _resource = resource;
- _configuration = configuration;
- _writable = !resource.isReadOnly();
- }
-
- /**
- * Constructs a CSV DataContext based on a {@link URL}
- *
- * @param url
- * @param configuration
- */
- public CsvDataContext(URL url, CsvConfiguration configuration) {
- _resource = new UrlResource(url);
- _configuration = configuration;
- _writable = false;
- }
-
- /**
- * Constructs a CSV DataContext based on a file
- *
- * @param file
- */
- public CsvDataContext(File file) {
- this(file, new CsvConfiguration());
- }
-
- /**
- * Constructs a CSV DataContext based on an {@link InputStream}
- *
- * @param inputStream
- * @param configuration
- */
- public CsvDataContext(InputStream inputStream, CsvConfiguration configuration) {
- File file = createFileFromInputStream(inputStream, configuration.getEncoding());
- _configuration = configuration;
- _writable = false;
- _resource = new FileResource(file);
- }
-
- /**
- * @deprecated use {@link #CsvDataContext(File, CsvConfiguration)} instead.
- */
- @Deprecated
- public CsvDataContext(File file, char separatorChar) {
- this(file, separatorChar, CsvConfiguration.DEFAULT_QUOTE_CHAR);
- }
-
- /**
- * @deprecated use {@link #CsvDataContext(File, CsvConfiguration)} instead.
- */
- @Deprecated
- public CsvDataContext(File file, char separatorChar, char quoteChar) {
- this(file, new CsvConfiguration(CsvConfiguration.DEFAULT_COLUMN_NAME_LINE, FileHelper.DEFAULT_ENCODING,
- separatorChar, quoteChar, CsvConfiguration.DEFAULT_ESCAPE_CHAR));
- }
-
- /**
- * @deprecated use {@link #CsvDataContext(File, CsvConfiguration)} instead.
- */
- @Deprecated
- public CsvDataContext(File file, char separatorChar, char quoteChar, String encoding) {
- this(file, new CsvConfiguration(CsvConfiguration.DEFAULT_COLUMN_NAME_LINE, encoding, separatorChar, quoteChar,
- CsvConfiguration.DEFAULT_ESCAPE_CHAR));
- }
-
- /**
- * @deprecated use {@link #CsvDataContext(URL, CsvConfiguration)} instead.
- */
- @Deprecated
- public CsvDataContext(URL url, char separatorChar, char quoteChar) {
- this(url, separatorChar, quoteChar, FileHelper.DEFAULT_ENCODING);
- }
-
- /**
- * @deprecated use {@link #CsvDataContext(URL, CsvConfiguration)} instead.
- */
- @Deprecated
- public CsvDataContext(URL url, char separatorChar, char quoteChar, String encoding) {
- this(url, new CsvConfiguration(CsvConfiguration.DEFAULT_COLUMN_NAME_LINE, encoding, separatorChar, quoteChar,
- CsvConfiguration.DEFAULT_ESCAPE_CHAR));
- }
-
- /**
- * @deprecated use {@link #CsvDataContext(InputStream, CsvConfiguration)}
- * instead.
- */
- @Deprecated
- public CsvDataContext(InputStream inputStream, char separatorChar, char quoteChar) {
- this(inputStream, new CsvConfiguration(CsvConfiguration.DEFAULT_COLUMN_NAME_LINE, FileHelper.DEFAULT_ENCODING,
- separatorChar, quoteChar, CsvConfiguration.DEFAULT_ESCAPE_CHAR));
- }
-
- /**
- * @deprecated use {@link #CsvDataContext(InputStream, CsvConfiguration)}
- * instead.
- */
- @Deprecated
- public CsvDataContext(InputStream inputStream, char separatorChar, char quoteChar, String encoding) {
- this(inputStream, new CsvConfiguration(CsvConfiguration.DEFAULT_COLUMN_NAME_LINE, encoding, separatorChar,
- quoteChar, CsvConfiguration.DEFAULT_ESCAPE_CHAR));
- }
-
- /**
- * Gets the CSV configuration used
- *
- * @return a CSV configuration
- */
- public CsvConfiguration getConfiguration() {
- return _configuration;
- }
-
- /**
- * Gets the CSV file being read
- *
- * @return a file
- *
- * @deprecated use {@link #getResource()} instead.
- */
- @Deprecated
- public File getFile() {
- if (_resource instanceof FileResource) {
- return ((FileResource) _resource).getFile();
- }
- return null;
- }
-
- /**
- * Gets the resource that is being read from.
- *
- * @return
- */
- public Resource getResource() {
- return _resource;
- }
-
- private static File createFileFromInputStream(InputStream inputStream, String encoding) {
- final File file;
- final File tempDir = FileHelper.getTempDir();
-
- File fileCandidate = null;
- boolean usableName = false;
- int index = 0;
-
- while (!usableName) {
- index++;
- fileCandidate = new File(tempDir, "metamodel" + index + ".csv");
- usableName = !fileCandidate.exists();
- }
- file = fileCandidate;
-
- final BufferedWriter writer = FileHelper.getBufferedWriter(file, encoding);
- final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
-
- try {
- file.createNewFile();
- file.deleteOnExit();
-
- boolean firstLine = true;
-
- for (String line = reader.readLine(); line != null; line = reader.readLine()) {
- if (firstLine) {
- firstLine = false;
- } else {
- writer.write('\n');
- }
- writer.write(line);
- }
- } catch (IOException e) {
- throw new IllegalStateException(e);
- } finally {
- FileHelper.safeClose(writer, reader);
- }
-
- return file;
- }
-
- @Override
- protected Number executeCountQuery(Table table, List<FilterItem> whereItems, boolean functionApproximationAllowed) {
- if (!functionApproximationAllowed) {
- return null;
- }
-
- return _resource.read(new Func<InputStream, Number>() {
- @Override
- public Number eval(InputStream inputStream) {
- try {
- final long length = _resource.getSize();
- // read up to 5 megs of the file and approximate number of
- // lines
- // based on that.
-
- final int sampleSize = (int) Math.min(length, 1024 * 1024 * 5);
- final int chunkSize = Math.min(sampleSize, 1024 * 1024);
-
- int readSize = 0;
- int newlines = 0;
- int carriageReturns = 0;
- byte[] byteBuffer = new byte[chunkSize];
- char[] charBuffer = new char[chunkSize];
-
- while (readSize < sampleSize) {
- final int read = inputStream.read(byteBuffer);
- if (read == -1) {
- break;
- } else {
- readSize += read;
- }
-
- Reader reader = getReader(byteBuffer, _configuration.getEncoding());
- reader.read(charBuffer);
- for (char c : charBuffer) {
- if ('\n' == c) {
- newlines++;
- } else if ('\r' == c) {
- carriageReturns++;
- }
- }
- }
-
- int lines = Math.max(newlines, carriageReturns);
-
- logger.info("Found {} lines breaks in {} bytes", lines, sampleSize);
-
- long approxCount = (long) (lines * length / sampleSize);
- return approxCount;
- } catch (IOException e) {
- logger.error("Unexpected error during COUNT(*) approximation", e);
- throw new IllegalStateException(e);
- }
- }
- });
- }
-
- private Reader getReader(byte[] byteBuffer, String encoding) throws UnsupportedEncodingException {
- try {
- return new InputStreamReader(new ByteArrayInputStream(byteBuffer), encoding);
- } catch (UnsupportedEncodingException e1) {
- // this may happen on more exotic encodings, but since this reader
- // is only meant for finding newlines, we'll try again with UTF8
- try {
- return new InputStreamReader(new ByteArrayInputStream(byteBuffer), "UTF8");
- } catch (UnsupportedEncodingException e2) {
- throw e1;
- }
- }
- }
-
- @Override
- public DataSet materializeMainSchemaTable(Table table, Column[] columns, int maxRows) {
- final int lineNumber = _configuration.getColumnNameLineNumber();
- final CSVReader reader = createCsvReader(lineNumber);
- final int columnCount = table.getColumnCount();
- final boolean failOnInconsistentRowLength = _configuration.isFailOnInconsistentRowLength();
- if (maxRows < 0) {
- return new CsvDataSet(reader, columns, null, columnCount, failOnInconsistentRowLength);
- } else {
- return new CsvDataSet(reader, columns, maxRows, columnCount, failOnInconsistentRowLength);
- }
- }
-
- protected CSVReader createCsvReader(int skipLines) {
- final Reader fileReader = FileHelper.getReader(_resource.read(), _configuration.getEncoding());
- final CSVReader csvReader = new CSVReader(fileReader, _configuration.getSeparatorChar(),
- _configuration.getQuoteChar(), _configuration.getEscapeChar(), skipLines);
- return csvReader;
- }
-
- @Override
- protected CsvSchema getMainSchema() throws MetaModelException {
- CsvSchema schema = new CsvSchema(getMainSchemaName(), this);
- if (_resource.isExists()) {
- schema.setTable(new CsvTable(schema));
- }
- return schema;
- }
-
- @Override
- protected String getMainSchemaName() {
- return _resource.getName();
- }
-
- protected boolean isWritable() {
- return _writable;
- }
-
- private void checkWritable() {
- if (!isWritable()) {
- throw new IllegalStateException(
- "This CSV DataContext is not writable, as it based on a read-only resource.");
- }
- }
-
- @Override
- public void executeUpdate(UpdateScript update) {
- checkWritable();
- CsvUpdateCallback callback = new CsvUpdateCallback(this);
- synchronized (WRITE_LOCK) {
- try {
- update.run(callback);
- } finally {
- callback.close();
- }
- }
- }
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/csv/src/main/java/org/eobjects/metamodel/csv/CsvDataSet.java
----------------------------------------------------------------------
diff --git a/csv/src/main/java/org/eobjects/metamodel/csv/CsvDataSet.java b/csv/src/main/java/org/eobjects/metamodel/csv/CsvDataSet.java
deleted file mode 100644
index ae31865..0000000
--- a/csv/src/main/java/org/eobjects/metamodel/csv/CsvDataSet.java
+++ /dev/null
@@ -1,127 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.eobjects.metamodel.csv;
-
-import java.io.IOException;
-
-import org.eobjects.metamodel.MetaModelException;
-import org.eobjects.metamodel.data.AbstractDataSet;
-import org.eobjects.metamodel.data.DefaultRow;
-import org.eobjects.metamodel.data.Row;
-import org.eobjects.metamodel.schema.Column;
-import org.eobjects.metamodel.util.FileHelper;
-
-import au.com.bytecode.opencsv.CSVReader;
-
-/**
- * Streaming DataSet implementation for CSV support
- *
- * @author Kasper Sørensen
- */
-final class CsvDataSet extends AbstractDataSet {
-
- private final CSVReader _reader;
- private final boolean _failOnInconsistentRowLength;
- private final int _columnsInTable;
- private volatile int _rowNumber;
- private volatile Integer _rowsRemaining;
- private volatile Row _row;
-
- public CsvDataSet(CSVReader reader, Column[] columns, Integer maxRows,
- int columnsInTable, boolean failOnInconsistentRowLength) {
- super(columns);
- _reader = reader;
- _columnsInTable = columnsInTable;
- _failOnInconsistentRowLength = failOnInconsistentRowLength;
- _rowNumber = 0;
- _rowsRemaining = maxRows;
- }
-
- @Override
- public void close() {
- FileHelper.safeClose(_reader);
- _row = null;
- _rowsRemaining = null;
- }
-
- @Override
- protected void finalize() throws Throwable {
- super.finalize();
- // close is always safe to invoke
- close();
- }
-
- @Override
- public Row getRow() throws MetaModelException {
- return _row;
- }
-
- @Override
- public boolean next() {
- if (_rowsRemaining != null && _rowsRemaining > 0) {
- _rowsRemaining--;
- return nextInternal();
- } else if (_rowsRemaining == null) {
- return nextInternal();
- } else {
- return false;
- }
- }
-
- private boolean nextInternal() {
- if (_reader == null) {
- return false;
- }
- final String[] csvValues;
- try {
- csvValues = _reader.readNext();
- } catch (IOException e) {
- throw new IllegalStateException("Exception reading from file", e);
- }
- if (csvValues == null) {
- close();
- return false;
- }
-
- final int size = getHeader().size();
- final Object[] rowValues = new Object[size];
- for (int i = 0; i < size; i++) {
- Column column = getHeader().getSelectItem(i).getColumn();
- int columnNumber = column.getColumnNumber();
- if (columnNumber < csvValues.length) {
- rowValues[i] = csvValues[columnNumber];
- } else {
- // Ticket #125: Missing values should be enterpreted as
- // null.
- rowValues[i] = null;
- }
- }
- _row = new DefaultRow(getHeader(), rowValues);
-
- if (_failOnInconsistentRowLength) {
- _rowNumber++;
- if (_columnsInTable != csvValues.length) {
- throw new InconsistentRowLengthException(_columnsInTable, _row,
- csvValues, _rowNumber);
- }
- }
-
- return true;
- }
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/csv/src/main/java/org/eobjects/metamodel/csv/CsvDeleteBuilder.java
----------------------------------------------------------------------
diff --git a/csv/src/main/java/org/eobjects/metamodel/csv/CsvDeleteBuilder.java b/csv/src/main/java/org/eobjects/metamodel/csv/CsvDeleteBuilder.java
deleted file mode 100644
index d245ff3..0000000
--- a/csv/src/main/java/org/eobjects/metamodel/csv/CsvDeleteBuilder.java
+++ /dev/null
@@ -1,103 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.eobjects.metamodel.csv;
-
-import java.io.File;
-import java.io.InputStream;
-import java.io.OutputStream;
-
-import org.eobjects.metamodel.MetaModelException;
-import org.eobjects.metamodel.UpdateCallback;
-import org.eobjects.metamodel.UpdateScript;
-import org.eobjects.metamodel.data.DataSet;
-import org.eobjects.metamodel.data.Row;
-import org.eobjects.metamodel.delete.AbstractRowDeletionBuilder;
-import org.eobjects.metamodel.schema.Table;
-import org.eobjects.metamodel.util.Action;
-import org.eobjects.metamodel.util.FileHelper;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-final class CsvDeleteBuilder extends AbstractRowDeletionBuilder {
-
- private static final Logger logger = LoggerFactory.getLogger(CsvDeleteBuilder.class);
-
- private final CsvUpdateCallback _updateCallback;
-
- public CsvDeleteBuilder(CsvUpdateCallback updateCallback, Table table) {
- super(table);
- _updateCallback = updateCallback;
- }
-
- @Override
- public void execute() throws MetaModelException {
- final File tempFile = FileHelper.createTempFile("metamodel_deletion", "csv");
-
- final CsvConfiguration configuration = _updateCallback.getConfiguration();
-
- final CsvDataContext copyDataContext = new CsvDataContext(tempFile, configuration);
- copyDataContext.executeUpdate(new UpdateScript() {
-
- @Override
- public void run(UpdateCallback callback) {
- final Table originalTable = getTable();
- final Table copyTable = callback.createTable(copyDataContext.getDefaultSchema(), originalTable.getName())
- .like(originalTable).execute();
-
- if (isTruncateTableOperation()) {
- // no need to iterate old records, they should all be
- // removed
- return;
- }
-
- final DataSet dataSet = _updateCallback.getDataContext().query().from(originalTable)
- .select(originalTable.getColumns()).execute();
- try {
- while (dataSet.next()) {
- final Row row = dataSet.getRow();
- if (!deleteRow(row)) {
- callback.insertInto(copyTable).like(row).execute();
- }
- }
- } finally {
- dataSet.close();
- }
- }
- });
-
- // copy the copy (which does not have deleted records) to overwrite the
- // original
- final InputStream in = FileHelper.getInputStream(tempFile);
- try {
- _updateCallback.getResource().write(new Action<OutputStream>() {
- @Override
- public void run(OutputStream out) throws Exception {
- FileHelper.copy(in, out);
- }
- });
- } finally {
- FileHelper.safeClose(in);
- }
-
- final boolean deleted = tempFile.delete();
- if (!deleted) {
- logger.warn("Could not delete temporary copy-file: {}", tempFile);
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/csv/src/main/java/org/eobjects/metamodel/csv/CsvInsertBuilder.java
----------------------------------------------------------------------
diff --git a/csv/src/main/java/org/eobjects/metamodel/csv/CsvInsertBuilder.java b/csv/src/main/java/org/eobjects/metamodel/csv/CsvInsertBuilder.java
deleted file mode 100644
index 8faccc6..0000000
--- a/csv/src/main/java/org/eobjects/metamodel/csv/CsvInsertBuilder.java
+++ /dev/null
@@ -1,40 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.eobjects.metamodel.csv;
-
-import org.eobjects.metamodel.insert.AbstractRowInsertionBuilder;
-import org.eobjects.metamodel.schema.Table;
-
-final class CsvInsertBuilder extends AbstractRowInsertionBuilder<CsvUpdateCallback> {
-
- public CsvInsertBuilder(CsvUpdateCallback updateCallback, Table table) {
- super(updateCallback, table);
- }
-
- @Override
- public void execute() {
- Object[] values = getValues();
- String[] stringValues = new String[values.length];
- for (int i = 0; i < stringValues.length; i++) {
- stringValues[i] = values[i] == null ? "" : values[i].toString();
- }
- getUpdateCallback().writeRow(stringValues, true);
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/csv/src/main/java/org/eobjects/metamodel/csv/CsvSchema.java
----------------------------------------------------------------------
diff --git a/csv/src/main/java/org/eobjects/metamodel/csv/CsvSchema.java b/csv/src/main/java/org/eobjects/metamodel/csv/CsvSchema.java
deleted file mode 100644
index c6d2ba9..0000000
--- a/csv/src/main/java/org/eobjects/metamodel/csv/CsvSchema.java
+++ /dev/null
@@ -1,63 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.eobjects.metamodel.csv;
-
-import org.eobjects.metamodel.schema.AbstractSchema;
-import org.eobjects.metamodel.schema.Table;
-
-final class CsvSchema extends AbstractSchema {
-
- private static final long serialVersionUID = 1L;
-
- private final String _name;
- private final transient CsvDataContext _dataContext;
- private CsvTable _table;
-
- public CsvSchema(String name, CsvDataContext dataContext) {
- super();
- _name = name;
- _dataContext = dataContext;
- }
-
- protected void setTable(CsvTable table) {
- _table = table;
- }
-
- @Override
- public String getName() {
- return _name;
- }
-
- protected CsvDataContext getDataContext() {
- return _dataContext;
- }
-
- @Override
- public String getQuote() {
- return null;
- }
-
- @Override
- public Table[] getTables() {
- if (_table == null) {
- return new Table[0];
- }
- return new Table[] { _table };
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/csv/src/main/java/org/eobjects/metamodel/csv/CsvTable.java
----------------------------------------------------------------------
diff --git a/csv/src/main/java/org/eobjects/metamodel/csv/CsvTable.java b/csv/src/main/java/org/eobjects/metamodel/csv/CsvTable.java
deleted file mode 100644
index eb9bdc0..0000000
--- a/csv/src/main/java/org/eobjects/metamodel/csv/CsvTable.java
+++ /dev/null
@@ -1,149 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.eobjects.metamodel.csv;
-
-import java.io.IOException;
-
-import org.eobjects.metamodel.schema.AbstractTable;
-import org.eobjects.metamodel.schema.Column;
-import org.eobjects.metamodel.schema.ColumnType;
-import org.eobjects.metamodel.schema.MutableColumn;
-import org.eobjects.metamodel.schema.Relationship;
-import org.eobjects.metamodel.schema.Schema;
-import org.eobjects.metamodel.schema.TableType;
-import org.eobjects.metamodel.util.AlphabeticSequence;
-import org.eobjects.metamodel.util.FileHelper;
-
-import au.com.bytecode.opencsv.CSVReader;
-
-final class CsvTable extends AbstractTable {
-
- private static final long serialVersionUID = 1L;
-
- private final CsvSchema _schema;
- private Column[] _columns;
-
- /**
- * Constructor for creating a new CSV table which has not yet been written.
- *
- * @param schema
- * @param columnNames
- */
- public CsvTable(CsvSchema schema, String[] columnNames) {
- this(schema);
- _columns = buildColumns(columnNames);
- }
-
- /**
- * Constructor for reading an existing CSV table.
- *
- * @param schema
- */
- public CsvTable(CsvSchema schema) {
- _schema = schema;
- }
-
- @Override
- public String getName() {
- String schemaName = _schema.getName();
- return schemaName.substring(0, schemaName.length() - 4);
- }
-
- @Override
- public Column[] getColumns() {
- if (_columns == null) {
- synchronized (this) {
- if (_columns == null) {
- _columns = buildColumns();
- }
- }
- }
- return _columns;
- }
-
- private Column[] buildColumns() {
- CSVReader reader = null;
- try {
- reader = _schema.getDataContext().createCsvReader(0);
-
- final int columnNameLineNumber = _schema.getDataContext().getConfiguration().getColumnNameLineNumber();
- for (int i = 1; i < columnNameLineNumber; i++) {
- reader.readNext();
- }
- final String[] columnHeaders = reader.readNext();
-
- reader.close();
- return buildColumns(columnHeaders);
- } catch (IOException e) {
- throw new IllegalStateException("Exception reading from resource: " + _schema.getDataContext().getResource().getName(), e);
- } finally {
- FileHelper.safeClose(reader);
- }
- }
-
- private Column[] buildColumns(final String[] columnNames) {
- if (columnNames == null) {
- return new Column[0];
- }
-
- final CsvConfiguration configuration = _schema.getDataContext().getConfiguration();
- final int columnNameLineNumber = configuration.getColumnNameLineNumber();
- final boolean nullable = !configuration.isFailOnInconsistentRowLength();
-
- final Column[] columns = new Column[columnNames.length];
- final AlphabeticSequence sequence = new AlphabeticSequence();
- for (int i = 0; i < columnNames.length; i++) {
- final String columnName;
- if (columnNameLineNumber == CsvConfiguration.NO_COLUMN_NAME_LINE) {
- columnName = sequence.next();
- } else {
- columnName = columnNames[i];
- }
- Column column = new MutableColumn(columnName, ColumnType.VARCHAR, this, i, null, null, nullable, null,
- false, null);
- columns[i] = column;
- }
- return columns;
- }
-
- @Override
- public Schema getSchema() {
- return _schema;
- }
-
- @Override
- public TableType getType() {
- return TableType.TABLE;
- }
-
- @Override
- public Relationship[] getRelationships() {
- return new Relationship[0];
- }
-
- @Override
- public String getRemarks() {
- return null;
- }
-
- @Override
- public String getQuote() {
- return null;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/csv/src/main/java/org/eobjects/metamodel/csv/CsvTableDropBuilder.java
----------------------------------------------------------------------
diff --git a/csv/src/main/java/org/eobjects/metamodel/csv/CsvTableDropBuilder.java b/csv/src/main/java/org/eobjects/metamodel/csv/CsvTableDropBuilder.java
deleted file mode 100644
index 29e0202..0000000
--- a/csv/src/main/java/org/eobjects/metamodel/csv/CsvTableDropBuilder.java
+++ /dev/null
@@ -1,38 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.eobjects.metamodel.csv;
-
-import org.eobjects.metamodel.drop.AbstractTableDropBuilder;
-import org.eobjects.metamodel.schema.Table;
-
-final class CsvTableDropBuilder extends AbstractTableDropBuilder {
-
- private final CsvUpdateCallback _updateCallback;
-
- public CsvTableDropBuilder(CsvUpdateCallback updateCallback, Table table) {
- super(table);
- _updateCallback = updateCallback;
- }
-
- @Override
- public void execute() {
- _updateCallback.dropTable();
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/csv/src/main/java/org/eobjects/metamodel/csv/CsvUpdateCallback.java
----------------------------------------------------------------------
diff --git a/csv/src/main/java/org/eobjects/metamodel/csv/CsvUpdateCallback.java b/csv/src/main/java/org/eobjects/metamodel/csv/CsvUpdateCallback.java
deleted file mode 100644
index d35ff3b..0000000
--- a/csv/src/main/java/org/eobjects/metamodel/csv/CsvUpdateCallback.java
+++ /dev/null
@@ -1,249 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.eobjects.metamodel.csv;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.RandomAccessFile;
-import java.io.Writer;
-import java.nio.ByteBuffer;
-import java.nio.channels.FileChannel;
-import java.nio.charset.Charset;
-
-import org.eobjects.metamodel.AbstractUpdateCallback;
-import org.eobjects.metamodel.MetaModelException;
-import org.eobjects.metamodel.UpdateCallback;
-import org.eobjects.metamodel.create.TableCreationBuilder;
-import org.eobjects.metamodel.delete.RowDeletionBuilder;
-import org.eobjects.metamodel.drop.TableDropBuilder;
-import org.eobjects.metamodel.insert.RowInsertionBuilder;
-import org.eobjects.metamodel.schema.Schema;
-import org.eobjects.metamodel.schema.Table;
-import org.eobjects.metamodel.update.RowUpdationBuilder;
-import org.eobjects.metamodel.util.Action;
-import org.eobjects.metamodel.util.EqualsBuilder;
-import org.eobjects.metamodel.util.FileHelper;
-import org.eobjects.metamodel.util.FileResource;
-import org.eobjects.metamodel.util.Resource;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-final class CsvUpdateCallback extends AbstractUpdateCallback implements UpdateCallback {
-
- private static final Logger logger = LoggerFactory.getLogger(CsvUpdateCallback.class);
-
- private final CsvConfiguration _configuration;
- private final Resource _resource;
- private Writer _writer;
-
- public CsvUpdateCallback(CsvDataContext dataContext) {
- super(dataContext);
- _resource = dataContext.getResource();
- _configuration = dataContext.getConfiguration();
- }
-
- @Override
- public TableCreationBuilder createTable(Schema schema, String name) throws IllegalArgumentException, IllegalStateException {
- return new CsvCreateTableBuilder(this, schema, name);
- }
-
- @Override
- public RowInsertionBuilder insertInto(Table table) throws IllegalArgumentException, IllegalStateException {
- validateTable(table);
- return new CsvInsertBuilder(this, table);
- }
-
- public CsvConfiguration getConfiguration() {
- return _configuration;
- }
-
- public Resource getResource() {
- return _resource;
- }
-
- private void validateTable(Table table) {
- if (!(table instanceof CsvTable)) {
- throw new IllegalArgumentException("Not a valid CSV table: " + table);
- }
- }
-
- protected synchronized void writeRow(final String[] stringValues, final boolean append) {
- final CsvWriter _csvWriter = new CsvWriter(_configuration);
- final String line = _csvWriter.buildLine(stringValues);
- if (_resource instanceof FileResource) {
- // optimized handling for file-based resources
- final File file = ((FileResource) _resource).getFile();
- final Writer writer = getFileWriter(file, append);
- try {
- writer.write(line);
- } catch (IOException e) {
- throw new MetaModelException("Failed to write line to file: " + line, e);
- }
- } else {
- // generic handling for any kind of resource
- final Action<OutputStream> action = new Action<OutputStream>() {
- @Override
- public void run(OutputStream out) throws Exception {
- final String encoding = _configuration.getEncoding();
- final OutputStreamWriter writer = new OutputStreamWriter(out, encoding);
- writer.write(line);
- writer.flush();
- }
- };
- if (append) {
- _resource.append(action);
- } else {
- _resource.write(action);
- }
- }
- }
-
- private Writer getFileWriter(File file, boolean append) {
- if (_writer == null || !append) {
- final boolean needsLineBreak = needsLineBreak(file, _configuration);
-
- final Writer writer = FileHelper.getWriter(file, _configuration.getEncoding(), append);
- if (needsLineBreak) {
- try {
- writer.write('\n');
- } catch (IOException e) {
- logger.debug("Failed to insert newline", e);
- }
- }
- _writer = writer;
- }
- return _writer;
- }
-
- protected static boolean needsLineBreak(File file, CsvConfiguration configuration) {
- if (!file.exists() || file.length() == 0) {
- return false;
- }
-
- try {
- // find the bytes a newline would match under the encoding
- final byte[] bytesInLineBreak;
- {
- ByteBuffer encodedLineBreak = Charset.forName(configuration.getEncoding()).encode("\n");
- bytesInLineBreak = new byte[encodedLineBreak.capacity()];
- encodedLineBreak.get(bytesInLineBreak);
- }
-
- // find the last bytes of the file
- final byte[] bytesFromFile = new byte[bytesInLineBreak.length];
- {
- final RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
- try {
- FileChannel channel = randomAccessFile.getChannel();
- try {
- long length = randomAccessFile.length();
-
- channel = channel.position(length - bytesInLineBreak.length);
- channel.read(ByteBuffer.wrap(bytesFromFile));
- } finally {
- channel.close();
- }
- } finally {
- randomAccessFile.close();
- }
- }
-
- // if the two byte arrays match, then the newline is not needed.
- if (EqualsBuilder.equals(bytesInLineBreak, bytesFromFile)) {
- return false;
- }
- return true;
- } catch (Exception e) {
- logger.error("Error occurred while checking if file needs linebreak, omitting check", e);
- }
- return false;
- }
-
- /**
- * Closes all open handles
- */
- protected void close() {
- if (_writer != null) {
- try {
- _writer.flush();
- } catch (IOException e) {
- logger.warn("Failed to flush CSV writer", e);
- }
- try {
- _writer.close();
- } catch (IOException e) {
- logger.error("Failed to close CSV writer", e);
- } finally {
- _writer = null;
- }
- }
- }
-
- @Override
- public RowUpdationBuilder update(Table table) throws IllegalArgumentException, IllegalStateException {
- close();
- return super.update(table);
- }
-
- @Override
- public boolean isDropTableSupported() {
- return true;
- }
-
- @Override
- public TableDropBuilder dropTable(Table table) {
- validateTable(table);
- return new CsvTableDropBuilder(this, table);
- }
-
- /**
- * Callback method used by {@link CsvTableDropBuilder} when execute is
- * called
- */
- protected void dropTable() {
- close();
- if (_resource instanceof FileResource) {
- final File file = ((FileResource) _resource).getFile();
- final boolean success = file.delete();
- if (!success) {
- throw new MetaModelException("Could not delete (drop) file: " + file);
- }
- } else {
- _resource.write(new Action<OutputStream>() {
- @Override
- public void run(OutputStream arg) throws Exception {
- // do nothing, just write an empty file
- }
- });
- }
- }
-
- @Override
- public boolean isDeleteSupported() {
- return true;
- }
-
- @Override
- public RowDeletionBuilder deleteFrom(Table table) {
- validateTable(table);
- return new CsvDeleteBuilder(this, table);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/csv/src/main/java/org/eobjects/metamodel/csv/CsvWriter.java
----------------------------------------------------------------------
diff --git a/csv/src/main/java/org/eobjects/metamodel/csv/CsvWriter.java b/csv/src/main/java/org/eobjects/metamodel/csv/CsvWriter.java
deleted file mode 100644
index 4a95af4..0000000
--- a/csv/src/main/java/org/eobjects/metamodel/csv/CsvWriter.java
+++ /dev/null
@@ -1,94 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.eobjects.metamodel.csv;
-
-import org.eobjects.metamodel.util.Resource;
-
-/**
- * This class is an adaptation of the CSVWriter class of OpenCSV. We've made the
- * writer work without having the output stream as state (suiting for
- * {@link Resource} usage).
- */
-public final class CsvWriter {
-
- public static final int INITIAL_STRING_SIZE = 128;
-
- private final CsvConfiguration _configuration;
-
- public CsvWriter(CsvConfiguration configuration) {
- _configuration = configuration;
- }
-
- /**
- * Builds a line for the CSV file output
- *
- * @param nextLine
- * a string array with each comma-separated element as a separate
- * entry.
- */
- public String buildLine(String[] nextLine) {
- final StringBuilder sb = new StringBuilder(INITIAL_STRING_SIZE);
- for (int i = 0; i < nextLine.length; i++) {
-
- if (i != 0) {
- sb.append(_configuration.getSeparatorChar());
- }
-
- final String nextElement = nextLine[i];
- if (nextElement == null) {
- continue;
- }
- final char quoteChar = _configuration.getQuoteChar();
- if (quoteChar != CsvConfiguration.NOT_A_CHAR) {
- sb.append(quoteChar);
- }
-
- sb.append(stringContainsSpecialCharacters(nextElement) ? processLine(nextElement) : nextElement);
-
- if (quoteChar != CsvConfiguration.NOT_A_CHAR) {
- sb.append(quoteChar);
- }
- }
-
- sb.append('\n');
- return sb.toString();
-
- }
-
- private boolean stringContainsSpecialCharacters(String line) {
- return line.indexOf(_configuration.getQuoteChar()) != -1 || line.indexOf(_configuration.getEscapeChar()) != -1;
- }
-
- private StringBuilder processLine(String nextElement) {
- final StringBuilder sb = new StringBuilder(INITIAL_STRING_SIZE);
- for (int j = 0; j < nextElement.length(); j++) {
- final char nextChar = nextElement.charAt(j);
- final char escapeChar = _configuration.getEscapeChar();
- if (escapeChar != CsvConfiguration.NOT_A_CHAR && nextChar == _configuration.getQuoteChar()) {
- sb.append(escapeChar).append(nextChar);
- } else if (escapeChar != CsvConfiguration.NOT_A_CHAR && nextChar == escapeChar) {
- sb.append(escapeChar).append(nextChar);
- } else {
- sb.append(nextChar);
- }
- }
-
- return sb;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/csv/src/main/java/org/eobjects/metamodel/csv/InconsistentRowLengthException.java
----------------------------------------------------------------------
diff --git a/csv/src/main/java/org/eobjects/metamodel/csv/InconsistentRowLengthException.java b/csv/src/main/java/org/eobjects/metamodel/csv/InconsistentRowLengthException.java
deleted file mode 100644
index 3fd80d7..0000000
--- a/csv/src/main/java/org/eobjects/metamodel/csv/InconsistentRowLengthException.java
+++ /dev/null
@@ -1,101 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.eobjects.metamodel.csv;
-
-import org.eobjects.metamodel.InconsistentRowFormatException;
-import org.eobjects.metamodel.data.DataSet;
-import org.eobjects.metamodel.data.Row;
-
-/**
- * Exception thrown when a line in a CSV file has an inconsistent amount of
- * columns compared to the previous lines (and headers). The exception will be
- * thrown when {@link DataSet#next()} is called.
- *
- * Note that this exception is only thrown if the
- * {@link CsvConfiguration#isFailOnInconsistentRowLength()} property is true.
- * Enabling it allows a somewhat different approach to iterating through a
- * resulting DataSet. For example something like:
- *
- * <pre>
- * while (true) {
- * try {
- * if (!dataSet.next) {
- * break;
- * }
- * Row row = dataSet.getRow();
- * handleRegularRow(row);
- * } catch (InconsistentRowLengthException e) {
- * handleIrregularRow(e.getSourceLine());
- * }
- * }
- * </pre>
- *
- * @author Kasper Sørensen
- */
-public final class InconsistentRowLengthException extends
- InconsistentRowFormatException {
-
- private static final long serialVersionUID = 1L;
-
- private final int _columnsInTable;
- private final String[] _line;
-
- public InconsistentRowLengthException(int columnsInTable, Row proposedRow,
- String[] line, int rowNumber) {
- super(proposedRow, rowNumber);
- _columnsInTable = columnsInTable;
- _line = line;
- }
-
- @Override
- public String getMessage() {
- return "Inconsistent length of row no. " + getRowNumber()
- + ". Expected " + getColumnsInTable() + " columns but found "
- + getColumnsInLine() + ".";
- }
-
- /**
- * Gets the source line, as parsed by the CSV parser (regardless of table
- * metadata).
- *
- * @return an array of string values.
- */
- public String[] getSourceLine() {
- return _line;
- }
-
- /**
- * Gets the amount of columns in the parsed line.
- *
- * @return an int representing the amount of values in the inconsistent
- * line.
- */
- public int getColumnsInLine() {
- return _line.length;
- }
-
- /**
- * Gets the expected amounts of columns, as defined by the table metadata.
- *
- * @return an int representing the amount of columns defined in the table.
- */
- public int getColumnsInTable() {
- return _columnsInTable;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/csv/src/main/java/org/eobjects/metamodel/csv/package-info.java
----------------------------------------------------------------------
diff --git a/csv/src/main/java/org/eobjects/metamodel/csv/package-info.java b/csv/src/main/java/org/eobjects/metamodel/csv/package-info.java
deleted file mode 100644
index b390eb8..0000000
--- a/csv/src/main/java/org/eobjects/metamodel/csv/package-info.java
+++ /dev/null
@@ -1,23 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-/**
- * Module package for Comma Separated Values (CSV) files
- */
-package org.eobjects.metamodel.csv;
-
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/csv/src/test/java/org/apache/metamodel/csv/CsvBigFileMemoryTest.java
----------------------------------------------------------------------
diff --git a/csv/src/test/java/org/apache/metamodel/csv/CsvBigFileMemoryTest.java b/csv/src/test/java/org/apache/metamodel/csv/CsvBigFileMemoryTest.java
new file mode 100644
index 0000000..ffc11ca
--- /dev/null
+++ b/csv/src/test/java/org/apache/metamodel/csv/CsvBigFileMemoryTest.java
@@ -0,0 +1,103 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.csv;
+
+import java.io.File;
+
+import org.eobjects.metamodel.DataContext;
+import org.eobjects.metamodel.csv.CsvConfiguration;
+import org.eobjects.metamodel.csv.CsvDataContext;
+import org.eobjects.metamodel.data.DataSet;
+import org.eobjects.metamodel.query.Query;
+import org.eobjects.metamodel.query.SelectItem;
+import org.eobjects.metamodel.schema.Table;
+
+import junit.framework.TestCase;
+
+public class CsvBigFileMemoryTest extends TestCase {
+
+ private final int hugeFileRows = 3000;
+ private final int hugeFileCols = 2000;
+
+ private File getHugeFile() {
+ final File file = new File("target/huge_csv.csv");
+ if (!file.exists()) {
+
+ final ExampleDataGenerator exampleDataGenerator = new ExampleDataGenerator(
+ hugeFileRows, hugeFileCols);
+ exampleDataGenerator.createFile(file);
+ }
+ return file;
+ }
+
+ /**
+ * Runs a performance test based on the data created by the
+ * ExampleDataCreator utility.
+ *
+ * @see ExampleDataGenerator
+ * @throws Exception
+ */
+ public void testHugeFile() throws Exception {
+ final File file = getHugeFile();
+
+ final long timeAtStart = System.currentTimeMillis();
+ System.out.println("time at start: " + timeAtStart);
+
+ final DataContext dc = new CsvDataContext(file, new CsvConfiguration());
+ final Table t = dc.getDefaultSchema().getTables()[0];
+
+ final long timeAfterDataContext = System.currentTimeMillis();
+ System.out.println("time after DataContext: " + timeAfterDataContext);
+
+ final Query q = new Query().select(t.getColumns()).from(t);
+ DataSet ds = dc.executeQuery(q);
+
+ long timeAfterQuery = System.currentTimeMillis();
+ System.out.println("time after query: " + timeAfterQuery);
+
+ while (ds.next()) {
+ assertEquals(hugeFileCols, ds.getRow().getValues().length);
+ }
+ ds.close();
+
+ long timeAfterDataSet = System.currentTimeMillis();
+ System.out.println("time after dataSet: " + timeAfterDataSet);
+
+ if (!file.delete()) {
+ file.deleteOnExit();
+ }
+ }
+
+ public void testApproximatedCountHugeFile() throws Exception {
+ DataContext dc = new CsvDataContext(getHugeFile());
+
+ Table table = dc.getDefaultSchema().getTables()[0];
+ Query q = dc.query().from(table).selectCount().toQuery();
+ SelectItem selectItem = q.getSelectClause().getItem(0);
+ selectItem.setFunctionApproximationAllowed(true);
+
+ DataSet ds = dc.executeQuery(q);
+ assertTrue(ds.next());
+ Object[] values = ds.getRow().getValues();
+ assertEquals(1, values.length);
+ assertEquals(3332, ((Long) ds.getRow().getValue(selectItem)).intValue());
+ assertEquals(3332, ((Long) values[0]).intValue());
+ assertFalse(ds.next());
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/csv/src/test/java/org/apache/metamodel/csv/CsvConfigurationTest.java
----------------------------------------------------------------------
diff --git a/csv/src/test/java/org/apache/metamodel/csv/CsvConfigurationTest.java b/csv/src/test/java/org/apache/metamodel/csv/CsvConfigurationTest.java
new file mode 100644
index 0000000..2948762
--- /dev/null
+++ b/csv/src/test/java/org/apache/metamodel/csv/CsvConfigurationTest.java
@@ -0,0 +1,46 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.csv;
+
+import junit.framework.TestCase;
+
+public class CsvConfigurationTest extends TestCase {
+
+ public void testToString() throws Exception {
+ CsvConfiguration conf = new CsvConfiguration(0, "UTF8", ',', '"', '\\',
+ true);
+ assertEquals(
+ "CsvConfiguration[columnNameLineNumber=0, encoding=UTF8, separatorChar=,, quoteChar=\", escapeChar=\\, failOnInconsistentRowLength=true]",
+ conf.toString());
+ }
+
+ public void testEquals() throws Exception {
+ CsvConfiguration conf1 = new CsvConfiguration(0, "UTF8", ',', '"',
+ '\\', true);
+ CsvConfiguration conf2 = new CsvConfiguration(0, "UTF8", ',', '"',
+ '\\', true);
+
+ assertEquals(conf1, conf2);
+
+ CsvConfiguration conf3 = new CsvConfiguration(1, "UTF8", ',', '"',
+ '\\', true);
+ assertFalse(conf1.equals(conf3));
+ }
+
+}
\ No newline at end of file