SNAP Library, Developer Reference  2012-10-15 15:06:59
SNAP, a general purpose network analysis and graph mining library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
blobbs.h
Go to the documentation of this file.
00001 
00002 // Blob-Pointer
00003 ClassTVQ(TBlobPt, TBlobPtV, TBlobPtQ)//{
00004 public:
00005   static const int MnBlobBfL;
00006   static const int Flags;
00007   uchar Seg;
00008   uint Addr;
00009   TB8Set FSet1, FSet2, FSet3;
00010 public:
00011   TBlobPt():
00012     Seg(0), Addr(TUInt::Mx), FSet1(), FSet2(), FSet3(){}
00013   TBlobPt(const TBlobPt& Pt):
00014     Seg(Pt.Seg), Addr(Pt.Addr),
00015     FSet1(Pt.FSet1), FSet2(Pt.FSet2), FSet3(Pt.FSet3){}
00016   TBlobPt(const uchar& _Seg, const uint& _Addr,
00017    const TB8Set& _FSet1, const TB8Set& _FSet2, const TB8Set& _FSet3):
00018     Seg(_Seg), Addr(_Addr), FSet1(_FSet1), FSet2(_FSet2), FSet3(_FSet3){}
00019   TBlobPt(const uchar& _Seg, const uint& _Addr):
00020     Seg(_Seg), Addr(_Addr), FSet1(), FSet2(), FSet3(){}
00021   TBlobPt(const uint& _Addr):
00022     Seg(0), Addr(_Addr), FSet1(), FSet2(), FSet3(){}
00023   TBlobPt(const int& _Addr):
00024     Seg(0), Addr(uint(_Addr)), FSet1(), FSet2(), FSet3(){IAssert(_Addr>=0);}
00025   ~TBlobPt(){}
00026   TBlobPt(TSIn& SIn){
00027     SIn.Load(Seg); SIn.Load(Addr);
00028     FSet1=TB8Set(SIn); FSet2=TB8Set(SIn); FSet3=TB8Set(SIn);}
00029   void Save(TSOut& SOut) const {
00030     SOut.Save(Seg); SOut.Save(Addr);
00031     FSet1.Save(SOut); FSet2.Save(SOut); FSet3.Save(SOut);}
00032 
00033   TBlobPt& operator=(const TBlobPt& Pt){
00034     if (this!=&Pt){
00035       Seg=Pt.Seg; Addr=Pt.Addr;
00036       FSet1=Pt.FSet1; FSet2=Pt.FSet2; FSet3=Pt.FSet3;}
00037     return *this;}
00038   bool operator==(const TBlobPt& Pt) const {
00039     return (Seg==Pt.Seg)&&(Addr==Pt.Addr);}
00040   bool operator<(const TBlobPt& Pt) const {
00041     return (Seg<Pt.Seg)||((Seg==Pt.Seg)&&(Addr<Pt.Addr));}
00042   int GetMemUsed() const {return sizeof(TBlobPt);}
00043 
00044   int GetPrimHashCd() const {return abs(int(Addr));}
00045   int GetSecHashCd() const {return (abs(int(Addr))+int(Seg)*0x10);}
00046 
00047   bool Empty() const {return Addr==TUInt::Mx;}
00048   void Clr(){Seg=0; Addr=TUInt::Mx;}
00049   void PutSeg(const uchar& _Seg){Seg=_Seg;}
00050   uchar GetSeg() const {return Seg;}
00051   void PutAddr(const uint& _Addr){Addr=_Addr;}
00052   uint GetAddr() const {return Addr;}
00053   void PutFlag(const int& FlagN, const bool& Val);
00054   bool IsFlag(const int& FlagN) const;
00055   void MergeFlags(const TBlobPt& Pt){
00056     FSet1|=Pt.FSet1; FSet2|=Pt.FSet2; FSet3|=Pt.FSet3;}
00057   void PutFSet(const int& FSetN, const TB8Set& FSet);
00058   TB8Set GetFSet(const int& FSetN);
00059 
00060   static TBlobPt Load(const PFRnd& FRnd){
00061     uchar Seg=FRnd->GetUCh(); uint Addr=FRnd->GetUInt();
00062     TB8Set B8Set1(FRnd->GetUCh()); TB8Set B8Set2(FRnd->GetUCh());
00063     TB8Set B8Set3(FRnd->GetUCh());
00064     return TBlobPt(Seg, Addr, B8Set1, B8Set2, B8Set3);}
00065   void Save(const PFRnd& FRnd) const {
00066     FRnd->PutUCh(Seg); FRnd->PutUInt(Addr);
00067     FRnd->PutUCh(FSet1.GetUCh()); FRnd->PutUCh(FSet2.GetUCh());
00068     FRnd->PutUCh(FSet3.GetUCh());}
00069   static TBlobPt LoadAddr(const PFRnd& FRnd, const uchar& Seg=0){
00070     return TBlobPt(Seg, FRnd->GetUInt());}
00071   void SaveAddr(const PFRnd& FRnd) const {
00072     FRnd->PutUInt(Addr);}
00073 
00074   TStr GetAddrStr() const {
00075     TChA AddrChA; AddrChA+=TInt::GetStr(Seg); AddrChA+=':';
00076     AddrChA+=TUInt::GetStr(Addr); return AddrChA;}
00077 
00078   TStr GetStr() const;
00079 };
00080 
00082 // Blob-Base
00083 typedef enum {bbsUndef, bbsOpened, bbsClosed} TBlobBsState;
00084 typedef enum {btUndef, btBegin, btEnd} TBlobTag;
00085 typedef enum {bsUndef, bsActive, bsFree} TBlobState;
00086 
00087 ClassTPV(TBlobBs, PBlobBs, TBlobBsV)//{
00088 public:
00089   static const int MnBlobBfL;
00090   static const int MxBlobFLen;
00091   UndefCopyAssign(TBlobBs);
00092 public:
00093   TBlobBs(){}
00094   virtual ~TBlobBs(){}
00095   TBlobBs(TSIn&){Fail;}
00096   static PBlobBs Load(TSIn&){Fail; return NULL;}
00097   void Save(TSOut&) const {Fail;}
00098 
00099   virtual TStr GetVersionStr() const=0;
00100   void PutVersionStr(const PFRnd& FBlobBs);
00101   void AssertVersionStr(const PFRnd& FBlobBs);
00102 
00103   TStr GetBlobBsStateStr(const TBlobBsState& BlobBsState);
00104   int GetStateStrLen(){return 6;}
00105   void PutBlobBsStateStr(const PFRnd& FBlobBs, const TBlobBsState& State);
00106   void AssertBlobBsStateStr(const PFRnd& FBlobBs, const TBlobBsState& State);
00107 
00108   static const TStr MxSegLenVNm;
00109   void PutMxSegLen(const PFRnd& FBlobBs, const int& MxSegLen);
00110   int GetMxSegLen(const PFRnd& FBlobBs);
00111 
00112   static const TStr BlockLenVNm;
00113   void GenBlockLenV(TIntV& BlockLenV);
00114   void PutBlockLenV(const PFRnd& FBlobBs, const TIntV& BlockLenV);
00115   void GetBlockLenV(const PFRnd& FBlobBs, TIntV& BlockLenV);
00116 
00117   static const TStr FFreeBlobPtVNm;
00118   void GenFFreeBlobPtV(const TIntV& BlockLenV, TBlobPtV& FFreeBlobPtV);
00119   void PutFFreeBlobPtV(const PFRnd& FBlobBs, const TBlobPtV& FFreeBlobPtV);
00120   void GetFFreeBlobPtV(const PFRnd& FBlobBs, TBlobPtV& FFreeBlobPtV);
00121 
00122   void GetAllocInfo(
00123    const int& BfL, const TIntV& BlockLenV, int& MxBfL, int& FFreeBlobPtN);
00124 
00125   uint GetBeginBlobTag(){return 0xABCDEFFF;}
00126   uint GetEndBlobTag(){return 0xFFFEDCBA;}
00127   void PutBlobTag(const PFRnd& FBlobBs, const TBlobTag& BlobTag);
00128   void AssertBlobTag(const PFRnd& FBlobBs, const TBlobTag& BlobTag);
00129 
00130   void PutBlobState(const PFRnd& FBlobBs, const TBlobState& State);
00131   TBlobState GetBlobState(const PFRnd& FBlobBs);
00132   void AssertBlobState(const PFRnd& FBlobBs, const TBlobState& State);
00133 
00134   void AssertBfCsEqFlCs(const TCs& BfCs, const TCs& FCs);
00135 
00136   virtual TBlobPt PutBlob(const PSIn& SIn)=0;
00137   TBlobPt PutBlob(const TStr& Str){
00138     PSIn SIn=TStrIn::New(Str); return PutBlob(SIn);}
00139   virtual TBlobPt PutBlob(const TBlobPt& BlobPt, const PSIn& SIn)=0;
00140   virtual PSIn GetBlob(const TBlobPt& BlobPt)=0;
00141   virtual void DelBlob(const TBlobPt& BlobPt)=0;
00142 
00143   virtual TBlobPt GetFirstBlobPt()=0;
00144   virtual TBlobPt FFirstBlobPt()=0;
00145   virtual bool FNextBlobPt(TBlobPt& TrvBlobPt, TBlobPt& BlobPt, PSIn& BlobSIn)=0;
00146   bool FNextBlobPt(TBlobPt& TrvBlobPt, PSIn& BlobSIn){
00147     TBlobPt BlobPt; return FNextBlobPt(TrvBlobPt, BlobPt, BlobSIn);}
00148 };
00149 
00151 // General-Blob-Base
00152 class TGBlobBs: public TBlobBs{
00153 private:
00154   PFRnd FBlobBs;
00155   TFAccess Access;
00156   int MxSegLen;
00157   TIntV BlockLenV;
00158   TBlobPtV FFreeBlobPtV;
00159   TBlobPt FirstBlobPt;
00160   static TStr GetNrBlobBsFNm(const TStr& BlobBsFNm);
00161 public:
00162   TGBlobBs(const TStr& BlobBsFNm, const TFAccess& _Access=faRdOnly,
00163    const int& _MxSegLen=-1);
00164   static PBlobBs New(const TStr& BlobBsFNm, const TFAccess& Access=faRdOnly,
00165    const int& MxSegLen=-1){
00166     return PBlobBs(new TGBlobBs(BlobBsFNm, Access, MxSegLen));}
00167   ~TGBlobBs();
00168 
00169   TGBlobBs& operator=(const TGBlobBs&){Fail; return *this;}
00170 
00171   TStr GetVersionStr() const {return TStr("General Blob Base Format 1.0");}
00172   TBlobPt PutBlob(const PSIn& SIn);
00173   TBlobPt PutBlob(const TBlobPt& BlobPt, const PSIn& SIn);
00174   PSIn GetBlob(const TBlobPt& BlobPt);
00175   void DelBlob(const TBlobPt& BlobPt);
00176 
00177   TBlobPt GetFirstBlobPt(){return FirstBlobPt;}
00178   TBlobPt FFirstBlobPt();
00179   bool FNextBlobPt(TBlobPt& TrvBlobPt, TBlobPt& BlobPt, PSIn& BlobSIn);
00180 
00181   static bool Exists(const TStr& BlobBsFNm);
00182 };
00183 
00185 // Multiple-File-Blob-Base
00186 class TMBlobBs: public TBlobBs{
00187 private:
00188   TFAccess Access;
00189   int MxSegLen;
00190   TStr NrFPath, NrFMid;
00191   TBlobBsV SegV;
00192   int CurSegN;
00193   static void GetNrFPathFMid(const TStr& BlobBsFNm, TStr& NrFPath, TStr& NrFMid);
00194   static TStr GetMainFNm(const TStr& NrFPath, const TStr& NrFMid);
00195   static TStr GetSegFNm(const TStr& NrFPath, const TStr& NrFMid, const int& SegN);
00196   void LoadMain(int& Segs);
00197   void SaveMain() const;
00198 public:
00199   TMBlobBs(const TStr& BlobBsFNm, const TFAccess& _Access=faRdOnly,
00200    const int& _MxSegLen=-1);
00201   static PBlobBs New(const TStr& BlobBsFNm, const TFAccess& Access=faRdOnly,
00202    const int& MxSegLen=-1){
00203     return PBlobBs(new TMBlobBs(BlobBsFNm, Access, MxSegLen));}
00204   ~TMBlobBs();
00205 
00206   TMBlobBs& operator=(const TMBlobBs&){Fail; return *this;}
00207 
00208   TStr GetVersionStr() const {
00209     return TStr("Multiple-File Blob Base Format 1.0");}
00210   TBlobPt PutBlob(const PSIn& SIn);
00211   TBlobPt PutBlob(const TBlobPt& BlobPt, const PSIn& SIn);
00212   PSIn GetBlob(const TBlobPt& BlobPt);
00213   void DelBlob(const TBlobPt& BlobPt);
00214 
00215   TBlobPt GetFirstBlobPt();
00216   TBlobPt FFirstBlobPt();
00217   bool FNextBlobPt(TBlobPt& TrvBlobPt, TBlobPt& BlobPt, PSIn& BlobSIn);
00218 
00219   static bool Exists(const TStr& BlobBsFNm);
00220 };
00221