lsst.afw  15.0-24-g02ed2a30c+2
OutputArchive.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 #include <typeinfo>
4 #include <vector>
5 #include <map>
6 
7 #include "boost/format.hpp"
8 
9 #include "lsst/pex/exceptions.h"
14 #include "lsst/afw/fits.h"
15 
16 namespace lsst {
17 namespace afw {
18 namespace table {
19 namespace io {
20 
21 namespace {
22 
23 ArchiveIndexSchema const &indexKeys = ArchiveIndexSchema::get();
24 
25 typedef std::map<void const *, int> Map;
26 
27 typedef Map::value_type MapItem;
28 
29 } // anonymous
30 
31 // ----- OutputArchive::Impl --------------------------------------------------------------------------------
32 
34 public:
36  int catArchive = 1;
37  CatalogVector::iterator iter = _catalogs.begin();
39  for (; iter != _catalogs.end(); ++iter, ++catArchive) {
40  if (iter->getSchema().compare(schema, flags) == flags) {
41  break;
42  }
43  }
44  if (iter == _catalogs.end()) {
45  iter = _catalogs.insert(_catalogs.end(), BaseCatalog(schema));
46  }
47  if (!iter->getTable()->getMetadata()) {
49  iter->getTable()->setMetadata(metadata);
50  metadata->set("EXTTYPE", "ARCHIVE_DATA");
51  metadata->set("AR_CATN", catArchive, "# of this catalog relative to the start of this archive");
52  }
53  return BaseCatalog(iter->getTable());
54  }
55 
57  int id, std::string const & name, std::string const & module
58  ) {
59  auto indexRecord = _index.addNew();
60  indexRecord->set(indexKeys.id, id);
61  indexRecord->set(indexKeys.name, name);
62  indexRecord->set(indexKeys.module, module);
63  return indexRecord;
64  }
65 
66  void saveEmpty(int id, std::string const & name, std::string const & module) {
67  auto indexRecord = addIndexRecord(id, name, module);
68  indexRecord->set(indexKeys.nRows, 0);
69  indexRecord->set(indexKeys.catPersistable, ArchiveIndexSchema::NO_CATALOGS_SAVED);
70  indexRecord->set(indexKeys.row0, ArchiveIndexSchema::NO_CATALOGS_SAVED);
71  indexRecord->set(indexKeys.catArchive, ArchiveIndexSchema::NO_CATALOGS_SAVED);
72  }
73 
74  void saveCatalog(BaseCatalog const &catalog, int id, std::string const &name, std::string const &module,
75  int catPersistable) {
76  auto indexRecord = addIndexRecord(id, name, module);
77  indexRecord->set(indexKeys.catPersistable, catPersistable);
78  indexRecord->set(indexKeys.nRows, catalog.size());
79  int catArchive = 1;
80  CatalogVector::iterator iter = _catalogs.begin();
81  for (; iter != _catalogs.end(); ++iter, ++catArchive) {
82  if (iter->getTable() == catalog.getTable()) {
83  break;
84  }
85  }
86  if (iter == _catalogs.end()) {
88  "All catalogs passed to saveCatalog must be created by makeCatalog");
89  }
90  // Add the name of the class to the header so anyone looking at it can
91  // tell what's stored there. But we don't want to add it multiple times.
92  try {
93  auto names = iter->getTable()->getMetadata()->getArray<std::string>("AR_NAME");
94  if (std::find(names.begin(), names.end(), name) == names.end()) {
95  iter->getTable()->getMetadata()->add("AR_NAME", name, "Class name for objects stored here");
96  }
97  } catch (pex::exceptions::NotFoundError &) {
98  iter->getTable()->getMetadata()->add("AR_NAME", name, "Class name for objects stored here");
99  }
100  indexRecord->set(indexKeys.row0, iter->size());
101  indexRecord->set(indexKeys.catArchive, catArchive);
102  iter->insert(iter->end(), catalog.begin(), catalog.end(), false);
103  }
104 
105  int put(Persistable const *obj, std::shared_ptr<Impl> const &self, bool permissive) {
106  if (!obj) return 0;
107  if (permissive && !obj->isPersistable()) return 0;
108  MapItem item(obj, _nextId);
109  std::pair<Map::iterator, bool> r = _map.insert(item);
110  if (r.second) {
111  ++_nextId;
112  OutputArchiveHandle handle(r.first->second, obj->getPersistenceName(), obj->getPythonModule(),
113  self);
114  obj->write(handle);
115  }
116  assert(r.first->first == obj);
117  // either way we return the ID of the object in the archive
118  return r.first->second;
119  }
120 
121  void writeFits(fits::Fits &fitsfile) {
122  _index.getTable()->getMetadata()->set("AR_NCAT", int(_catalogs.size() + 1),
123  "# of catalogs in this archive, including the index");
124  _index.writeFits(fitsfile);
125  int n = 1;
126  for (CatalogVector::const_iterator iter = _catalogs.begin(); iter != _catalogs.end(); ++iter, ++n) {
127  iter->writeFits(fitsfile);
128  }
129  }
130 
133  metadata->set("EXTTYPE", "ARCHIVE_INDEX");
134  metadata->set("AR_CATN", 0, "# of this catalog relative to the start of this archive");
135  _index.getTable()->setMetadata(metadata);
136  }
137 
138  int _nextId;
139  Map _map;
142 };
143 
144 // ----- OutputArchive --------------------------------------------------------------------------------------
145 
147 
148 OutputArchive::OutputArchive(OutputArchive const &other) : _impl(other._impl) {}
149 // Delegate to copy constructor for backward compatibility
151 
153  _impl = other._impl;
154  return *this;
155 }
156 // Delegate to copy assignment for backward compatibility
158 
160 
161 int OutputArchive::put(Persistable const *obj, bool permissive) {
162  if (!_impl.unique()) { // copy on write
163  std::shared_ptr<Impl> tmp(new Impl(*_impl));
164  _impl.swap(tmp);
165  }
166  return _impl->put(obj, _impl, permissive);
167 }
168 
169 BaseCatalog const &OutputArchive::getIndexCatalog() const { return _impl->_index; }
170 
172  if (n == 0) return _impl->_index;
173  if (std::size_t(n) > _impl->_catalogs.size() || n < 0) {
174  throw LSST_EXCEPT(
176  (boost::format("Catalog number %d is out of range [0,%d]") % n % _impl->_catalogs.size())
177  .str());
178  }
179  return _impl->_catalogs[n - 1];
180 }
181 
182 int OutputArchive::countCatalogs() const { return _impl->_catalogs.size() + 1; }
183 
184 void OutputArchive::writeFits(fits::Fits &fitsfile) const { _impl->writeFits(fitsfile); }
185 
186 // ----- OutputArchiveHandle ------------------------------------------------------------------------------
187 
188 BaseCatalog OutputArchiveHandle::makeCatalog(Schema const &schema) { return _impl->makeCatalog(schema); }
189 
191  _impl->saveEmpty(_id, _name, _module);
192 }
193 
195  _impl->saveCatalog(catalog, _id, _name, _module, _catPersistable);
196  ++_catPersistable;
197 }
198 
199 int OutputArchiveHandle::put(Persistable const *obj, bool permissive) {
200  // Handle doesn't worry about copy-on-write, because Handles should only exist
201  // while an OutputArchive::put() call is active.
202  return _impl->put(obj, _impl, permissive);
203 }
204 
207  : _id(id), _catPersistable(0), _name(name), _module(module), _impl(impl) {}
208 
210 }
211 }
212 }
213 } // namespace lsst::afw::table::io
Defines the fields and offsets for a table.
Definition: Schema.h:47
int countCatalogs() const
Return the total number of catalogs, including the index.
void saveEmpty(int id, std::string const &name, std::string const &module)
Schema for the index catalog that specifies where objects are stored in the data catalogs.
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...
Keys have the same types offsets, and sizes.
Definition: Schema.h:62
OutputArchive & operator=(OutputArchive const &other)
Assign from another OutputArchive. Saved objects are not deep-copied.
OutputArchive()
Construct an empty OutputArchive containing no objects.
OutputArchiveHandle(const OutputArchiveHandle &)=delete
CatalogT< BaseRecord > BaseCatalog
Definition: fwd.h:71
An object passed to Persistable::write to allow it to persist itself.
std::string const & _name
Definition: Mask.cc:576
afw::table::Schema schema
Definition: GaussianPsf.cc:43
std::shared_ptr< Table > getTable() const
Return the table associated with the catalog.
Definition: Catalog.h:114
void saveEmpty()
Indicate that the object being persisted has no state, and hence will never call makeCatalog() or sav...
T end(T... args)
virtual bool isPersistable() const
Return true if this particular object can be persisted using afw::table::io.
Definition: Persistable.h:102
ItemVariant const * other
Definition: Schema.cc:55
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:298
STL class.
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
Definition: Persistable.cc:38
BaseCatalog const & getIndexCatalog() const
Return the index catalog that specifies where objects are stored in the data catalogs.
STL class.
A base class for objects that can be persisted via afw::table::io Archive classes.
Definition: Persistable.h:74
A multi-catalog archive object used to save table::io::Persistable objects.
Definition: OutputArchive.h:34
static constexpr int const NO_CATALOGS_SAVED
Special value used for catArchive, catPersistable, and row0 when an object with no state is saved...
void writeFits(fits::Fits &fitsfile) const
Write the archive to an already-open FITS object.
void saveCatalog(BaseCatalog const &catalog, int id, std::string const &name, std::string const &module, int catPersistable)
Fields have the same names (ordered).
Definition: Schema.h:63
A base class for image defects.
virtual std::string getPythonModule() const
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
Definition: Persistable.cc:36
BaseCatalog makeCatalog(Schema const &schema)
iterator end()
Iterator access.
Definition: Catalog.h:397
int _id
Definition: Mask.cc:577
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:306
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
T insert(T... args)
T find(T... args)
T size(T... args)
#define LSST_EXCEPT(type,...)
A vector of catalogs used by Persistable.
Definition: CatalogVector.h:29
T begin(T... args)
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...
int put(Persistable const *obj, std::shared_ptr< Impl > const &self, bool permissive)
void writeFits(fits::Fits &fitsfile)
std::shared_ptr< BaseRecord > addIndexRecord(int id, std::string const &name, std::string const &module)
size_type size() const
Return the number of elements in the catalog.
Definition: Catalog.h:408
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
Definition: Persistable.cc:34
iterator begin()
Iterator access.
Definition: Catalog.h:396
static ArchiveIndexSchema const & get()
Return the singleton instance.
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:472
BaseCatalog const & getCatalog(int n) const
Return the nth catalog. Catalog 0 is always the index catalog.