IODA Bundle
TablesReader.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 1996-2018 ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  * In applying this licence, ECMWF does not waive the privileges and immunities
7  * granted to it by virtue of its status as an intergovernmental organisation nor
8  * does it submit to any jurisdiction.
9  */
10 
11 #include "odc/core/TablesReader.h"
12 
13 #include "eckit/exception/Exceptions.h"
14 
15 using namespace eckit;
16 
17 namespace odc {
18 namespace core {
19 
20 //----------------------------------------------------------------------------------------------------------------------
21 
22 ReadTablesIterator::ReadTablesIterator(TablesReader& owner, long pos) :
23  owner_(owner),
24  pos_(pos) {
25 
26  // Ensure the first table is loaded
27  if (pos_ != -1) {
28  if (!owner_.ensureTable(0)) pos_ = -1;
29  }
30 }
31 
32 
34  return !(*this == other);
35 }
36 
38  return (&owner_ == &other.owner_) && (pos_ == other.pos_);
39 }
40 
42 
43  ++pos_;
44 
45  if (!owner_.ensureTable(pos_)) {
46  pos_ = -1;
47  }
48 
49  return *this;
50 }
51 
53 
54  auto copy = *this;
55  ++(*this);
56  return copy;
57 }
58 
60  ASSERT(pos_ != -1);
61  return &owner_.getTable(pos_);
62 }
63 
65  ASSERT(pos_ != -1);
66  return &owner_.getTable(pos_);
67 }
68 
70  ASSERT(pos_ != -1);
71  return owner_.getTable(pos_);
72 }
73 
75  ASSERT(pos_ != -1);
76  return owner_.getTable(pos_);
77 }
78 
79 //----------------------------------------------------------------------------------------------------------------------
80 
81 
82 TablesReader::TablesReader(DataHandle& dh) :
83  dh_(dh) {}
84 
85 
86 TablesReader::TablesReader(DataHandle* dh) :
87  dh_(dh) {}
88 
89 
90 TablesReader::TablesReader(const PathName& path) :
91  TablesReader(path.fileHandle()) {}
92 
93 
95  return ReadTablesIterator(*this);
96 }
97 
99  return ReadTablesIterator(*this, -1);
100 }
101 
103 
104  // We only read-ahead by a maximum of one table
105 
106  std::lock_guard<std::mutex> lock(m_);
107 
108  ASSERT(idx >= 0);
109  ASSERT(idx <= long(tables_.size()));
110 
111  if (idx == long(tables_.size())) {
112 
113  // n.b. Some DataHandles don't implement estimate() --> accept "0"
114  Offset nextPosition = (tables_.empty() ? Offset(0) : tables_.back()->nextPosition());
115  ASSERT(nextPosition <= dh_.estimate() || dh_.estimate() == Length(0));
116 
117  // If the table has been truncated, this is an error, and we cannot read on.
118  Offset pos = dh_.seek(nextPosition);
119  if (pos < nextPosition) {
120  throw ODBIncomplete(dh_.title(), Here());
121  }
122 
123  std::unique_ptr<Table> tbl(Table::readTable(dh_));
124  if (!tbl) return false;
125  tables_.emplace_back(std::move(tbl));
126  }
127 
128  return true;
129 }
130 
132 
133  ASSERT(idx >= 0);
134  ASSERT(idx < long(tables_.size()));
135 
136  return *tables_[idx];
137 }
138 
139 
140 //----------------------------------------------------------------------------------------------------------------------
141 
142 }
143 }
bool operator!=(const ReadTablesIterator &other)
Definition: TablesReader.cc:33
bool operator==(const ReadTablesIterator &other)
Definition: TablesReader.cc:37
ReadTablesIterator & operator++()
Definition: TablesReader.cc:41
static std::unique_ptr< Table > readTable(ThreadSharedDataHandle &dh)
Definition: Table.cc:386
Table & getTable(long idx)
std::vector< std::unique_ptr< Table > > tables_
Definition: TablesReader.h:104
ThreadSharedDataHandle dh_
Definition: TablesReader.h:106
bool ensureTable(long idx)
TablesReader(eckit::DataHandle &dh)
Definition: TablesReader.cc:82
friend class ReadTablesIterator
Definition: TablesReader.h:94
std::string title() const override
eckit::Offset seek(const eckit::Offset &) override
IODA_DL void copy(const ObjectSelection &from, ObjectSelection &to, const ScaleMapping &scale_map)
Generic data copying function.
Definition: Copying.cpp:63
Definition: ColumnInfo.h:23