| Kea 3.0.2-git

versioned_csv_file.h
Go to the documentation of this file.
1// Copyright (C) 2015-2024 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 namespace isc {
13 namespace util {
14
16 class VersionedCSVFileError : public Exception {
17 public:
18 VersionedCSVFileError(const char* file, size_t line, const char* what) :
19 isc::Exception(file, line, what) { }
20};
21
23 class VersionedColumn {
24 public:
32 VersionedColumn(const std::string& name, const std::string& version,
33 const std::string& default_value = "")
34 : name_(name), version_(version), default_value_(default_value) {
35 };
36
38 virtual ~VersionedColumn(){};
39
41 std::string name_;
42
45 std::string version_;
46
49 std::string default_value_;
50};
51
53 typedef boost::shared_ptr<VersionedColumn> VersionedColumnPtr;
54
120 class VersionedCSVFile : public CSVFile {
121 public:
122
126 enum InputSchemaState {
127 CURRENT,
128 NEEDS_UPGRADE,
129 NEEDS_DOWNGRADE
130 };
131
135 VersionedCSVFile(const std::string& filename);
136
138 virtual ~VersionedCSVFile();
139
154 void addColumn(const std::string& col_name, const std::string& version,
155 const std::string& default_value = "");
156
162 void setMinimumValidColumns(const std::string& column_name);
163
166 size_t getMinimumValidColumns() const;
167
169 size_t getInputHeaderCount() const;
170
178 size_t getValidColumnCount() const;
179
197 virtual void open(const bool seek_to_end = false);
198
208 virtual void recreate();
209
230 bool next(CSVRow& row);
231
236 std::string getInputSchemaVersion() const;
237
243 std::string getSchemaVersion() const;
244
250 const VersionedColumnPtr& getVersionedColumn(const size_t index) const;
251
260 enum InputSchemaState getInputSchemaState() const;
261
263 bool needsConversion() const;
264
265 protected:
266
280 virtual bool validateHeader(const CSVRow& header);
281
289 void columnCountError(const CSVRow& row, const std::string& reason);
290
291 private:
293 std::vector<VersionedColumnPtr> columns_;
294
298 size_t valid_column_count_;
299
302 size_t minimum_valid_columns_;
303
307 size_t input_header_count_;
308
310 enum InputSchemaState input_schema_state_;
311};
312
313
314} // namespace isc::util
315} // namespace isc
316
317#endif // VERSIONED_CSV_FILE_H
isc::Exception::Exception
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
Definition exceptions/exceptions.cc:17
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::util::CSVFile::CSVFile
CSVFile(const std::string &filename)
Constructor.
Definition csv_file.cc:114
isc::util::CSVRow
Represents a single row of the CSV file.
Definition csv_file.h:51
isc::util::VersionedCSVFileError::VersionedCSVFileError
VersionedCSVFileError(const char *file, size_t line, const char *what)
Definition versioned_csv_file.h:18
isc::util::VersionedCSVFile::needsConversion
bool needsConversion() const
Returns true if the input file schema state is not CURRENT.
Definition versioned_csv_file.cc:90
isc::util::VersionedCSVFile::open
virtual void open(const bool seek_to_end=false)
Opens existing file or creates a new one.
Definition versioned_csv_file.cc:61
isc::util::VersionedCSVFile::~VersionedCSVFile
virtual ~VersionedCSVFile()
Destructor.
Definition versioned_csv_file.cc:20
isc::util::VersionedCSVFile::getVersionedColumn
const VersionedColumnPtr & getVersionedColumn(const size_t index) const
Fetch the column descriptor for a given index.
Definition versioned_csv_file.cc:113
isc::util::VersionedCSVFile::getSchemaVersion
std::string getSchemaVersion() const
text version of current schema supported by the file's metadata
Definition versioned_csv_file.cc:104
isc::util::VersionedCSVFile::InputSchemaState
InputSchemaState
Possible input file schema states.
Definition versioned_csv_file.h:126
isc::util::VersionedCSVFile::NEEDS_UPGRADE
@ NEEDS_UPGRADE
Definition versioned_csv_file.h:128
isc::util::VersionedCSVFile::NEEDS_DOWNGRADE
@ NEEDS_DOWNGRADE
Definition versioned_csv_file.h:129
isc::util::VersionedCSVFile::CURRENT
@ CURRENT
Definition versioned_csv_file.h:127
isc::util::VersionedCSVFile::getInputHeaderCount
size_t getInputHeaderCount() const
Returns the number of columns found in the input header.
Definition versioned_csv_file.cc:56
isc::util::VersionedCSVFile::validateHeader
virtual bool validateHeader(const CSVRow &header)
Validates the header of a VersionedCSVFile.
Definition versioned_csv_file.cc:192
isc::util::VersionedCSVFile::VersionedCSVFile
VersionedCSVFile(const std::string &filename)
Constructor.
Definition versioned_csv_file.cc:14
isc::util::VersionedCSVFile::getInputSchemaVersion
std::string getInputSchemaVersion() const
Returns the schema version of the physical file.
Definition versioned_csv_file.cc:95
isc::util::VersionedCSVFile::recreate
virtual void recreate()
Creates a new CSV file.
Definition versioned_csv_file.cc:72
isc::util::VersionedCSVFile::setMinimumValidColumns
void setMinimumValidColumns(const std::string &column_name)
Sets the minimum number of valid columns based on a given column.
Definition versioned_csv_file.cc:33
isc::util::VersionedCSVFile::next
bool next(CSVRow &row)
Reads next row from the file file.
Definition versioned_csv_file.cc:124
isc::util::VersionedCSVFile::addColumn
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.
Definition versioned_csv_file.cc:24
isc::util::VersionedCSVFile::getMinimumValidColumns
size_t getMinimumValidColumns() const
Returns the minimum number of columns which must be present for the file to be considered valid.
Definition versioned_csv_file.cc:46
isc::util::VersionedCSVFile::columnCountError
void columnCountError(const CSVRow &row, const std::string &reason)
Convenience method for adding an error message.
Definition versioned_csv_file.cc:182
isc::util::VersionedCSVFile::getValidColumnCount
size_t getValidColumnCount() const
Returns the number of valid columns found in the header For newly created files this will always matc...
Definition versioned_csv_file.cc:51
isc::util::VersionedCSVFile::getInputSchemaState
enum InputSchemaState getInputSchemaState() const
Fetches the state of the input file's schema.
Definition versioned_csv_file.cc:85
isc::util::VersionedColumn::default_value_
std::string default_value_
default_value The value the column should be assigned if it is not present in a data row.
Definition versioned_csv_file.h:49
isc::util::VersionedColumn::~VersionedColumn
virtual ~VersionedColumn()
Destructor.
Definition versioned_csv_file.h:38
isc::util::VersionedColumn::version_
std::string version_
Text representation of the schema version in which this column first appeared.
Definition versioned_csv_file.h:45
isc::util::VersionedColumn::name_
std::string name_
Name of the column.
Definition versioned_csv_file.h:41
isc::util::VersionedColumn::VersionedColumn
VersionedColumn(const std::string &name, const std::string &version, const std::string &default_value="")
Constructor.
Definition versioned_csv_file.h:32
csv_file.h
version
int version()
returns Kea hooks version.
Definition d2/gss_tsig/version.cc:13
isc::util::file
Definition filesystem.cc:28
isc::util
Definition time_utils.cc:43
isc::util::VersionedColumnPtr
boost::shared_ptr< VersionedColumn > VersionedColumnPtr
Defines a smart pointer to VersionedColumn.
Definition versioned_csv_file.h:53
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148