LSST Applications 27.0.0,g0265f82a02+469cd937ee,g02d81e74bb+21ad69e7e1,g1470d8bcf6+cbe83ee85a,g2079a07aa2+e67c6346a6,g212a7c68fe+04a9158687,g2305ad1205+94392ce272,g295015adf3+81dd352a9d,g2bbee38e9b+469cd937ee,g337abbeb29+469cd937ee,g3939d97d7f+72a9f7b576,g487adcacf7+71499e7cba,g50ff169b8f+5929b3527e,g52b1c1532d+a6fc98d2e7,g591dd9f2cf+df404f777f,g5a732f18d5+be83d3ecdb,g64a986408d+21ad69e7e1,g858d7b2824+21ad69e7e1,g8a8a8dda67+a6fc98d2e7,g99cad8db69+f62e5b0af5,g9ddcbc5298+d4bad12328,ga1e77700b3+9c366c4306,ga8c6da7877+71e4819109,gb0e22166c9+25ba2f69a1,gb6a65358fc+469cd937ee,gbb8dafda3b+69d3c0e320,gc07e1c2157+a98bf949bb,gc120e1dc64+615ec43309,gc28159a63d+469cd937ee,gcf0d15dbbd+72a9f7b576,gdaeeff99f8+a38ce5ea23,ge6526c86ff+3a7c1ac5f1,ge79ae78c31+469cd937ee,gee10cc3b42+a6fc98d2e7,gf1cff7945b+21ad69e7e1,gfbcc870c63+9a11dc8c8f
LSST Data Management Base Package
Loading...
Searching...
No Matches
OutputArchive.cc
Go to the documentation of this file.
1// -*- lsst-c++ -*-
2
3#include <typeinfo>
4#include <vector>
5#include <map>
6#include <memory>
7
8#include "boost/format.hpp"
9
10#include "lsst/pex/exceptions.h"
15#include "lsst/afw/fits.h"
16
17namespace lsst {
18namespace afw {
19namespace table {
20namespace io {
21
22namespace {
23
24ArchiveIndexSchema const &indexKeys = ArchiveIndexSchema::get();
25
26// we don't need sorting, but you can't use weak_ptrs as keys in an
27// unordered_map
30
31using MapItem = Map::value_type;
32
33} // namespace
34
35// ----- OutputArchive::Impl --------------------------------------------------------------------------------
36
38public:
40 int catArchive = 1;
41 CatalogVector::iterator iter = _catalogs.begin();
43 for (; iter != _catalogs.end(); ++iter, ++catArchive) {
44 if (iter->getSchema().compare(schema, flags) == flags) {
45 break;
46 }
47 }
48 if (iter == _catalogs.end()) {
49 iter = _catalogs.insert(_catalogs.end(), BaseCatalog(schema));
50 }
51 if (!iter->getTable()->getMetadata()) {
53 iter->getTable()->setMetadata(metadata);
54 metadata->set("EXTTYPE", "ARCHIVE_DATA");
55 metadata->set("AR_CATN", catArchive, "# of this catalog relative to the start of this archive");
56 }
57 return BaseCatalog(iter->getTable());
58 }
59
61 auto indexRecord = _index.addNew();
62 indexRecord->set(indexKeys.id, id);
63 indexRecord->set(indexKeys.name, name);
64 indexRecord->set(indexKeys.module, module);
65 return indexRecord;
66 }
67
68 void saveEmpty(int id, std::string const &name, std::string const &module) {
69 auto indexRecord = addIndexRecord(id, name, module);
70 indexRecord->set(indexKeys.nRows, 0);
74 }
75
76 void saveCatalog(BaseCatalog const &catalog, int id, std::string const &name, std::string const &module,
77 int catPersistable) {
78 auto indexRecord = addIndexRecord(id, name, module);
79 indexRecord->set(indexKeys.catPersistable, catPersistable);
80 indexRecord->set(indexKeys.nRows, catalog.size());
81 int catArchive = 1;
82 CatalogVector::iterator iter = _catalogs.begin();
83 for (; iter != _catalogs.end(); ++iter, ++catArchive) {
84 if (iter->getTable() == catalog.getTable()) {
85 break;
86 }
87 }
88 if (iter == _catalogs.end()) {
90 "All catalogs passed to saveCatalog must be created by makeCatalog");
91 }
92 // Add the name of the class to the header so anyone looking at it can
93 // tell what's stored there. But we don't want to add it multiple times.
94 try {
95 auto names = iter->getTable()->getMetadata()->getArray<std::string>("AR_NAME");
96 if (std::find(names.begin(), names.end(), name) == names.end()) {
97 iter->getTable()->getMetadata()->add("AR_NAME", name, "Class name for objects stored here");
98 }
100 iter->getTable()->getMetadata()->add("AR_NAME", name, "Class name for objects stored here");
101 }
102 // Also add an EXTNAME. The most recent AR_NAME given will be used.
103 iter->getTable()->getMetadata()->set("EXTNAME", name);
104 indexRecord->set(indexKeys.row0, iter->size());
105 indexRecord->set(indexKeys.catArchive, catArchive);
106 iter->insert(iter->end(), catalog.begin(), catalog.end(), false);
107 }
108
109 int put(Persistable const *obj, std::shared_ptr<Impl> const &self, bool permissive) {
110 if (!obj) return 0;
111 if (permissive && !obj->isPersistable()) return 0;
112 int const currentId = _nextId;
113 ++_nextId;
114 OutputArchiveHandle handle(currentId, obj->getPersistenceName(), obj->getPythonModule(), self);
115 obj->write(handle);
116 return currentId;
117 }
118
120 if (!obj) return 0;
121 if (permissive && !obj->isPersistable()) return 0;
122 MapItem item(obj, _nextId);
124 if (r.second) {
125 // We've never seen this object before. Save it.
126 return put(obj.get(), self, permissive);
127 } else {
128 // We had already saved this object, and insert returned an iterator
129 // to the ID we used before; return that.
130 return r.first->second;
131 }
132 }
133
135 _index.getTable()->getMetadata()->set("AR_NCAT", int(_catalogs.size() + 1),
136 "# of catalogs in this archive, including the index");
138 int n = 1;
139 for (CatalogVector::const_iterator iter = _catalogs.begin(); iter != _catalogs.end(); ++iter, ++n) {
140 iter->writeFits(fitsfile);
141 }
142 }
143
144 Impl() : _map(), _index(ArchiveIndexSchema::get().schema) {
146 metadata->set("EXTTYPE", "ARCHIVE_INDEX");
147 metadata->set("EXTNAME", "ARCHIVE_INDEX");
148 metadata->set("AR_CATN", 0, "# of this catalog relative to the start of this archive");
149 _index.getTable()->setMetadata(metadata);
150 }
151
152 int _nextId{1};
153 Map _map;
156};
157
158// ----- OutputArchive --------------------------------------------------------------------------------------
159
161
162OutputArchive::OutputArchive(OutputArchive const &other) = default;
163// Delegate to copy constructor for backward compatibility
165
167// Delegate to copy assignment for backward compatibility
168OutputArchive &OutputArchive::operator=(OutputArchive &&other) { return *this = other; }
169
171
173 if (!_impl.unique()) { // copy on write
174 std::shared_ptr<Impl> tmp(new Impl(*_impl));
175 _impl.swap(tmp);
176 }
177 return _impl->put(obj, _impl, permissive);
178}
179
181 if (!_impl.unique()) { // copy on write
182 std::shared_ptr<Impl> tmp(new Impl(*_impl));
183 _impl.swap(tmp);
184 }
185 return _impl->put(std::move(obj), _impl, permissive);
186}
187
188BaseCatalog const &OutputArchive::getIndexCatalog() const { return _impl->_index; }
189
191 if (n == 0) return _impl->_index;
192 if (std::size_t(n) > _impl->_catalogs.size() || n < 0) {
193 throw LSST_EXCEPT(
195 (boost::format("Catalog number %d is out of range [0,%d]") % n % _impl->_catalogs.size())
196 .str());
197 }
198 return _impl->_catalogs[n - 1];
199}
200
202
204
205// ----- OutputArchiveHandle ------------------------------------------------------------------------------
206
207BaseCatalog OutputArchiveHandle::makeCatalog(Schema const &schema) { return _impl->makeCatalog(schema); }
208
209void OutputArchiveHandle::saveEmpty() { _impl->saveEmpty(_id, _name, _module); }
210
212 _impl->saveCatalog(catalog, _id, _name, _module, _catPersistable);
213 ++_catPersistable;
214}
215
217 // Handle doesn't worry about copy-on-write, because Handles should only exist
218 // while an OutputArchive::put() call is active.
219 return _impl->put(obj, _impl, permissive);
220}
221
223 // Handle doesn't worry about copy-on-write, because Handles should only exist
224 // while an OutputArchive::put() call is active.
225 return _impl->put(std::move(obj), _impl, permissive);
226}
227
228OutputArchiveHandle::OutputArchiveHandle(int id, std::string const &name, std::string const &module,
230 : _id(id), _catPersistable(0), _name(name), _module(module), _impl(impl) {}
231
233} // namespace io
234} // namespace table
235} // namespace afw
236} // namespace lsst
table::Key< int > id
Definition Detector.cc:162
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition Exception.h:48
T begin(T... args)
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition fits.h:308
Tag types used to declare specialized field types.
Definition misc.h:31
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.
Definition Catalog.h:489
std::shared_ptr< Table > getTable() const
Return the table associated with the catalog.
Definition Catalog.h:114
void writeFits(std::string const &filename, std::string const &mode="w", int flags=0) const
Write a FITS binary table to a regular file.
Definition Catalog.h:310
Defines the fields and offsets for a table.
Definition Schema.h:51
@ EQUAL_NAMES
Fields have the same names (ordered).
Definition Schema.h:67
@ EQUAL_KEYS
Keys have the same types offsets, and sizes.
Definition Schema.h:66
A vector of catalogs used by Persistable.
std::shared_ptr< BaseRecord > addIndexRecord(int id, std::string const &name, std::string const &module)
int put(Persistable const *obj, std::shared_ptr< Impl > const &self, bool permissive)
BaseCatalog makeCatalog(Schema const &schema)
int put(std::shared_ptr< Persistable const > obj, std::shared_ptr< Impl > const &self, bool permissive)
void saveEmpty(int id, std::string const &name, std::string const &module)
void saveCatalog(BaseCatalog const &catalog, int id, std::string const &name, std::string const &module, int catPersistable)
An object passed to Persistable::write to allow it to persist itself.
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
OutputArchiveHandle(const OutputArchiveHandle &)=delete
void saveEmpty()
Indicate that the object being persisted has no state, and hence will never call makeCatalog() or sav...
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
int put(Persistable const *obj, bool permissive=false)
Save an object to the archive and return a unique ID that can be used to retrieve it from an InputArc...
A multi-catalog archive object used to save table::io::Persistable objects.
std::size_t countCatalogs() const
Return the total number of catalogs, including the index.
int put(std::shared_ptr< Persistable const > obj, bool permissive=false)
Save an object to the archive and return a unique ID that can be used to retrieve it from an InputArc...
void writeFits(fits::Fits &fitsfile) const
Write the archive to an already-open FITS object.
BaseCatalog const & getCatalog(int n) const
Return the nth catalog. Catalog 0 is always the index catalog.
OutputArchive & operator=(OutputArchive const &other)
Assign from another OutputArchive. Saved objects are not deep-copied.
BaseCatalog const & getIndexCatalog() const
Return the index catalog that specifies where objects are stored in the data catalogs.
OutputArchive()
Construct an empty OutputArchive containing no objects.
A base class for objects that can be persisted via afw::table::io Archive classes.
Definition Persistable.h:74
Class for storing ordered metadata with comments.
Reports attempts to exceed implementation-defined length limits for some classes.
Definition Runtime.h:76
Reports errors in the logical structure of the program.
Definition Runtime.h:46
Reports attempts to access elements using an invalid key.
Definition Runtime.h:151
T end(T... args)
T find(T... args)
T insert(T... args)
T move(T... args)
CatalogT< BaseRecord > BaseCatalog
Definition fwd.h:72
T size(T... args)
Schema for the index catalog that specifies where objects are stored in the data catalogs.
static constexpr int const NO_CATALOGS_SAVED
Special value used for catArchive, catPersistable, and row0 when an object with no state is saved.
static ArchiveIndexSchema const & get()
Return the singleton instance.
T swap(T... args)
T unique(T... args)