Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:34:30

0001 #ifndef GENERS_ABSARCHIVE_HH_
0002 #define GENERS_ABSARCHIVE_HH_
0003 
0004 #include <vector>
0005 
0006 #include "Alignment/Geners/interface/AbsRecord.hh"
0007 #include "Alignment/Geners/interface/AbsReference.hh"
0008 #include "Alignment/Geners/interface/CatalogEntry.hh"
0009 #include "Alignment/Geners/interface/SearchSpecifier.hh"
0010 
0011 #include <memory>
0012 
0013 namespace gs {
0014   class AbsArchive;
0015 }
0016 
0017 gs::AbsArchive &operator<<(gs::AbsArchive &ar, const gs::AbsRecord &record);
0018 
0019 namespace gs {
0020   //
0021   // If you need to retrieve the items, use the interface provided
0022   // by the "Reference" class. Public interface of this class only
0023   // allows to examine the item metadata and to copy items as BLOBs.
0024   //
0025   class AbsArchive {
0026   public:
0027     AbsArchive(const char *name);
0028     inline virtual ~AbsArchive() {}
0029 
0030     // Archive name
0031     const std::string &name() const { return name_; }
0032 
0033     // Is it correctly open?
0034     virtual bool isOpen() const = 0;
0035 
0036     // If an attempt to open the archive failed, call the following
0037     // method to find out why
0038     virtual std::string error() const = 0;
0039 
0040     // Is the archive readable?
0041     virtual bool isReadable() const = 0;
0042 
0043     // Is the archive writable?
0044     virtual bool isWritable() const = 0;
0045 
0046     // Number of items in the archive. Note that id value
0047     // of 0 refers to an invalid item.
0048     virtual unsigned long long size() const = 0;
0049 
0050     // Smallest and largest ids of any item in the archive
0051     virtual unsigned long long smallestId() const = 0;
0052     virtual unsigned long long largestId() const = 0;
0053 
0054     // Are the item ids contiguous between the smallest and
0055     // the largest?
0056     virtual bool idsAreContiguous() const = 0;
0057 
0058     // Check if the item with given id is actually present
0059     // in the archive
0060     virtual bool itemExists(unsigned long long id) const = 0;
0061 
0062     // Search for matching items based on item name and
0063     // category (no type match required)
0064     virtual void itemSearch(const SearchSpecifier &namePattern,
0065                             const SearchSpecifier &categoryPattern,
0066                             std::vector<unsigned long long> *found) const = 0;
0067 
0068     // Fetch metadata for the item with given id. NULL pointer is
0069     // returned if there is no item in the archive with the given id
0070     // (and there is an automatic cast from std::shared_ptr to bool).
0071     virtual std::shared_ptr<const CatalogEntry> catalogEntry(unsigned long long id) = 0;
0072 
0073     // Dump everything to storage (if the archive is open for writing
0074     // and if this makes sense for the archive)
0075     virtual void flush() = 0;
0076 
0077     // Copy an item from this archive to a destination archive.
0078     // This archive and destination archive must be distinct.
0079     // Note that, while it is always possible to copy an item
0080     // with correct id, a standalone operation like this is
0081     // not necessarily going to make sense because the item
0082     // could be just a part of some distributed object.
0083     //
0084     // If "newName" and/or "newCategory" arguments are left at their
0085     // default value of 0, the existing name and/or category are used.
0086     //
0087     // This method returns the id of the copy in the destination
0088     // archive.
0089     unsigned long long copyItem(unsigned long long id,
0090                                 AbsArchive *destination,
0091                                 const char *newName = nullptr,
0092                                 const char *newCategory = nullptr);
0093 
0094     // The id and the length of the last item written
0095     // (the results make sense only for the archives
0096     //  that have been open for writing)
0097     inline unsigned long long lastItemId() const { return lastItemId_; }
0098     inline unsigned long long lastItemLength() const { return lastItemLength_; }
0099 
0100     inline bool operator==(const AbsArchive &r) const { return (typeid(*this) == typeid(r)) && this->isEqual(r); }
0101     inline bool operator!=(const AbsArchive &r) const { return !(*this == r); }
0102 
0103   protected:
0104     void addItemToReference(AbsReference &r, unsigned long long id) const;
0105 
0106     // Archives which want to implement reasonable comparisons
0107     // should override the function below
0108     virtual bool isEqual(const AbsArchive &) const { return false; }
0109 
0110   private:
0111     friend class AbsReference;
0112     friend gs::AbsArchive & ::operator<<(gs::AbsArchive & ar, const gs::AbsRecord & record);
0113 
0114     // Search for items which correspond to the given reference.
0115     // The "reference" works both as the input and as the output in
0116     // this query. The concrete implementations should utilize the
0117     // "addItemToReference" method in order to produce results.
0118     virtual void search(AbsReference &reference) = 0;
0119 
0120     // Position the input stream for reading the item with given id.
0121     // The reading must follow immediately, any other operaction on
0122     // the archive can invalidate the result.
0123     //
0124     // The "sz" argument could be NULL in which case it should be
0125     // ignored. If it is not NULL, the *sz value on completion
0126     // should be set as follows:
0127     //
0128     // *sz = -1  -- if the uncompressed object size in the stream
0129     //              is the same as in the catalog
0130     // *sz >= 0  -- *sz is the object size after decompression
0131     //              (usually not the same as in the catalog)
0132     //
0133     virtual std::istream &inputStream(unsigned long long id, long long *sz) = 0;
0134 
0135     // Get the stream for writing the next object
0136     virtual std::ostream &outputStream() = 0;
0137 
0138     // The following function should return 0 on failure and
0139     // the item id in the archive on success. The id must be positive.
0140     virtual unsigned long long addToCatalog(const AbsRecord &record,
0141                                             unsigned compressCode,
0142                                             unsigned long long itemLength) = 0;
0143 
0144     // The archives which want to support compression should
0145     // override the following two methods
0146     virtual std::ostream &compressedStream(std::ostream &uncompressed) { return uncompressed; }
0147 
0148     // The following function returns some kind of a code which
0149     // tells us how the item was compressed. Default value of 0
0150     // means no compression.
0151     virtual unsigned flushCompressedRecord(std::ostream & /* compressed */) { return 0U; }
0152 
0153     std::string name_;
0154     unsigned long long lastItemId_;
0155     unsigned long long lastItemLength_;
0156   };
0157 }  // namespace gs
0158 
0159 #endif  // GENERS_ABSARCHIVE_HH_