HOPS
HOPS class reference
MHO_FileKey.hh
Go to the documentation of this file.
1 #ifndef MHO_FileKey_HH__
2 #define MHO_FileKey_HH__
3 
4 #include "MHO_Types.hh"
5 #include "MHO_UUID.hh"
6 
7 namespace hops
8 {
9 
18 //fixed values
19 static constexpr uint32_t MHO_FileKeySyncWord = 0xEFBEADDE; //DEADBEEF
20 static constexpr uint32_t MHO_FileKeyNameLength = 16;
21 
22 //v0 parameters
23 static constexpr uint16_t MHO_FileKeyVersion_v0 = 0;
24 static constexpr uint16_t MHO_FileKeySize_v0 = 64;
25 
26 //points to the current version
27 static constexpr uint16_t MHO_FileKeyVersion = MHO_FileKeyVersion_v0;
28 static constexpr uint16_t MHO_FileKeySize = MHO_FileKeySize_v0;
29 
30 //reserved label
31 static constexpr uint32_t MHO_FileKeyVersionReserved = 0xFFFFFFFF;
32 
37 {
38  uint32_t fLabel;
39  uint16_t fVersionSize[2];
40  //fVersionSize format is:
41  //1st uint16_t is the file key version
42  //2nd uint16_t is the size of the file key, (cannot exceed UINT16_MAX)
43 };
44 
45 
46 
47 
57 {
58  public:
60  {
61  //sync word is always the same
62  fSync = MHO_FileKeySyncWord;
63  //define the current file key version/size
65  u.fVersionSize[0] = MHO_FileKeyVersion;
66  u.fVersionSize[1] = MHO_FileKeySize;
67  fLabel = u.fLabel;
68 
69  for(uint32_t i = 0; i < MHO_FileKeyNameLength; i++)
70  {
71  fName[i] = '\0';
72  }
73  fSize = 0;
74  }
75 
76  MHO_FileKey(const MHO_FileKey& copy)
77  {
78  fSync = copy.fSync;
79  fLabel = copy.fLabel;
80  fObjectId = copy.fObjectId;
81  fTypeId = copy.fTypeId;
82  for(uint32_t i = 0; i < MHO_FileKeyNameLength; i++)
83  {
84  fName[i] = copy.fName[i];
85  }
86  fSize = copy.fSize;
87  }
88 
89  virtual ~MHO_FileKey(){};
90 
91  bool operator==(const MHO_FileKey& rhs)
92  {
93  if(fSync != rhs.fSync)
94  {
95  return false;
96  }
97  if(fLabel != rhs.fLabel)
98  {
99  return false;
100  }
101  if(fObjectId != rhs.fObjectId)
102  {
103  return false;
104  }
105  if(fTypeId != rhs.fTypeId)
106  {
107  return false;
108  }
109 
110  for(uint32_t i = 0; i < MHO_FileKeyNameLength; i++)
111  {
112  if(fName[i] != rhs.fName[i])
113  {
114  return false;
115  }
116  }
117 
118  if(fSize != rhs.fSize)
119  {
120  return false;
121  }
122  return true;
123  }
124 
125  bool operator!=(const MHO_FileKey& rhs) { return !(*this == rhs); }
126 
128  {
129  if(this != &rhs)
130  {
131  fSync = rhs.fSync;
132  fLabel = rhs.fLabel;
133  fObjectId = rhs.fObjectId;
134  fTypeId = rhs.fTypeId;
135  for(uint32_t i = 0; i < MHO_FileKeyNameLength; i++)
136  {
137  fName[i] = rhs.fName[i];
138  }
139  fSize = rhs.fSize;
140  }
141  return *this;
142  }
143 
144 
154  static uint64_t ByteSize() { return MHO_FileKeySize_v0; };
155 
156  //public access to members:
157  public:
158  uint32_t fSync; //32 bits for sync word for location of object key
159  uint32_t fLabel; //32 bits for version/size label
160  MHO_UUID fObjectId; //128 bits for random (or otherwise determined) unique object ID
161  MHO_UUID fTypeId; //128 bits for full MD5 hash of class-type + version
162  char fName[MHO_FileKeyNameLength]; //16 bytes for a (shorthand) name (i.e.probably to replace type_XXX codes)
163  uint64_t fSize; //total number of bytes of incoming object (distance in bytes to next key)
164 
165  //Future version of the file key could include additional information/parameters here
166  //However, version/size flag must be defined and additional I/O functionality must be added.
167  //For example, we could include space for a checksum of the object data, or additional tags/meta-data.
168 
170  //streaming functions and I/O defined below
171 
172  template< typename XStream > friend XStream& operator>>(XStream& s, MHO_FileKey& aData)
173  {
174  s >> aData.fSync;
175  s >> aData.fLabel;
176 
177  //the reserved 0xFFFFFFFF label forces us to use the current key version and size
178  //regardless of what is on disk, use with caution!
179  if(aData.fLabel == MHO_FileKeyVersionReserved)
180  {
182  r.fVersionSize[0] = MHO_FileKeyVersion;
183  r.fVersionSize[1] = MHO_FileKeySize;
184  aData.fLabel = r.fLabel;
185  }
186 
188  u.fLabel = aData.fLabel;
189  uint16_t version = u.fVersionSize[0];
190  uint16_t vsize = u.fVersionSize[1];
191 
192  switch(version)
193  {
194  case 0:
195  aData.StreamInData_V0(s);
196  break;
197  default:
198  aData.StreamInData_V0(s); //version-0 data must always be present.
199  //However, we don't understand this file-key version, so increment the stream
200  //data for this unknown portion into the trash, if the object type is know, it will
201  //still get read, but the extended file key data will be lost
202  if(vsize > 64)
203  {
204  int n = vsize - 64;
205  char trash;
206  for(int i = 0; i < n; i++)
207  {
208  s >> trash;
209  }
210  }
211  msg_error("utility", "encountered a MHO_FileKey with unknown version: "
212  << version << " attempting to continue. " << eom);
213  }
214  return s;
215  }
216 
217  template< typename XStream > friend XStream& operator<<(XStream& s, const MHO_FileKey& aData)
218  {
220  u.fLabel = aData.fLabel;
221  uint16_t version = u.fVersionSize[0];
222  uint16_t vsize = u.fVersionSize[1];
223 
224  s << aData.fSync;
225  s << aData.fLabel;
226 
227  switch(version)
228  {
229  case 0:
230  aData.StreamOutData_V0(s);
231  break;
232  case 0xFFFF: //reserved case, use the current streamer version (v0 at the moment)
233  aData.StreamOutData_V0(s);
234  break;
235  default:
236  //this should never happen (would require having a file-key version in memory that we don't know about)
237  msg_error("utility",
238  "error, cannot stream out MHO_FileKey object with unknown version: " << version << eom);
239  }
240  return s;
241  }
242 
243  private:
250  template< typename XStream > void StreamOutData_V0(XStream& s) const
251  {
252  // s << aKey.fSync;
253  // s << aKey.fLabel;
254  s << this->fObjectId;
255  s << this->fTypeId;
256  for(uint32_t i = 0; i < MHO_FileKeyNameLength; i++)
257  {
258  s << this->fName[i];
259  }
260  s << this->fSize;
261  }
262 
269  template< typename XStream > void StreamInData_V0(XStream& s)
270  {
271  // s >> aKey.fSync;
272  // s >> aKey.fLabel;
273  s >> this->fObjectId;
274  s >> this->fTypeId;
275  for(uint32_t i = 0; i < MHO_FileKeyNameLength; i++)
276  {
277  s >> this->fName[i];
278  }
279  s >> this->fSize;
280  }
281 };
282 
283 } // namespace hops
284 
285 #endif
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:244
Class MHO_FileKey the version-0 size of the file key is (512 bits / 64 bytes), and all of the version...
Definition: MHO_FileKey.hh:57
MHO_FileKey & operator=(const MHO_FileKey &rhs)
Definition: MHO_FileKey.hh:127
char fName[MHO_FileKeyNameLength]
Definition: MHO_FileKey.hh:162
friend XStream & operator>>(XStream &s, MHO_FileKey &aData)
Definition: MHO_FileKey.hh:172
MHO_FileKey()
Definition: MHO_FileKey.hh:59
MHO_UUID fTypeId
Definition: MHO_FileKey.hh:161
bool operator==(const MHO_FileKey &rhs)
Definition: MHO_FileKey.hh:91
virtual ~MHO_FileKey()
Definition: MHO_FileKey.hh:89
friend XStream & operator<<(XStream &s, const MHO_FileKey &aData)
Definition: MHO_FileKey.hh:217
uint32_t fSync
Definition: MHO_FileKey.hh:154
bool operator!=(const MHO_FileKey &rhs)
Definition: MHO_FileKey.hh:125
uint64_t fSize
Definition: MHO_FileKey.hh:163
MHO_UUID fObjectId
Definition: MHO_FileKey.hh:160
uint32_t fLabel
Definition: MHO_FileKey.hh:159
static uint64_t ByteSize()
Returns the size of MHO_FileKey in bytes. this is the size of a MHO_FileKey on disk DO NOT USE sizeof...
Definition: MHO_FileKey.hh:154
MHO_FileKey(const MHO_FileKey &copy)
Definition: MHO_FileKey.hh:76
Class MHO_UUID - a class for a 16 byte UUID (for object and type identification)
Definition: MHO_UUID.hh:27
const char version[]
Definition: difx2mark4.c:37
Definition: MHO_ChannelLabeler.hh:17
uint32_t fLabel
Definition: MHO_FileKey.hh:38
uint16_t fVersionSize[2]
Definition: MHO_FileKey.hh:39
union MHO_FileKeyVersionInfo - this union allows us to store the file key version and size info into ...
Definition: MHO_FileKey.hh:37