Kea 3.3.3
versioned_csv_file.h
Go to the documentation of this file.
1// Copyright (C) 2015-2026 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef VERSIONED_CSV_FILE_H
8#define VERSIONED_CSV_FILE_H
9
10#include <util/csv_file.h>
11
12#include <cstdint>
13
14namespace isc {
15namespace util {
16
19public:
20 VersionedCSVFileError(const char* file, size_t line, const char* what) :
21 isc::Exception(file, line, what) { }
22};
23
26public:
34 VersionedColumn(const std::string& name, const std::string& version,
35 const std::string& default_value = "")
36 : name_(name), version_(version), default_value_(default_value) {
37 };
38
40 virtual ~VersionedColumn(){};
41
43 std::string name_;
44
47 std::string version_;
48
51 std::string default_value_;
52};
53
55typedef boost::shared_ptr<VersionedColumn> VersionedColumnPtr;
56
122class VersionedCSVFile : public CSVFile {
123public:
124
133
137 VersionedCSVFile(const std::string& filename);
138
140 virtual ~VersionedCSVFile();
141
156 void addColumn(const std::string& col_name, const std::string& version,
157 const std::string& default_value = "");
158
164 void setMinimumValidColumns(const std::string& column_name);
165
168 size_t getMinimumValidColumns() const;
169
171 size_t getInputHeaderCount() const;
172
180 size_t getValidColumnCount() const;
181
199 virtual void open(const bool seek_to_end = false);
200
210 virtual void recreate();
211
233 bool next(CSVRow& row);
234
239 std::string getInputSchemaVersion() const;
240
246 std::string getSchemaVersion() const;
247
253 const VersionedColumnPtr& getVersionedColumn(const size_t index) const;
254
264
266 bool needsConversion() const;
267
268protected:
269
283 virtual bool validateHeader(const CSVRow& header);
284
292 void columnCountError(const CSVRow& row, const std::string& reason);
293
294private:
296 std::vector<VersionedColumnPtr> columns_;
297
301 size_t valid_column_count_;
302
305 size_t minimum_valid_columns_;
306
310 size_t input_header_count_;
311
313 enum InputSchemaState input_schema_state_;
314};
315
316
317} // namespace isc::util
318} // namespace isc
319
320#endif // VERSIONED_CSV_FILE_H
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
CSVFile(const std::string &filename)
Constructor.
Definition csv_file.cc:115
Represents a single row of the CSV file.
Definition csv_file.h:59
VersionedCSVFileError(const char *file, size_t line, const char *what)
bool needsConversion() const
Returns true if the input file schema state is not CURRENT.
virtual void open(const bool seek_to_end=false)
Opens existing file or creates a new one.
virtual ~VersionedCSVFile()
Destructor.
const VersionedColumnPtr & getVersionedColumn(const size_t index) const
Fetch the column descriptor for a given index.
std::string getSchemaVersion() const
text version of current schema supported by the file's metadata
size_t getInputHeaderCount() const
Returns the number of columns found in the input header.
virtual bool validateHeader(const CSVRow &header)
Validates the header of a VersionedCSVFile.
VersionedCSVFile(const std::string &filename)
Constructor.
std::string getInputSchemaVersion() const
Returns the schema version of the physical file.
virtual void recreate()
Creates a new CSV file.
void setMinimumValidColumns(const std::string &column_name)
Sets the minimum number of valid columns based on a given column.
bool next(CSVRow &row)
Reads next row from the file file.
void addColumn(const std::string &col_name, const std::string &version, const std::string &default_value="")
Adds metadata for a single column to the schema.
size_t getMinimumValidColumns() const
Returns the minimum number of columns which must be present for the file to be considered valid.
void columnCountError(const CSVRow &row, const std::string &reason)
Convenience method for adding an error message.
InputSchemaState
Possible input file schema states.
size_t getValidColumnCount() const
Returns the number of valid columns found in the header For newly created files this will always matc...
enum InputSchemaState getInputSchemaState() const
Fetches the state of the input file's schema.
std::string default_value_
default_value The value the column should be assigned if it is not present in a data row.
virtual ~VersionedColumn()
Destructor.
std::string version_
Text representation of the schema version in which this column first appeared.
std::string name_
Name of the column.
VersionedColumn(const std::string &name, const std::string &version, const std::string &default_value="")
Constructor.
int version()
returns Kea hooks version.
boost::shared_ptr< VersionedColumn > VersionedColumnPtr
Defines a smart pointer to VersionedColumn.
Defines the logger used by the top-level component of kea-lfc.