LSSTApplications  17.0+124,17.0+14,17.0+73,18.0.0+37,18.0.0+80,18.0.0-4-g68ffd23+4,18.1.0-1-g0001055+12,18.1.0-1-g03d53ef+5,18.1.0-1-g1349e88+55,18.1.0-1-g2505f39+44,18.1.0-1-g5315e5e+4,18.1.0-1-g5e4b7ea+14,18.1.0-1-g7e8fceb+4,18.1.0-1-g85f8cd4+48,18.1.0-1-g8ff0b9f+4,18.1.0-1-ga2c679d+1,18.1.0-1-gd55f500+35,18.1.0-10-gb58edde+2,18.1.0-11-g0997b02+4,18.1.0-13-gfe4edf0b+12,18.1.0-14-g259bd21+21,18.1.0-19-gdb69f3f+2,18.1.0-2-g5f9922c+24,18.1.0-2-gd3b74e5+11,18.1.0-2-gfbf3545+32,18.1.0-26-g728bddb4+5,18.1.0-27-g6ff7ca9+2,18.1.0-3-g52aa583+25,18.1.0-3-g8ea57af+9,18.1.0-3-gb69f684+42,18.1.0-3-gfcaddf3+6,18.1.0-32-gd8786685a,18.1.0-4-gf3f9b77+6,18.1.0-5-g1dd662b+2,18.1.0-5-g6dbcb01+41,18.1.0-6-gae77429+3,18.1.0-7-g9d75d83+9,18.1.0-7-gae09a6d+30,18.1.0-9-gc381ef5+4,w.2019.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 
122  return std::shared_ptr<BaseTable>(new BaseTable(schema));
123 }
124 
126  std::shared_ptr<BaseRecord> output = makeRecord();
127  output->assign(input);
128  return output;
129 }
130 
132  std::shared_ptr<BaseRecord> output = makeRecord();
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 
149 BaseTable::BaseTable(Schema const &schema) : _schema(schema) {
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
Defines the fields and offsets for a table.
Definition: Schema.h:50
char * data
Definition: BaseTable.cc:181
static void padSchema(Schema &schema, int bytes)
Definition: Access.h:88
int getOffset() const noexcept
Return the offset (in bytes) of this field within a record.
Definition: Key.h:87
bool isVariableLength() const noexcept
Return true if the field is variable-length (each record can have a different size array)...
Definition: FieldBase.h:268
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:21
void disconnectAliases()
Sever the connection between this schema and any others with which it shares aliases.
Definition: Schema.cc:729
virtual std::shared_ptr< BaseRecord > _makeRecord()
Default-construct an associated record (protected implementation).
Definition: BaseTable.cc:145
SchemaMapper * mapper
Definition: SchemaMapper.cc:78
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:297
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
STL class.
double element[2]
Definition: BaseTable.cc:91
void preallocate(std::size_t nRecords)
Allocate contiguous space for new records in advance.
Definition: BaseTable.cc:111
T remainder(T... args)
static int nRecordsPerBlock
Number of records in each memory block.
Definition: BaseTable.h:76
A base class for image defects.
table::Schema schema
Definition: Amplifier.cc:115
Tag types used to declare specialized field types.
Definition: misc.h:32
BaseTable(Schema const &schema)
Construct from a schema.
Definition: BaseTable.cc:149
std::shared_ptr< AliasMap > getAliasMap() const
Return the map of aliases.
Definition: Schema.h:269
STL class.
Base class for all records.
Definition: BaseRecord.h:31
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
std::size_t getBufferSize() const
Return the number of additional records space has been already been allocated for.
Definition: BaseTable.cc:113
T fill(T... args)
Base class for all tables.
Definition: BaseTable.h:61
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:25