SNAP Library 2.3, Developer Reference  2014-06-16 11:58:46
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
fds.h
Go to the documentation of this file.
1 // Forward
3 template <class TKey, class TFDat, class TVDat>
4 class TFHash;
5 
7 
9 // File-Hash-Table-Key-Data
10 template <class TKey, class TFDat, class TVDat>
11 class TFHashKey{
12 private:
14  bool Modified;
15 public:
19  TKey Key;
20  TFDat FDat;
22 public:
24  Modified(false), Next(), Key(), FDat(), VDatBPt(){}
25  TFHashKey(const TBlobPt& _Next,
26  const TKey& _Key, const TFDat& _FDat, const TBlobPt& _VDatBPt=TBlobPt()):
27  Modified(false), Next(_Next), Key(_Key), FDat(_FDat), VDatBPt(_VDatBPt){}
29  TFHashKey(TSIn& SIn):
30  Modified(false), Next(SIn), Key(SIn), FDat(SIn), VDatBPt(SIn){}
31  static PFHashKey Load(TSIn& SIn){return new TFHashKey(SIn);}
32  void Save(TSOut& SOut){
33  Next.Save(SOut); Key.Save(SOut); FDat.Save(SOut); VDatBPt.Save(SOut);}
34 
35  TFHashKey& operator=(const TFHashKey& FHashKey){
36  if (this!=&FHashKey){
37  Modified=true; Next=FHashKey.Next;
38  Key=FHashKey.Key; FDat=FHashKey.FDat; VDatBPt=FHashKey.VDatBPt;}
39  return *this;}
40  int GetMemUsed() const {
41  return sizeof(THash*)+Next.GetMemUsed()+
42  Key.GetMemUsed()+FDat.GetMemUsed()+VDatBPt.GetMemUsed();}
43 
44  void PutModified(const bool& _Modified){Modified=_Modified;}
45 
46  void OnDelFromCache(const TBlobPt& BlobPt, void* RefToBs);
47 
48  friend class TPt<TFHashKey<TKey, TFDat, TVDat> >;
49 };
50 
51 template <class TKey, class TFDat, class TVDat>
53  const TBlobPt& BlobPt, void* RefToBs){
54  if (Modified){
55  // prepare hash table object
56  THash* FHash=(THash*)RefToBs;
57  // save the key
58  TMOut MOut; TInt(int(fhbtKey)).Save(MOut); Save(MOut);
59  TBlobPt NewBlobPt=FHash->GetHashBBs()->PutBlob(BlobPt, MOut.GetSIn());
60  // blob-pointer for key should not change
61  IAssert(NewBlobPt==BlobPt);
62  }
63 }
64 
66 // File-Hash-Table
67 
68 template <class TKey, class TFDat, class TVDat>
69 class TFHash{
70 private:
72 private:
81 private:
82  void* GetVoidThis() const {return (void*)this;}
84  PHashKey GetFHashKey(const TBlobPt& KeyId){
85  PHashKey FHashKey;
86  if (!FHashKeyCache.Get(KeyId, FHashKey)){ // if the key is in cache
87  // read the key from blob-base
88  PSIn SIn=HashBBs->GetBlob(KeyId);
89  TFHashBlobType Type=TFHashBlobType(int(TInt(*SIn))); IAssert(Type==fhbtKey);
90  FHashKey=PHashKey(new THashKey(*SIn));
91  }
92  FHashKeyCache.Put(KeyId, FHashKey); // refresh/put key in cache
93  return FHashKey;
94  }
95  void GetKeyInfo(const TKey& Key,
96  int& PortN, TBlobPt& PrevKeyId, TBlobPt& KeyId, PHashKey& FHashKey);
97  void GetKeyInfo(const TKey& Key, TBlobPt& KeyId, PHashKey& FHashKey){
98  int PortN=-1; TBlobPt PrevKeyId;
99  GetKeyInfo(Key, PortN, PrevKeyId, KeyId, FHashKey);}
100 private:
101  TBlobPt AddKey(const TKey& Key,
102  const bool& ChangeFDat, const TFDat& FDat,
103  const bool& ChangeVDatBPt, const TBlobPt& VDatBPt);
104  TBlobPt AddDat(
105  const TKey& Key,
106  const bool& ChangeFDat, const TFDat& FDat,
107  const bool& ChangeVDat, const TVDat& VDat);
108 public:
109  TFHash(const TStr& HashFNm, const TFAccess& _Access,
110  const int& Ports, const int& MxMemUsed);
111  ~TFHash();
113  static PFHash Load(TSIn&){Fail; return NULL;}
114  void Save(TSOut&){Fail;}
115 
116  TFHash& operator=(const TFHash&){Fail; return *this;}
117  int GetMemUsed(){
118  return PortV.GetMemUsed()+(int)FHashKeyCache.GetMemUsed();} //TODO:64bit
119  void CacheFlushAndClr(){FHashKeyCache.FlushAndClr();}
120 
121  bool Empty() const {return Keys==0;}
122  int Len() const {return Keys;}
123 
124  TBlobPt AddFDat(const TKey& Key, const TFDat& FDat){
125  return AddKey(Key, true, FDat, false, TBlobPt());}
126  TBlobPt AddVDat(const TKey& Key, const TVDat& VDat){
127  return AddDat(Key, false, TFDat(), true, VDat);}
128  TBlobPt AddFVDat(const TKey& Key, const TFDat& FDat, const TVDat& VDat){
129  return AddDat(Key, true, FDat, true, VDat);}
130 
131  void DelKey(const TKey& Key);
132  void DelKeyId(const TBlobPt& KeyId){
133  TKey Key; GetKey(KeyId, Key); DelKey(Key);}
134 
135  void GetKey(const TBlobPt& KeyId, TKey& Key){
136  PHashKey FHashKey=GetFHashKey(KeyId); Key=FHashKey->Key;}
137  TBlobPt GetKeyId(const TKey& Key){
138  TBlobPt KeyId; PHashKey FHashKey; GetKeyInfo(Key, KeyId, FHashKey);
139  return KeyId;}
140  bool IsKey(const TKey& Key){
141  return !GetKeyId(Key).Empty();}
142  bool IsKey(const TKey& Key, TBlobPt& KeyId){
143  KeyId=GetKeyId(Key); return !KeyId.Empty();}
144 
145  TBlobPt GetFDat(const TKey& Key, TFDat& FDat);
146  TBlobPt GetVDat(const TKey& Key, TVDat& VDat);
147  TBlobPt GetFVDat(const TKey& Key, TFDat& FDat, TVDat& VDat);
148  void GetKeyFDat(const TBlobPt& KeyId, TKey& Key, TFDat& FDat);
149  void GetKeyFVDat(const TBlobPt& KeyId, TKey& Key, TFDat& FDat, TVDat& VDat);
150 
152  bool FNextKeyId(TBlobPt& TrvBlobPt, TBlobPt& KeyId);
153 
154  friend class TFHashKey<TKey, TFDat, TVDat>;
155  friend class TPt<TFHash<TKey, TFDat, TVDat> >;
156 };
157 
158 
159 template <class TKey, class TFDat, class TVDat>
161  const TKey& Key,
162  int& PortN, TBlobPt& PrevKeyId, TBlobPt& KeyId, PHashKey& FHashKey){
163  // prepare key data
164  PortN=abs(Key.GetPrimHashCd())%PortV.Len();
165  PrevKeyId.Clr();
166  KeyId=PortV[PortN];
167 
168  // test if the key exists
169  if (!KeyId.Empty()){
170  FHashKey=GetFHashKey(KeyId);
171  while ((!KeyId.Empty())&&(FHashKey->Key!=Key)){
172  PrevKeyId=KeyId;
173  KeyId=FHashKey->Next;
174  if (!KeyId.Empty()){FHashKey=GetFHashKey(KeyId);}
175  }
176  }
177 }
178 
179 template <class TKey, class TFDat, class TVDat>
181  const TStr& HashFNm, const TFAccess& _Access,
182  const int& Ports, const int& MxMemUsed):
183  Access(_Access), HashBBs(), PortV(), Keys(0),
184  FHashKeyCache(MxMemUsed, 100003, GetVoidThis()){
185  if (Access==faCreate){
186  IAssert(Ports>0);
187  // create blob-base
188  HashBBs=PBlobBs(new TGBlobBs(HashFNm, faCreate));
189  // save initial no. of keys and port-vector
190  PortV.Gen(Ports);
191  TMOut HdSOut; Keys.Save(HdSOut); PortV.Save(HdSOut);
192  HashBBs->PutBlob(HdSOut.GetSIn());
193  } else {
195  IAssert(Ports==-1);
196  // open blob-base
197  HashBBs=PBlobBs(new TGBlobBs(HashFNm, Access));
198  // load initial no. of keys and port-vector
199  TBlobPt HdBPt=HashBBs->GetFirstBlobPt();
200  PSIn HdSIn=HashBBs->GetBlob(HdBPt);
201  Keys=TInt(*HdSIn);
202  PortV=TBlobPtV(*HdSIn);
203  }
204 }
205 
206 template <class TKey, class TFDat, class TVDat>
208  if ((Access==faCreate)||(Access==faUpdate)){
209  // flush hash-key cache
210  FHashKeyCache.Flush();
211  // save port-vector
212  TBlobPt HdBPt=HashBBs->GetFirstBlobPt();
213  TMOut HdSOut; Keys.Save(HdSOut); PortV.Save(HdSOut);
214  HashBBs->PutBlob(HdBPt, HdSOut.GetSIn());
215  }
216 }
217 
218 template <class TKey, class TFDat, class TVDat>
220  const TKey& Key,
221  const bool& ChangeFDat, const TFDat& FDat,
222  const bool& ChangeVDatBPt, const TBlobPt& VDatBPt){
223  // prepare key info
224  int PortN=-1; TBlobPt PrevKeyId; TBlobPt KeyId; PHashKey FHashKey;
225  GetKeyInfo(Key, PortN, PrevKeyId, KeyId, FHashKey);
226 
227  if (KeyId.Empty()){
228  // generate key
229  FHashKey=PHashKey(new THashKey(TBlobPt(), Key, FDat, VDatBPt));
230  // save key to blob-base
231  TMOut FHashKeyMOut;
232  TInt(int(fhbtKey)).Save(FHashKeyMOut); FHashKey->Save(FHashKeyMOut);
233  TBlobPt FHashKeyBPt=HashBBs->PutBlob(FHashKeyMOut.GetSIn());
234  // save key to key-cache
235  FHashKeyCache.Put(FHashKeyBPt, FHashKey);
236  FHashKey->PutModified(false);
237  // connect key to the structure
238  KeyId=FHashKeyBPt;
239  Keys++;
240  if (PrevKeyId.Empty()){
241  PortV[PortN]=KeyId;
242  } else {
243  PHashKey PrevFHashKey=GetFHashKey(PrevKeyId);
244  PrevFHashKey->Next=KeyId;
245  PrevFHashKey->PutModified(true);
246  }
247  } else {
248  // update the data
249  if (ChangeFDat){FHashKey->FDat=FDat;}
250  if (ChangeVDatBPt){FHashKey->VDatBPt=VDatBPt;}
251  if (ChangeFDat||ChangeVDatBPt){
252  FHashKey->PutModified(true);}
253  }
254  return KeyId;
255 }
256 
257 template <class TKey, class TFDat, class TVDat>
259  const TKey& Key,
260  const bool& ChangeFDat, const TFDat& FDat,
261  const bool& ChangeVDat, const TVDat& VDat){
262  // prepare key info
263  TBlobPt KeyId; PHashKey FHashKey;
264  GetKeyInfo(Key, KeyId, FHashKey);
265 
266  // prepare new variable-data blob-pointer
267  TBlobPt VDatBPt;
268  if (ChangeVDat){
269  // save variable-data
270  TMOut VDatMOut;
271  TInt(int(fhbtVDat)).Save(VDatMOut); VDat.Save(VDatMOut);
272  if (KeyId.Empty()){
273  VDatBPt=HashBBs->PutBlob(VDatMOut.GetSIn());
274  } else {
275  VDatBPt=HashBBs->PutBlob(FHashKey->VDatBPt, VDatMOut.GetSIn());
276  }
277  }
278 
279  // save the data
280  KeyId=AddKey(Key, ChangeFDat, FDat, ChangeVDat, VDatBPt);
281  return KeyId;
282 }
283 
284 template <class TKey, class TFDat, class TVDat>
286  // prepare key info
287  int PortN=-1; TBlobPt PrevKeyId; TBlobPt KeyId; PHashKey FHashKey;
288  GetKeyInfo(Key, PortN, PrevKeyId, KeyId, FHashKey);
289 
290  // disconnect key
291  IAssert(!KeyId.Empty());
292  if (PrevKeyId.Empty()){
293  PortV[PortN]=FHashKey->Next;
294  } else {
295  PHashKey PrevFHashKey=GetFHashKey(PrevKeyId);
296  PrevFHashKey->Next=FHashKey->Next;
297  PrevFHashKey->PutModified(true);
298  }
299  // delete variable data
300  if (!FHashKey->VDatBPt.Empty()){
301  HashBBs->DelBlob(FHashKey->VDatBPt);}
302  // delete key/fixed data
303  HashBBs->DelBlob(KeyId);
304  FHashKeyCache.Del(KeyId, false);
305 }
306 
307 template <class TKey, class TFDat, class TVDat>
309  const TKey& Key, TFDat& FDat){
310  // prepare key info
311  TBlobPt KeyId; PHashKey FHashKey;
312  GetKeyInfo(Key, KeyId, FHashKey);
313  // get fixed data
314  FDat=FHashKey->FDat;
315  return KeyId;
316 }
317 
318 template <class TKey, class TFDat, class TVDat>
319 TBlobPt TFHash<TKey, TFDat, TVDat>::GetVDat(const TKey& Key, TVDat& VDat){
320  // prepare key info
321  TBlobPt KeyId; PHashKey FHashKey;
322  GetKeyInfo(Key, KeyId, FHashKey);
323  // get variable data
324  PSIn SIn=HashBBs->GetBlob(FHashKey->VDatBPt);
325  TFHashBlobType Type=TFHashBlobType(int(TInt(*SIn))); IAssert(Type==fhbtVDat);
326  VDat=TVDat(*SIn);
327  return KeyId;
328 }
329 
330 template <class TKey, class TFDat, class TVDat>
332  const TKey& Key, TFDat& FDat, TVDat& VDat){
333  // prepare key info
334  TBlobPt KeyId; PHashKey FHashKey;
335  GetKeyInfo(Key, KeyId, FHashKey);
336  // get fixed data
337  FDat=FHashKey->FDat;
338  // get variable data
339  PSIn SIn=HashBBs->GetBlob(FHashKey->VDatBPt);
340  TFHashBlobType Type=TFHashBlobType(int(TInt(*SIn))); IAssert(Type==fhbtVDat);
341  VDat=TVDat(*SIn);
342  return KeyId;
343 }
344 
345 template <class TKey, class TFDat, class TVDat>
347  const TBlobPt& KeyId, TKey& Key, TFDat& FDat){
348  // prepare key info
349  PHashKey FHashKey=GetFHashKey(KeyId);
350  // get key
351  Key=FHashKey->Key;
352  // get fixed data
353  FDat=FHashKey->FDat;
354 }
355 
356 template <class TKey, class TFDat, class TVDat>
358  const TBlobPt& KeyId, TKey& Key, TFDat& FDat, TVDat& VDat){
359  // prepare key info
360  PHashKey FHashKey=GetFHashKey(KeyId);
361  // get key
362  Key=FHashKey->Key;
363  // get fixed data
364  FDat=FHashKey->FDat;
365  // get variable data
366  PSIn SIn=HashBBs->GetBlob(FHashKey->VDatBPt);
367  TFHashBlobType Type=TFHashBlobType(int(TInt(*SIn))); IAssert(Type==fhbtVDat);
368  VDat=TVDat(*SIn);
369 }
370 
371 template <class TKey, class TFDat, class TVDat>
373  return HashBBs->FFirstBlobPt();
374 }
375 
376 template <class TKey, class TFDat, class TVDat>
378  TBlobPt& TrvBlobPt, TBlobPt& KeyId){
379  PSIn SIn;
380  while (HashBBs->FNextBlobPt(TrvBlobPt, KeyId, SIn)){
381  TFHashBlobType Type=TFHashBlobType(int(TInt(*SIn)));
382  if (Type==fhbtKey){return true;}
383  }
384  return false;
385 }
386 
Definition: fds.h:6
Definition: bd.h:440
#define IAssert(Cond)
Definition: bd.h:262
Definition: fds.h:11
void Clr()
Definition: blobbs.h:50
PHashKey GetFHashKey(const TBlobPt &KeyId)
Definition: fds.h:84
TFHashKey & operator=(const TFHashKey &FHashKey)
Definition: fds.h:35
virtual TBlobPt GetFirstBlobPt()=0
TBlobPt AddVDat(const TKey &Key, const TVDat &VDat)
Definition: fds.h:126
Definition: fl.h:347
Definition: fds.h:6
void GetKeyInfo(const TKey &Key, TBlobPt &KeyId, PHashKey &FHashKey)
Definition: fds.h:97
void GetKeyFDat(const TBlobPt &KeyId, TKey &Key, TFDat &FDat)
Definition: fds.h:346
void Save(TSOut &)
Definition: fds.h:114
void Save(TSOut &SOut) const
Definition: dt.h:1057
PSIn GetSIn(const bool &IsCut=true, const int &CutBfL=-1)
Definition: fl.cpp:666
#define Fail
Definition: bd.h:238
TPt< TFHash< TKey, TFDat, TVDat > > PFHash
Definition: fds.h:73
bool Empty() const
Definition: bd.h:501
TVec< TBlobPt > TBlobPtV
Definition: blobbs.h:5
void Save(TSOut &SOut)
Definition: fds.h:32
TBlobPt GetKeyId(const TKey &Key)
Definition: fds.h:137
bool IsKey(const TKey &Key)
Definition: fds.h:140
bool Modified
Definition: fds.h:14
TSizeTy GetMemUsed() const
Returns the memory footprint (the number of bytes) of the vector.
Definition: ds.h:474
TKey Key
Definition: fds.h:19
TPt< TFHashKey< TKey, TFDat, TVDat > > PFHashKey
Definition: fds.h:16
void GetKey(const TBlobPt &KeyId, TKey &Key)
Definition: fds.h:135
Definition: fl.h:58
void Save(TSOut &SOut) const
Definition: ds.h:885
bool IsKey(const TKey &Key, TBlobPt &KeyId)
Definition: fds.h:142
static PFHashKey Load(TSIn &SIn)
Definition: fds.h:31
TFHashBlobType
Definition: fds.h:6
TFHashKey()
Definition: fds.h:23
TBlobPt GetFVDat(const TKey &Key, TFDat &FDat, TVDat &VDat)
Definition: fds.h:331
int GetMemUsed() const
Definition: fds.h:40
TFAccess Access
Definition: fds.h:76
TFHashKey< TKey, TFDat, TVDat > THashKey
Definition: fds.h:74
void GetKeyFVDat(const TBlobPt &KeyId, TKey &Key, TFDat &FDat, TVDat &VDat)
Definition: fds.h:357
TFHashKey(TSIn &SIn)
Definition: fds.h:29
Definition: fl.h:347
TBlobPt AddFDat(const TKey &Key, const TFDat &FDat)
Definition: fds.h:124
PBlobBs GetHashBBs()
Definition: fds.h:83
TCRef CRef
Definition: fds.h:71
int GetMemUsed()
Definition: fds.h:117
TBlobPt VDatBPt
Definition: fds.h:21
int GetMemUsed() const
Definition: blobbs.h:44
int Len() const
Definition: fds.h:122
TFHash(TSIn &)
Definition: fds.h:112
void CacheFlushAndClr()
Definition: fds.h:119
~TFHash()
Definition: fds.h:207
TBlobPtV PortV
Definition: fds.h:78
TBlobPt GetVDat(const TKey &Key, TVDat &VDat)
Definition: fds.h:319
TPt< THashKey > PHashKey
Definition: fds.h:75
TFDat FDat
Definition: fds.h:20
~TFHashKey()
Definition: fds.h:28
void DelKey(const TKey &Key)
Definition: fds.h:285
TBlobPt AddKey(const TKey &Key, const bool &ChangeFDat, const TFDat &FDat, const bool &ChangeVDatBPt, const TBlobPt &VDatBPt)
Definition: fds.h:219
Definition: fl.h:128
void Save(const bool &Bool)
Definition: fl.h:173
Definition: dt.h:1041
TFHash & operator=(const TFHash &)
Definition: fds.h:116
Definition: fl.h:417
bool FNextKeyId(TBlobPt &TrvBlobPt, TBlobPt &KeyId)
Definition: fds.h:377
Definition: fds.h:6
void GetKeyInfo(const TKey &Key, int &PortN, TBlobPt &PrevKeyId, TBlobPt &KeyId, PHashKey &FHashKey)
Definition: fds.h:160
bool Empty() const
Definition: blobbs.h:49
void OnDelFromCache(const TBlobPt &BlobPt, void *RefToBs)
Definition: fds.h:52
Definition: fds.h:4
void Save(TSOut &SOut) const
Definition: xmlser.h:16
Definition: dt.h:412
TFHash< TKey, TFDat, TVDat > THash
Definition: fds.h:17
Definition: blobbs.h:5
TBlobPt AddFVDat(const TKey &Key, const TFDat &FDat, const TVDat &VDat)
Definition: fds.h:128
PBlobBs HashBBs
Definition: fds.h:77
TCache< TBlobPt, PHashKey > FHashKeyCache
Definition: fds.h:80
virtual TBlobPt PutBlob(const PSIn &SIn)=0
TCRef CRef
Definition: fds.h:13
bool Empty() const
Definition: fds.h:121
void DelKeyId(const TBlobPt &KeyId)
Definition: fds.h:132
TBlobPt Next
Definition: fds.h:18
TPt< TBlobBs > PBlobBs
Definition: blobbs.h:89
Definition: bd.h:196
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:486
static PFHash Load(TSIn &)
Definition: fds.h:113
TFHashKey(const TBlobPt &_Next, const TKey &_Key, const TFDat &_FDat, const TBlobPt &_VDatBPt=TBlobPt())
Definition: fds.h:25
TBlobPt GetFDat(const TKey &Key, TFDat &FDat)
Definition: fds.h:308
TInt Keys
Definition: fds.h:79
void * GetVoidThis() const
Definition: fds.h:82
Definition: hash.h:967
void Save(TSOut &SOut) const
Definition: blobbs.h:31
TFAccess
Definition: fl.h:347
Definition: fl.h:347
TBlobPt FFirstKeyId()
Definition: fds.h:372
TFHash(const TStr &HashFNm, const TFAccess &_Access, const int &Ports, const int &MxMemUsed)
Definition: fds.h:180
void PutModified(const bool &_Modified)
Definition: fds.h:44
TBlobPt AddDat(const TKey &Key, const bool &ChangeFDat, const TFDat &FDat, const bool &ChangeVDat, const TVDat &VDat)
Definition: fds.h:258
virtual PSIn GetBlob(const TBlobPt &BlobPt)=0