Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / store / workben / t_store.cxx
bloba6184eb480c290b81c133991eb6e80258b10e11b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #define _T_STORE_CXX
31 #include <sal/types.h>
32 #include <osl/diagnose.h>
33 #include <osl/thread.h>
34 #include <osl/time.h>
35 #include <rtl/ustring.hxx>
36 #include <store/store.hxx>
38 #include <stdio.h>
40 #if (defined(WNT) && defined(PROFILE))
41 extern "C"
43 void StartCAP (void);
44 void StopCAP (void);
45 void DumpCAP (void);
47 #endif /* PROFILE */
49 using rtl::OUString;
51 /*========================================================================
53 * Internals.
55 *======================================================================*/
56 #define _DEMOSTOR_BUFSIZ 512 /* 4096, 1024, 512 */
57 #define _DEMOSTOR_LOOPS 1000 /* 1000, 2000 */
59 #define _DEMOSTOR_REMOVE 0
60 #define _DEMOSTOR_REBUILD 0
62 enum Options
64 OPTION_HELP = 0x0001,
65 OPTION_FILE = 0x0002,
66 OPTION_PAUSE = 0x0004,
67 OPTION_REBUILD = 0x0008,
69 OPTION_DUMP = 0x0010,
70 OPTION_ITER = 0x0020,
71 OPTION_LINK = 0x0040,
73 OPTION_READ = 0x0100,
74 OPTION_WRITE = 0x0200,
75 OPTION_CREAT = 0x0400,
76 OPTION_TRUNC = 0x0800
79 inline sal_Char ascii_toLowerCase (sal_Char ch)
81 if ((ch >= 0x41) && (ch <= 0x5A))
82 return (ch + 0x20);
83 else
84 return (ch);
87 /*========================================================================
89 * Timing.
91 *======================================================================*/
92 struct OTime : public TimeValue
94 OTime (void)
96 Seconds = 0;
97 Nanosec = 0;
100 static OTime getSystemTime (void)
102 OTime tv;
103 osl_getSystemTime (&tv);
104 return tv;
107 OTime& operator-= (const OTime& rPast)
109 Seconds -= rPast.Seconds;
110 if (Nanosec < rPast.Nanosec)
112 Seconds -= 1;
113 Nanosec += 1000000000;
115 Nanosec -= rPast.Nanosec;
116 return *this;
119 friend OTime operator- (const OTime& rTimeA, const OTime& rTimeB)
121 OTime aTimeC (rTimeA);
122 aTimeC -= rTimeB;
123 return aTimeC;
127 /*========================================================================
129 * DirectoryTraveller.
131 *======================================================================*/
132 typedef store::OStoreDirectory Directory;
134 class DirectoryTraveller : public Directory::traveller
136 typedef store::OStoreFile file;
137 typedef Directory::iterator iter;
139 store::OStoreFile m_aFile;
140 OUString m_aPath;
142 sal_uInt32 m_nOptions;
143 unsigned int m_nLevel;
144 unsigned int m_nCount;
146 public:
147 DirectoryTraveller (
148 const file& rFile,
149 const OUString &rPath,
150 const OUString &rName,
151 sal_uInt32 nOptions,
152 unsigned int nLevel = 0);
154 virtual ~DirectoryTraveller (void);
156 virtual sal_Bool visit (const iter& it);
160 * DirectoryTraveller.
162 DirectoryTraveller::DirectoryTraveller (
163 const file& rFile,
164 const OUString &rPath,
165 const OUString &rName,
166 sal_uInt32 nOptions,
167 unsigned int nLevel)
168 : m_aFile (rFile),
169 m_aPath (rPath),
170 m_nOptions (nOptions),
171 m_nLevel (nLevel),
172 m_nCount (0)
174 m_aPath += rName;
175 m_aPath += OUString("/");
179 * ~DirectoryTraveller.
181 DirectoryTraveller::~DirectoryTraveller (void)
186 * visit.
188 sal_Bool DirectoryTraveller::visit (const iter& it)
190 m_nCount++;
191 if (m_nOptions & OPTION_DUMP)
193 rtl::OString aName (it.m_pszName, it.m_nLength, RTL_TEXTENCODING_UTF8);
194 printf ("Visit(%d,%d): %s [0x%08x] %d [Bytes]\n",
195 m_nLevel, m_nCount,
196 aName.pData->buffer, (unsigned int)(it.m_nAttrib), (unsigned int)(it.m_nSize));
198 if (it.m_nAttrib & STORE_ATTRIB_ISDIR)
200 OUString aName (it.m_pszName, it.m_nLength);
201 if (aName.compareToAscii ("XTextViewCursorSupplier") == 0)
203 m_nCount += 1 - 1;
205 Directory aSubDir;
207 storeError eErrCode = aSubDir.create (
208 m_aFile, m_aPath, aName, store_AccessReadOnly);
209 if (eErrCode == store_E_None)
211 sal_uInt32 nRefCount = 0;
212 m_aFile.getRefererCount (nRefCount);
214 DirectoryTraveller aSubTraveller (
215 m_aFile, m_aPath, aName, m_nOptions, m_nLevel + 1);
216 aSubDir.travel (aSubTraveller);
219 return sal_True;
222 /*========================================================================
224 * main.
226 *======================================================================*/
227 int SAL_CALL main (int argc, char **argv)
229 #if (defined(WNT) && defined(PROFILE))
230 StartCAP();
231 #else
232 OTime aMainStartTime (OTime::getSystemTime());
233 #endif /* PROFILE */
235 store::OStoreFile aFile;
236 storeError eErrCode = store_E_None;
238 sal_uInt32 nOptions = 0;
239 for (int i = 1; i < argc; i++)
241 const char *opt = argv[i];
242 if (opt[0] == '-')
244 switch (ascii_toLowerCase(sal_Char(opt[1])))
246 case 'f':
247 nOptions |= OPTION_FILE;
248 break;
250 case 'd':
251 nOptions |= OPTION_DUMP;
252 break;
253 case 'i':
254 nOptions |= OPTION_ITER;
255 break;
256 case 'l':
257 nOptions |= OPTION_LINK;
258 break;
260 case 'r':
261 nOptions |= OPTION_READ;
262 break;
263 case 'w':
264 nOptions |= OPTION_WRITE;
265 break;
266 case 'c':
267 nOptions |= OPTION_CREAT;
268 break;
269 case 't':
270 nOptions |= OPTION_TRUNC;
271 break;
273 case 'p':
274 nOptions |= OPTION_PAUSE;
275 break;
277 case 'h':
278 default:
279 nOptions |= OPTION_HELP;
280 break;
283 else
285 if (nOptions & OPTION_FILE)
287 OUString aName (
288 argv[i], rtl_str_getLength(argv[i]),
289 osl_getThreadTextEncoding());
290 if ((nOptions & OPTION_CREAT) && (nOptions & OPTION_TRUNC))
291 eErrCode = aFile.create (aName, store_AccessCreate);
292 else if (nOptions & OPTION_CREAT)
293 eErrCode = aFile.create (aName, store_AccessReadCreate);
294 else if (nOptions & OPTION_WRITE)
295 eErrCode = aFile.create (aName, store_AccessReadWrite);
296 else
297 eErrCode = aFile.create (aName, store_AccessReadOnly);
298 if (eErrCode != store_E_None)
300 printf ("Error: can't open file: %s\n", argv[i]);
301 exit (0);
307 if ((nOptions == 0) || (nOptions & OPTION_HELP))
309 printf ("Usage:\tt_store "
310 "[[-c] [-t] [-r] [-w]] [[-i] [-d] [-h]] "
311 "[-f filename]\n");
313 printf ("\nOptions:\n");
314 printf ("-c\tcreate\n");
315 printf ("-t\ttruncate\n");
316 printf ("-r\tread\n");
317 printf ("-w\twrite\n");
318 printf ("-i\titerate\n");
319 printf ("-d\tdump\n");
320 printf ("-h\thelp\n");
321 printf ("-f\tfilename\n");
323 printf ("\nExamples:");
324 printf ("\nt_store -c -w -f t_store.rdb\n");
325 printf ("\tCreate file 't_store.rdb',\n"
326 "\twrite fixed number (1000) of streams.\n");
327 printf ("\nt_store -c -i -d -f t_store.rdb\n");
328 printf ("\tOpen file 't_store.rdb', "
329 "create '/' directory,\n"
330 "\titerate directory tree, "
331 "dump directory info.\n");
333 exit (0);
336 if (!aFile.isValid())
338 eErrCode = aFile.createInMemory();
339 if (eErrCode != store_E_None)
341 printf ("Error: can't create memory file\n");
342 exit (0);
346 // Stream Read/Write.
347 OUString aPath ("/");
348 if ((nOptions & OPTION_READ) || (nOptions & OPTION_WRITE))
350 // Mode.
351 storeAccessMode eMode = store_AccessReadOnly;
352 if (nOptions & OPTION_WRITE)
353 eMode = store_AccessReadWrite;
354 if (nOptions & OPTION_CREAT)
355 eMode = store_AccessCreate;
357 // Buffer.
358 char pBuffer[_DEMOSTOR_BUFSIZ] = "Hello World";
359 pBuffer[_DEMOSTOR_BUFSIZ - 2] = 'B';
360 pBuffer[_DEMOSTOR_BUFSIZ - 1] = '\0';
362 // Load/Save.
363 #ifndef PROFILE
364 OTime aStartTime (OTime::getSystemTime());
365 #endif /* PROFILE */
367 for (int i = 0; i < _DEMOSTOR_LOOPS; i++)
369 OUString aName ("demostor-");
370 aName += OUString::valueOf ((sal_Int32)(i + 1), 10);
371 aName += OUString(".dat");
373 #if (_DEMOSTOR_REMOVE == 1)
374 eErrCode = aFile.remove (aPath, aName);
375 if ((eErrCode != store_E_None ) &&
376 (eErrCode != store_E_NotExists) )
377 break;
378 #endif /* _REMOVE */
380 store::OStoreStream aStream;
381 eErrCode = aStream.create (aFile, aPath, aName, eMode);
382 if (eErrCode != store_E_None)
384 OSL_TRACE("OStoreStream(%d)::create(): error: %d", i, eErrCode);
385 break;
388 if (nOptions & OPTION_TRUNC)
390 eErrCode = aStream.setSize(0);
391 if (eErrCode != store_E_None)
393 OSL_TRACE("OStoreStream(%d)::setSize(0): error: %d", i, eErrCode);
394 break;
398 sal_uInt32 nDone = 0;
399 if (nOptions & OPTION_WRITE)
401 eErrCode = aStream.writeAt (
402 0, pBuffer, sizeof(pBuffer), nDone);
403 if (eErrCode != store_E_None)
405 OSL_TRACE("OStoreStream(%d)::writeAt(): error: %d", i, eErrCode);
406 break;
410 if (nOptions & OPTION_READ)
412 sal_uInt32 nOffset = 0;
413 for (;;)
415 eErrCode = aStream.readAt (
416 nOffset, pBuffer, sizeof(pBuffer), nDone);
417 if (eErrCode != store_E_None)
419 OSL_TRACE("OStoreStream(%d)::readAt(): error: %d", i, eErrCode);
420 break;
422 if (nDone == 0)
423 break;
424 nOffset += nDone;
428 aStream.close();
430 #ifndef PROFILE
431 if (((i + 1) % (_DEMOSTOR_LOOPS/10)) == 0)
433 OTime aDelta (OTime::getSystemTime() - aStartTime);
435 sal_uInt32 nDelta = aDelta.Seconds * 1000000;
436 nDelta += (aDelta.Nanosec / 1000);
438 printf ("%d: %12.4g[usec]\n", (i+1),
439 (double)(nDelta)/(double)(i+1));
441 #endif /* PROFILE */
444 #ifndef PROFILE
445 OTime aDelta (OTime::getSystemTime() - aStartTime);
447 sal_uInt32 nDelta = aDelta.Seconds * 1000000;
448 nDelta += (aDelta.Nanosec / 1000);
450 printf ("Total(rd,wr): %d[usec]\n", (unsigned int)(nDelta));
451 #endif /* PROFILE */
454 // Link/Rename.
455 if (nOptions & OPTION_LINK)
457 // Create symlink to (root) directory.
458 eErrCode = aFile.symlink (
459 aPath, OUString("000000/"),
460 OUString(), aPath);
461 OSL_POSTCOND(
462 ((eErrCode == store_E_None ) ||
463 (eErrCode == store_E_AlreadyExists) ),
464 "t_store::main(): store_symlink() failed");
466 // Create symlink to file.
467 OUString aLinkName ("demostor-1.lnk");
469 eErrCode = aFile.symlink (
470 aPath, aLinkName,
471 aPath, OUString("demostor-1.dat"));
472 OSL_POSTCOND(
473 ((eErrCode == store_E_None ) ||
474 (eErrCode == store_E_AlreadyExists) ),
475 "t_store::main(): store_symlink() failed");
476 if ((eErrCode == store_E_None ) ||
477 (eErrCode == store_E_AlreadyExists) )
479 OUString aShortcut (
480 "Shortcut to demostor-1.dat");
481 eErrCode = aFile.rename (
482 aPath, aLinkName,
483 aPath, aShortcut);
484 OSL_POSTCOND(
485 ((eErrCode == store_E_None ) ||
486 (eErrCode == store_E_AlreadyExists) ),
487 "t_store::main(): store_rename() failed");
490 // Create directory.
491 OUString aDirName ("demostor-1.dir");
492 store::OStoreDirectory aDir;
494 eErrCode = aDir.create (
495 aFile, aPath, aDirName, store_AccessReadCreate);
496 OSL_POSTCOND(
497 (eErrCode == store_E_None),
498 "t_store::main(): store_createDirectory() failed");
499 if (eErrCode == store_E_None)
504 // Directory iteration.
505 if (nOptions & OPTION_ITER)
507 #ifndef PROFILE
508 OTime aStartTime (OTime::getSystemTime());
509 #endif /* PROFILE */
510 OUString aEmpty;
512 // Root directory.
513 store::OStoreDirectory aRootDir;
514 if (nOptions & OPTION_LINK)
516 // Open symlink entry.
517 eErrCode = aRootDir.create (
518 aFile, aPath, OUString("000000"),
519 store_AccessReadOnly);
521 else
523 // Open direct entry.
524 if (nOptions & OPTION_CREAT)
525 eErrCode = aRootDir.create (
526 aFile, aEmpty, aEmpty, store_AccessReadCreate);
527 else if (nOptions & OPTION_WRITE)
528 eErrCode = aRootDir.create (
529 aFile, aEmpty, aEmpty, store_AccessReadWrite);
530 else
531 eErrCode = aRootDir.create (
532 aFile, aEmpty, aEmpty, store_AccessReadOnly);
535 if (eErrCode == store_E_None)
537 // Traverse directory tree.
538 DirectoryTraveller aTraveller (
539 aFile, aEmpty, aEmpty, nOptions, 0);
540 aRootDir.travel (aTraveller);
542 else
544 // Failure.
545 printf ("Error: can't open directory: \"/\"\n");
548 #ifndef PROFILE
549 OTime aDelta (OTime::getSystemTime() - aStartTime);
551 sal_uInt32 nDelta = aDelta.Seconds * 1000000;
552 nDelta += (aDelta.Nanosec / 1000);
554 printf ("Total(iter): %d[usec]\n", (unsigned int)(nDelta));
555 #endif /* PROFILE */
558 if (nOptions & OPTION_PAUSE)
560 TimeValue tv;
561 tv.Seconds = 300;
562 tv.Nanosec = 0;
563 osl_waitThread (&tv);
566 // Size.
567 sal_uInt32 nSize = 0;
568 aFile.getSize (nSize);
570 // Done.
571 aFile.close();
573 #if (defined(WNT) && defined(PROFILE))
574 StopCAP();
575 DumpCAP();
576 #endif /* PROFILE */
577 #ifndef PROFILE
578 OTime aDelta (OTime::getSystemTime() - aMainStartTime);
580 sal_uInt32 nDelta = aDelta.Seconds * 1000000;
581 nDelta += (aDelta.Nanosec / 1000);
583 printf ("Total: %d[usec]\n", (unsigned int)(nDelta));
584 #endif /* PROFILE */
586 return 0;
589 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */