lsst.afw  16.0-51-gb4b4a8566+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 } // namespace
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  auto indexRecord = _index.addNew();
58  indexRecord->set(indexKeys.id, id);
59  indexRecord->set(indexKeys.name, name);
60  indexRecord->set(indexKeys.module, module);
61  return indexRecord;
62  }
63 
64  void saveEmpty(int id, std::string const &name, std::string const &module) {
65  auto indexRecord = addIndexRecord(id, name, module);
66  indexRecord->set(indexKeys.nRows, 0);
67  indexRecord->set(indexKeys.catPersistable, ArchiveIndexSchema::NO_CATALOGS_SAVED);
68  indexRecord->set(indexKeys.row0, ArchiveIndexSchema::NO_CATALOGS_SAVED);
69  indexRecord->set(indexKeys.catArchive, ArchiveIndexSchema::NO_CATALOGS_SAVED);
70  }
71 
72  void saveCatalog(BaseCatalog const &catalog, int id, std::string const &name, std::string const &module,
73  int catPersistable) {
74  auto indexRecord = addIndexRecord(id, name, module);
75  indexRecord->set(indexKeys.catPersistable, catPersistable);
76  indexRecord->set(indexKeys.nRows, catalog.size());
77  int catArchive = 1;
78  CatalogVector::iterator iter = _catalogs.begin();
79  for (; iter != _catalogs.end(); ++iter, ++catArchive) {
80  if (iter->getTable() == catalog.getTable()) {
81  break;
82  }
83  }
84  if (iter == _catalogs.end()) {
86  "All catalogs passed to saveCatalog must be created by makeCatalog");
87  }
88  // Add the name of the class to the header so anyone looking at it can
89  // tell what's stored there. But we don't want to add it multiple times.
90  try {
91  auto names = iter->getTable()->getMetadata()->getArray<std::string>("AR_NAME");
92  if (std::find(names.begin(), names.end(), name) == names.end()) {
93  iter->getTable()->getMetadata()->add("AR_NAME", name, "Class name for objects stored here");
94  }
95  } catch (pex::exceptions::NotFoundError &) {
96  iter->getTable()->getMetadata()->add("AR_NAME", name, "Class name for objects stored here");
97  }
98  indexRecord->set(indexKeys.row0, iter->size());
99  indexRecord->set(indexKeys.catArchive, catArchive);
100  iter->insert(iter->end(), catalog.begin(), catalog.end(), false);
101  }
102 
103  int put(Persistable const *obj, std::shared_ptr<Impl> const &self, bool permissive) {
104  if (!obj) return 0;
105  if (permissive && !obj->isPersistable()) return 0;
106  MapItem item(obj, _nextId);
107  std::pair<Map::iterator, bool> r = _map.insert(item);
108  if (r.second) {
109  ++_nextId;
110  OutputArchiveHandle handle(r.first->second, obj->getPersistenceName(), obj->getPythonModule(),
111  self);
112  obj->write(handle);
113  }
114  assert(r.first->first == obj);
115  // either way we return the ID of the object in the archive
116  return r.first->second;
117  }
118 
119  void writeFits(fits::Fits &fitsfile) {
120  _index.getTable()->getMetadata()->set("AR_NCAT", int(_catalogs.size() + 1),
121  "# of catalogs in this archive, including the index");
122  _index.writeFits(fitsfile);
123  int n = 1;
124  for (CatalogVector::const_iterator iter = _catalogs.begin(); iter != _catalogs.end(); ++iter, ++n) {
125  iter->writeFits(fitsfile);
126  }
127  }
128 
131  metadata->set("EXTTYPE", "ARCHIVE_INDEX");
132  metadata->set("AR_CATN", 0, "# of this catalog relative to the start of this archive");
133  _index.getTable()->setMetadata(metadata);
134  }
135 
136  int _nextId;
137  Map _map;
140 };
141 
142 // ----- OutputArchive --------------------------------------------------------------------------------------
143 
145 
146 OutputArchive::OutputArchive(OutputArchive const &other) : _impl(other._impl) {}
147 // Delegate to copy constructor for backward compatibility
149 
151  _impl = other._impl;
152  return *this;
153 }
154 // Delegate to copy assignment for backward compatibility
156 
158 
159 int OutputArchive::put(Persistable const *obj, bool permissive) {
160  if (!_impl.unique()) { // copy on write
161  std::shared_ptr<Impl> tmp(new Impl(*_impl));
162  _impl.swap(tmp);
163  }
164  return _impl->put(obj, _impl, permissive);
165 }
166 
167 BaseCatalog const &OutputArchive::getIndexCatalog() const { return _impl->_index; }
168 
170  if (n == 0) return _impl->_index;
171  if (std::size_t(n) > _impl->_catalogs.size() || n < 0) {
172  throw LSST_EXCEPT(
174  (boost::format("Catalog number %d is out of range [0,%d]") % n % _impl->_catalogs.size())
175  .str());
176  }
177  return _impl->_catalogs[n - 1];
178 }
179 
180 int OutputArchive::countCatalogs() const { return _impl->_catalogs.size() + 1; }
181 
182 void OutputArchive::writeFits(fits::Fits &fitsfile) const { _impl->writeFits(fitsfile); }
183 
184 // ----- OutputArchiveHandle ------------------------------------------------------------------------------
185 
186 BaseCatalog OutputArchiveHandle::makeCatalog(Schema const &schema) { return _impl->makeCatalog(schema); }
187 
188 void OutputArchiveHandle::saveEmpty() { _impl->saveEmpty(_id, _name, _module); }
189 
191  _impl->saveCatalog(catalog, _id, _name, _module, _catPersistable);
192  ++_catPersistable;
193 }
194 
195 int OutputArchiveHandle::put(Persistable const *obj, bool permissive) {
196  // Handle doesn't worry about copy-on-write, because Handles should only exist
197  // while an OutputArchive::put() call is active.
198  return _impl->put(obj, _impl, permissive);
199 }
200 
203  : _id(id), _catPersistable(0), _name(name), _module(module), _impl(impl) {}
204 
206 } // namespace io
207 } // namespace table
208 } // namespace afw
209 } // namespace lsst
Defines the fields and offsets for a table.
Definition: Schema.h:50
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:65
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.
afw::table::Schema schema
Definition: GaussianPsf.cc:48
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)
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:296
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:66
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
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
virtual bool isPersistable() const noexcept
Return true if this particular object can be persisted using afw::table::io.
Definition: Persistable.h:102
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.