22 #ifndef __CSV_DATA_SOURCE_H__
23 #define __CSV_DATA_SOURCE_H__
25 #include "services/daal_memory.h"
26 #include "data_management/data_source/data_source.h"
27 #include "data_management/data/data_dictionary.h"
28 #include "data_management/data/numeric_table.h"
29 #include "data_management/data/homogen_numeric_table.h"
30 #include "data_management/data_source/internal/data_source_options.h"
34 namespace data_management
47 class CsvDataSourceOptions
53 allocateNumericTable = 1 << 0,
54 createDictionaryFromContext = 1 << 1,
58 static CsvDataSourceOptions::Value unite(
const CsvDataSourceOptions::Value &lhs,
59 const CsvDataSourceOptions::Value &rhs)
61 return internal::DataSourceOptionsImpl<Value>::unite(lhs, rhs);
64 CsvDataSourceOptions(Value flags = byDefault) :
67 DataSource::NumericTableAllocationFlag getNumericTableAllocationFlag()
const
69 return (_impl.getFlag(allocateNumericTable))
70 ? DataSource::doAllocateNumericTable
71 : DataSource::notAllocateNumericTable;
74 DataSource::DictionaryCreationFlag getDictionaryCreationFlag()
const
76 return (_impl.getFlag(createDictionaryFromContext))
77 ? DataSource::doDictionaryFromContext
78 : DataSource::notDictionaryFromContext;
81 bool getParseHeaderFlag()
const
83 return _impl.getFlag(parseHeader);
87 internal::DataSourceOptionsImpl<Value> _impl;
96 template<
typename FeatureManager,
typename SummaryStatisticsType = DAAL_SUMMARY_STATISTICS_TYPE>
97 class CsvDataSource :
public DataSourceTemplate<data_management::HomogenNumericTable<DAAL_DATA_TYPE>, SummaryStatisticsType>
100 typedef data_management::HomogenNumericTable<DAAL_DATA_TYPE> DefaultNumericTableType;
101 typedef DataSourceTemplate<DefaultNumericTableType, SummaryStatisticsType> super;
105 using super::_initialMaxRows;
116 CsvDataSource(DataSourceIface::NumericTableAllocationFlag doAllocateNumericTable = DataSource::notAllocateNumericTable,
117 DataSourceIface::DictionaryCreationFlag doCreateDictionaryFromContext = DataSource::notDictionaryFromContext,
118 size_t initialMaxRows = 10) :
119 super(doAllocateNumericTable,
120 doCreateDictionaryFromContext)
122 initialize(initialMaxRows);
130 CsvDataSource(
const CsvDataSourceOptions &options,
size_t initialMaxRows = 10) :
131 super(options.getNumericTableAllocationFlag(),
132 options.getDictionaryCreationFlag())
134 initialize(initialMaxRows);
135 _parseHeader = options.getParseHeaderFlag();
138 virtual ~CsvDataSource()
140 daal::services::daal_free(_rawLineBuffer);
146 FeatureManager &getFeatureManager()
148 return _featureManager;
151 size_t getNumericTableNumberOfColumns() DAAL_C11_OVERRIDE
153 return _featureManager.getNumericTableNumberOfColumns();
156 services::Status setDictionary(DataSourceDictionary *dict) DAAL_C11_OVERRIDE
158 services::Status s = DataSource::setDictionary(dict);
159 _featureManager.setFeatureDetailsFromDictionary(dict);
164 size_t loadDataBlock(NumericTable* nt) DAAL_C11_OVERRIDE
166 services::Status s = super::checkDictionary();
169 this->_status.add(services::throwIfPossible(s));
172 s = checkInputNumericTable(nt);
175 this->_status.add(services::throwIfPossible(s));
179 size_t maxRows = (_initialMaxRows > 0 ? _initialMaxRows : 10);
181 const size_t ncols = getNumericTableNumberOfColumns();
182 DataCollection tables;
183 for( ;; maxRows *= 2)
185 NumericTablePtr ntCurrent = HomogenNumericTable<DAAL_DATA_TYPE>::create(ncols, maxRows, NumericTableIface::doAllocate, &s);
188 this->_status.add(services::throwIfPossible(services::Status(services::ErrorNumericTableNotAllocated)));
191 tables.push_back(ntCurrent);
192 const size_t rows = loadDataBlock(maxRows, ntCurrent.get());
198 s = resetNumericTable(nt, nrows);
201 this->_status.add(services::throwIfPossible(s));
205 BlockDescriptor<DAAL_DATA_TYPE> blockCurrent, block;
207 for (
size_t i = 0; i < tables.size(); i++)
209 NumericTable *ntCurrent = (NumericTable*)(tables[i].
get());
210 size_t rows = ntCurrent->getNumberOfRows();
215 ntCurrent->getBlockOfRows(0, rows, readOnly, blockCurrent);
216 nt->getBlockOfRows(pos, rows, writeOnly, block);
218 services::daal_memcpy_s(block.getBlockPtr(), rows * ncols *
sizeof(DAAL_DATA_TYPE), blockCurrent.getBlockPtr(), rows * ncols *
sizeof(DAAL_DATA_TYPE));
220 ntCurrent->releaseBlockOfRows(blockCurrent);
221 nt->releaseBlockOfRows(block);
223 super::combineStatistics( ntCurrent, nt, pos == 0);
229 size_t loadDataBlock(
size_t maxRows, NumericTable* nt) DAAL_C11_OVERRIDE
231 size_t nLines = loadDataBlock(maxRows, 0, maxRows, nt);
232 nt->resize( nLines );
236 size_t loadDataBlock(
size_t maxRows,
size_t rowOffset,
size_t fullRows, NumericTable *nt) DAAL_C11_OVERRIDE
238 services::Status s = super::checkDictionary();
241 this->_status.add(services::throwIfPossible(s));
244 s = checkInputNumericTable(nt);
247 this->_status.add(services::throwIfPossible(s));
251 if (rowOffset + maxRows > fullRows)
253 this->_status.add(services::throwIfPossible(services::ErrorIncorrectDataRange));
257 s = resetNumericTable(nt, fullRows);
260 this->_status.add(services::throwIfPossible(s));
264 if (_parseHeader && !_firstRowRead)
268 _firstRowRead =
true;
272 for(; j < maxRows && !iseof() ; j++ )
275 if(!s || !_rawLineLength)
280 _featureManager.parseRowIn( _rawLineBuffer, _rawLineLength, this->_dict.get(), nt, rowOffset + j );
282 super::updateStatistics( j, nt, rowOffset );
284 _featureManager.finalize(this->_dict.get());
286 return rowOffset + j;
289 size_t loadDataBlock() DAAL_C11_OVERRIDE
291 return DataSource::loadDataBlock();
294 size_t loadDataBlock(
size_t maxRows) DAAL_C11_OVERRIDE
296 return DataSource::loadDataBlock(maxRows);
299 size_t loadDataBlock(
size_t maxRows,
size_t rowOffset,
size_t fullRows) DAAL_C11_OVERRIDE
301 return DataSource::loadDataBlock(maxRows, rowOffset, fullRows);
305 services::Status createDictionaryFromContext() DAAL_C11_OVERRIDE
311 return services::throwIfPossible(services::Status(services::ErrorDictionaryAlreadyAvailable));
314 _dict = DataSourceDictionary::create(&s);
315 if (!s) {
return s; }
316 _contextDictFlag =
true;
321 if (!s) {
return services::throwIfPossible(s); }
322 _featureManager.parseRowAsHeader(_rawLineBuffer, _rawLineLength);
326 if (!s) {
return services::throwIfPossible(s); }
327 _featureManager.parseRowAsDictionary(_rawLineBuffer, _rawLineLength, this->_dict.get());
329 return services::Status();
332 size_t getNumberOfAvailableRows() DAAL_C11_OVERRIDE
338 virtual bool iseof()
const = 0;
339 virtual services::Status readLine() = 0;
341 virtual services::Status resetNumericTable(NumericTable *nt,
const size_t newSize)
345 NumericTableDictionaryPtr ntDict = nt->getDictionarySharedPtr();
346 const size_t nFeatures = getNumericTableNumberOfColumns();
347 ntDict->setNumberOfFeatures(nFeatures);
348 for (
size_t i = 0; i < nFeatures; i++)
349 ntDict->setFeature((*_dict)[i].ntFeature, i);
351 s = super::resizeNumericTableImpl(newSize, nt);
357 nt->setNormalizationFlag(NumericTable::nonNormalized);
358 return services::Status();
361 virtual services::Status checkInputNumericTable(
const NumericTable*
const nt)
const
365 return services::Status(services::ErrorNullInputNumericTable);
368 const NumericTable::StorageLayout layout = nt->getDataLayout();
369 if (layout == NumericTable::csrArray)
371 return services::Status(services::ErrorIncorrectTypeOfInputNumericTable);
374 return services::Status();
379 int newRawLineBufferLen = _rawLineBufferLen * 2;
380 char* newRawLineBuffer = (
char *)daal::services::daal_malloc( newRawLineBufferLen );
381 if(newRawLineBuffer == 0)
383 daal::services::daal_memcpy_s(newRawLineBuffer, newRawLineBufferLen, _rawLineBuffer, _rawLineBufferLen);
384 daal::services::daal_free( _rawLineBuffer );
385 _rawLineBuffer = newRawLineBuffer;
386 _rawLineBufferLen = newRawLineBufferLen;
391 services::Status initialize(
size_t initialMaxRows)
393 _parseHeader =
false;
394 _firstRowRead =
false;
395 _contextDictFlag =
false;
397 _initialMaxRows = initialMaxRows;
399 _rawLineBufferLen = (int)INITIAL_LINE_BUFFER_LENGTH;
400 _rawLineBuffer = (
char *)daal::services::daal_malloc(_rawLineBufferLen);
401 if (!_rawLineBuffer) {
return services::throwIfPossible(services::ErrorMemoryAllocationFailed); }
403 return services::Status();
407 char *_rawLineBuffer;
408 int _rawLineBufferLen;
414 bool _contextDictFlag;
415 FeatureManager _featureManager;
417 static const size_t INITIAL_LINE_BUFFER_LENGTH = 1024;
423 using interface1::CsvDataSource;
424 using interface1::CsvDataSourceOptions;
426 inline CsvDataSourceOptions::Value operator |(
const CsvDataSourceOptions::Value &lhs,
427 const CsvDataSourceOptions::Value &rhs)
428 {
return CsvDataSourceOptions::unite(lhs, rhs); }
daal::data_management::interface1::CsvDataSource::loadDataBlock
size_t loadDataBlock(size_t maxRows, size_t rowOffset, size_t fullRows, NumericTable *nt) DAAL_C11_OVERRIDE
Definition: csv_data_source.h:236
daal::data_management::interface1::DataSource::checkDictionary
services::Status checkDictionary()
Definition: data_source.h:359
daal::services::ErrorDictionaryAlreadyAvailable
Definition: error_indexes.h:151
daal::services::ErrorNullInputNumericTable
Definition: error_indexes.h:81
daal::data_management::interface1::BlockDescriptor::getBlockPtr
DataType * getBlockPtr() const
Definition: numeric_table.h:69
daal::data_management::interface1::CsvDataSource::setDictionary
services::Status setDictionary(DataSourceDictionary *dict) DAAL_C11_OVERRIDE
Definition: csv_data_source.h:156
daal
Definition: algorithm_base_common.h:31
daal::services::ErrorNumericTableNotAllocated
Definition: error_indexes.h:155
daal::data_management::interface1::CsvDataSource::createDictionaryFromContext
services::Status createDictionaryFromContext() DAAL_C11_OVERRIDE
Definition: csv_data_source.h:305
daal::data_management::interface1::DataCollection
Class that provides functionality of Collection container for objects derived from SerializationIface...
Definition: data_collection.h:45
daal::data_management::interface1::CsvDataSource::CsvDataSource
CsvDataSource(const CsvDataSourceOptions &options, size_t initialMaxRows=10)
Definition: csv_data_source.h:130
daal::data_management::interface1::NumericTable::getDictionarySharedPtr
virtual NumericTableDictionaryPtr getDictionarySharedPtr() const DAAL_C11_OVERRIDE
Definition: numeric_table.h:632
daal::data_management::interface1::CsvDataSource::getNumberOfAvailableRows
size_t getNumberOfAvailableRows() DAAL_C11_OVERRIDE
Definition: csv_data_source.h:332
daal::data_management::interface1::DataSourceIface::DictionaryCreationFlag
DictionaryCreationFlag
Specifies whether a Data Dictionary is created from the context of a Data Source. ...
Definition: data_source.h:69
daal::services::ErrorMemoryAllocationFailed
Definition: error_indexes.h:146
daal::data_management::interface1::CsvDataSource::loadDataBlock
size_t loadDataBlock(NumericTable *nt) DAAL_C11_OVERRIDE
Definition: csv_data_source.h:164
daal::data_management::interface1::DataSourceIface::notAllocateNumericTable
Definition: data_source.h:81
daal::data_management::interface1::DataCollection::push_back
DataCollection & push_back(const SerializationIfacePtr &x)
daal::data_management::interface1::DataSourceIface::doAllocateNumericTable
Definition: data_source.h:82
daal::data_management::interface1::DataSourceTemplate
Implements the abstract DataSourceIface interface.
Definition: data_source.h:463
daal::services::daal_memcpy_s
DAAL_EXPORT void daal_memcpy_s(void *dest, size_t numberOfElements, const void *src, size_t count)
daal::data_management::interface1::NumericTable::setNormalizationFlag
NormalizationType setNormalizationFlag(NormalizationType flag)
Definition: numeric_table.h:736
daal::data_management::interface1::CsvDataSource
Specifies methods to access data stored in files.
Definition: csv_data_source.h:97
daal::data_management::interface1::NumericTableIface::nonNormalized
Definition: numeric_table.h:317
daal::services::daal_malloc
DAAL_EXPORT void * daal_malloc(size_t size, size_t alignment=DAAL_MALLOC_DEFAULT_ALIGNMENT)
daal::data_management::interface1::HomogenNumericTable::create
static services::SharedPtr< HomogenNumericTable< DataType > > create(NumericTableDictionaryPtr ddictForHomogenNumericTable, services::Status *stat=NULL)
Definition: homogen_numeric_table.h:93
daal::data_management::interface1::NumericTable
Class for a data management component responsible for representation of data in the numeric format...
Definition: numeric_table.h:574
daal::data_management::interface1::NumericTableIface::StorageLayout
StorageLayout
Storage layouts that may need to be supported.
Definition: numeric_table.h:326
daal::data_management::interface1::DataSource::loadDataBlock
size_t loadDataBlock() DAAL_C11_OVERRIDE
Definition: data_source.h:272
daal::data_management::interface1::CsvDataSource::getFeatureManager
FeatureManager & getFeatureManager()
Definition: csv_data_source.h:146
daal::data_management::interface1::DataSourceIface::doDictionaryFromContext
Definition: data_source.h:72
daal::data_management::interface1::CsvDataSource::CsvDataSource
CsvDataSource(DataSourceIface::NumericTableAllocationFlag doAllocateNumericTable=DataSource::notAllocateNumericTable, DataSourceIface::DictionaryCreationFlag doCreateDictionaryFromContext=DataSource::notDictionaryFromContext, size_t initialMaxRows=10)
Definition: csv_data_source.h:116
daal::services::ErrorIncorrectTypeOfInputNumericTable
Definition: error_indexes.h:91
daal::data_management::interface1::CsvDataSource::loadDataBlock
size_t loadDataBlock() DAAL_C11_OVERRIDE
Definition: csv_data_source.h:289
daal::services::daal_free
DAAL_EXPORT void daal_free(void *ptr)
daal::services::ErrorIncorrectDataRange
Definition: error_indexes.h:77
daal::data_management::interface1::CsvDataSource::loadDataBlock
size_t loadDataBlock(size_t maxRows, size_t rowOffset, size_t fullRows) DAAL_C11_OVERRIDE
Definition: csv_data_source.h:299
daal::data_management::interface1::DataCollection::size
size_t size() const
daal::data_management::interface1::NumericTableIface::doAllocate
Definition: numeric_table.h:287
daal::data_management::interface1::CsvDataSource::loadDataBlock
size_t loadDataBlock(size_t maxRows) DAAL_C11_OVERRIDE
Definition: csv_data_source.h:294
daal::data_management::interface1::CsvDataSourceOptions
Options of CSV data source.
Definition: csv_data_source.h:47
daal::data_management::interface1::NumericTable::getNumberOfRows
size_t getNumberOfRows() const
Definition: numeric_table.h:660
daal::data_management::interface1::DataSourceIface::notDictionaryFromContext
Definition: data_source.h:71
daal::data_management::interface1::BlockDescriptor< DAAL_DATA_TYPE >
daal::data_management::interface1::CsvDataSource::loadDataBlock
size_t loadDataBlock(size_t maxRows, NumericTable *nt) DAAL_C11_OVERRIDE
Definition: csv_data_source.h:229
daal::data_management::interface1::DenseNumericTableIface::releaseBlockOfRows
virtual services::Status releaseBlockOfRows(BlockDescriptor< double > &block)=0
daal::data_management::interface1::DataSourceIface::NumericTableAllocationFlag
NumericTableAllocationFlag
Specifies whether a Numeric Table is allocated inside of the Data Source object.
Definition: data_source.h:79
daal::data_management::interface1::Dictionary::create
static services::SharedPtr< Dictionary > create(size_t nfeat, FeaturesEqual featuresEqual=notEqual, services::Status *stat=NULL)
Definition: data_dictionary.h:186
daal::data_management::interface1::DataSource::setDictionary
services::Status setDictionary(DataSourceDictionary *dict) DAAL_C11_OVERRIDE
Definition: data_source.h:217
daal::data_management::interface1::CsvDataSource::getNumericTableNumberOfColumns
size_t getNumericTableNumberOfColumns() DAAL_C11_OVERRIDE
Definition: csv_data_source.h:151
daal::data_management::interface1::DenseNumericTableIface::getBlockOfRows
virtual services::Status getBlockOfRows(size_t vector_idx, size_t vector_num, ReadWriteMode rwflag, BlockDescriptor< double > &block)=0
daal::data_management::internal::DataSourceOptionsImpl
Class that helps to define data source options.
Definition: data_source_options.h:31
daal::data_management::interface1::Dictionary
Class that represents a dictionary of a data set and provides methods to work with the data dictionar...
Definition: data_dictionary.h:161