Update contrib.
1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // Store database compression code
20 #include "U32STD_DBMS.H"
23 #define __EXTRA_DEFLATE
26 // deflation constants
27 const TInt KDeflateMinLength=3;
28 const TInt KDeflateMaxLength=258;
29 const TInt KDeflateMaxDistance=4096;
30 const TInt KDeflateDistCodeBase=0x200;
31 // huffman coding/decoding
32 const TInt KHuffMaxCodeLength=25;
33 const TInt KHuffTerminate=1;
34 const TUint KBitsEmpty=0x80000000u;
35 const TUint KBitsInit=KBitsEmpty>>1;
36 const TUint KBitsFull=KBitsEmpty>>8;
37 const TUint KBitsEOF=KBitsEmpty>>9;
38 const TUint KBitsNext=0x80u;
40 const TInt KDeflateMetaCodes=26;
42 const TUint KDeflateHashMultiplier=0xAC4B9B19u;
43 const TInt KDeflateHashShift=24;
48 static void EncodingL(TUint32* aEncoding,TInt aCodes);
49 static void Decoding(TUint32* aDecoding,TInt aCodes,TInt aBase=0);
51 typedef TUint16 THuff;
64 static void Lengths(TUint32* aLengths,const TNode* aNodes,TInt aNode,TInt aLen);
65 static TUint32* SubTree(TUint32* aPtr,const TUint32* aValue,TUint32** aLevel);
71 THuffEncoder(RWriteStream& aStream);
73 void EncodeL(TUint aCode,TInt aLength);
74 void EncodeL(TUint32 aHuffCode);
77 enum {EBufSize=0x100};
79 TUint8 iBuf[EBufSize];
80 RWriteStream& iStream;
81 TUint32 iCode; // code in production
89 inline static HDeflateHash& NewLC(TInt aLinks);
91 inline TInt First(const TUint8* aPtr,TInt aPos);
92 inline TInt Next(TInt aPos,TInt aOffset) const;
94 inline HDeflateHash();
95 inline static TInt Hash(const TUint8* aPtr);
97 typedef TUint16 TOffset;
100 TOffset iOffset[1]; // or more
106 void DeflateL(const TUint8* aBase,TInt aLength);
108 const TUint8* DoDeflateL(const TUint8* aBase,const TUint8* aEnd,HDeflateHash& aHash);
109 static TInt Match(const TUint8* aPtr,const TUint8* aEnd,TInt aPos,HDeflateHash& aHas);
110 void SegmentL(TInt aLength,TInt aDistance);
111 virtual void LitLenL(TInt aCode) =0;
112 virtual void OffsetL(TInt aCode) =0;
113 virtual void ExtraL(TInt aLen,TUint aBits) =0;
119 TInflater(const TUint8* aIn,const CDbStoreCompression::TEncoding& aDecoding);
121 inline const TUint8* Ptr() const;
122 inline static TInt BufferSize();
126 const TUint8* iRptr; // partial segment
128 const TUint32* iLitLenTree;
129 const TUint32* iDistTree;
130 TUint8 iOut[KDeflateMaxDistance]; // circular buffer for distance matches
133 NONSHARABLE_CLASS(TDeflateStats) : public MDeflater
136 inline TDeflateStats(CDbStoreCompression::TEncoding& aEncoding);
139 void LitLenL(TInt aCode);
140 void OffsetL(TInt aCode);
141 void ExtraL(TInt aLen,TUint aBits);
143 CDbStoreCompression::TEncoding& iEncoding;
146 NONSHARABLE_CLASS(TDeflater) : public MDeflater
149 inline TDeflater(THuffEncoder& aEncoder,const CDbStoreCompression::TEncoding& aEncoding);
152 void LitLenL(TInt aCode);
153 void OffsetL(TInt aCode);
154 void ExtraL(TInt aLen,TUint aBits);
156 THuffEncoder& iEncoder;
157 const CDbStoreCompression::TEncoding& iEncoding;
160 NONSHARABLE_CLASS(HDeflateBuf) : public TBufBuf
163 enum TMode {EAnalysis,EDeflate}; // mirror CDbStoreCompression enum
165 static HDeflateBuf* NewL(MStreamBuf* aHost,CDbStoreCompression::TEncoding& aEncoding,TMode aMode);
167 inline HDeflateBuf(MStreamBuf* aHost,CDbStoreCompression::TEncoding& aEncoding,CBufBase* aBuf,TMode aMode);
168 virtual inline ~HDeflateBuf();
174 CDbStoreCompression::TEncoding& iEncoding;
179 NONSHARABLE_CLASS(HInflateBuf) : public TBufBuf
182 static HInflateBuf* NewL(MStreamBuf* aHost,const CDbStoreCompression::TEncoding& aEncoding);
184 inline HInflateBuf(CBufBase* aBuf);
185 virtual inline ~HInflateBuf();
192 NONSHARABLE_CLASS(CDbStoreTable::CCompressor) : public CBase, public CCluster::MAlter
195 inline CCompressor();
197 void ProcessL(CDbStoreTable* aTable);
199 TUint8* AlterRecordL(TUint8* aWPtr,const TUint8* aRPtr,TInt aLength);
201 CDbStoreTable* iTable;
207 // This class builds a huffman encoding from a frequency table and builds
208 // a decoding tree from a code-lengths table
210 // the encoding generated is based on the rule that given two symbols s1 and s2, with
211 // code length l1 and l2, and huffman codes h1 and h2:
213 // if l1<l2 then h1<h2 when compared lexicographically
214 // if l1==l2 and s1<s2 then h1<h2 ditto
216 // This allows the encoding to be stored compactly as a table of code lengths
219 // recursive function to calculate the code lengths from the node tree
221 void Huffman::Lengths(TUint32* aLengths,const TNode* aNodes,TInt aNode,TInt aLen)
223 __ASSERT(aLen<KHuffMaxCodeLength);
225 const TNode& node=aNodes[aNode];
226 if (node.iLeft&KLeaf)
227 aLengths[node.iLeft&~KLeaf]=aLen;
229 Lengths(aLengths,aNodes,node.iLeft,aLen);
230 if (node.iRight&KLeaf)
231 aLengths[node.iRight&~KLeaf]=aLen;
233 Lengths(aLengths,aNodes,node.iRight,aLen);
237 // write the subtree below aPtr and return the head
239 TUint32* Huffman::SubTree(TUint32* aPtr,const TUint32* aValue,TUint32** aLevel)
241 TUint32* l=*aLevel++;
244 TUint32* sub1=SubTree(aPtr,aValue,aLevel); // 0-tree first
245 aPtr=SubTree(sub1,aValue-(aPtr-sub1)-1,aLevel); // 1-tree
246 TInt branch=(TUint8*)sub1-(TUint8*)aPtr;
251 TUint term0=*aValue--; // 0-term
252 aPtr=SubTree(aPtr,aValue,aLevel); // 1-tree
257 TUint term0=*aValue--; // 0-term
258 TUint term1=*aValue--;
259 *--aPtr=(term1>>16<<16)|(term0>>16);
265 // Build a huffman encoding table from a symbol frequency table
266 // aTable contains frequency data on input for aCodes symbols
267 // aTable contains the huffman encoding on output
269 void Huffman::EncodingL(TUint32* aTable,TInt aCodes)
272 // step 1. Sort the values into decreasing order of frequency
274 TLeaf* leaves=new(ELeave) TLeaf[aCodes];
275 CleanupArrayDeletePushL(leaves);
278 for (ii=0;ii<aCodes;++ii)
282 continue; // no coding for ii
284 for (jj=0;jj<lCount;++jj)
286 if (leaves[jj].iCount<=c)
289 Mem::Move(leaves+jj+1,leaves+jj,sizeof(TLeaf)*(lCount-jj));
291 leaves[jj].iVal=THuff(ii|KLeaf);
295 // Huffman algorithm: pair off least frequent nodes and reorder
296 // result is the code lengths in aTable[]
298 if (lCount==1) // special case for a single value (always encode as "0")
299 aTable[leaves[0].iVal&~KLeaf]=1;
301 { // don't encode for empty coding: leaves in order now
303 TNode* nodes=new(ELeave) TNode[lCount-1];
306 TNode& node=nodes[max];
307 node.iLeft=leaves[lCount-1].iVal;
308 node.iRight=leaves[lCount].iVal;
309 // re-order the leaves now to reflect new combined frequency
310 TUint c=leaves[lCount-1].iCount+leaves[lCount].iCount;
314 if (leaves[jj-1].iCount>=c)
317 Mem::Move(leaves+jj+1,leaves+jj,sizeof(TLeaf)*(lCount-1-jj));
320 leaves[jj].iVal=THuff(max);
323 Lengths(aTable,nodes,leaves[0].iVal,0);
326 CleanupStack::PopAndDestroy(); // leaves
328 // step 3: Generate the coding based on the code lengths
330 TInt lenCount[KHuffMaxCodeLength];
331 Mem::FillZ(lenCount,sizeof(lenCount));
333 for (ii=aCodes;--ii>=0;)
335 TInt len=aTable[ii]-1;
340 TUint nextCode[KHuffMaxCodeLength];
342 for (ii=0;ii<KHuffMaxCodeLength;++ii)
345 code=(code+lenCount[ii])<<1;
348 for (ii=0;ii<aCodes;++ii)
353 aTable[ii] = (nextCode[len-1]<<(KHuffMaxCodeLength-len))|(len<<KHuffMaxCodeLength);
360 // generate the decoding tree from the huffman code length data
361 // output alphabet is [aBase,aBase+aCodes)
363 void Huffman::Decoding(TUint32* aDecoding,TInt aCodes,TInt aBase)
365 TInt counts[KHuffMaxCodeLength];
366 Mem::FillZ(counts,sizeof(counts));
371 TUint len=aDecoding[ii];
372 __ASSERT(len<=(TUint)KHuffMaxCodeLength);
380 TUint32* level[KHuffMaxCodeLength];
381 TUint32* lit=aDecoding+codes;
382 for (ii=0;ii<KHuffMaxCodeLength;++ii)
387 aBase=(aBase<<17)+(KHuffTerminate<<16);
388 for (ii=0;ii<aCodes;++ii)
390 TUint len=TUint8(aDecoding[ii]);
392 *--level[len-1]|=(ii<<17)+aBase;
394 if (codes==1) // codes==1 special case: tree is not complete
395 *aDecoding>>=16; // 0-terminate at root
398 __DEBUG(TUint32* p =)SubTree(aDecoding+codes-1,aDecoding+codes-1,level);
399 __ASSERT(p==aDecoding);
403 // Class HDeflateHash
405 inline HDeflateHash::HDeflateHash()
406 {TInt* p=iHash+256;do *--p=-KDeflateMaxDistance-1; while (p>iHash);}
408 inline HDeflateHash& HDeflateHash::NewLC(TInt aLinks)
410 __ASSERT(!(KDeflateMaxDistance&(KDeflateMaxDistance-1))); // ensure power of two
411 return *new(User::AllocLC(_FOFF(HDeflateHash,iOffset[Min(aLinks,KDeflateMaxDistance)]))) HDeflateHash;
414 inline TInt HDeflateHash::Hash(const TUint8* aPtr)
416 TUint x=aPtr[0]|(aPtr[1]<<8)|(aPtr[2]<<16);
417 return (x*KDeflateHashMultiplier)>>KDeflateHashShift;
420 inline TInt HDeflateHash::First(const TUint8* aPtr,TInt aPos)
423 TInt offset=Min(aPos-iHash[h],KDeflateMaxDistance<<1);
425 iOffset[aPos&(KDeflateMaxDistance-1)]=TOffset(offset);
429 inline TInt HDeflateHash::Next(TInt aPos,TInt aOffset) const
430 {return aOffset+iOffset[(aPos-aOffset)&(KDeflateMaxDistance-1)];}
435 // generic deflation algorithm, can do either statistics and the encoder
437 TInt MDeflater::Match(const TUint8* aPtr,const TUint8* aEnd,TInt aPos,HDeflateHash& aHash)
439 TInt offset=aHash.First(aPtr,aPos);
440 if (offset>KDeflateMaxDistance)
443 aEnd=Min(aEnd,aPtr+KDeflateMaxLength);
447 const TUint8* p=aPtr-offset;
449 { // might be a better match
450 const TUint8* m=aPtr;
457 return ((m-aPtr)<<16)|offset;
462 match=(l<<16)|offset;
466 offset=aHash.Next(aPos,offset);
467 } while (offset<=KDeflateMaxDistance);
472 // Apply the deflation algorithm to the data [aBase,aEnd)
473 // Return a pointer after the last byte that was deflated (which may not be aEnd)
475 const TUint8* MDeflater::DoDeflateL(const TUint8* aBase,const TUint8* aEnd,HDeflateHash& aHash)
477 __ASSERT(aEnd-aBase>KDeflateMinLength);
479 const TUint8* ptr=aBase;
480 #ifdef __EXTRA_DEFLATE
481 TInt prev=0; // the previous deflation match
485 TInt match=Match(ptr,aEnd,ptr-aBase,aHash);
486 #ifdef __EXTRA_DEFLATE
487 // Extra deflation applies two optimisations which double the time taken
488 // 1. If we have a match at p, then test for a better match at p+1 before using it
489 // 2. When we have a match, add the hash links for all the data which will be skipped
490 if (match>>16 < prev>>16)
491 { // use the previous match--it was better
493 SegmentL(len,prev-(len<<16));
494 // fill in missing hash entries for better compression
495 const TUint8* e=ptr+len-2;
499 aHash.First(ptr,ptr-aBase);
503 else if (match<=(KDeflateMinLength<<16))
504 LitLenL(*ptr); // no deflation match here
506 { // save this match and test the next position
507 if (prev) // we had a match at ptr-1, but this is better
513 // Basic deflation will store any match found, and not update the hash links for the
514 // data which is skipped
515 if (match<=(KDeflateMinLength<<16)) // no match
520 SegmentL(len,match-(len<<16));
524 } while (ptr+KDeflateMinLength-1<aEnd);
525 #ifdef __EXTRA_DEFLATE
527 { // emit the stored match
529 SegmentL(len,prev-(len<<16));
537 // The generic deflation algorithm
539 void MDeflater::DeflateL(const TUint8* aBase,TInt aLength)
541 const TUint8* end=aBase+aLength;
542 if (aLength>KDeflateMinLength)
543 { // deflation kicks in if there is enough data
544 HDeflateHash& hash=HDeflateHash::NewLC(aLength);
545 aBase=DoDeflateL(aBase,end,hash);
546 CleanupStack::PopAndDestroy();
548 while (aBase<end) // emit remaining bytes
550 LitLenL(CDbStoreCompression::TEncoding::EEos); // eos marker
554 // Turn a (length,offset) pair into the deflation codes+extra bits before calling
555 // the specific LitLen(), Offset() and Extra() functions.
557 void MDeflater::SegmentL(TInt aLength,TInt aDistance)
559 __ASSERT(aLength>=KDeflateMinLength && aLength<=KDeflateMaxLength);
560 aLength-=KDeflateMinLength;
568 __ASSERT((extralen<<2)+len<CDbStoreCompression::TEncoding::ELengths);
569 LitLenL((extralen<<2)+len+CDbStoreCompression::TEncoding::ELiterals);
571 ExtraL(extralen,TUint(aLength)<<(32-extralen));
573 __ASSERT(aDistance>0 && aDistance<=KDeflateMaxDistance);
576 TUint dist=aDistance;
582 __ASSERT((extralen<<2)+dist<CDbStoreCompression::TEncoding::EDistances);
583 OffsetL((extralen<<2)+dist);
585 ExtraL(extralen,TUint(aDistance)<<(32-extralen));
588 // Class TDeflateStats
590 // This class analyses the data stream to generate the frequency tables
591 // for the deflation algorithm
593 inline TDeflateStats::TDeflateStats(CDbStoreCompression::TEncoding& aEncoding)
594 :iEncoding(aEncoding)
597 void TDeflateStats::LitLenL(TInt aCode)
599 ++iEncoding.iLitLen[aCode];
602 void TDeflateStats::OffsetL(TInt aCode)
604 ++iEncoding.iDistance[aCode];
607 void TDeflateStats::ExtraL(TInt,TUint)
610 // Class THuffEncoder
612 // This class generates the byte stream of huffman codes, writing them out to the stream
614 THuffEncoder::THuffEncoder(RWriteStream& aStream)
615 :iStream(aStream),iCode(0),iBits(-8),iWrite(iBuf)
619 // Store a huffman code generated by Huffman::EncodingL()
621 void THuffEncoder::EncodeL(TUint32 aHuffCode)
623 EncodeL(aHuffCode<<(32-KHuffMaxCodeLength),aHuffCode>>KHuffMaxCodeLength);
627 // Store aLength bits from the most significant bits of aCode
629 void THuffEncoder::EncodeL(TUint aCode,TInt aLength)
632 TUint code=iCode|(aCode>>(bits+8));
636 TUint8* write=iWrite;
639 if (write-EBufSize==iBuf)
641 iStream.WriteL(iBuf,EBufSize);
644 *write++=TUint8(code>>24);
655 // Terminate the huffman coding. The longest code is always 1111111111
657 void THuffEncoder::CompleteL()
660 EncodeL(0xffffffffu,-iBits);
662 iStream.WriteL(iBuf,iWrite-iBuf);
667 // Extends MDeflater to provide huffman encoding of the output
670 // construct for encoding
672 inline TDeflater::TDeflater(THuffEncoder& aEncoder,const CDbStoreCompression::TEncoding& aEncoding)
673 :iEncoder(aEncoder),iEncoding(aEncoding)
676 void TDeflater::LitLenL(TInt aCode)
678 iEncoder.EncodeL(iEncoding.iLitLen[aCode]);
681 void TDeflater::OffsetL(TInt aCode)
683 iEncoder.EncodeL(iEncoding.iDistance[aCode]);
686 void TDeflater::ExtraL(TInt aLen,TUint aBits)
688 iEncoder.EncodeL(aBits,aLen);
693 // The inflation algorithm, complete with huffman decoding
695 TInflater::TInflater(const TUint8* aIn,const CDbStoreCompression::TEncoding& aEncoding)
696 :iIn(aIn),iBits(KBitsInit),iLen(0),iLitLenTree(aEncoding.iLitLen),iDistTree(aEncoding.iDistance)
700 // consume all data lag in the history buffer, then decode to fill up the output buffer
702 TInt TInflater::Inflate()
704 // empty the history buffer into the output
705 const TUint8* data=iIn;
707 const TUint8* from=iRptr;
710 TUint8* const end=out+KDeflateMaxDistance;
721 // get a huffman code
727 if ((bits<<=1)&KBitsEmpty)
728 bits=*data++|KBitsFull;
731 if (huff&(KHuffTerminate<<16))
736 if (huff&KHuffTerminate)
742 node=PtrAdd(node,huff);
745 TInt val=TInt(huff>>17)-CDbStoreCompression::TEncoding::ELiterals;
750 continue; // another literal/length combo
752 if (val==CDbStoreCompression::TEncoding::EEos-CDbStoreCompression::TEncoding::ELiterals)
753 { // eos marker. we're done
757 // get the extra bits for the code
761 TInt xtra=(code>>2)-1;
765 if ((bits<<=1)&KBitsEmpty)
766 bits=*data++|KBitsFull;
772 if (val<KDeflateDistCodeBase-CDbStoreCompression::TEncoding::ELiterals)
773 { // length code... get the code
774 len=code+KDeflateMinLength;
775 __ASSERT(len<=KDeflateMaxLength);
777 continue; // read the huffman code
780 __ASSERT(code<KDeflateMaxDistance);
782 if (from+KDeflateMaxDistance<end)
783 from+=KDeflateMaxDistance;
786 TInt tfr=Min(end-out,len);
792 from-=KDeflateMaxDistance;
803 inline const TUint8* TInflater::Ptr() const
805 inline TInt TInflater::BufferSize()
806 {return KDeflateMaxDistance;}
810 // This stream buffer applies the analysis or deflation and huffman coding
811 // on the entire stream data when it is committed
813 inline HDeflateBuf::HDeflateBuf(MStreamBuf* aHost,CDbStoreCompression::TEncoding& aEncoding,CBufBase* aBuf,TMode aMode)
814 :iHost(aHost),iEncoding(aEncoding),iBuf(aBuf),iMode(aMode)
817 HDeflateBuf* HDeflateBuf::NewL(MStreamBuf* aHost,CDbStoreCompression::TEncoding& aEncoding,TMode aMode)
819 CBufBase* buf=CBufFlat::NewL(512);
820 CleanupStack::PushL(buf);
821 HDeflateBuf* self=new(ELeave) HDeflateBuf(aHost,aEncoding,buf,aMode);
826 inline HDeflateBuf::~HDeflateBuf()
827 {delete iBuf;iHost.Release();}
829 void HDeflateBuf::DoRelease()
835 // This is where it all happens
837 void HDeflateBuf::DoSynchL()
839 if (iMode==EAnalysis)
841 TDeflateStats deflater(iEncoding);
842 deflater.DeflateL(iBuf->Ptr(0).Ptr(),iBuf->Size());
846 THuffEncoder encoder(iHost);
847 TDeflater deflater(encoder,iEncoding);
848 deflater.DeflateL(iBuf->Ptr(0).Ptr(),iBuf->Size());
856 // Inflate the input stream. This is not a filter, it reads all the input, inflates it and
857 // keeps it in a memory buffer.
859 const TInt KInflateBufSize=0x800; // 2K
861 HInflateBuf::HInflateBuf(CBufBase* aBuf)
867 inline HInflateBuf::~HInflateBuf()
870 void HInflateBuf::DoRelease()
875 HInflateBuf* HInflateBuf::NewL(MStreamBuf* aHost,const CDbStoreCompression::TEncoding& aEncoding)
877 CBufFlat* host=CBufFlat::NewL(256);
878 CleanupStack::PushL(host);
879 TUint8 buffer[KInflateBufSize];
882 TInt len=aHost->ReadL(buffer,KInflateBufSize);
884 host->InsertL(host->Size(),buffer,len);
885 if (len<KInflateBufSize)
888 CBufSeg* out=CBufSeg::NewL(256);
889 CleanupStack::PushL(out);
890 TInflater* inflater=new(ELeave) TInflater(host->Ptr(0).Ptr(),aEncoding);
891 CleanupStack::PushL(inflater);
894 TInt len=inflater->Inflate();
896 out->InsertL(out->Size(),inflater->Ptr(),len);
897 if (len<inflater->BufferSize())
900 HInflateBuf* buf=new(ELeave) HInflateBuf(out);
901 CleanupStack::PopAndDestroy(); // inflater
902 CleanupStack::Pop(); // out
903 CleanupStack::PopAndDestroy(); // host
904 aHost->Release(); // don't need this anymore
908 // Class CDbStoreTable::Compressor
910 // This class processes an entire table for analysis or compression, using the
911 // CDbStoreRecords::AlterL() functionality and call back to ensure that all clusters
912 // and BLOBs are read and written.
914 inline CDbStoreTable::CCompressor::CCompressor()
917 CDbStoreTable::CCompressor::~CCompressor()
925 // Walk through every cluster in the table
927 void CDbStoreTable::CCompressor::ProcessL(CDbStoreTable* aTable)
930 CDbStoreRecords& rec=aTable->StoreRecordsL();
931 for (TClusterId cluster=rec.Head();cluster!=KNullClusterId;cluster=rec.AlterL(cluster,*this))
936 // Compress every blob, and transfer the record from aRPtr to aWPtr
938 TUint8* CDbStoreTable::CCompressor::AlterRecordL(TUint8* aWPtr,const TUint8* aRPtr,TInt aLength)
940 if (iTable->Def().Columns().HasLongColumns())
942 iTable->CopyToRowL(iRow,TPtrC8(aRPtr,aLength));
943 CDbBlobSpace* blobs=iTable->BlobsL();
945 HDbColumnSet::TIteratorC iter=iTable->Def().Columns().Begin();
946 const HDbColumnSet::TIteratorC end=iTable->Def().Columns().End();
949 if (!TDbCol::IsLong(iter->Type()))
951 TDbBlob& blob=CONST_CAST(TDbBlob&,TDbColumnC(iRow,col).Blob());
954 // do what has to be done...?
955 TUint8* data=(TUint8*)User::AllocLC(blob.Size());
956 blobs->ReadLC(blob.Id(),iter->Type())->ReadL(data,blob.Size());
957 CleanupStack::PopAndDestroy(); // stream buffer
958 // re-write the Blob to compress it
959 blobs->DeleteL(blob.Id());
960 blob.SetId(blobs->CreateL(iter->Type(),data,blob.Size()));
961 CleanupStack::PopAndDestroy(); // data
962 } while (++col,++iter<end);
963 iTable->CopyFromRow(aWPtr,iRow);
966 Mem::Copy(aWPtr,aRPtr,aLength);
967 return aWPtr+aLength;
970 // Class CDbStoreCompression
972 // This class manages the compression for the database, applying filters as appropriate
973 // It also defines the extrenalisation format for the huffman trees
975 const TInt KDeflationCodes=3*(CDbStoreCompression::TEncoding::ELitLens+CDbStoreCompression::TEncoding::EDistances);
977 inline CDbStoreCompression::CDbStoreCompression()
978 // :iState(EAnalysis)
981 CDbStoreCompression* CDbStoreCompression::NewL()
983 return new(ELeave) CDbStoreCompression;
987 // Build huffman codings from the freqeuncy tables
989 void CDbStoreCompression::EncodeL()
991 __ASSERT(iState==EAnalysis);
992 TUint32* p=iEncoding[0].iLitLen;
993 TUint32* end=p+KDeflationCodes;
996 Huffman::EncodingL(p,TEncoding::ELitLens);
997 p+=TEncoding::ELitLens;
998 Huffman::EncodingL(p,TEncoding::EDistances);
999 p+=TEncoding::EDistances;
1005 // Store the encoding tables as a sequence of code lengths
1006 // The code lengths (0-25) are themselves huffman coded, and the meta coding is stored first
1008 void CDbStoreCompression::ExternalizeL(RWriteStream& aStream) const
1010 __ASSERT(iState==EEncoding);
1011 const TUint32* base=iEncoding[0].iLitLen;
1012 const TUint32* end=base+KDeflationCodes;
1013 TUint32 codes[KDeflateMetaCodes];
1014 Mem::FillZ(codes,sizeof(codes));
1015 const TUint32* p=base;
1016 do ++codes[*p++>>KHuffMaxCodeLength]; while (p<end);
1017 Huffman::EncodingL(codes,KDeflateMetaCodes);
1018 // save the meta encoding
1019 p=codes+KDeflateMetaCodes;
1024 c0>>=KHuffMaxCodeLength;
1025 c1>>=KHuffMaxCodeLength;
1026 aStream.WriteUint8L((c0<<4)|c1);
1028 // write the encoding
1029 THuffEncoder encoder(aStream);
1031 do encoder.EncodeL(codes[*p++>>KHuffMaxCodeLength]); while (p<end);
1032 encoder.CompleteL();
1036 // Internalize a previous saved encoding
1038 void CDbStoreCompression::InternalizeL(RReadStream& aStream)
1040 __ASSERT(iState!=EEncoding);
1042 // read the meta encoding
1043 TUint32 decode[KDeflateMetaCodes];
1044 TUint32* p=decode+KDeflateMetaCodes;
1047 TUint8 c=aStream.ReadUint8L();
1051 Huffman::Decoding(decode,KDeflateMetaCodes);
1052 // decode the encoding
1053 p=iEncoding[0].iLitLen;
1054 TUint32* end=p+KDeflationCodes;
1055 TUint bits=KBitsInit;
1058 const TUint32* node=decode;
1063 if ((bits<<=1)&KBitsEmpty)
1064 bits=aStream.ReadUint8L()|KBitsFull;
1067 if (huff&(KHuffTerminate<<16))
1072 if (huff&KHuffTerminate)
1078 node=PtrAdd(node,huff);
1083 // convert the length tables into huffman decoding trees
1084 p=iEncoding[0].iLitLen;
1087 Huffman::Decoding(p,TEncoding::ELitLens);
1088 p+=TEncoding::ELitLens;
1089 Huffman::Decoding(p,TEncoding::EDistances,KDeflateDistCodeBase);
1090 p+=TEncoding::EDistances;
1092 if (iState==EAnalysis)
1097 // Apply an inflation filter to a read stream
1099 MStreamBuf* CDbStoreCompression::FilterL(MStreamBuf* aHost,TUint32,RDbStoreReadStream::TType aType)
1101 if (iState==EDecoding || iState==EInflating)
1102 return HInflateBuf::NewL(aHost,iEncoding[aType]);
1107 // Apply a statistics or inflation filter to a write stream
1109 MStreamBuf* CDbStoreCompression::FilterL(MStreamBuf* aHost,TUint32,RDbStoreWriteStream::TType aType)
1113 __LEAVE(KErrWrite); // read-only database
1114 else if (s!=EInflating)
1116 __ASSERT(TInt(EAnalysis)==TInt(HDeflateBuf::EAnalysis));
1117 __ASSERT(TInt(EEncoding)==TInt(HDeflateBuf::EDeflate));
1118 return HDeflateBuf::NewL(aHost,iEncoding[aType],HDeflateBuf::TMode(s));
1123 // Class CDbStoreDatabase
1125 // Compression related code is maintained in this source file
1128 // Iterate across all tables applying analysis or compression to them
1130 void CDbStoreDatabase::CompressTablesL()
1132 TSglQueIterC<CDbStoreDef> iter(SchemaL());
1133 const CDbStoreDef* def;
1134 while ((def=iter++)!=0)
1136 CDbStoreTable::CCompressor* comp=new(ELeave) CDbStoreTable::CCompressor;
1137 CleanupStack::PushL(comp);
1138 comp->ProcessL(STATIC_CAST(CDbStoreTable*,TableL(*def)));
1139 CleanupStack::PopAndDestroy(); // comp
1144 // Compress or decompress the whole database
1146 void CDbStoreDatabase::CompressL(TStreamId aStreamId,TZipType aZip)
1149 iSchemaId=aStreamId;
1150 // read the databse header for encryption information
1151 RStoreReadStream strm;
1152 strm.OpenLC(Store(),aStreamId);
1154 CleanupStack::PopAndDestroy(); // strm
1157 if (iVersion==EDbStoreCompressed)
1159 iCompression->Inflate();
1161 __LEAVE(KErrArgument); // already compressed
1163 else if (aZip==EInflate)
1164 __LEAVE(KErrArgument); // not compressed
1166 { // deflate pass #1: analyse the database
1167 CompressionL(); // construct the compression filter
1168 Transaction().DDLBeginLC();
1170 iClusterCache->FlushL(); // force through the stats buffer
1171 ReplaceSchemaL(); // force through the stats buffer
1172 CleanupStack::PopAndDestroy(); // rollback after analysis!
1173 iCompression->EncodeL();
1175 // now inflate or deflate the data
1176 Transaction().DDLBeginLC();
1178 iVersion=TUint8(aZip==EDeflate ? EDbStoreCompressed : EDbStoreVersion2);
1179 Transaction().DDLCommitL();
1180 CleanupStack::Pop(); // rollback not required
1183 void CDbStoreDatabase::CompressL(CStreamStore* aStore,TStreamId aStreamId,TZipType aZip)
1185 CDbStoreDatabase* self=NewLC(aStore);
1186 CDbObject* db=self->InterfaceL(); // a reference to the database is required
1187 CleanupStack::Pop(); // self
1189 self->Transaction().DDLPrepareL(*db);
1190 self->CompressL(aStreamId,aZip);
1191 CleanupStack::PopAndDestroy(); // db
1194 // Class RDbStoreDatabase
1196 EXPORT_C void RDbStoreDatabase::CompressL(CStreamStore& aStore,TStreamId aId)
1198 CDbStoreDatabase::CompressL(&aStore,aId,CDbStoreDatabase::EDeflate);
1201 EXPORT_C void RDbStoreDatabase::DecompressL(CStreamStore& aStore,TStreamId aId)
1203 CDbStoreDatabase::CompressL(&aStore,aId,CDbStoreDatabase::EInflate);