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 
54 {
55  public:
57  {
58  //sync word is always the same
59  fSync = MHO_FileKeySyncWord;
60  //define the current file key version/size
62  u.fVersionSize[0] = MHO_FileKeyVersion;
63  u.fVersionSize[1] = MHO_FileKeySize;
64  fLabel = u.fLabel;
65 
66  for(uint32_t i = 0; i < MHO_FileKeyNameLength; i++)
67  {
68  fName[i] = '\0';
69  }
70  fSize = 0;
71  }
72 
73  MHO_FileKey(const MHO_FileKey& copy)
74  {
75  fSync = copy.fSync;
76  fLabel = copy.fLabel;
77  fObjectId = copy.fObjectId;
78  fTypeId = copy.fTypeId;
79  for(uint32_t i = 0; i < MHO_FileKeyNameLength; i++)
80  {
81  fName[i] = copy.fName[i];
82  }
83  fSize = copy.fSize;
84  }
85 
86  virtual ~MHO_FileKey(){};
87 
88  bool operator==(const MHO_FileKey& rhs)
89  {
90  if(fSync != rhs.fSync)
91  {
92  return false;
93  }
94  if(fLabel != rhs.fLabel)
95  {
96  return false;
97  }
98  if(fObjectId != rhs.fObjectId)
99  {
100  return false;
101  }
102  if(fTypeId != rhs.fTypeId)
103  {
104  return false;
105  }
106 
107  for(uint32_t i = 0; i < MHO_FileKeyNameLength; i++)
108  {
109  if(fName[i] != rhs.fName[i])
110  {
111  return false;
112  }
113  }
114 
115  if(fSize != rhs.fSize)
116  {
117  return false;
118  }
119  return true;
120  }
121 
122  bool operator!=(const MHO_FileKey& rhs) { return !(*this == rhs); }
123 
125  {
126  if(this != &rhs)
127  {
128  fSync = rhs.fSync;
129  fLabel = rhs.fLabel;
130  fObjectId = rhs.fObjectId;
131  fTypeId = rhs.fTypeId;
132  for(uint32_t i = 0; i < MHO_FileKeyNameLength; i++)
133  {
134  fName[i] = rhs.fName[i];
135  }
136  fSize = rhs.fSize;
137  }
138  return *this;
139  }
140 
150  static uint64_t ByteSize() { return MHO_FileKeySize_v0; };
151 
152  //public access to members:
153  public:
154  uint32_t fSync; //32 bits for sync word for location of object key
155  uint32_t fLabel; //32 bits for version/size label
156  MHO_UUID fObjectId; //128 bits for random (or otherwise determined) unique object ID
157  MHO_UUID fTypeId; //128 bits for full MD5 hash of class-type + version
158  char fName[MHO_FileKeyNameLength]; //16 bytes for a (shorthand) name (i.e.probably to replace type_XXX codes)
159  uint64_t fSize; //total number of bytes of incoming object (distance in bytes to next key)
160 
161  //Future version of the file key could include additional information/parameters here
162  //However, version/size flag must be defined and additional I/O functionality must be added.
163  //For example, we could include space for a checksum of the object data, or additional tags/meta-data.
164 
166  //streaming functions and I/O defined below
167 
168  template< typename XStream > friend XStream& operator>>(XStream& s, MHO_FileKey& aData)
169  {
170  s >> aData.fSync;
171  s >> aData.fLabel;
172 
173  //the reserved 0xFFFFFFFF label forces us to use the current key version and size
174  //regardless of what is on disk, use with caution!
175  if(aData.fLabel == MHO_FileKeyVersionReserved)
176  {
178  r.fVersionSize[0] = MHO_FileKeyVersion;
179  r.fVersionSize[1] = MHO_FileKeySize;
180  aData.fLabel = r.fLabel;
181  }
182 
184  u.fLabel = aData.fLabel;
185  uint16_t version = u.fVersionSize[0];
186  uint16_t vsize = u.fVersionSize[1];
187 
188  switch(version)
189  {
190  case 0:
191  aData.StreamInData_V0(s);
192  break;
193  default:
194  aData.StreamInData_V0(s); //version-0 data must always be present.
195  //However, we don't understand this file-key version, so increment the stream
196  //data for this unknown portion into the trash, if the object type is know, it will
197  //still get read, but the extended file key data will be lost
198  if(vsize > 64)
199  {
200  int n = vsize - 64;
201  char trash;
202  for(int i = 0; i < n; i++)
203  {
204  s >> trash;
205  }
206  }
207  msg_error("utility", "encountered a MHO_FileKey with unknown version: "
208  << version << " attempting to continue. " << eom);
209  }
210  return s;
211  }
212 
213  template< typename XStream > friend XStream& operator<<(XStream& s, const MHO_FileKey& aData)
214  {
216  u.fLabel = aData.fLabel;
217  uint16_t version = u.fVersionSize[0];
218  uint16_t vsize = u.fVersionSize[1];
219 
220  s << aData.fSync;
221  s << aData.fLabel;
222 
223  switch(version)
224  {
225  case 0:
226  aData.StreamOutData_V0(s);
227  break;
228  case 0xFFFF: //reserved case, use the current streamer version (v0 at the moment)
229  aData.StreamOutData_V0(s);
230  break;
231  default:
232  //this should never happen (would require having a file-key version in memory that we don't know about)
233  msg_error("utility",
234  "error, cannot stream out MHO_FileKey object with unknown version: " << version << eom);
235  }
236  return s;
237  }
238 
239  private:
246  template< typename XStream > void StreamOutData_V0(XStream& s) const
247  {
248  // s << aKey.fSync;
249  // s << aKey.fLabel;
250  s << this->fObjectId;
251  s << this->fTypeId;
252  for(uint32_t i = 0; i < MHO_FileKeyNameLength; i++)
253  {
254  s << this->fName[i];
255  }
256  s << this->fSize;
257  }
258 
265  template< typename XStream > void StreamInData_V0(XStream& s)
266  {
267  // s >> aKey.fSync;
268  // s >> aKey.fLabel;
269  s >> this->fObjectId;
270  s >> this->fTypeId;
271  for(uint32_t i = 0; i < MHO_FileKeyNameLength; i++)
272  {
273  s >> this->fName[i];
274  }
275  s >> this->fSize;
276  }
277 };
278 
279 } // namespace hops
280 
281 #endif
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:238
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:54
MHO_FileKey & operator=(const MHO_FileKey &rhs)
Definition: MHO_FileKey.hh:124
char fName[MHO_FileKeyNameLength]
Definition: MHO_FileKey.hh:158
friend XStream & operator>>(XStream &s, MHO_FileKey &aData)
Definition: MHO_FileKey.hh:168
MHO_FileKey()
Definition: MHO_FileKey.hh:56
MHO_UUID fTypeId
Definition: MHO_FileKey.hh:157
bool operator==(const MHO_FileKey &rhs)
Definition: MHO_FileKey.hh:88
virtual ~MHO_FileKey()
Definition: MHO_FileKey.hh:86
friend XStream & operator<<(XStream &s, const MHO_FileKey &aData)
Definition: MHO_FileKey.hh:213
uint32_t fSync
Definition: MHO_FileKey.hh:150
bool operator!=(const MHO_FileKey &rhs)
Definition: MHO_FileKey.hh:122
uint64_t fSize
Definition: MHO_FileKey.hh:159
MHO_UUID fObjectId
Definition: MHO_FileKey.hh:156
uint32_t fLabel
Definition: MHO_FileKey.hh:155
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:150
MHO_FileKey(const MHO_FileKey &copy)
Definition: MHO_FileKey.hh:73
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_AdhocFlagging.hh:18
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