LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.45
LSSTDataManagementBasePackage
BaseTable.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 #include <memory>
4 
5 #include "boost/shared_ptr.hpp" // only for ndarray
6 
10 #include "lsst/afw/table/Catalog.h"
14 
15 namespace lsst {
16 namespace afw {
17 namespace table {
18 
19 // =============== Block ====================================================================================
20 
21 // This is a block of memory that doles out record-sized chunks when a table asks for them.
22 // It inherits from ndarray::Manager so we can return ndarrays that refer to the memory in the
23 // block with correct reference counting (ndarray::Manager is just an empty base class with an
24 // internal reference count - it's like a shared_ptr without the pointer and template parameter.
25 //
26 // Records are allocated in Blocks for two reasons:
27 // - it allows tables to be either totally contiguous in memory (enabling column views) or
28 // not (enabling dynamic addition of records) all in one class.
29 // - it saves us from ever having to reallocate all the records associated with a table
30 // when we run out of space (that's what a std::vector-like model would require). This keeps
31 // records and/or iterators to them from being invalidated, and it keeps tables from having
32 // to track all the records whose data it owns.
33 
34 namespace {
35 
36 class Block : public ndarray::Manager {
37 public:
38  typedef boost::intrusive_ptr<Block> Ptr;
39 
40  // If the last chunk allocated isn't needed after all (usually because of an exception in a constructor)
41  // we reuse it immediately. If it wasn't the last chunk allocated, it can't be reclaimed until
42  // the entire block goes out of scope.
43  static void reclaim(std::size_t recordSize, void *data, ndarray::Manager::Ptr const &manager) {
44  Ptr block = boost::static_pointer_cast<Block>(manager);
45  if (reinterpret_cast<char *>(data) + recordSize == block->_next) {
46  block->_next -= recordSize;
47  }
48  }
49 
50  // Ensure we have space for at least the given number of records as a contiguous block.
51  // May not actually allocate anything if we already do.
52  static void preallocate(std::size_t recordSize, std::size_t recordCount, ndarray::Manager::Ptr &manager) {
53  Ptr block = boost::static_pointer_cast<Block>(manager);
54  if (!block || static_cast<std::size_t>(block->_end - block->_next) < recordSize * recordCount) {
55  block = Ptr(new Block(recordSize, recordCount));
56  manager = block;
57  }
58  }
59 
60  static std::size_t getBufferSize(std::size_t recordSize, ndarray::Manager::Ptr const &manager) {
61  Ptr block = boost::static_pointer_cast<Block>(manager);
62  return static_cast<std::size_t>(block->_end - block->_next) / recordSize;
63  }
64 
65  // Get the next chunk from the block, making a new block and installing it into the table
66  // if we're all out of space.
67  static void *get(std::size_t recordSize, ndarray::Manager::Ptr &manager) {
68  Ptr block = boost::static_pointer_cast<Block>(manager);
69  if (!block || block->_next == block->_end) {
70  block = Ptr(new Block(recordSize, BaseTable::nRecordsPerBlock));
71  manager = block;
72  }
73  void *r = block->_next;
74  block->_next += recordSize;
75  return r;
76  }
77 
78  // Block is also keeper of the special number that says what alignment boundaries are needed for
79  // schemas. Before we start using a schema, we need to first ensure it meets that requirement,
80  // and pad it if not.
81  static void padSchema(Schema &schema) {
82  static int const MIN_RECORD_ALIGN = sizeof(AllocType);
83  int remainder = schema.getRecordSize() % MIN_RECORD_ALIGN;
84  if (remainder) {
85  detail::Access::padSchema(schema, MIN_RECORD_ALIGN - remainder);
86  }
87  }
88 
89 private:
90  struct AllocType {
91  double element[2];
92  };
93 
94  explicit Block(std::size_t recordSize, std::size_t recordCount)
95  : _mem(new AllocType[(recordSize * recordCount) / sizeof(AllocType)]),
96  _next(reinterpret_cast<char *>(_mem.get())),
97  _end(_next + recordSize * recordCount) {
98  assert((recordSize * recordCount) % sizeof(AllocType) == 0);
99  std::fill(_next, _end, 0); // initialize to zero; we'll later initialize floats to NaN.
100  }
101 
103  char *_next;
104  char *_end;
105 };
106 
107 } // namespace
108 
109 // =============== BaseTable implementation (see header for docs) ===========================================
110 
111 void BaseTable::preallocate(std::size_t n) { Block::preallocate(_schema.getRecordSize(), n, _manager); }
112 
114  if (_manager) {
115  return Block::getBufferSize(_schema.getRecordSize(), _manager);
116  } else {
117  return 0;
118  }
119 }
120 
123 }
124 
127  output->assign(input);
128  return output;
129 }
130 
133  output->assign(input, mapper);
134  return output;
135 }
136 
137 std::shared_ptr<io::FitsWriter> BaseTable::makeFitsWriter(fits::Fits *fitsfile, int flags) const {
138  return std::make_shared<io::FitsWriter>(fitsfile, flags);
139 }
140 
142  return std::shared_ptr<BaseTable>(new BaseTable(*this));
143 }
144 
146  return constructRecord<BaseRecord>();
147 }
148 
150  Block::padSchema(_schema);
151  _schema.disconnectAliases();
152  _schema.getAliasMap()->_table = this;
153 }
154 
155 BaseTable::~BaseTable() { _schema.getAliasMap()->_table = 0; }
156 
157 namespace {
158 
159 // A Schema Functor used to set destroy variable-length array fields using an explicit call to their
160 // destructor (necessary since we used placement new). All other fields are ignored, as they're POD.
161 struct RecordDestroyer {
162  template <typename T>
163  void operator()(SchemaItem<T> const &item) const {}
164 
165  template <typename T>
166  void operator()(SchemaItem<Array<T> > const &item) const {
167  typedef ndarray::Array<T, 1, 1> Element;
168  if (item.key.isVariableLength()) {
169  (*reinterpret_cast<Element *>(data + item.key.getOffset())).~Element();
170  }
171  }
172 
173  void operator()(SchemaItem<std::string> const &item) const {
174  if (item.key.isVariableLength()) {
175  using std::string; // invoking the destructor on a qualified name doesn't compile in gcc 4.8.1
176  // https://stackoverflow.com/q/24593942
177  (*reinterpret_cast<string *>(data + item.key.getOffset())).~string();
178  }
179  }
180 
181  char *data;
182 };
183 
184 } // namespace
185 
186 detail::RecordData BaseTable::_makeNewRecordData() {
187  auto data = Block::get(_schema.getRecordSize(), _manager);
188  return detail::RecordData{
189  data,
191  _manager // manager always points to the most recently-used block.
192  };
193 }
194 
195 void BaseTable::_destroy(BaseRecord &record) {
196  assert(record._table.get() == this);
197  RecordDestroyer f = {reinterpret_cast<char *>(record._data)};
198  _schema.forEach(f);
199  if (record._manager == _manager) Block::reclaim(_schema.getRecordSize(), record._data, _manager);
200 }
201 
202 /*
203  * JFB has no idea whether the default value below is sensible, or even whether
204  * it should be expressed ultimately as an approximate size in bytes rather than a
205  * number of records; the answer probably depends on both the typical size of
206  * records and the typical number of records.
207  */
209 
210 // =============== BaseCatalog instantiation =================================================================
211 
212 template class CatalogT<BaseRecord>;
213 template class CatalogT<BaseRecord const>;
214 } // namespace table
215 } // namespace afw
216 } // namespace lsst
schema
table::Schema schema
Definition: Amplifier.cc:115
SchemaMapper.h
std::string
STL class.
std::shared_ptr
STL class.
lsst::afw::fits::Fits
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:297
lsst::afw::table::Schema::getRecordSize
int getRecordSize() const
Return the raw size of a record in bytes.
Definition: Schema.h:148
lsst::afw
Definition: imageAlgorithm.dox:1
BaseColumnView.h
std::remainder
T remainder(T... args)
lsst::afw::table::BaseTable::_clone
virtual std::shared_ptr< BaseTable > _clone() const
Clone implementation with noncovariant return types.
Definition: BaseTable.cc:141
lsst::afw::table::Schema
Defines the fields and offsets for a table.
Definition: Schema.h:50
lsst::afw::table::Schema::forEach
void forEach(F &&func) const
Apply a functor to each SchemaItem in the Schema.
Definition: Schema.h:212
std::fill
T fill(T... args)
BaseRecord.h
std::enable_shared_from_this< BaseTable >::shared_from_this
T shared_from_this(T... args)
lsst::afw::table::BaseTable::makeRecord
std::shared_ptr< BaseRecord > makeRecord()
Default-construct an associated record.
Definition: BaseTable.h:108
lsst::afw::table::detail::Access::padSchema
static void padSchema(Schema &schema, int bytes)
Definition: Access.h:88
data
char * data
Definition: BaseTable.cc:181
lsst::afw::table::BaseRecord
Base class for all records.
Definition: BaseRecord.h:31
lsst::afw::table::SchemaMapper
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:21
lsst::afw::table::SchemaItem
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:25
lsst::afw::table::BaseTable::nRecordsPerBlock
static int nRecordsPerBlock
Number of records in each memory block.
Definition: BaseTable.h:76
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
lsst::afw::table::BaseTable::preallocate
void preallocate(std::size_t nRecords)
Allocate contiguous space for new records in advance.
Definition: BaseTable.cc:111
lsst::afw::table::BaseTable::copyRecord
std::shared_ptr< BaseRecord > copyRecord(BaseRecord const &input)
Deep-copy a record, requiring that it have the same schema as this table.
Definition: BaseTable.cc:125
mapper
SchemaMapper * mapper
Definition: SchemaMapper.cc:78
BaseTable.h
std::size_t
lsst::afw::table::Schema::disconnectAliases
void disconnectAliases()
Sever the connection between this schema and any others with which it shares aliases.
Definition: Schema.cc:729
lsst::afw::table::BaseTable::BaseTable
BaseTable(Schema const &schema)
Construct from a schema.
Definition: BaseTable.cc:149
FitsWriter.h
element
double element[2]
Definition: BaseTable.cc:91
lsst::afw::table::BaseTable::_makeRecord
virtual std::shared_ptr< BaseRecord > _makeRecord()
Default-construct an associated record (protected implementation).
Definition: BaseTable.cc:145
Catalog.h
lsst::afw::table::BaseTable::make
static std::shared_ptr< BaseTable > make(Schema const &schema)
Construct a new table.
Definition: BaseTable.cc:121
lsst::afw::table::BaseTable::getBufferSize
std::size_t getBufferSize() const
Return the number of additional records space has been already been allocated for.
Definition: BaseTable.cc:113
std::unique_ptr
STL class.
lsst::afw::table::Schema::getAliasMap
std::shared_ptr< AliasMap > getAliasMap() const
Return the map of aliases.
Definition: Schema.h:269
lsst::afw::table::BaseTable::~BaseTable
virtual ~BaseTable()
Definition: BaseTable.cc:155
Access.h