File indexing completed on 2024-04-06 11:56:18
0001 #ifndef GENERS_BINARYARCHIVEBASE_HH_
0002 #define GENERS_BINARYARCHIVEBASE_HH_
0003
0004 #include <fstream>
0005 #include <sstream>
0006
0007 #include "Alignment/Geners/interface/AbsArchive.hh"
0008 #include "Alignment/Geners/interface/AbsCatalog.hh"
0009 #include "Alignment/Geners/interface/CStringStream.hh"
0010
0011 namespace gs {
0012 class BinaryArchiveBase : public AbsArchive {
0013 public:
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059 BinaryArchiveBase(const char *name, const char *mode);
0060
0061 ~BinaryArchiveBase() override;
0062
0063 inline bool isOpen() const override { return modeIsValid_ && catalog_; }
0064
0065 inline bool isReadable() const override { return modeIsValid_ && catalog_ && (mode_ & std::ios_base::in); }
0066
0067 inline bool isWritable() const override { return modeIsValid_ && catalog_ && (mode_ & std::ios_base::out); }
0068
0069
0070 inline std::string error() const override { return errorStream_ ? errorStream_->str() : std::string(""); }
0071
0072
0073
0074
0075 inline bool modeValid() const { return modeIsValid_; }
0076
0077 inline unsigned long long size() const override { return catalog_ ? catalog_->size() : 0ULL; }
0078
0079 inline unsigned long long smallestId() const override { return catalog_ ? catalog_->smallestId() : 0ULL; }
0080
0081 inline unsigned long long largestId() const override { return catalog_ ? catalog_->largestId() : 0ULL; }
0082
0083 inline bool idsAreContiguous() const override { return catalog_ ? catalog_->isContiguous() : false; }
0084
0085 inline bool itemExists(const unsigned long long id) const override {
0086 return catalog_ ? catalog_->itemExists(id) : false;
0087 }
0088
0089 void itemSearch(const SearchSpecifier &namePattern,
0090 const SearchSpecifier &categoryPattern,
0091 std::vector<unsigned long long> *idsFound) const override;
0092
0093 inline std::shared_ptr<const CatalogEntry> catalogEntry(const unsigned long long id) override {
0094 return catalog_ ? catalog_->retrieveEntry(id)
0095 : std::shared_ptr<const CatalogEntry>((const CatalogEntry *)nullptr);
0096 }
0097
0098
0099 inline CStringStream::CompressionMode compressionMode() const { return cStream_->compressionMode(); }
0100
0101 inline std::size_t compressionBufferSize() const { return cStream_->bufferSize(); }
0102
0103 inline int compressionLevel() const { return cStream_->compressionLevel(); }
0104
0105 inline unsigned minSizeToCompress() const { return cStream_->minSizeToCompress(); }
0106
0107
0108
0109
0110
0111 inline bool injectMetadata() const { return addCatalogToData_; }
0112
0113
0114
0115 static bool isEmptyFile(std::fstream &s);
0116
0117
0118
0119 static std::ios_base::openmode parseMode(const char *mode);
0120
0121 protected:
0122 inline AbsCatalog *catalog() const { return catalog_; }
0123
0124
0125
0126
0127
0128
0129 void setCatalog(AbsCatalog *c);
0130
0131
0132
0133
0134 inline void setCompressionMode(const unsigned cMode) {
0135 cStream_->setCompressionMode(static_cast<CStringStream::CompressionMode>(cMode));
0136 }
0137
0138
0139
0140
0141 inline std::ostringstream &errorStream() {
0142 if (!errorStream_)
0143 errorStream_ = new std::ostringstream();
0144 return *errorStream_;
0145 }
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158 void openDataFile(std::fstream &stream, const char *filename);
0159
0160
0161 inline std::ios_base::openmode openmode() const { return mode_; }
0162
0163
0164
0165 const ClassId *catalogEntryClassId() const { return storedEntryId_; }
0166 const ClassId *itemLocationClassId() const { return storedLocationId_; }
0167
0168 public:
0169 BinaryArchiveBase() = delete;
0170 BinaryArchiveBase(const BinaryArchiveBase &) = delete;
0171 BinaryArchiveBase &operator=(const BinaryArchiveBase &) = delete;
0172
0173 private:
0174 static bool parseArchiveOptions(std::ostringstream &errmes,
0175 const char *mode,
0176 CStringStream::CompressionMode *m,
0177 int *compressionLevel,
0178 unsigned *minSizeToCompress,
0179 unsigned *bufSize,
0180 bool *multiplexCatalog);
0181
0182 void writeHeader(std::ostream &os);
0183
0184
0185
0186 bool readHeader(std::istream &is);
0187
0188 void search(AbsReference &reference) override;
0189
0190
0191 virtual std::ostream &plainOutputStream() = 0;
0192 virtual std::istream &plainInputStream(unsigned long long id,
0193 unsigned *compressionCode,
0194 unsigned long long *length) = 0;
0195
0196 std::istream &inputStream(unsigned long long id, long long *sz) override;
0197 std::ostream &outputStream() override;
0198 std::ostream &compressedStream(std::ostream &uncompressed) override;
0199 unsigned flushCompressedRecord(std::ostream &compressed) override;
0200 void releaseClassIds();
0201
0202 const std::ios_base::openmode mode_;
0203 std::ostringstream *errorStream_;
0204 CStringStream *cStream_;
0205 AbsCatalog *catalog_;
0206 ClassId *storedEntryId_;
0207 ClassId *storedLocationId_;
0208 bool catalogIsSet_;
0209 bool modeIsValid_;
0210 bool addCatalogToData_;
0211 };
0212 }
0213
0214 #endif