8#include "boost/format.hpp"
28using Map = std::map<std::weak_ptr<Persistable const>, int,
29 std::owner_less<std::weak_ptr<Persistable const>>>;
31using MapItem = Map::value_type;
41 CatalogVector::iterator iter =
_catalogs.begin();
43 for (; iter !=
_catalogs.end(); ++iter, ++catArchive) {
44 if (iter->getSchema().compare(schema, flags) == flags) {
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");
61 auto indexRecord =
_index.addNew();
62 indexRecord->set(indexKeys.id,
id);
63 indexRecord->set(indexKeys.name, name);
64 indexRecord->set(indexKeys.module, module);
70 indexRecord->set(indexKeys.nRows, 0);
79 indexRecord->set(indexKeys.catPersistable, catPersistable);
80 indexRecord->set(indexKeys.nRows, catalog.
size());
82 CatalogVector::iterator iter =
_catalogs.begin();
83 for (; iter !=
_catalogs.end(); ++iter, ++catArchive) {
84 if (iter->getTable() == catalog.
getTable()) {
90 "All catalogs passed to saveCatalog must be created by makeCatalog");
92 auto metadata = iter->getTable()->getMetadata();
97 auto found_extname = metadata->exists(
"EXTNAME");
102 CatalogVector::iterator ver_iter =
_catalogs.begin();
103 for (; ver_iter !=
_catalogs.end(); ++ver_iter) {
104 auto cat_metadata = ver_iter->getTable()->getMetadata();
105 if (cat_metadata->exists(
"EXTNAME") && cat_metadata->getAsString(
"EXTNAME") == name) {
110 metadata->set(
"EXTVER", extver);
116 auto names = metadata->getArray<
std::string>(
"AR_NAME");
117 if (
std::find(names.begin(), names.end(), name) == names.end()) {
118 iter->getTable()->getMetadata()->add(
"AR_NAME", name,
"Class name for objects stored here");
121 metadata->add(
"AR_NAME", name,
"Class name for objects stored here");
124 metadata->set(
"EXTNAME", name);
125 indexRecord->set(indexKeys.row0, iter->size());
126 indexRecord->set(indexKeys.catArchive, catArchive);
127 iter->insert(iter->end(), catalog.
begin(), catalog.
end(),
false);
142 if (permissive && !obj->isPersistable())
return 0;
147 return put(obj.
get(), self, permissive);
151 return r.first->second;
156 _index.getTable()->getMetadata()->set(
"AR_NCAT",
int(
_catalogs.size() + 1),
157 "# of catalogs in this archive, including the index");
158 _index.writeFits(fitsfile);
160 for (CatalogVector::const_iterator iter =
_catalogs.begin(); iter !=
_catalogs.end(); ++iter, ++n) {
161 iter->writeFits(fitsfile);
167 metadata->set(
"EXTTYPE",
"ARCHIVE_INDEX");
168 metadata->set(
"EXTNAME",
"ARCHIVE_INDEX");
169 metadata->set(
"AR_CATN", 0,
"# of this catalog relative to the start of this archive");
170 _index.getTable()->setMetadata(metadata);
194 if (_impl.use_count() != 1) {
198 return _impl->
put(obj, _impl, permissive);
202 if (_impl.use_count() != 1) {
206 return _impl->put(
std::move(obj), _impl, permissive);
212 if (n == 0)
return _impl->_index;
213 if (
std::size_t(n) > _impl->_catalogs.size() || n < 0) {
216 (boost::format(
"Catalog number %d is out of range [0,%d]") % n % _impl->_catalogs.size())
219 return _impl->_catalogs[n - 1];
233 _impl->saveCatalog(catalog, _id, _name, _module, _catPersistable);
240 return _impl->put(obj, _impl, permissive);
246 return _impl->put(
std::move(obj), _impl, permissive);
251 : _id(id), _catPersistable(0), _name(name), _module(module), _impl(impl) {}
#define LSST_EXCEPT(type,...)
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
size_type size() const
Return the number of elements in the catalog.
iterator begin()
Iterator access.
std::shared_ptr< Table > getTable() const
Return the table associated with the catalog.
Defines the fields and offsets for a table.
@ EQUAL_NAMES
Fields have the same names (ordered).
@ EQUAL_KEYS
Keys have the same types offsets, and sizes.
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 writeFits(fits::Fits &fitsfile)
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)
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.
friend class OutputArchiveHandle
A base class for objects that can be persisted via afw::table::io Archive classes.
virtual bool isPersistable() const noexcept
Return true if this particular object can be persisted using afw::table::io.
virtual std::string getPythonModule() const
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
CatalogT< BaseRecord > BaseCatalog
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.