LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
LSST Data Management Base Package
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 
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
double element[2]
Definition: BaseTable.cc:91
char * data
Definition: BaseTable.cc:181
SchemaMapper * mapper
Definition: SchemaMapper.cc:78
table::Schema schema
Definition: python.h:134
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:297
Base class for all records.
Definition: BaseRecord.h:31
std::shared_ptr< BaseRecord > makeRecord()
Default-construct an associated record.
Definition: BaseTable.h:108
virtual std::shared_ptr< BaseRecord > _makeRecord()
Default-construct an associated record (protected implementation).
Definition: BaseTable.cc:145
virtual std::shared_ptr< io::FitsWriter > makeFitsWriter(fits::Fits *fitsfile, int flags) const
Definition: BaseTable.cc:137
void preallocate(std::size_t nRecords)
Allocate contiguous space for new records in advance.
Definition: BaseTable.cc:111
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
BaseTable(Schema const &schema)
Construct from a schema.
Definition: BaseTable.cc:149
std::size_t getBufferSize() const
Return the number of additional records space has been already been allocated for.
Definition: BaseTable.cc:113
static int nRecordsPerBlock
Number of records in each memory block.
Definition: BaseTable.h:76
static std::shared_ptr< BaseTable > make(Schema const &schema)
Construct a new table.
Definition: BaseTable.cc:121
virtual std::shared_ptr< BaseTable > _clone() const
Clone implementation with noncovariant return types.
Definition: BaseTable.cc:141
Defines the fields and offsets for a table.
Definition: Schema.h:50
void disconnectAliases()
Sever the connection between this schema and any others with which it shares aliases.
Definition: Schema.cc:729
void forEach(F &&func) const
Apply a functor to each SchemaItem in the Schema.
Definition: Schema.h:212
int getRecordSize() const
Return the raw size of a record in bytes.
Definition: Schema.h:148
std::shared_ptr< AliasMap > getAliasMap() const
Return the map of aliases.
Definition: Schema.h:269
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:21
static void padSchema(Schema &schema, int bytes)
Definition: Access.h:88
T fill(T... args)
A base class for image defects.
T remainder(T... args)
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:25