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