sl@0
|
1 |
// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
//
|
sl@0
|
15 |
|
sl@0
|
16 |
#include "US_STD.H"
|
sl@0
|
17 |
|
sl@0
|
18 |
// Class CDbStoreRecords::TIteratorC
|
sl@0
|
19 |
|
sl@0
|
20 |
class CDbStoreRecords::TIteratorC
|
sl@0
|
21 |
{
|
sl@0
|
22 |
friend class CDbStoreRecords;
|
sl@0
|
23 |
public:
|
sl@0
|
24 |
inline TDbRecordId Current() const;
|
sl@0
|
25 |
private:
|
sl@0
|
26 |
TClusterDes iDes;
|
sl@0
|
27 |
TDbRecordId iCurrent;
|
sl@0
|
28 |
};
|
sl@0
|
29 |
|
sl@0
|
30 |
inline TDbRecordId CDbStoreRecords::TIteratorC::Current() const
|
sl@0
|
31 |
{return iCurrent;}
|
sl@0
|
32 |
|
sl@0
|
33 |
|
sl@0
|
34 |
// Class CDbStoreRecords::CIter
|
sl@0
|
35 |
|
sl@0
|
36 |
NONSHARABLE_CLASS(CDbStoreRecords::CIter) : public CDbRecordIter
|
sl@0
|
37 |
{
|
sl@0
|
38 |
public:
|
sl@0
|
39 |
CIter(CDbStoreRecords& aRecords);
|
sl@0
|
40 |
private:
|
sl@0
|
41 |
inline CDbStoreRecords& Records() const;
|
sl@0
|
42 |
//
|
sl@0
|
43 |
TInt Count() const;
|
sl@0
|
44 |
TDbRecordId CurrentL();
|
sl@0
|
45 |
TBool GotoL(TDbPosition aPosition);
|
sl@0
|
46 |
TBool GotoL(TDbRecordId aRecordId,RDbTableRow& aBuffer);
|
sl@0
|
47 |
TBool SeekL(const TDbLookupKey& aKey,RDbTable::TComparison aComparison);
|
sl@0
|
48 |
TDeleted DoDeletedL(TDbPosition aPosition,TDbRecordId aRecordId,const RDbTableRow* aRow);
|
sl@0
|
49 |
private:
|
sl@0
|
50 |
CDbStoreRecords::TIteratorC iIter;
|
sl@0
|
51 |
};
|
sl@0
|
52 |
|
sl@0
|
53 |
CDbStoreRecords::CIter::CIter(CDbStoreRecords& aRecords)
|
sl@0
|
54 |
: CDbRecordIter(aRecords)
|
sl@0
|
55 |
{}
|
sl@0
|
56 |
|
sl@0
|
57 |
inline CDbStoreRecords& CDbStoreRecords::CIter::Records() const
|
sl@0
|
58 |
{return STATIC_CAST(CDbStoreRecords&,Host());}
|
sl@0
|
59 |
|
sl@0
|
60 |
TInt CDbStoreRecords::CIter::Count() const
|
sl@0
|
61 |
{
|
sl@0
|
62 |
return Records().Count();
|
sl@0
|
63 |
}
|
sl@0
|
64 |
|
sl@0
|
65 |
TDbRecordId CDbStoreRecords::CIter::CurrentL()
|
sl@0
|
66 |
{
|
sl@0
|
67 |
return iIter.Current();
|
sl@0
|
68 |
}
|
sl@0
|
69 |
|
sl@0
|
70 |
TBool CDbStoreRecords::CIter::GotoL(TDbPosition aPosition)
|
sl@0
|
71 |
{
|
sl@0
|
72 |
return Records().GotoL(aPosition,iIter);
|
sl@0
|
73 |
}
|
sl@0
|
74 |
|
sl@0
|
75 |
TBool CDbStoreRecords::CIter::GotoL(TDbRecordId aRecordId,RDbTableRow&)
|
sl@0
|
76 |
{
|
sl@0
|
77 |
return Records().GotoL(aRecordId,iIter);
|
sl@0
|
78 |
}
|
sl@0
|
79 |
|
sl@0
|
80 |
TBool CDbStoreRecords::CIter::SeekL(const TDbLookupKey&,RDbTable::TComparison)
|
sl@0
|
81 |
//
|
sl@0
|
82 |
// Cannot do this on a table iterator
|
sl@0
|
83 |
//
|
sl@0
|
84 |
{
|
sl@0
|
85 |
Panic(EDbCannotSeek);
|
sl@0
|
86 |
return EFalse;
|
sl@0
|
87 |
}
|
sl@0
|
88 |
|
sl@0
|
89 |
CDbStoreRecords::CIter::TDeleted CDbStoreRecords::CIter::DoDeletedL(TDbPosition aPosition,TDbRecordId,const RDbTableRow*)
|
sl@0
|
90 |
//
|
sl@0
|
91 |
// reposition to next after a record is deleted
|
sl@0
|
92 |
// Previous only required for reversed (index) iterators
|
sl@0
|
93 |
//
|
sl@0
|
94 |
{
|
sl@0
|
95 |
return Records().DeletedL(aPosition,iIter) ? EAtRow : ENoRow;
|
sl@0
|
96 |
}
|
sl@0
|
97 |
|
sl@0
|
98 |
|
sl@0
|
99 |
// Class CDbStoreRecords::TToken
|
sl@0
|
100 |
|
sl@0
|
101 |
void CDbStoreRecords::TToken::ExternalizeL(RWriteStream& aStream) const
|
sl@0
|
102 |
{
|
sl@0
|
103 |
aStream<<iHead<<iNext.Value()<<TCardinality(iCount)<<TUint32(iAutoIncrement);
|
sl@0
|
104 |
}
|
sl@0
|
105 |
|
sl@0
|
106 |
void CDbStoreRecords::TToken::InternalizeL(RReadStream& aStream)
|
sl@0
|
107 |
{
|
sl@0
|
108 |
aStream>>iHead;
|
sl@0
|
109 |
iNext=aStream.ReadUint32L();
|
sl@0
|
110 |
TCardinality card;
|
sl@0
|
111 |
aStream>>card;
|
sl@0
|
112 |
iCount=card;
|
sl@0
|
113 |
iAutoIncrement=aStream.ReadUint32L();
|
sl@0
|
114 |
}
|
sl@0
|
115 |
|
sl@0
|
116 |
|
sl@0
|
117 |
// Class CDbStoreRecords
|
sl@0
|
118 |
|
sl@0
|
119 |
CDbStoreRecords::CDbStoreRecords(CClusterCache& aCache)
|
sl@0
|
120 |
: iCache(aCache)
|
sl@0
|
121 |
{}
|
sl@0
|
122 |
|
sl@0
|
123 |
CDbStoreRecords::~CDbStoreRecords()
|
sl@0
|
124 |
{
|
sl@0
|
125 |
iMap.Close();
|
sl@0
|
126 |
}
|
sl@0
|
127 |
|
sl@0
|
128 |
TStreamId CDbStoreRecords::CreateL(CClusterCache& aCache)
|
sl@0
|
129 |
//
|
sl@0
|
130 |
// Create a new record space in the store, do not create a records object
|
sl@0
|
131 |
//
|
sl@0
|
132 |
{
|
sl@0
|
133 |
TToken token;
|
sl@0
|
134 |
token.iHead=ClusterId(aCache.Store().ExtendL());
|
sl@0
|
135 |
aCache.ClusterL().Create(token.iHead);
|
sl@0
|
136 |
token.iNext=RecordId(token.iHead,0);
|
sl@0
|
137 |
token.iCount=0;
|
sl@0
|
138 |
token.iAutoIncrement=0;
|
sl@0
|
139 |
RStoreWriteStream strm;
|
sl@0
|
140 |
TStreamId id=strm.CreateLC(aCache.Store());
|
sl@0
|
141 |
strm<<token;
|
sl@0
|
142 |
strm.CommitL();
|
sl@0
|
143 |
CleanupStack::PopAndDestroy();
|
sl@0
|
144 |
return id;
|
sl@0
|
145 |
}
|
sl@0
|
146 |
|
sl@0
|
147 |
CDbStoreRecords* CDbStoreRecords::NewL(CClusterCache& aCache,const CDbStoreDef& aDef)
|
sl@0
|
148 |
//
|
sl@0
|
149 |
// Create a record space
|
sl@0
|
150 |
//
|
sl@0
|
151 |
{
|
sl@0
|
152 |
CDbStoreRecords* self=new(ELeave) CDbStoreRecords(aCache);
|
sl@0
|
153 |
CleanupStack::PushL(self);
|
sl@0
|
154 |
self->iClustering=aDef.Clustering();
|
sl@0
|
155 |
self->iTokenId=aDef.TokenId();
|
sl@0
|
156 |
CleanupStack::Pop();
|
sl@0
|
157 |
return self;
|
sl@0
|
158 |
}
|
sl@0
|
159 |
|
sl@0
|
160 |
TBool CDbStoreRecords::RestoreL()
|
sl@0
|
161 |
//
|
sl@0
|
162 |
// Restore an existing record space from the store
|
sl@0
|
163 |
//
|
sl@0
|
164 |
{
|
sl@0
|
165 |
RStoreReadStream strm;
|
sl@0
|
166 |
strm.OpenLC(iCache.Store(),iTokenId);
|
sl@0
|
167 |
strm>>iToken;
|
sl@0
|
168 |
CleanupStack::PopAndDestroy();
|
sl@0
|
169 |
iLinks.Invalidate();
|
sl@0
|
170 |
iMap.ResetL(iToken.iHead);
|
sl@0
|
171 |
return EFalse;
|
sl@0
|
172 |
}
|
sl@0
|
173 |
|
sl@0
|
174 |
void CDbStoreRecords::DestroyL()
|
sl@0
|
175 |
//
|
sl@0
|
176 |
// Destroy the record space
|
sl@0
|
177 |
//
|
sl@0
|
178 |
{
|
sl@0
|
179 |
iCache.Store().DeleteL(iTokenId);
|
sl@0
|
180 |
}
|
sl@0
|
181 |
|
sl@0
|
182 |
TInt CDbStoreRecords::CardinalityL(CStreamStore& aStore,const CDbStoreDef& aDef)
|
sl@0
|
183 |
//
|
sl@0
|
184 |
// Return the record count without constructing the entire table
|
sl@0
|
185 |
//
|
sl@0
|
186 |
{
|
sl@0
|
187 |
RStoreReadStream strm;
|
sl@0
|
188 |
strm.OpenLC(aStore,aDef.TokenId());
|
sl@0
|
189 |
TToken token;
|
sl@0
|
190 |
strm>>token;
|
sl@0
|
191 |
CleanupStack::PopAndDestroy();
|
sl@0
|
192 |
return token.iCount;
|
sl@0
|
193 |
}
|
sl@0
|
194 |
|
sl@0
|
195 |
void CDbStoreRecords::SynchL()
|
sl@0
|
196 |
//
|
sl@0
|
197 |
// write persistent token to the store
|
sl@0
|
198 |
//
|
sl@0
|
199 |
{
|
sl@0
|
200 |
RStoreWriteStream strm;
|
sl@0
|
201 |
strm.ReplaceLC(iCache.Store(),iTokenId);
|
sl@0
|
202 |
strm<<iToken;
|
sl@0
|
203 |
strm.CommitL();
|
sl@0
|
204 |
CleanupStack::PopAndDestroy();
|
sl@0
|
205 |
}
|
sl@0
|
206 |
|
sl@0
|
207 |
TInt CDbStoreRecords::DiscardL(TClusterId& aCluster)
|
sl@0
|
208 |
//
|
sl@0
|
209 |
// discard the cluster as part of the incremental drop
|
sl@0
|
210 |
// aCluster is updated to the next cluster, the number of records contained is returned
|
sl@0
|
211 |
//
|
sl@0
|
212 |
{
|
sl@0
|
213 |
TClusterDes des;
|
sl@0
|
214 |
DesL(des,aCluster);
|
sl@0
|
215 |
CCluster* cluster=iCache.Cluster(aCluster);
|
sl@0
|
216 |
if (cluster)
|
sl@0
|
217 |
cluster->Discard();
|
sl@0
|
218 |
iCache.Store().DeleteL(aCluster);
|
sl@0
|
219 |
aCluster=des.iNext;
|
sl@0
|
220 |
TInt records=0;
|
sl@0
|
221 |
for (TUint members=des.iMembership;members;members&=members-1)
|
sl@0
|
222 |
++records;
|
sl@0
|
223 |
return records;
|
sl@0
|
224 |
}
|
sl@0
|
225 |
|
sl@0
|
226 |
TClusterId CDbStoreRecords::AlterL(TClusterId aCluster,CCluster::MAlter& aAlterer)
|
sl@0
|
227 |
{
|
sl@0
|
228 |
CCluster& cluster=iCache.ClusterL(aCluster);
|
sl@0
|
229 |
cluster.AlterL(aAlterer);
|
sl@0
|
230 |
return cluster.Des().iNext;
|
sl@0
|
231 |
}
|
sl@0
|
232 |
|
sl@0
|
233 |
TPtrC8 CDbStoreRecords::ReadL(TDbRecordId aRecordId) const
|
sl@0
|
234 |
{
|
sl@0
|
235 |
return iCache.ClusterL(ClusterId(aRecordId)).RecordL(RecordIndex(aRecordId));
|
sl@0
|
236 |
}
|
sl@0
|
237 |
|
sl@0
|
238 |
TUint CDbStoreRecords::AutoIncrementL()
|
sl@0
|
239 |
//
|
sl@0
|
240 |
// Provide the next value for an auto-increment column
|
sl@0
|
241 |
//
|
sl@0
|
242 |
{
|
sl@0
|
243 |
return iToken.iAutoIncrement++;
|
sl@0
|
244 |
}
|
sl@0
|
245 |
|
sl@0
|
246 |
TUint8* CDbStoreRecords::UpdateRecordL(TDbRecordId aRecordId,TInt aNewSize)
|
sl@0
|
247 |
//
|
sl@0
|
248 |
// read the cluster and return a writable descriptor over the new record data
|
sl@0
|
249 |
//
|
sl@0
|
250 |
{
|
sl@0
|
251 |
return iCache.ClusterL(ClusterId(aRecordId)).UpdateL(RecordIndex(aRecordId),aNewSize);
|
sl@0
|
252 |
}
|
sl@0
|
253 |
|
sl@0
|
254 |
|
sl@0
|
255 |
TUint8* CDbStoreRecords::DoNewL(TInt aRecordSize)
|
sl@0
|
256 |
//
|
sl@0
|
257 |
// Phase 1 of appending a records
|
sl@0
|
258 |
//
|
sl@0
|
259 |
{
|
sl@0
|
260 |
return UpdateRecordL(iToken.iNext,aRecordSize);
|
sl@0
|
261 |
}
|
sl@0
|
262 |
|
sl@0
|
263 |
TDbRecordId CDbStoreRecords::AppendL()
|
sl@0
|
264 |
//
|
sl@0
|
265 |
// Phase 2 of appending a record
|
sl@0
|
266 |
//
|
sl@0
|
267 |
{
|
sl@0
|
268 |
TDbRecordId id=iToken.iNext;
|
sl@0
|
269 |
TClusterId clusterId=ClusterId(id);
|
sl@0
|
270 |
CCluster* cluster=iCache.Cluster(clusterId);
|
sl@0
|
271 |
__ASSERT(cluster);
|
sl@0
|
272 |
TInt nextIndex=RecordIndex(id)+1;
|
sl@0
|
273 |
if (nextIndex>=iClustering || cluster->IsFull())
|
sl@0
|
274 |
{
|
sl@0
|
275 |
TClusterId newcluster=ClusterId(iCache.Store().ExtendL());
|
sl@0
|
276 |
cluster->Relink(newcluster);
|
sl@0
|
277 |
cluster->FlushL();
|
sl@0
|
278 |
cluster->Create(newcluster);
|
sl@0
|
279 |
iMap.BindL(clusterId,newcluster);
|
sl@0
|
280 |
iLinks.Bind(clusterId,newcluster,iMap);
|
sl@0
|
281 |
iToken.iNext=RecordId(newcluster,0);
|
sl@0
|
282 |
}
|
sl@0
|
283 |
else
|
sl@0
|
284 |
iToken.iNext=RecordId(clusterId,nextIndex);
|
sl@0
|
285 |
++iToken.iCount;
|
sl@0
|
286 |
return id;
|
sl@0
|
287 |
}
|
sl@0
|
288 |
|
sl@0
|
289 |
TUint8* CDbStoreRecords::DoReplaceL(TDbRecordId aRecordId,TInt aRecordSize)
|
sl@0
|
290 |
{
|
sl@0
|
291 |
return UpdateRecordL(aRecordId,aRecordSize);
|
sl@0
|
292 |
}
|
sl@0
|
293 |
|
sl@0
|
294 |
void CDbStoreRecords::DoEraseL(TDbRecordId aRecordId)
|
sl@0
|
295 |
{
|
sl@0
|
296 |
TClusterId clusterId=ClusterId(aRecordId);
|
sl@0
|
297 |
CCluster& cluster=iCache.ClusterL(clusterId);
|
sl@0
|
298 |
if (!cluster.DeleteL(RecordIndex(aRecordId)) && clusterId!=ClusterId(iToken.iNext))
|
sl@0
|
299 |
{ // cluster is now empty, but don't drop the last cluster, coz it hasn't all been used!
|
sl@0
|
300 |
TClusterDes des;
|
sl@0
|
301 |
TClusterId prev=PreviousClusterL(des,clusterId);
|
sl@0
|
302 |
TClusterId next=cluster.Des().iNext; // next cluster
|
sl@0
|
303 |
cluster.Discard(); // discard the cluster
|
sl@0
|
304 |
iCache.Store().DeleteL(clusterId);
|
sl@0
|
305 |
if (prev!=KNullClusterId)
|
sl@0
|
306 |
iCache.ClusterL(prev).Relink(next);
|
sl@0
|
307 |
else
|
sl@0
|
308 |
iToken.iHead=next;
|
sl@0
|
309 |
iLinks.Drop(clusterId,next);
|
sl@0
|
310 |
iMap.DropL(clusterId,next);
|
sl@0
|
311 |
}
|
sl@0
|
312 |
--iToken.iCount;
|
sl@0
|
313 |
}
|
sl@0
|
314 |
|
sl@0
|
315 |
CDbRecordIter* CDbStoreRecords::IteratorL()
|
sl@0
|
316 |
{
|
sl@0
|
317 |
return new(ELeave) CIter(*this);
|
sl@0
|
318 |
}
|
sl@0
|
319 |
|
sl@0
|
320 |
void CDbStoreRecords::CompleteMapL()
|
sl@0
|
321 |
{
|
sl@0
|
322 |
TClusterId cluster=iMap.LastBound();
|
sl@0
|
323 |
TClusterDes des;
|
sl@0
|
324 |
DesL(des,cluster);
|
sl@0
|
325 |
do cluster=NextClusterL(des,cluster); while (cluster!=KNullClusterId);
|
sl@0
|
326 |
}
|
sl@0
|
327 |
|
sl@0
|
328 |
void CDbStoreRecords::DesL(TClusterDes& aDes,TClusterId aCluster)
|
sl@0
|
329 |
//
|
sl@0
|
330 |
// Read just the cluster descriptor
|
sl@0
|
331 |
//
|
sl@0
|
332 |
{
|
sl@0
|
333 |
CCluster* cluster=iCache.Cluster(aCluster);
|
sl@0
|
334 |
if (cluster)
|
sl@0
|
335 |
aDes=cluster->Des();
|
sl@0
|
336 |
else
|
sl@0
|
337 |
{
|
sl@0
|
338 |
RStoreReadStream stream;
|
sl@0
|
339 |
stream.OpenLC(iCache.Store(),aCluster);
|
sl@0
|
340 |
stream>>aDes;
|
sl@0
|
341 |
CleanupStack::PopAndDestroy();
|
sl@0
|
342 |
}
|
sl@0
|
343 |
}
|
sl@0
|
344 |
|
sl@0
|
345 |
TClusterId CDbStoreRecords::NextClusterL(TClusterDes& aDes,TClusterId aCluster)
|
sl@0
|
346 |
{
|
sl@0
|
347 |
TClusterId next=aDes.iNext;
|
sl@0
|
348 |
if (next==KNullClusterId)
|
sl@0
|
349 |
iMap.Complete(aCluster);
|
sl@0
|
350 |
else
|
sl@0
|
351 |
{
|
sl@0
|
352 |
iMap.BindL(aCluster,next);
|
sl@0
|
353 |
iLinks.Bind(aCluster,next,iMap);
|
sl@0
|
354 |
DesL(aDes,next);
|
sl@0
|
355 |
}
|
sl@0
|
356 |
return next;
|
sl@0
|
357 |
}
|
sl@0
|
358 |
|
sl@0
|
359 |
TBool CDbStoreRecords::LocateL(TClusterId aCluster)
|
sl@0
|
360 |
//
|
sl@0
|
361 |
// Locate the cluster in the table. If not present return EFalse
|
sl@0
|
362 |
// If present fill up the cluster link cache with the loop
|
sl@0
|
363 |
// containing the predecessor to aCluster
|
sl@0
|
364 |
// aDes will have the previous cluster des
|
sl@0
|
365 |
//
|
sl@0
|
366 |
{
|
sl@0
|
367 |
TClusterId cluster=aCluster;
|
sl@0
|
368 |
__ASSERT(aCluster!=iToken.iHead);
|
sl@0
|
369 |
__ASSERT(!iLinks.At(aCluster,cluster));
|
sl@0
|
370 |
//
|
sl@0
|
371 |
if (!iMap.IsComplete())
|
sl@0
|
372 |
CompleteMapL();
|
sl@0
|
373 |
//
|
sl@0
|
374 |
TClusterDes des;
|
sl@0
|
375 |
TClusterId links[RClusterMap::ESeparation];
|
sl@0
|
376 |
TClusterId* p=links;
|
sl@0
|
377 |
for (TInt n=RClusterMap::ESeparation;n>0;--n)
|
sl@0
|
378 |
{
|
sl@0
|
379 |
*p++=cluster;
|
sl@0
|
380 |
TBool r=iMap.At(cluster,cluster);
|
sl@0
|
381 |
DesL(des,cluster);
|
sl@0
|
382 |
if (r)
|
sl@0
|
383 |
{
|
sl@0
|
384 |
__ASSERT(cluster!=KNullClusterId); // only iHead->Null
|
sl@0
|
385 |
iLinks.Reset(cluster);
|
sl@0
|
386 |
while (aCluster!=des.iNext)
|
sl@0
|
387 |
cluster=NextClusterL(des,cluster);
|
sl@0
|
388 |
iLinks.Add(links,p);
|
sl@0
|
389 |
return ETrue;
|
sl@0
|
390 |
}
|
sl@0
|
391 |
cluster=des.iNext;
|
sl@0
|
392 |
}
|
sl@0
|
393 |
return EFalse; // not in this table!
|
sl@0
|
394 |
}
|
sl@0
|
395 |
|
sl@0
|
396 |
TClusterId CDbStoreRecords::PreviousClusterL(TClusterDes& aDes,TClusterId aCluster)
|
sl@0
|
397 |
{
|
sl@0
|
398 |
if (aCluster==iToken.iHead)
|
sl@0
|
399 |
return KNullClusterId;
|
sl@0
|
400 |
if (!iLinks.At(aCluster,aCluster))
|
sl@0
|
401 |
{
|
sl@0
|
402 |
__DEBUG(TBool dbgchk=) LocateL(aCluster);
|
sl@0
|
403 |
__ASSERT(dbgchk);
|
sl@0
|
404 |
__DEBUG(dbgchk=) iLinks.At(aCluster,aCluster);
|
sl@0
|
405 |
__ASSERT(dbgchk);
|
sl@0
|
406 |
}
|
sl@0
|
407 |
DesL(aDes,aCluster);
|
sl@0
|
408 |
return aCluster;
|
sl@0
|
409 |
}
|
sl@0
|
410 |
|
sl@0
|
411 |
TBool CDbStoreRecords::GotoL(TDbPosition aPosition,TIteratorC& anIterator)
|
sl@0
|
412 |
{
|
sl@0
|
413 |
TClusterId cluster=ClusterId(anIterator.iCurrent);
|
sl@0
|
414 |
TInt index=RecordIndex(anIterator.iCurrent);
|
sl@0
|
415 |
switch (aPosition)
|
sl@0
|
416 |
{
|
sl@0
|
417 |
default:
|
sl@0
|
418 |
__ASSERT(0);
|
sl@0
|
419 |
case EDbFirst:
|
sl@0
|
420 |
DesL(anIterator.iDes,cluster=iToken.iHead);
|
sl@0
|
421 |
iLinks.Reset(cluster);
|
sl@0
|
422 |
index=-1;
|
sl@0
|
423 |
// drop through to next
|
sl@0
|
424 |
case EDbNext:
|
sl@0
|
425 |
for (;;)
|
sl@0
|
426 |
{
|
sl@0
|
427 |
TUint membership=anIterator.iDes.iMembership;
|
sl@0
|
428 |
while (++index<KMaxClustering)
|
sl@0
|
429 |
{
|
sl@0
|
430 |
if ((membership>>index)&1)
|
sl@0
|
431 |
{
|
sl@0
|
432 |
__ASSERT(cluster!=ClusterId(iToken.iNext)||index<RecordIndex(iToken.iNext));
|
sl@0
|
433 |
anIterator.iCurrent=RecordId(cluster,index);
|
sl@0
|
434 |
return ETrue;
|
sl@0
|
435 |
}
|
sl@0
|
436 |
}
|
sl@0
|
437 |
cluster=NextClusterL(anIterator.iDes,cluster);
|
sl@0
|
438 |
if (cluster==KNullClusterId)
|
sl@0
|
439 |
return EFalse; // ran out of data
|
sl@0
|
440 |
index=-1;
|
sl@0
|
441 |
}
|
sl@0
|
442 |
case EDbLast:
|
sl@0
|
443 |
DesL(anIterator.iDes,cluster=ClusterId(iToken.iNext));
|
sl@0
|
444 |
index=KMaxClustering;
|
sl@0
|
445 |
// drop through to previous
|
sl@0
|
446 |
case EDbPrevious:
|
sl@0
|
447 |
for (;;)
|
sl@0
|
448 |
{
|
sl@0
|
449 |
TUint membership=anIterator.iDes.iMembership;
|
sl@0
|
450 |
while (--index>=0)
|
sl@0
|
451 |
{
|
sl@0
|
452 |
if ((membership>>index)&1)
|
sl@0
|
453 |
{
|
sl@0
|
454 |
anIterator.iCurrent=RecordId(cluster,index);
|
sl@0
|
455 |
return ETrue;
|
sl@0
|
456 |
}
|
sl@0
|
457 |
}
|
sl@0
|
458 |
__ASSERT(index==-1);
|
sl@0
|
459 |
cluster=PreviousClusterL(anIterator.iDes,cluster);
|
sl@0
|
460 |
if (cluster==KNullClusterId)
|
sl@0
|
461 |
return EFalse; // ran out of data
|
sl@0
|
462 |
index=KMaxClustering;
|
sl@0
|
463 |
}
|
sl@0
|
464 |
}
|
sl@0
|
465 |
}
|
sl@0
|
466 |
|
sl@0
|
467 |
TBool CDbStoreRecords::DeletedL(TDbPosition aPosition,TIteratorC& anIterator)
|
sl@0
|
468 |
//
|
sl@0
|
469 |
// Record has been deleted
|
sl@0
|
470 |
//
|
sl@0
|
471 |
{
|
sl@0
|
472 |
anIterator.iDes.iMembership&=~(1<<RecordIndex(anIterator.iCurrent));
|
sl@0
|
473 |
return GotoL(aPosition,anIterator);
|
sl@0
|
474 |
}
|
sl@0
|
475 |
|
sl@0
|
476 |
TBool CDbStoreRecords::GotoL(TDbRecordId aRecordId,TIteratorC& anIterator)
|
sl@0
|
477 |
//
|
sl@0
|
478 |
// Set the iterator to the record id, return false if the record is not present
|
sl@0
|
479 |
//
|
sl@0
|
480 |
{
|
sl@0
|
481 |
TClusterId cluster=ClusterId(aRecordId);
|
sl@0
|
482 |
if (cluster!=iToken.iHead && !iLinks.Has(cluster) && !LocateL(cluster))
|
sl@0
|
483 |
return EFalse;
|
sl@0
|
484 |
anIterator.iCurrent=aRecordId;
|
sl@0
|
485 |
DesL(anIterator.iDes,cluster);
|
sl@0
|
486 |
return (anIterator.iDes.iMembership>>RecordIndex(aRecordId))&1;
|
sl@0
|
487 |
}
|
sl@0
|
488 |
|
sl@0
|
489 |
TBool CDbStoreRecords::ExistsL(TDbRecordId aRecordId)
|
sl@0
|
490 |
//
|
sl@0
|
491 |
// Ensure that the record is in this table
|
sl@0
|
492 |
//
|
sl@0
|
493 |
{
|
sl@0
|
494 |
TIteratorC iter;
|
sl@0
|
495 |
return GotoL(aRecordId,iter);
|
sl@0
|
496 |
}
|
sl@0
|
497 |
|
sl@0
|
498 |
// Class HUnicodeCompressor
|
sl@0
|
499 |
|
sl@0
|
500 |
NONSHARABLE_CLASS(HUnicodeCompressor) : public TStreamFilter
|
sl@0
|
501 |
{
|
sl@0
|
502 |
public:
|
sl@0
|
503 |
HUnicodeCompressor(MStreamBuf* aSink);
|
sl@0
|
504 |
private:
|
sl@0
|
505 |
void DoRelease();
|
sl@0
|
506 |
void DoSynchL();
|
sl@0
|
507 |
TInt Capacity(TInt aMaxLength);
|
sl@0
|
508 |
TInt FilterL(TAny* aPtr,TInt aMaxLength,const TUint8*& aFrom,const TUint8* anEnd);
|
sl@0
|
509 |
private:
|
sl@0
|
510 |
enum {EFlushBufferSize=16};
|
sl@0
|
511 |
private:
|
sl@0
|
512 |
TUnicodeCompressor iCompressor;
|
sl@0
|
513 |
};
|
sl@0
|
514 |
|
sl@0
|
515 |
HUnicodeCompressor::HUnicodeCompressor(MStreamBuf* aSink)
|
sl@0
|
516 |
{
|
sl@0
|
517 |
Set(aSink,EAttached|EWrite);
|
sl@0
|
518 |
}
|
sl@0
|
519 |
|
sl@0
|
520 |
void HUnicodeCompressor::DoRelease()
|
sl@0
|
521 |
{
|
sl@0
|
522 |
TStreamFilter::DoRelease();
|
sl@0
|
523 |
delete this;
|
sl@0
|
524 |
}
|
sl@0
|
525 |
|
sl@0
|
526 |
TInt HUnicodeCompressor::Capacity(TInt aMaxLength)
|
sl@0
|
527 |
//
|
sl@0
|
528 |
// Return the maximum guaranteed input used for aMaxLength output.
|
sl@0
|
529 |
// SUC at worst expands n chars to 3n bytes
|
sl@0
|
530 |
//
|
sl@0
|
531 |
{
|
sl@0
|
532 |
aMaxLength=(aMaxLength+2)/3; // # chars input guaranteed
|
sl@0
|
533 |
return aMaxLength*2; // # bytes
|
sl@0
|
534 |
}
|
sl@0
|
535 |
|
sl@0
|
536 |
TInt HUnicodeCompressor::FilterL(TAny* aPtr,TInt aMaxLength,const TUint8*& aFrom,const TUint8* aEnd)
|
sl@0
|
537 |
{
|
sl@0
|
538 |
TMemoryUnicodeSource source(reinterpret_cast<const TUint16*>(aFrom));
|
sl@0
|
539 |
TInt used;
|
sl@0
|
540 |
iCompressor.CompressL(reinterpret_cast<TUint8*>(aPtr),source,aMaxLength,(aEnd-aFrom)>>1,&aMaxLength,&used);
|
sl@0
|
541 |
aFrom+=used<<1;
|
sl@0
|
542 |
return aMaxLength;
|
sl@0
|
543 |
}
|
sl@0
|
544 |
|
sl@0
|
545 |
void HUnicodeCompressor::DoSynchL()
|
sl@0
|
546 |
{
|
sl@0
|
547 |
if (IsCommitted())
|
sl@0
|
548 |
return;
|
sl@0
|
549 |
//
|
sl@0
|
550 |
TUint8 buf[EFlushBufferSize];
|
sl@0
|
551 |
TInt emit;
|
sl@0
|
552 |
iCompressor.FlushL(buf,EFlushBufferSize,emit);
|
sl@0
|
553 |
if (emit)
|
sl@0
|
554 |
EmitL(buf,emit);
|
sl@0
|
555 |
//
|
sl@0
|
556 |
TStreamFilter::DoSynchL();
|
sl@0
|
557 |
Committed();
|
sl@0
|
558 |
}
|
sl@0
|
559 |
|
sl@0
|
560 |
// Class HUnicodeExander
|
sl@0
|
561 |
|
sl@0
|
562 |
NONSHARABLE_CLASS(HUnicodeExpander) : public TStreamFilter
|
sl@0
|
563 |
{
|
sl@0
|
564 |
public:
|
sl@0
|
565 |
HUnicodeExpander(MStreamBuf* aSource);
|
sl@0
|
566 |
private:
|
sl@0
|
567 |
void DoRelease();
|
sl@0
|
568 |
// void DoSynchL();
|
sl@0
|
569 |
TInt Capacity(TInt aMaxLength);
|
sl@0
|
570 |
TInt FilterL(TAny* aPtr,TInt aMaxLength,const TUint8*& aFrom,const TUint8* anEnd);
|
sl@0
|
571 |
private:
|
sl@0
|
572 |
enum {EFlushBufferSize=16};
|
sl@0
|
573 |
private:
|
sl@0
|
574 |
TUnicodeExpander iExpander;
|
sl@0
|
575 |
};
|
sl@0
|
576 |
|
sl@0
|
577 |
HUnicodeExpander::HUnicodeExpander(MStreamBuf* aSource)
|
sl@0
|
578 |
{
|
sl@0
|
579 |
Set(aSource,EAttached|ERead);
|
sl@0
|
580 |
}
|
sl@0
|
581 |
|
sl@0
|
582 |
void HUnicodeExpander::DoRelease()
|
sl@0
|
583 |
{
|
sl@0
|
584 |
TStreamFilter::DoRelease();
|
sl@0
|
585 |
delete this;
|
sl@0
|
586 |
}
|
sl@0
|
587 |
|
sl@0
|
588 |
TInt HUnicodeExpander::Capacity(TInt aMaxLength)
|
sl@0
|
589 |
//
|
sl@0
|
590 |
// Return the maximum guaranteed input used for aMaxLength output.
|
sl@0
|
591 |
// SUC at worst expands n chars to 3n bytes
|
sl@0
|
592 |
//
|
sl@0
|
593 |
{
|
sl@0
|
594 |
return aMaxLength>>1; // best expansion from ASCII chars
|
sl@0
|
595 |
}
|
sl@0
|
596 |
|
sl@0
|
597 |
TInt HUnicodeExpander::FilterL(TAny* aPtr,TInt aMaxLength,const TUint8*& aFrom,const TUint8* aEnd)
|
sl@0
|
598 |
{
|
sl@0
|
599 |
TMemoryUnicodeSink sink(reinterpret_cast<TUint16*>(aPtr));
|
sl@0
|
600 |
TInt used;
|
sl@0
|
601 |
iExpander.ExpandL(sink,aFrom,aMaxLength>>1,aEnd-aFrom,&aMaxLength,&used);
|
sl@0
|
602 |
aFrom+=used;
|
sl@0
|
603 |
return aMaxLength<<1;
|
sl@0
|
604 |
}
|
sl@0
|
605 |
|
sl@0
|
606 |
/*
|
sl@0
|
607 |
void HUnicodeExpander::DoSynchL()
|
sl@0
|
608 |
{
|
sl@0
|
609 |
if (IsCommitted())
|
sl@0
|
610 |
return;
|
sl@0
|
611 |
//
|
sl@0
|
612 |
// TUint8 buf[EFlushBufferSize];
|
sl@0
|
613 |
// TInt emit;
|
sl@0
|
614 |
// iCompressor.FlushL(buf,EFlushBufferSize,&emit);
|
sl@0
|
615 |
// if (emit)
|
sl@0
|
616 |
// EmitL(buf,emit);
|
sl@0
|
617 |
//
|
sl@0
|
618 |
TStreamFilter::DoSynchL();
|
sl@0
|
619 |
Committed();
|
sl@0
|
620 |
}
|
sl@0
|
621 |
*/
|
sl@0
|
622 |
|
sl@0
|
623 |
// Class CDbStoreBlobs
|
sl@0
|
624 |
|
sl@0
|
625 |
CDbStoreBlobs::CDbStoreBlobs(CDbStoreDatabase& aDatabase,TInt aInlineLimit)
|
sl@0
|
626 |
: iDatabase(aDatabase)
|
sl@0
|
627 |
{
|
sl@0
|
628 |
SetInlineLimit(aInlineLimit);
|
sl@0
|
629 |
}
|
sl@0
|
630 |
|
sl@0
|
631 |
MStreamBuf* CDbStoreBlobs::DoCreateL(TDbBlobId& aBlobId,TDbColType aType)
|
sl@0
|
632 |
{
|
sl@0
|
633 |
__ASSERT(TDbCol::IsLong(aType));
|
sl@0
|
634 |
//
|
sl@0
|
635 |
RDbStoreWriteStream strm(iDatabase);
|
sl@0
|
636 |
aBlobId=strm.CreateLC(iDatabase.Store()).Value();
|
sl@0
|
637 |
strm.FilterL(aType!=EDbColLongBinary?strm.EText:strm.EBinary,aBlobId);
|
sl@0
|
638 |
MStreamBuf* blob=strm.Sink();
|
sl@0
|
639 |
if (aType==EDbColLongText16)
|
sl@0
|
640 |
blob=new(ELeave) HUnicodeCompressor(blob);
|
sl@0
|
641 |
CleanupStack::Pop();
|
sl@0
|
642 |
return blob;
|
sl@0
|
643 |
}
|
sl@0
|
644 |
|
sl@0
|
645 |
MStreamBuf* CDbStoreBlobs::ReadL(TDbBlobId aBlobId,TDbColType aType) const
|
sl@0
|
646 |
{
|
sl@0
|
647 |
__ASSERT(TDbCol::IsLong(aType));
|
sl@0
|
648 |
//
|
sl@0
|
649 |
RDbStoreReadStream strm(iDatabase);
|
sl@0
|
650 |
strm.OpenLC(iDatabase.Store(),aBlobId);
|
sl@0
|
651 |
strm.FilterL(aType!=EDbColLongBinary?strm.EText:strm.EBinary,aBlobId);
|
sl@0
|
652 |
MStreamBuf* blob=strm.Source();
|
sl@0
|
653 |
if (aType==EDbColLongText16)
|
sl@0
|
654 |
blob=new(ELeave) HUnicodeExpander(blob);
|
sl@0
|
655 |
CleanupStack::Pop();
|
sl@0
|
656 |
return blob;
|
sl@0
|
657 |
}
|
sl@0
|
658 |
|
sl@0
|
659 |
void CDbStoreBlobs::DoDeleteL(TDbBlobId aBlobId)
|
sl@0
|
660 |
{
|
sl@0
|
661 |
iDatabase.Store().DeleteL(TStreamId(aBlobId));
|
sl@0
|
662 |
}
|