#include "TBasket.h"
#include "TBufferFile.h"
#include "TTree.h"
#include "TBranch.h"
#include "TFile.h"
#include "TBufferFile.h"
#include "TMath.h"
#include "TTreeCache.h"
#include "TTreeCacheUnzip.h"
extern "C" void R__zip (Int_t cxlevel, Int_t *nin, char *bufin, Int_t *lout, char *bufout, Int_t *nout);
extern "C" void R__unzip(Int_t *nin, UChar_t *bufin, Int_t *lout, char *bufout, Int_t *nout);
const Int_t kMAXBUF = 0xFFFFFF;
const UInt_t kDisplacementMask = 0xFF000000;
ClassImp(TBasket)
TBasket::TBasket()
{
fDisplacement = 0;
fEntryOffset = 0;
fBufferRef = 0;
fBuffer = 0;
fHeaderOnly = kFALSE;
fBufferSize = 0;
fNevBufSize = 0;
fNevBuf = 0;
fLast = 0;
fBranch = 0;
}
TBasket::TBasket(TDirectory *motherDir) : TKey(motherDir)
{
fDisplacement = 0;
fEntryOffset = 0;
fBufferRef = 0;
fBuffer = 0;
fHeaderOnly = kFALSE;
fBufferSize = 0;
fNevBufSize = 0;
fNevBuf = 0;
fLast = 0;
fBranch = 0;
}
TBasket::TBasket(const char *name, const char *title, TBranch *branch) :
TKey(branch->GetDirectory())
{
SetName(name);
SetTitle(title);
fClassName = "TBasket";
fBufferSize = branch->GetBasketSize();
fNevBufSize = branch->GetEntryOffsetLen();
fNevBuf = 0;
fEntryOffset = 0;
fDisplacement= 0;
fBuffer = 0;
fBufferRef = new TBufferFile(TBuffer::kWrite, fBufferSize);
fVersion += 1000;
if (branch->GetDirectory()) {
TFile *file = branch->GetFile();
fBufferRef->SetParent(file);
}
fHeaderOnly = kTRUE;
fLast = 0;
Streamer(*fBufferRef);
fKeylen = fBufferRef->Length();
fObjlen = fBufferSize - fKeylen;
fLast = fKeylen;
fBuffer = 0;
fBranch = branch;
fHeaderOnly = kFALSE;
if (fNevBufSize) {
fEntryOffset = new Int_t[fNevBufSize];
for (Int_t i=0;i<fNevBufSize;i++) fEntryOffset[i] = 0;
}
branch->GetTree()->IncrementTotalBuffers(fBufferSize);
}
TBasket::~TBasket()
{
if (fDisplacement) delete [] fDisplacement;
if (fEntryOffset) delete [] fEntryOffset;
fDisplacement= 0;
fEntryOffset = 0;
}
void TBasket::AdjustSize(Int_t newsize)
{
char *newbuf = TStorage::ReAllocChar(fBuffer,newsize,fBufferSize);
fBufferSize = newsize;
fBuffer = newbuf;
}
Long64_t TBasket::CopyTo(TFile *to)
{
fBufferRef->SetWriteMode();
Int_t nout = fNbytes - fKeylen;
fBuffer = fBufferRef->Buffer();
Create(nout, to);
fBufferRef->SetBufferOffset(0);
fHeaderOnly = kTRUE;
Streamer(*fBufferRef);
fHeaderOnly = kFALSE;
Int_t nBytes = WriteFile(0, to);
return nBytes>0 ? nBytes : -1;
}
void TBasket::DeleteEntryOffset()
{
if (fEntryOffset) delete [] fEntryOffset;
fEntryOffset = 0;
fNevBufSize = 0;
}
Int_t TBasket::DropBuffers()
{
if (!fBuffer && !fBufferRef) return 0;
if (fDisplacement) delete [] fDisplacement;
if (fEntryOffset) delete [] fEntryOffset;
if (fBufferRef) delete fBufferRef;
fBufferRef = 0;
fBuffer = 0;
fDisplacement= 0;
fEntryOffset = 0;
fBranch->GetTree()->IncrementTotalBuffers(-fBufferSize);
return fBufferSize;
}
Int_t TBasket::GetEntryPointer(Int_t entry)
{
Int_t offset;
if (fEntryOffset) offset = fEntryOffset[entry];
else offset = fKeylen + entry*fNevBufSize;
fBufferRef->SetBufferOffset(offset);
return offset;
}
Int_t TBasket::LoadBasketBuffers(Long64_t pos, Int_t len, TFile *file)
{
fBufferRef = new TBufferFile(TBuffer::kRead, len);
fBufferRef->SetParent(file);
char *buffer = fBufferRef->Buffer();
file->Seek(pos);
if (file->ReadBuffer(buffer,len)) {
return 1;
}
fBufferRef->SetReadMode();
fBufferRef->SetBufferOffset(0);
Streamer(*fBufferRef);
return 0;
}
void TBasket::MoveEntries(Int_t dentries)
{
Int_t i;
if (dentries >= fNevBuf) return;
Int_t bufbegin;
Int_t moved;
if (fEntryOffset) {
bufbegin = fEntryOffset[dentries];
moved = bufbegin-GetKeylen();
if (!fDisplacement) {
fDisplacement = new Int_t[fNevBufSize];
}
for (i = 0; i<(fNevBufSize-dentries); ++i) {
fDisplacement[i] = fEntryOffset[i+dentries];
fEntryOffset[i] = fEntryOffset[i+dentries] - moved;
}
for (i = fNevBufSize-dentries; i<fNevBufSize; ++i) {
fDisplacement[i] = 0;
fEntryOffset[i] = 0;
}
} else {
bufbegin = GetKeylen() + dentries*fNevBufSize;
moved = bufbegin-GetKeylen();
}
TBuffer *buf = GetBufferRef();
char *buffer = buf->Buffer();
memmove(buffer+GetKeylen(),buffer+bufbegin,buf->Length()-bufbegin);
buf->SetBufferOffset(buf->Length()-moved);
fNevBuf -= dentries;
}
Int_t TBasket::ReadBasketBuffers(Long64_t pos, Int_t len, TFile *file)
{
Int_t badread= 0;
TDirectory *cursav = gDirectory;
TDirectory *brdir = fBranch->GetDirectory();
if(!brdir) {
return -1;
}
brdir->cd();
if (fBranch->GetTree()->MemoryFull(fBufferSize)) fBranch->DropBaskets();
TFileCacheRead *pf = file->GetCacheRead();
if (pf && pf->InheritsFrom(TTreeCacheUnzip::Class())) {
TTreeCacheUnzip *tpfu = (TTreeCacheUnzip*)pf;
char *buffer = 0;
Bool_t free = kTRUE;
Int_t res = tpfu->GetUnzipBuffer(&buffer, pos, len, &free);
if (res == -1) {
badread = 1;
return badread;
}
fBufferRef = new TBufferFile(TBuffer::kRead, res, buffer, free);
fBufferRef->SetParent(file);
Streamer(*fBufferRef);
Bool_t oldCase = fObjlen==fNbytes-fKeylen
&& GetBranch()->GetCompressionLevel()!=0
&& file->GetVersion()<=30401;
if (fObjlen > fNbytes-fKeylen || oldCase) {
if (TestBit(TBufferFile::kNotDecompressed) && (fNevBuf==1)) {
fBuffer = fBufferRef->Buffer();
fBufferRef->SetBufferOffset(fNbytes);
fBufferRef->SetBit(TBufferFile::kNotDecompressed);
delete [] fEntryOffset; fEntryOffset = 0;
delete [] fDisplacement; fDisplacement = 0;
fBranch->GetTree()->IncrementTotalBuffers(fBufferSize);
return badread;
}
}
fBuffer = fBufferRef->Buffer();
len = fObjlen+fKeylen;
}
else{
fBufferRef = new TBufferFile(TBuffer::kRead, len);
fBufferRef->SetParent(file);
char *buffer = fBufferRef->Buffer();
file->Seek(pos);
if (file->ReadBuffer(buffer,len)) {
badread = 1;
return badread;
}
Streamer(*fBufferRef);
Bool_t oldCase = fObjlen==fNbytes-fKeylen
&& GetBranch()->GetCompressionLevel()!=0
&& file->GetVersion()<=30401;
if (fObjlen > fNbytes-fKeylen || oldCase) {
if (TestBit(TBufferFile::kNotDecompressed) && (fNevBuf==1)) {
fBuffer = fBufferRef->Buffer();
fBufferRef->SetBufferOffset(fNbytes);
fBufferRef->SetBit(TBufferFile::kNotDecompressed);
delete [] fEntryOffset; fEntryOffset = 0;
delete [] fDisplacement; fDisplacement = 0;
fBranch->GetTree()->IncrementTotalBuffers(fBufferSize);
return badread;
}
fBuffer = new char[fObjlen+fKeylen];
memcpy(fBuffer,buffer,fKeylen);
char *objbuf = fBuffer + fKeylen;
UChar_t *bufcur = (UChar_t *)&buffer[fKeylen];
Int_t nin, nout, nbuf;
Int_t noutot = 0;
while (1) {
nin = 9 + ((Int_t)bufcur[3] | ((Int_t)bufcur[4] << 8) | ((Int_t)bufcur[5] << 16));
nbuf = (Int_t)bufcur[6] | ((Int_t)bufcur[7] << 8) | ((Int_t)bufcur[8] << 16);
if (oldCase && (nin > fObjlen || nbuf > fObjlen)) {
delete [] fBuffer;
fBuffer = fBufferRef->Buffer();
goto AfterBuffer;
}
R__unzip(&nin, bufcur, &nbuf, objbuf, &nout);
if (!nout) break;
noutot += nout;
if (noutot >= fObjlen) break;
bufcur += nin;
objbuf += nout;
}
if (noutot != fObjlen) {
Error("ReadBasketBuffers", "fNbytes = %d, fKeylen = %d, fObjlen = %d, noutot = %d, nout=%d, nin=%d, nbuf=%d", fNbytes,fKeylen,fObjlen, noutot,nout,nin,nbuf);
badread = 1;
}
fBufferRef->SetBuffer(fBuffer, fObjlen+fKeylen );
len = fObjlen+fKeylen;
} else {
fBuffer = fBufferRef->Buffer();
}
}
AfterBuffer:
cursav->cd();
fBranch->GetTree()->IncrementTotalBuffers(fBufferSize);
if (!fBranch->GetEntryOffsetLen()) {
return badread;
}
delete [] fEntryOffset;
fEntryOffset = 0;
fBufferRef->SetBufferOffset(fLast);
fBufferRef->ReadArray(fEntryOffset);
if (!fEntryOffset) {
fEntryOffset = new Int_t[fNevBuf+1];
fEntryOffset[0] = fKeylen;
Warning("ReadBasketBuffers","basket:%s has fNevBuf=%d but fEntryOffset=0, pos=%lld, len=%d, fNbytes=%d, fObjlen=%d, trying to repair",GetName(),fNevBuf,pos,len,fNbytes,fObjlen);
return badread;
}
delete [] fDisplacement;
fDisplacement = 0;
if (fBufferRef->Length() != len) {
fBufferRef->ReadArray(fDisplacement);
}
return badread;
}
Int_t TBasket::ReadBasketBytes(Long64_t pos, TFile *file)
{
const Int_t len = 128;
char buffer[len];
Int_t keylen;
file->GetRecordHeader(buffer, pos,len, fNbytes, fObjlen, keylen);
fKeylen = keylen;
return fNbytes;
}
void TBasket::SetReadMode()
{
fLast = fBufferRef->Length();
fBufferRef->SetReadMode();
}
void TBasket::SetWriteMode()
{
fBufferRef->SetWriteMode();
fBufferRef->SetBufferOffset(fLast);
}
void TBasket::Streamer(TBuffer &b)
{
char flag;
if (b.IsReading()) {
TKey::Streamer(b);
Version_t v = b.ReadVersion();
b >> fBufferSize;
b >> fNevBufSize;
b >> fNevBuf;
b >> fLast;
b >> flag;
if (fLast > fBufferSize) fBufferSize = fLast;
if (!flag) return;
if (flag%10 != 2) {
delete [] fEntryOffset;
fEntryOffset = new Int_t[fNevBufSize];
if (fNevBuf) b.ReadArray(fEntryOffset);
if (20<flag && flag<40) {
for(int i=0; i<fNevBuf; i++){
fEntryOffset[i] &= ~kDisplacementMask;
}
}
if (flag>40) {
fDisplacement = new Int_t[fNevBufSize];
b.ReadArray(fDisplacement);
}
}
if (flag == 1 || flag > 10) {
fBufferRef = new TBufferFile(TBuffer::kRead,fBufferSize);
fBufferRef->SetParent(b.GetParent());
char *buf = fBufferRef->Buffer();
if (v > 1) b.ReadFastArray(buf,fLast);
else b.ReadArray(buf);
fBufferRef->SetBufferOffset(fLast);
}
} else {
TKey::Streamer(b);
b.WriteVersion(TBasket::IsA());
Int_t curLast = fBufferRef->Length();
if (fBufferRef && !fHeaderOnly && !fSeekKey && curLast > fLast) fLast = curLast;
if (fLast > fBufferSize) fBufferSize = fLast;
b << fBufferSize;
b << fNevBufSize;
b << fNevBuf;
b << fLast;
flag = 1;
if (!fEntryOffset) flag = 2;
if (fBufferRef) flag += 10;
if (fDisplacement) flag += 40;
if (fHeaderOnly) flag = 0;
b << flag;
if (fHeaderOnly) return;
if (fEntryOffset && fNevBuf) {
b.WriteArray(fEntryOffset, fNevBuf);
if (fDisplacement) b.WriteArray(fDisplacement, fNevBuf);
}
if (fBufferRef) {
char *buf = fBufferRef->Buffer();
b.WriteFastArray(buf, fLast);
}
}
}
void TBasket::Update(Int_t offset, Int_t skipped)
{
if (fEntryOffset) {
if (fNevBuf+1 >= fNevBufSize) {
Int_t newsize = TMath::Max(10,2*fNevBufSize);
Int_t *newoff = TStorage::ReAllocInt(fEntryOffset, newsize,
fNevBufSize);
if (fDisplacement) {
Int_t *newdisp = TStorage::ReAllocInt(fDisplacement, newsize,
fNevBufSize);
fDisplacement = newdisp;
}
fEntryOffset = newoff;
fNevBufSize = newsize;
if (fBranch->GetWriteBasket() < 10) fBranch->SetEntryOffsetLen(newsize);
}
fEntryOffset[fNevBuf] = offset;
if (skipped!=offset && !fDisplacement){
fDisplacement = new Int_t[fNevBufSize];
for (Int_t i = 0; i<fNevBufSize; i++) fDisplacement[i] = fEntryOffset[i];
}
if (fDisplacement) {
fDisplacement[fNevBuf] = skipped;
fBufferRef->SetBufferDisplacement(skipped);
}
}
fNevBuf++;
}
Int_t TBasket::WriteBuffer()
{
const Int_t kWrite = 1;
TDirectory::TContext ctxt(0);
TFile *file = fBranch->GetFile(kWrite);
if (!file) return 0;
if (!file->IsWritable()) {
return -1;
}
fMotherDir = file;
if (fBufferRef->TestBit(TBufferFile::kNotDecompressed)) {
Bool_t writing = fBufferRef->IsWriting();
fBufferRef->SetReadMode();
fBufferRef->SetBufferOffset(0);
Streamer(*fBufferRef);
if (writing) fBufferRef->SetWriteMode();
Int_t nout = fNbytes - fKeylen;
fBuffer = fBufferRef->Buffer();
Create(nout,file);
fBufferRef->SetBufferOffset(0);
fHeaderOnly = kTRUE;
Streamer(*fBufferRef);
int nBytes = WriteFile(0);
fHeaderOnly = kFALSE;
return nBytes>0 ? fKeylen+nout : -1;
}
fLast = fBufferRef->Length();
if (fEntryOffset) {
fBufferRef->WriteArray(fEntryOffset,fNevBuf+1);
delete [] fEntryOffset; fEntryOffset = 0;
if (fDisplacement) {
fBufferRef->WriteArray(fDisplacement,fNevBuf+1);
delete [] fDisplacement; fDisplacement = 0;
}
}
Int_t lbuf, nout, noutot, bufmax, nzip;
lbuf = fBufferRef->Length();
fObjlen = lbuf - fKeylen;
fHeaderOnly = kTRUE;
fCycle = fBranch->GetWriteBasket();
Int_t cxlevel = fBranch->GetCompressionLevel();
if (cxlevel > 0) {
Int_t nbuffers = fObjlen/kMAXBUF;
Int_t buflen = fKeylen + fObjlen + 28;
fBuffer = new char[buflen];
char *objbuf = fBufferRef->Buffer() + fKeylen;
char *bufcur = &fBuffer[fKeylen];
noutot = 0;
nzip = 0;
for (Int_t i=0;i<=nbuffers;i++) {
if (i == nbuffers) bufmax = fObjlen -nzip;
else bufmax = kMAXBUF;
R__zip(cxlevel, &bufmax, objbuf, &bufmax, bufcur, &nout);
if (nout == 0 || nout >= fObjlen) {
nout = fObjlen;
delete [] fBuffer;
fBuffer = fBufferRef->Buffer();
Create(fObjlen,file);
fBufferRef->SetBufferOffset(0);
Streamer(*fBufferRef);
if ((nout+fKeylen)>buflen) {
Warning("WriteBuffer","Possible memory corruption due to compression algorithm, wrote %d bytes past the end of a block of %d bytes. fNbytes=%d, fObjLen=%d, fKeylen=%d",
(nout+fKeylen-buflen),buflen,fNbytes,fObjlen,fKeylen);
}
goto WriteFile;
}
bufcur += nout;
noutot += nout;
objbuf += kMAXBUF;
nzip += kMAXBUF;
}
nout = noutot;
Create(noutot,file);
fBufferRef->SetBufferOffset(0);
Streamer(*fBufferRef);
memcpy(fBuffer,fBufferRef->Buffer(),fKeylen);
delete fBufferRef; fBufferRef = 0;
} else {
fBuffer = fBufferRef->Buffer();
Create(fObjlen,file);
fBufferRef->SetBufferOffset(0);
Streamer(*fBufferRef);
nout = fObjlen;
}
WriteFile:
Int_t nBytes = WriteFile(0);
fHeaderOnly = kFALSE;
return nBytes>0 ? fKeylen+nout : -1;
}