Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * The Contents of this file are made available subject to the terms of
5 : : * either of the following licenses
6 : : *
7 : : * - GNU Lesser General Public License Version 2.1
8 : : * - Sun Industry Standards Source License Version 1.1
9 : : *
10 : : * Sun Microsystems Inc., October, 2000
11 : : *
12 : : * GNU Lesser General Public License Version 2.1
13 : : * =============================================
14 : : * Copyright 2000 by Sun Microsystems, Inc.
15 : : * 901 San Antonio Road, Palo Alto, CA 94303, USA
16 : : *
17 : : * This library is free software; you can redistribute it and/or
18 : : * modify it under the terms of the GNU Lesser General Public
19 : : * License version 2.1, as published by the Free Software Foundation.
20 : : *
21 : : * This library is distributed in the hope that it will be useful,
22 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 : : * Lesser General Public License for more details.
25 : : *
26 : : * You should have received a copy of the GNU Lesser General Public
27 : : * License along with this library; if not, write to the Free Software
28 : : * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 : : * MA 02111-1307 USA
30 : : *
31 : : *
32 : : * Sun Industry Standards Source License Version 1.1
33 : : * =================================================
34 : : * The contents of this file are subject to the Sun Industry Standards
35 : : * Source License Version 1.1 (the "License"); You may not use this file
36 : : * except in compliance with the License. You may obtain a copy of the
37 : : * License at http://www.openoffice.org/license.html.
38 : : *
39 : : * Software provided under this License is provided on an "AS IS" basis,
40 : : * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
41 : : * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
42 : : * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
43 : : * See the License for the specific provisions governing your rights and
44 : : * obligations concerning the Software.
45 : : *
46 : : * The Initial Developer of the Original Code is: IBM Corporation
47 : : *
48 : : * Copyright: 2008 by IBM Corporation
49 : : *
50 : : * All Rights Reserved.
51 : : *
52 : : * Contributor(s): _______________________________________
53 : : *
54 : : *
55 : : ************************************************************************/
56 : :
57 : : /**********************************************************************************************************************
58 : : * @file
59 : : * index manager implementation
60 : : * Three record types are related with index information, and two ways are used.
61 : : * 1. VO_ROOTOBJINDEX works with one or more VO_LEAFOBJINDEX records to
62 : : * provide the map of all object ids and their offsets.
63 : : * VO_ROOTOBJINDEX includes k (object id, offset) and timetable which is used to map index to actual low id
64 : : * 2. VO_ROOTLEAFOBJINDEX is used when the file is so small that the number of objects is less than 256(?)
65 : : * VO_ROOTLEAFOBJINDEX contains directly the (object id, offset) map and time table.
66 : :
67 : : * Change History
68 : : Jan 2005 Created
69 : : **********************************************************************************************************************/
70 : :
71 : : #include "lwpidxmgr.hxx"
72 : : #include "lwptools.hxx"
73 : :
74 : : const sal_uInt8 LwpIndexManager::MAXOBJECTIDS = 255;
75 : :
76 : 18 : LwpIndexManager::LwpIndexManager()
77 [ + - ][ + - ]: 18 : :m_nKeyCount(0), m_nLeafCount(0)
[ + - ]
78 : : {
79 : :
80 [ + - ]: 18 : m_TempVec.resize( LwpIndexManager::MAXOBJECTIDS );
81 : :
82 : 18 : }
83 : :
84 : 18 : LwpIndexManager::~LwpIndexManager()
85 : : {
86 : : //Clear m_ObjectKeys
87 : 18 : std::vector<LwpKey*>::iterator it;
88 : :
89 [ + - ][ + + ]: 6705 : for( it = m_ObjectKeys.begin(); it != m_ObjectKeys.end(); ++it )
90 : : {
91 : 6687 : LwpKey* pKey = *it;
92 [ + - ]: 6687 : if( pKey )
93 : : {
94 : 6687 : delete pKey;
95 : 6687 : pKey = NULL;
96 : : }
97 : : }
98 : 18 : m_ObjectKeys.clear();
99 : 18 : }
100 : :
101 : : /**
102 : : * @descr Read all index records, VO_ROOTLEAFOBJINDEX, VO_ROOTOBJINDEX, VO_LEAFOBJINDEX
103 : : */
104 : 18 : void LwpIndexManager::Read(LwpSvStream* pStrm)
105 : : {
106 : : //Read index obj
107 [ + - ]: 18 : LwpObjectHeader ObjHdr;
108 [ + - ]: 18 : ObjHdr.Read(*pStrm);
109 : 18 : LwpObjectStream* pObjStrm = new LwpObjectStream(pStrm, ObjHdr.IsCompressed(),
110 [ + - ][ + - ]: 18 : static_cast<sal_uInt16>(ObjHdr.GetSize()) );
111 : :
112 [ - + ]: 18 : if( ObjHdr.GetTag() == VO_ROOTLEAFOBJINDEX )
113 : : {
114 [ # # ]: 0 : ReadLeafData(pObjStrm);
115 [ # # ]: 0 : ReadTimeTable(pObjStrm);
116 [ # # ][ # # ]: 0 : delete pObjStrm;
117 : : }
118 : : else
119 : : {
120 [ + - ]: 18 : ReadRootData(pObjStrm);
121 [ + - ][ + - ]: 18 : delete pObjStrm;
122 : :
123 [ + + ]: 54 : for (sal_uInt16 k = 0; k < m_nLeafCount; k++)
124 : : {
125 : : //Read leaf
126 : 36 : sal_Int64 nPos = m_ChildIndex[k]+LwpSvStream::LWP_STREAM_BASE;
127 [ + - ]: 36 : sal_Int64 nActualPos = pStrm->Seek(nPos);
128 : :
129 [ - + ]: 36 : if (nPos != nActualPos)
130 [ # # ]: 0 : throw BadSeek();
131 : :
132 : : //Old Code
133 : : //ReadLeafIndex(pStrm);
134 : : //New Code
135 [ + - ]: 36 : ReadObjIndex( pStrm );
136 : :
137 : :
138 : : //Read object in root, these objects are between the leaf objects
139 [ + + ]: 36 : if(k!=m_nLeafCount-1)
140 : : {
141 [ + - ]: 18 : m_ObjectKeys.push_back(m_RootObjs[k]);
142 : 18 : m_nKeyCount ++;
143 : : }
144 : : }
145 : 18 : m_RootObjs.clear();
146 : : }
147 : 18 : }
148 : :
149 : : /**
150 : : * @descr Read data in VO_ROOTOBJINDEX
151 : : */
152 : 18 : void LwpIndexManager::ReadRootData(LwpObjectStream* pObjStrm)
153 : : {
154 : :
155 : 18 : sal_uInt16 KeyCount = pObjStrm->QuickReaduInt16();
156 [ + - ]: 18 : m_nLeafCount = KeyCount ? KeyCount + 1 : 0;
157 : :
158 [ + - ]: 18 : if(KeyCount)
159 : : {
160 : : //read object keys
161 [ + - ][ + - ]: 18 : LwpKey* akey = new LwpKey();
162 [ + - ]: 18 : akey->id.Read(pObjStrm);
163 [ + - ]: 18 : m_RootObjs.push_back(akey);
164 : :
165 : :
166 : 18 : sal_uInt16 k = 0;
167 : :
168 [ - + ]: 18 : for (k = 1; k < KeyCount; k++)
169 : : {
170 [ # # ][ # # ]: 0 : akey = new LwpKey();
171 [ # # ]: 0 : akey->id.ReadCompressed(pObjStrm, m_RootObjs[k-1]->id);
172 [ # # ]: 0 : m_RootObjs.push_back(akey);
173 : : }
174 : :
175 [ + + ]: 36 : for (k = 0; k < KeyCount; k++)
176 [ + - ]: 18 : m_RootObjs[k]->offset = pObjStrm->QuickReaduInt32();
177 : :
178 : : //read leaf index offset
179 [ + + ]: 54 : for (k = 0; k < m_nLeafCount; k++)
180 [ + - ]: 36 : m_ChildIndex[k] = pObjStrm->QuickReaduInt32();
181 : : }
182 : :
183 : 18 : ReadTimeTable(pObjStrm);
184 : :
185 : 18 : }
186 : :
187 : :
188 : : //Add new method to handle ObjIndex data
189 : : /**
190 : : * @descr Read data in VO_OBJINDEX
191 : : */
192 : 0 : void LwpIndexManager::ReadObjIndexData(LwpObjectStream* pObjStrm)
193 : : {
194 [ # # ]: 0 : sal_uInt16 KeyCount = pObjStrm->QuickReaduInt16();
195 : 0 : sal_uInt16 LeafCount = KeyCount + 1;
196 : :
197 [ # # ]: 0 : std::vector<LwpKey*> vObjIndexs;
198 : :
199 [ # # ]: 0 : if(KeyCount)
200 : : {
201 [ # # ][ # # ]: 0 : LwpKey* akey = new LwpKey();
202 [ # # ]: 0 : akey->id.Read(pObjStrm);
203 [ # # ]: 0 : vObjIndexs.push_back(akey);
204 : :
205 : 0 : sal_uInt16 k = 0;
206 : :
207 [ # # ]: 0 : for (k = 1; k < KeyCount; k++)
208 : : {
209 [ # # ][ # # ]: 0 : akey = new LwpKey();
210 [ # # ]: 0 : akey->id.ReadCompressed(pObjStrm, vObjIndexs[k-1]->id);
211 [ # # ]: 0 : vObjIndexs.push_back(akey);
212 : : }
213 : :
214 [ # # ]: 0 : for (k = 0; k < KeyCount; k++)
215 [ # # ]: 0 : vObjIndexs[k]->offset = pObjStrm->QuickReaduInt32();
216 : :
217 [ # # ]: 0 : for (k = 0; k < LeafCount; k++)
218 [ # # ][ # # ]: 0 : m_TempVec[k] = pObjStrm->QuickReaduInt32();
219 : : }
220 : :
221 [ # # ]: 0 : for( sal_uInt16 j=0; j<LeafCount; j++ )
222 : : {
223 [ # # ]: 0 : sal_Int64 nPos = m_TempVec[j]+LwpSvStream::LWP_STREAM_BASE;
224 [ # # ]: 0 : sal_Int64 nActualPos = pObjStrm->GetStream()->Seek(nPos);
225 : :
226 [ # # ]: 0 : if (nPos != nActualPos)
227 [ # # ]: 0 : throw BadSeek();
228 : :
229 : :
230 [ # # ]: 0 : ReadLeafIndex(pObjStrm->GetStream());
231 : :
232 [ # # ]: 0 : if(j!=LeafCount-1)
233 : : {
234 [ # # ]: 0 : m_ObjectKeys.push_back(vObjIndexs[j]);
235 : :
236 : 0 : m_nKeyCount ++;
237 : : }
238 : : }
239 : :
240 : 0 : vObjIndexs.clear();
241 : 0 : m_TempVec.clear();
242 : 0 : }
243 : :
244 : :
245 : : /**
246 : : * @descr Read VO_OBJINDEX
247 : : */
248 : 36 : void LwpIndexManager::ReadObjIndex( LwpSvStream *pStrm )
249 : : {
250 : :
251 [ + - ]: 36 : LwpObjectHeader ObjHdr;
252 [ + - ]: 36 : ObjHdr.Read(*pStrm);
253 : 36 : LwpObjectStream* pObjStrm = new LwpObjectStream(pStrm, ObjHdr.IsCompressed(),
254 [ + - ][ + - ]: 36 : static_cast<sal_uInt16>(ObjHdr.GetSize()) );
255 : :
256 [ - + ]: 36 : if( (sal_uInt32)VO_OBJINDEX == ObjHdr.GetTag() )
257 : : {
258 [ # # ]: 0 : ReadObjIndexData( pObjStrm );
259 : : }
260 [ + - ]: 36 : else if( (sal_uInt32)VO_LEAFOBJINDEX == ObjHdr.GetTag() )
261 : : {
262 [ + - ]: 36 : ReadLeafData(pObjStrm);
263 : : }
264 : :
265 [ + - ][ + - ]: 36 : delete pObjStrm;
266 : 36 : }
267 : :
268 : :
269 : : /**
270 : : * @descr Read VO_LEAFOBJINDEX
271 : : */
272 : 0 : void LwpIndexManager::ReadLeafIndex( LwpSvStream *pStrm )
273 : : {
274 [ # # ]: 0 : LwpObjectHeader ObjHdr;
275 [ # # ]: 0 : ObjHdr.Read(*pStrm);
276 : 0 : LwpObjectStream* pObjStrm = new LwpObjectStream(pStrm, ObjHdr.IsCompressed(),
277 [ # # ][ # # ]: 0 : static_cast<sal_uInt16>(ObjHdr.GetSize()) );
278 : :
279 [ # # ]: 0 : ReadLeafData(pObjStrm);
280 : :
281 [ # # ][ # # ]: 0 : delete pObjStrm;
282 : 0 : }
283 : : /**
284 : : * @descr Read data in VO_LEAFOBJINDEX
285 : : */
286 : 36 : void LwpIndexManager::ReadLeafData( LwpObjectStream *pObjStrm )
287 : : {
288 : 36 : sal_uInt16 KeyCount = pObjStrm->QuickReaduInt16();
289 : :
290 [ + - ]: 36 : if(KeyCount)
291 : : {
292 [ + - ][ + - ]: 36 : LwpKey* akey = new LwpKey();
293 : : //read object keys: id & offset
294 [ + - ]: 36 : akey->id.Read(pObjStrm);
295 [ + - ]: 36 : m_ObjectKeys.push_back(akey);
296 : :
297 [ + + ]: 6669 : for (sal_uInt8 k = 1; k < KeyCount; k++)
298 : : {
299 [ + - ][ + - ]: 6633 : akey = new LwpKey();
300 [ + - ][ + - ]: 6633 : akey->id.ReadCompressed(pObjStrm, m_ObjectKeys.at(m_nKeyCount+k-1)->id);
301 [ + - ]: 6633 : m_ObjectKeys.push_back(akey);
302 : : }
303 : :
304 [ + + ]: 6705 : for (sal_uInt8 j = 0; j < KeyCount; j++)
305 [ + - ][ + - ]: 6669 : m_ObjectKeys.at(m_nKeyCount+j)->offset = pObjStrm->QuickReaduInt32();
306 : : }
307 : 36 : m_nKeyCount += KeyCount;
308 : 36 : }
309 : : /**
310 : : * @descr Read time table in VO_ROOTLEAFOBJINDEX and VO_ROOTOBJINDEX
311 : : */
312 : 18 : void LwpIndexManager::ReadTimeTable(LwpObjectStream *pObjStrm)
313 : : {
314 : 18 : sal_uInt16 nTimeCount = pObjStrm->QuickReaduInt16();
315 : :
316 [ + + ]: 288 : for(sal_uInt16 i=0; i<nTimeCount; ++i)
317 : : {
318 [ + - ]: 270 : sal_uInt32 atime = pObjStrm->QuickReaduInt32();
319 [ + - ]: 270 : m_TimeTable.push_back(atime);
320 : : }
321 : 18 : }
322 : : /**
323 : : * @descr get object offset per the object id
324 : : */
325 : 2760 : sal_uInt32 LwpIndexManager::GetObjOffset( LwpObjectID objid )
326 : : {
327 : :
328 : : //sal_uInt16 L, U, M;
329 : : sal_uInt32 L, U, M;
330 : :
331 : :
332 : 2760 : L = 0;
333 : 2760 : U = m_nKeyCount;
334 [ + - ]: 21282 : while (L != U)
335 : : {
336 : 21282 : M = (L + U) >> 1;
337 : :
338 [ + + ]: 21282 : if (objid.GetLow() > m_ObjectKeys[M]->id.GetLow())
339 : 3564 : L = M + 1;
340 [ + + ]: 17718 : else if (objid.GetLow() < m_ObjectKeys[M]->id.GetLow())
341 : 3153 : U = M;
342 [ + + ]: 14565 : else if (objid.GetHigh() > m_ObjectKeys[M]->id.GetHigh())
343 : 5739 : L = M + 1;
344 [ + + ]: 8826 : else if (objid.GetHigh() < m_ObjectKeys[M]->id.GetHigh())
345 : 6066 : U = M;
346 : : else
347 : : {
348 : 2760 : return(m_ObjectKeys[M]->offset);
349 : : }
350 : : }
351 : 2760 : return BAD_OFFSET;
352 : : }
353 : :
354 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|