DP3
SourceDB.h
Go to the documentation of this file.
1 // SourceDB.h: Base class for a table holding sources and their parameters
2 //
3 // Copyright (C) 2020 ASTRON (Netherlands Institute for Radio Astronomy)
4 // SPDX-License-Identifier: GPL-3.0-or-later
5 
9 
10 #ifndef LOFAR_PARMDB_SOURCEDB_H
11 #define LOFAR_PARMDB_SOURCEDB_H
12 
13 #include "SourceData.h"
14 #include "PatchInfo.h"
15 #include "ParmDBMeta.h"
16 #include "ParmDB.h"
17 
18 #include <filesystem>
19 
20 namespace dp3 {
21 namespace parmdb {
22 
23 class ParmMap;
24 
27 
29 class SourceDBRep {
30  public:
32  SourceDBRep(const ParmDBMeta& ptm, bool forceNew);
33 
34  virtual ~SourceDBRep();
35 
37  void link() { itsCount++; }
38 
40  int unlink() { return --itsCount; }
41 
47  virtual void lock(bool lockForWrite);
48  virtual void unlock();
50 
52  ParmDB& getParmDB() { return itsParmDB; }
53 
56  virtual void checkDuplicates() = 0;
57 
59  virtual std::vector<std::string> findDuplicatePatches() = 0;
60 
62  virtual std::vector<std::string> findDuplicateSources() = 0;
63 
65  virtual bool patchExists(const std::string& patchName) = 0;
66 
68  virtual bool sourceExists(const std::string& sourceName) = 0;
69 
74  virtual unsigned int addPatch(const std::string& patchName, int catType,
75  double apparentBrightness, double ra,
76  double dec, bool check) = 0;
77 
79  virtual void updatePatch(unsigned int patchId, double apparentBrightness,
80  double ra, double dec) = 0;
81 
90  virtual void addSource(const SourceInfo& sourceInfo,
91  const std::string& patchName,
92  const ParmMap& defaultParameters, double ra,
93  double dec, bool check) = 0;
94  virtual void addSource(const SourceData& source, bool check) = 0;
96 
99  virtual void addSource(const SourceInfo& sourceInfo,
100  const std::string& patchName, int catType,
101  double apparentBrightness,
102  const ParmMap& defaultParameters, double ra,
103  double dec, bool check) = 0;
104 
108  virtual std::vector<std::string> getPatches(int category,
109  const std::string& pattern,
110  double minBrightness,
111  double maxBrightness) = 0;
112 
114  virtual std::vector<PatchInfo> getPatchInfo(int category,
115  const std::string& pattern,
116  double minBrightness,
117  double maxBrightness) = 0;
118 
120  virtual std::vector<SourceInfo> getPatchSources(
121  const std::string& patchName) = 0;
122 
124  virtual std::vector<SourceData> getPatchSourceData(
125  const std::string& patchName) = 0;
126 
128  virtual SourceInfo getSource(const std::string& sourceName) = 0;
129 
131  virtual std::vector<SourceInfo> getSources(const std::string& pattern) = 0;
132 
134  virtual void deleteSources(const std::string& sourceNamePattern) = 0;
135 
137  virtual void clearTables() = 0;
138 
140  const ParmDBMeta& getParmDBMeta() const { return itsParmDB.getParmDBMeta(); }
141 
144  virtual void getNextSource(SourceData& src) = 0;
145 
147  virtual bool atEnd() = 0;
148 
150  virtual void rewind() = 0;
151 
152  private:
153  int itsCount;
154  ParmDB itsParmDB;
155 };
156 
158  public:
159  virtual ~SourceDBBase() = default;
160 
169  virtual void addSource(const SourceInfo& source_info,
170  const std::string& patch_name, int cat_type,
171  double apparent_brightness,
172  const ParmMap& default_parameters, double ra,
173  double dec, bool check) = 0;
174 
175  virtual void addSource(const SourceInfo& source_info,
176  const std::string& patch_name,
177  const ParmMap& default_parameters, double ra,
178  double dec, bool check) = 0;
180 
184  virtual unsigned addPatch(const std::string& patch_name, int cat_type,
185  double apparent_brightness, double ra, double dec,
186  bool check) = 0;
187 
188  virtual void updatePatch(unsigned patch_id, double apparent_brightness,
189  double ra, double dec) = 0;
190 };
191 
193 class SourceDB final : public SourceDBBase {
194  public:
201  explicit SourceDB(const ParmDBMeta& ptm, bool mustExist, bool forceNew);
202 
205 
207  ~SourceDB() override;
208 
211 
216  void lock(bool lockForWrite = true) { itsRep->lock(lockForWrite); }
217  void unlock() { itsRep->unlock(); }
219 
221  ParmDB& getParmDB() { return itsRep->getParmDB(); }
222 
225  void checkDuplicates() const { itsRep->checkDuplicates(); }
226 
228  std::vector<std::string> findDuplicatePatches() const {
229  return itsRep->findDuplicatePatches();
230  }
231 
233  std::vector<std::string> findDuplicateSources() const {
234  return itsRep->findDuplicateSources();
235  }
236 
238  bool patchExists(const std::string& patchName) const {
239  return itsRep->patchExists(patchName);
240  }
241 
243  bool sourceExists(const std::string& sourceName) const {
244  return itsRep->sourceExists(sourceName);
245  }
246 
251  unsigned int addPatch(const std::string& patchName, int catType,
252  double apparentBrightness, double ra, double dec,
253  bool check) override {
254  return itsRep->addPatch(patchName, catType, apparentBrightness, ra, dec,
255  check);
256  }
257 
259  void updatePatch(unsigned int patchId, double apparentBrightness, double ra,
260  double dec) override {
261  itsRep->updatePatch(patchId, apparentBrightness, ra, dec);
262  }
263 
272  void addSource(const SourceInfo& sourceInfo, const std::string& patchName,
273  const ParmMap& defaultParameters, double ra, double dec,
274  bool check) override {
275  itsRep->addSource(sourceInfo, patchName, defaultParameters, ra, dec, check);
276  }
277  void addSource(const SourceData& source, bool check = true) {
278  itsRep->addSource(source, check);
279  }
281 
283  void addSource(const SourceInfo& sourceInfo, const std::string& patchName,
284  int catType, double apparentBrightness,
285  const ParmMap& defaultParameters, double ra, double dec,
286  bool check) override {
287  itsRep->addSource(sourceInfo, patchName, catType, apparentBrightness,
288  defaultParameters, ra, dec, check);
289  }
290 
294  std::vector<std::string> getPatches(
295  int category = -1, const std::string& pattern = std::string(),
296  double minBrightness = -1, double maxBrightness = -1) const {
297  return itsRep->getPatches(category, pattern, minBrightness, maxBrightness);
298  }
299 
301  std::vector<PatchInfo> getPatchInfo(
302  int category = -1, const std::string& pattern = std::string(),
303  double minBrightness = -1, double maxBrightness = -1) const {
304  return itsRep->getPatchInfo(category, pattern, minBrightness,
305  maxBrightness);
306  }
307 
309  std::vector<SourceInfo> getPatchSources(const std::string& patchName) const {
310  return itsRep->getPatchSources(patchName);
311  }
312 
314  std::vector<SourceData> getPatchSourceData(
315  const std::string& patchName) const {
316  return itsRep->getPatchSourceData(patchName);
317  }
318 
320  SourceInfo getSource(const std::string& sourceName) const {
321  return itsRep->getSource(sourceName);
322  }
323 
325  std::vector<SourceInfo> getSources(
326  const std::string& sourceNamePattern) const {
327  return itsRep->getSources(sourceNamePattern);
328  }
329 
331  void deleteSources(const std::string& sourceNamePattern) {
332  itsRep->deleteSources(sourceNamePattern);
333  }
334 
336  void clearTables() { itsRep->clearTables(); }
337 
339  const ParmDBMeta& getParmDBMeta() const { return itsRep->getParmDBMeta(); }
340 
343  void getNextSource(SourceData& src) { itsRep->getNextSource(src); }
344 
346  bool atEnd() { return itsRep->atEnd(); }
347 
349  void rewind() { itsRep->rewind(); }
350 
351  private:
354 
356  void decrCount();
357 
358  SourceDBRep* itsRep;
359 
361  std::filesystem::path itsSourceDBPath;
362 };
363 
365 
366 } // namespace parmdb
367 } // namespace dp3
368 
369 #endif
Meta information for the name and type of a ParmDB.
Base class for a table holding parameters.
Info about a patch.
Base class for a table holding sources and their parameters.
Meta information for the name and type of a ParmDB.
Definition: ParmDBMeta.h:27
Envelope class for a table holding parameters.
Definition: ParmDB.h:159
const ParmDBMeta & getParmDBMeta() const
Get the name and type of the ParmDB.
Definition: ParmDB.h:273
A map of parameter name to value set.
Definition: ParmMap.h:30
Definition: SourceDB.h:157
virtual void addSource(const SourceInfo &source_info, const std::string &patch_name, const ParmMap &default_parameters, double ra, double dec, bool check)=0
virtual ~SourceDBBase()=default
virtual void updatePatch(unsigned patch_id, double apparent_brightness, double ra, double dec)=0
virtual unsigned addPatch(const std::string &patch_name, int cat_type, double apparent_brightness, double ra, double dec, bool check)=0
virtual void addSource(const SourceInfo &source_info, const std::string &patch_name, int cat_type, double apparent_brightness, const ParmMap &default_parameters, double ra, double dec, bool check)=0
Abstract base class for a table holding source parameters.
Definition: SourceDB.h:29
void link()
Link to the DBRep by incrementing the count.
Definition: SourceDB.h:37
virtual void getNextSource(SourceData &src)=0
virtual void addSource(const SourceInfo &sourceInfo, const std::string &patchName, const ParmMap &defaultParameters, double ra, double dec, bool check)=0
virtual void rewind()=0
Reset to the beginning of the file.
virtual bool atEnd()=0
Tell if we are the end of the file.
virtual void checkDuplicates()=0
virtual void clearTables()=0
Clear database or table.
SourceDBRep(const ParmDBMeta &ptm, bool forceNew)
This creates the underlying ParmDB object.
virtual void updatePatch(unsigned int patchId, double apparentBrightness, double ra, double dec)=0
Update the ra/dec and apparent brightness of a patch.
ParmDB & getParmDB()
Get the associated ParmDB.
Definition: SourceDB.h:52
virtual std::vector< SourceData > getPatchSourceData(const std::string &patchName)=0
Get all data of the sources belonging to the given patch.
virtual std::vector< PatchInfo > getPatchInfo(int category, const std::string &pattern, double minBrightness, double maxBrightness)=0
Get the info of selected patches (default all patches).
const ParmDBMeta & getParmDBMeta() const
Get the name and type of the SourceDB.
Definition: SourceDB.h:140
virtual std::vector< std::string > findDuplicatePatches()=0
Find non-unique patch names.
virtual std::vector< std::string > findDuplicateSources()=0
Find non-unique source names.
virtual std::vector< std::string > getPatches(int category, const std::string &pattern, double minBrightness, double maxBrightness)=0
virtual bool patchExists(const std::string &patchName)=0
Test if the patch already exists.
virtual void addSource(const SourceInfo &sourceInfo, const std::string &patchName, int catType, double apparentBrightness, const ParmMap &defaultParameters, double ra, double dec, bool check)=0
virtual bool sourceExists(const std::string &sourceName)=0
Test if the source already exists.
virtual unsigned int addPatch(const std::string &patchName, int catType, double apparentBrightness, double ra, double dec, bool check)=0
virtual std::vector< SourceInfo > getSources(const std::string &pattern)=0
Get the info of all sources matching the given (filename like) pattern.
virtual void lock(bool lockForWrite)
virtual SourceInfo getSource(const std::string &sourceName)=0
Get the source type of the given source.
int unlink()
Unlink by decrementing the count.
Definition: SourceDB.h:40
virtual void deleteSources(const std::string &sourceNamePattern)=0
Delete the sources records matching the given (filename like) pattern.
virtual std::vector< SourceInfo > getPatchSources(const std::string &patchName)=0
Get the sources belonging to the given patch.
virtual void addSource(const SourceData &source, bool check)=0
Envelope class for a table holding source parameters.
Definition: SourceDB.h:193
SourceInfo getSource(const std::string &sourceName) const
Get the source info of the given source.
Definition: SourceDB.h:320
void lock(bool lockForWrite=true)
Definition: SourceDB.h:216
void checkDuplicates() const
Definition: SourceDB.h:225
SourceDB(const ParmDBMeta &ptm, bool mustExist, bool forceNew)
std::vector< SourceInfo > getPatchSources(const std::string &patchName) const
Get the info of the sources belonging to the given patch.
Definition: SourceDB.h:309
bool patchExists(const std::string &patchName) const
Test if the patch already exists.
Definition: SourceDB.h:238
void addSource(const SourceInfo &sourceInfo, const std::string &patchName, const ParmMap &defaultParameters, double ra, double dec, bool check) override
Definition: SourceDB.h:272
std::vector< std::string > getPatches(int category=-1, const std::string &pattern=std::string(), double minBrightness=-1, double maxBrightness=-1) const
Definition: SourceDB.h:294
void unlock()
Definition: SourceDB.h:217
ParmDB & getParmDB()
Get the associated ParmDB.
Definition: SourceDB.h:221
void rewind()
Reset to the beginning of the file.
Definition: SourceDB.h:349
bool sourceExists(const std::string &sourceName) const
Test if the source already exists.
Definition: SourceDB.h:243
~SourceDB() override
Delete underlying object if no more references to it.
std::vector< SourceInfo > getSources(const std::string &sourceNamePattern) const
Get the info of all sources matching the given (filename like) pattern.
Definition: SourceDB.h:325
SourceDB(const SourceDB &)
Copy contructor has reference semantics.
void getNextSource(SourceData &src)
Definition: SourceDB.h:343
std::vector< SourceData > getPatchSourceData(const std::string &patchName) const
Get all data of the sources belonging to the given patch.
Definition: SourceDB.h:314
SourceDB & operator=(const SourceDB &)
Assignment has reference semantics.
bool atEnd()
Tell if we are the end of the file.
Definition: SourceDB.h:346
unsigned int addPatch(const std::string &patchName, int catType, double apparentBrightness, double ra, double dec, bool check) override
Definition: SourceDB.h:251
std::vector< PatchInfo > getPatchInfo(int category=-1, const std::string &pattern=std::string(), double minBrightness=-1, double maxBrightness=-1) const
Get the info of all patches (name, ra, dec).
Definition: SourceDB.h:301
void addSource(const SourceInfo &sourceInfo, const std::string &patchName, int catType, double apparentBrightness, const ParmMap &defaultParameters, double ra, double dec, bool check) override
Add a source which forms a patch in itself (with the same name).
Definition: SourceDB.h:283
void deleteSources(const std::string &sourceNamePattern)
Delete the sources records matching the given (filename like) pattern.
Definition: SourceDB.h:331
std::vector< std::string > findDuplicateSources() const
Find non-unique source names.
Definition: SourceDB.h:233
void addSource(const SourceData &source, bool check=true)
Definition: SourceDB.h:277
void updatePatch(unsigned int patchId, double apparentBrightness, double ra, double dec) override
Update the ra/dec and apparent brightness of a patch.
Definition: SourceDB.h:259
void clearTables()
Clear database tables (i.e. remove all rows from all tables).
Definition: SourceDB.h:336
const ParmDBMeta & getParmDBMeta() const
Get the name and type of the SourceDB.
Definition: SourceDB.h:339
std::vector< std::string > findDuplicatePatches() const
Find non-unique patch names.
Definition: SourceDB.h:228
Class holding a data of a source.
Definition: SourceData.h:31
Info about a source.
Definition: SourceInfo.h:29
This file has generic helper routines for testing steps.
Definition: AntennaConfig.h:53