evio  6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Compressor.h
Go to the documentation of this file.
1 //
2 // Copyright 2020, Jefferson Science Associates, LLC.
3 // Subject to the terms in the LICENSE file found in the top-level directory.
4 //
5 // EPSCI Group
6 // Thomas Jefferson National Accelerator Facility
7 // 12000, Jefferson Ave, Newport News, VA 23606
8 // (757)-269-7100
9 
10 
11 #ifndef EVIO_6_0_COMPRESSOR_H
12 #define EVIO_6_0_COMPRESSOR_H
13 
14 
15 #include <string>
16 #include <cstdint>
17 #include <iostream>
18 #include <iomanip>
19 #include <sstream>
20 
21 
22 #include "EvioException.h"
23 #include "ByteBuffer.h"
24 #include "lz4.h"
25 #include "lz4hc.h"
26 
27 #ifdef USE_GZIP
28  #include "zlib.h"
29 #endif
30 
31 
32 namespace evio {
33 
34 
41  class Compressor {
42 
43 
44  public:
45 
50  static Compressor& getInstance() {
51  static Compressor theCompressor; // Instantiated when this function is called
52  return theCompressor;
53  }
54 
55  private:
56 
57  Compressor(); // constructor is private
58  Compressor(const Compressor &); // copy constructor is private
59  Compressor& operator=(Compressor const&); // assignment operator is private
60  ~Compressor() = default; // destructor is private
61 
62  public:
63 
67  LZ4,
70  };
71 
72  static CompressionType toCompressionType(uint32_t type);
73 
74  private:
75 
76 #ifdef USE_GZIP
77  static z_stream strmDeflate;
78  static z_stream strmInflate;
79 #endif
80 
82  static const uint32_t MTU = 1024*1024;
83 
84  /* Makes regular lz4 compression to be lz4Acceleration * 3% speed up. */
85  static const int lz4Acceleration = 1;
86 
87  static uint32_t getYear( ByteBuffer & buf);
88  static uint32_t getRevisionId( ByteBuffer & buf, uint32_t board_id);
89  static uint32_t getSubsystemId(ByteBuffer & buf, uint32_t board_id);
90  static uint32_t getDeviceId( ByteBuffer & buf, uint32_t board_id);
91 
92  static void setUpCompressionHardware();
93  static void setUpZlib();
94 
95  public:
96 
97  static int getMaxCompressedLength(CompressionType compressionType, uint32_t uncompressedLength);
98 
99  //---------------
100  // GZIP
101  //---------------
102 #ifdef USE_GZIP
103  static uint8_t* compressGZIP(uint8_t* ungzipped, uint32_t offset,
104  uint32_t length, uint32_t *compLen);
105 
106  static uint8_t* uncompressGZIP(uint8_t* gzipped, uint32_t off,
107  uint32_t length, uint32_t *uncompLen, uint32_t origUncompLen);
108 
109  static int compressGZIP(uint8_t* dest, uint32_t *destLen,
110  const uint8_t* source, uint32_t sourceLen);
111 
112  static int uncompressGZIP(uint8_t* dest, uint32_t *destLen,
113  const uint8_t* source, uint32_t *sourceLen,
114  uint32_t uncompLen);
115 
116  static uint8_t* uncompressGZIP(ByteBuffer & gzipped, uint32_t *uncompLen);
117 #endif
118 
119  //---------------
120  // LZ4
121  //---------------
122  static int compressLZ4(ByteBuffer & src, int srcSize, ByteBuffer & dst, int maxSize);
123  static int compressLZ4(uint8_t *src, int srcOff, int srcSize,
124  uint8_t *dst, int dstOff, int maxSize);
125  static int compressLZ4(ByteBuffer & src, int srcOff, int srcSize,
126  ByteBuffer & dst, int dstOff, int maxSize);
127  static int compressLZ4Best(ByteBuffer & src, int srcSize, ByteBuffer & dst, int maxSize);
128  static int compressLZ4Best(uint8_t *src, int srcOff, int srcSize,
129  uint8_t *dst, int dstOff, int maxSize);
130  static int compressLZ4Best(ByteBuffer & src, int srcOff, int srcSize,
131  ByteBuffer & dst, int dstOff, int maxSize);
132 
133  static int uncompressLZ4(ByteBuffer & src, int srcSize, ByteBuffer & dst);
134  static int uncompressLZ4(ByteBuffer & src, int srcOff, int srcSize, ByteBuffer & dst);
135  static int uncompressLZ4(ByteBuffer & src, int srcOff, int srcSize, ByteBuffer & dst, int dstOff);
136  static int uncompressLZ4(uint8_t *src, int srcOff, int srcSize, uint8_t *dst,
137  int dstOff, int dstCapacity);
138 
139 
140  };
141 
142 }
143 
144 #endif //EVIO_6_0_COMPRESSOR_H
static Compressor & getInstance()
Get an instance of this singleton class.
Definition: Compressor.h:50
This class is copied from one of the same name in the Java programming language.
Definition: ByteBuffer.h:42
static int getMaxCompressedLength(CompressionType compressionType, uint32_t uncompressedLength)
Returns the maximum number of bytes needed to compress the given length of uncompressed data...
Definition: Compressor.cpp:231
Singleton class used to provide data compression and decompression in a variety of formats...
Definition: Compressor.h:41
CompressionType
Enum of supported data compression types.
Definition: Compressor.h:65
Definition: Compressor.h:66
Definition: Compressor.h:69
Definition: Compressor.h:67
static CompressionType toCompressionType(uint32_t type)
Method to convert an integer to a CompressionType object.
Definition: Compressor.cpp:79
static int compressLZ4Best(ByteBuffer &src, int srcSize, ByteBuffer &dst, int maxSize)
Highest LZ4 compression.
Definition: Compressor.cpp:594
static int compressLZ4(ByteBuffer &src, int srcSize, ByteBuffer &dst, int maxSize)
Fastest LZ4 compression.
Definition: Compressor.cpp:497
Definition: Compressor.h:68
static int uncompressLZ4(ByteBuffer &src, int srcSize, ByteBuffer &dst)
LZ4 decompression.
Definition: Compressor.cpp:689