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 6 : LwpIndexManager::LwpIndexManager()
77 6 : :m_nKeyCount(0), m_nLeafCount(0)
78 : {
79 :
80 6 : m_TempVec.resize( LwpIndexManager::MAXOBJECTIDS );
81 :
82 6 : }
83 :
84 12 : LwpIndexManager::~LwpIndexManager()
85 : {
86 : //Clear m_ObjectKeys
87 6 : std::vector<LwpKey*>::iterator it;
88 :
89 2235 : for( it = m_ObjectKeys.begin(); it != m_ObjectKeys.end(); ++it )
90 : {
91 2229 : LwpKey* pKey = *it;
92 2229 : if( pKey )
93 : {
94 2229 : delete pKey;
95 2229 : pKey = NULL;
96 : }
97 : }
98 6 : m_ObjectKeys.clear();
99 6 : }
100 :
101 : /**
102 : * @descr Read all index records, VO_ROOTLEAFOBJINDEX, VO_ROOTOBJINDEX, VO_LEAFOBJINDEX
103 : */
104 6 : void LwpIndexManager::Read(LwpSvStream* pStrm)
105 : {
106 : //Read index obj
107 6 : LwpObjectHeader ObjHdr;
108 6 : ObjHdr.Read(*pStrm);
109 6 : LwpObjectStream* pObjStrm = new LwpObjectStream(pStrm, ObjHdr.IsCompressed(),
110 6 : static_cast<sal_uInt16>(ObjHdr.GetSize()) );
111 :
112 6 : if( ObjHdr.GetTag() == VO_ROOTLEAFOBJINDEX )
113 : {
114 0 : ReadLeafData(pObjStrm);
115 0 : ReadTimeTable(pObjStrm);
116 0 : delete pObjStrm;
117 : }
118 : else
119 : {
120 6 : ReadRootData(pObjStrm);
121 6 : delete pObjStrm;
122 :
123 18 : for (sal_uInt16 k = 0; k < m_nLeafCount; k++)
124 : {
125 : //Read leaf
126 12 : sal_Int64 nPos = m_ChildIndex[k]+LwpSvStream::LWP_STREAM_BASE;
127 12 : sal_Int64 nActualPos = pStrm->Seek(nPos);
128 :
129 12 : if (nPos != nActualPos)
130 0 : throw BadSeek();
131 :
132 : //Old Code
133 : //ReadLeafIndex(pStrm);
134 : //New Code
135 12 : ReadObjIndex( pStrm );
136 :
137 :
138 : //Read object in root, these objects are between the leaf objects
139 12 : if(k!=m_nLeafCount-1)
140 : {
141 6 : m_ObjectKeys.push_back(m_RootObjs[k]);
142 6 : m_nKeyCount ++;
143 : }
144 : }
145 6 : m_RootObjs.clear();
146 : }
147 6 : }
148 :
149 : /**
150 : * @descr Read data in VO_ROOTOBJINDEX
151 : */
152 6 : void LwpIndexManager::ReadRootData(LwpObjectStream* pObjStrm)
153 : {
154 :
155 6 : sal_uInt16 KeyCount = pObjStrm->QuickReaduInt16();
156 6 : m_nLeafCount = KeyCount ? KeyCount + 1 : 0;
157 :
158 6 : if(KeyCount)
159 : {
160 : //read object keys
161 6 : LwpKey* akey = new LwpKey();
162 6 : akey->id.Read(pObjStrm);
163 6 : m_RootObjs.push_back(akey);
164 :
165 :
166 6 : sal_uInt16 k = 0;
167 :
168 6 : 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 12 : for (k = 0; k < KeyCount; k++)
176 6 : m_RootObjs[k]->offset = pObjStrm->QuickReaduInt32();
177 :
178 : //read leaf index offset
179 18 : for (k = 0; k < m_nLeafCount; k++)
180 12 : m_ChildIndex[k] = pObjStrm->QuickReaduInt32();
181 : }
182 :
183 6 : ReadTimeTable(pObjStrm);
184 :
185 6 : }
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 12 : void LwpIndexManager::ReadObjIndex( LwpSvStream *pStrm )
249 : {
250 :
251 12 : LwpObjectHeader ObjHdr;
252 12 : ObjHdr.Read(*pStrm);
253 12 : LwpObjectStream* pObjStrm = new LwpObjectStream(pStrm, ObjHdr.IsCompressed(),
254 12 : static_cast<sal_uInt16>(ObjHdr.GetSize()) );
255 :
256 12 : if( (sal_uInt32)VO_OBJINDEX == ObjHdr.GetTag() )
257 : {
258 0 : ReadObjIndexData( pObjStrm );
259 : }
260 12 : else if( (sal_uInt32)VO_LEAFOBJINDEX == ObjHdr.GetTag() )
261 : {
262 12 : ReadLeafData(pObjStrm);
263 : }
264 :
265 12 : delete pObjStrm;
266 12 : }
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 12 : void LwpIndexManager::ReadLeafData( LwpObjectStream *pObjStrm )
287 : {
288 12 : sal_uInt16 KeyCount = pObjStrm->QuickReaduInt16();
289 :
290 12 : if(KeyCount)
291 : {
292 12 : LwpKey* akey = new LwpKey();
293 : //read object keys: id & offset
294 12 : akey->id.Read(pObjStrm);
295 12 : m_ObjectKeys.push_back(akey);
296 :
297 2223 : for (sal_uInt8 k = 1; k < KeyCount; k++)
298 : {
299 2211 : akey = new LwpKey();
300 2211 : akey->id.ReadCompressed(pObjStrm, m_ObjectKeys.at(m_nKeyCount+k-1)->id);
301 2211 : m_ObjectKeys.push_back(akey);
302 : }
303 :
304 2235 : for (sal_uInt8 j = 0; j < KeyCount; j++)
305 2223 : m_ObjectKeys.at(m_nKeyCount+j)->offset = pObjStrm->QuickReaduInt32();
306 : }
307 12 : m_nKeyCount += KeyCount;
308 12 : }
309 : /**
310 : * @descr Read time table in VO_ROOTLEAFOBJINDEX and VO_ROOTOBJINDEX
311 : */
312 6 : void LwpIndexManager::ReadTimeTable(LwpObjectStream *pObjStrm)
313 : {
314 6 : sal_uInt16 nTimeCount = pObjStrm->QuickReaduInt16();
315 :
316 96 : for(sal_uInt16 i=0; i<nTimeCount; ++i)
317 : {
318 90 : sal_uInt32 atime = pObjStrm->QuickReaduInt32();
319 90 : m_TimeTable.push_back(atime);
320 : }
321 6 : }
322 : /**
323 : * @descr get object offset per the object id
324 : */
325 920 : sal_uInt32 LwpIndexManager::GetObjOffset( LwpObjectID objid )
326 : {
327 :
328 : //sal_uInt16 L, U, M;
329 : sal_uInt32 L, U, M;
330 :
331 :
332 920 : L = 0;
333 920 : U = m_nKeyCount;
334 8014 : while (L != U)
335 : {
336 7094 : M = (L + U) >> 1;
337 :
338 7094 : if (objid.GetLow() > m_ObjectKeys[M]->id.GetLow())
339 1188 : L = M + 1;
340 5906 : else if (objid.GetLow() < m_ObjectKeys[M]->id.GetLow())
341 1051 : U = M;
342 4855 : else if (objid.GetHigh() > m_ObjectKeys[M]->id.GetHigh())
343 1913 : L = M + 1;
344 2942 : else if (objid.GetHigh() < m_ObjectKeys[M]->id.GetHigh())
345 2022 : U = M;
346 : else
347 : {
348 920 : return(m_ObjectKeys[M]->offset);
349 : }
350 : }
351 0 : return BAD_OFFSET;
352 : }
353 :
354 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|