bump product version to 4.1.6.2
[LibreOffice.git] / store / workben / t_store.cxx
blobe1c1a1c4a1fd31dd67bbd0b4c83846f1c451cdd4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #define _T_STORE_CXX
22 #include <sal/types.h>
23 #include <osl/diagnose.h>
24 #include <osl/thread.h>
25 #include <osl/time.h>
26 #include <rtl/ustring.hxx>
27 #include <store/store.hxx>
29 #include <stdio.h>
31 #if (defined(WNT) && defined(PROFILE))
32 extern "C"
34 void StartCAP (void);
35 void StopCAP (void);
36 void DumpCAP (void);
38 #endif /* PROFILE */
41 /*========================================================================
43 * Internals.
45 *======================================================================*/
46 #define _DEMOSTOR_BUFSIZ 512 /* 4096, 1024, 512 */
47 #define _DEMOSTOR_LOOPS 1000 /* 1000, 2000 */
49 #define _DEMOSTOR_REMOVE 0
50 #define _DEMOSTOR_REBUILD 0
52 enum Options
54 OPTION_HELP = 0x0001,
55 OPTION_FILE = 0x0002,
56 OPTION_PAUSE = 0x0004,
57 OPTION_REBUILD = 0x0008,
59 OPTION_DUMP = 0x0010,
60 OPTION_ITER = 0x0020,
61 OPTION_LINK = 0x0040,
63 OPTION_READ = 0x0100,
64 OPTION_WRITE = 0x0200,
65 OPTION_CREAT = 0x0400,
66 OPTION_TRUNC = 0x0800
69 inline sal_Char ascii_toLowerCase (sal_Char ch)
71 if ((ch >= 0x41) && (ch <= 0x5A))
72 return (ch + 0x20);
73 else
74 return (ch);
77 /*========================================================================
79 * Timing.
81 *======================================================================*/
82 struct OTime : public TimeValue
84 OTime (void)
86 Seconds = 0;
87 Nanosec = 0;
90 static OTime getSystemTime (void)
92 OTime tv;
93 osl_getSystemTime (&tv);
94 return tv;
97 OTime& operator-= (const OTime& rPast)
99 Seconds -= rPast.Seconds;
100 if (Nanosec < rPast.Nanosec)
102 Seconds -= 1;
103 Nanosec += 1000000000;
105 Nanosec -= rPast.Nanosec;
106 return *this;
109 friend OTime operator- (const OTime& rTimeA, const OTime& rTimeB)
111 OTime aTimeC (rTimeA);
112 aTimeC -= rTimeB;
113 return aTimeC;
117 /*========================================================================
119 * DirectoryTraveller.
121 *======================================================================*/
122 typedef store::OStoreDirectory Directory;
124 class DirectoryTraveller : public Directory::traveller
126 typedef store::OStoreFile file;
127 typedef Directory::iterator iter;
129 store::OStoreFile m_aFile;
130 OUString m_aPath;
132 sal_uInt32 m_nOptions;
133 unsigned int m_nLevel;
134 unsigned int m_nCount;
136 public:
137 DirectoryTraveller (
138 const file& rFile,
139 const OUString &rPath,
140 const OUString &rName,
141 sal_uInt32 nOptions,
142 unsigned int nLevel = 0);
144 virtual ~DirectoryTraveller (void);
146 virtual sal_Bool visit (const iter& it);
150 * DirectoryTraveller.
152 DirectoryTraveller::DirectoryTraveller (
153 const file& rFile,
154 const OUString &rPath,
155 const OUString &rName,
156 sal_uInt32 nOptions,
157 unsigned int nLevel)
158 : m_aFile (rFile),
159 m_aPath (rPath),
160 m_nOptions (nOptions),
161 m_nLevel (nLevel),
162 m_nCount (0)
164 m_aPath += rName + "/";
168 * ~DirectoryTraveller.
170 DirectoryTraveller::~DirectoryTraveller (void)
175 * visit.
177 sal_Bool DirectoryTraveller::visit (const iter& it)
179 m_nCount++;
180 if (m_nOptions & OPTION_DUMP)
182 OString aName (it.m_pszName, it.m_nLength, RTL_TEXTENCODING_UTF8);
183 printf ("Visit(%u,%u): %s [0x%08x] %d [Bytes]\n",
184 m_nLevel, m_nCount,
185 aName.pData->buffer, (unsigned int)(it.m_nAttrib), (unsigned int)(it.m_nSize));
187 if (it.m_nAttrib & STORE_ATTRIB_ISDIR)
189 OUString aName (it.m_pszName, it.m_nLength);
190 if (aName.compareToAscii ("XTextViewCursorSupplier") == 0)
192 m_nCount += 1 - 1;
194 Directory aSubDir;
196 storeError eErrCode = aSubDir.create (
197 m_aFile, m_aPath, aName, store_AccessReadOnly);
198 if (eErrCode == store_E_None)
200 sal_uInt32 nRefCount = 0;
201 m_aFile.getRefererCount (nRefCount);
203 DirectoryTraveller aSubTraveller (
204 m_aFile, m_aPath, aName, m_nOptions, m_nLevel + 1);
205 aSubDir.travel (aSubTraveller);
208 return sal_True;
211 /*========================================================================
213 * main.
215 *======================================================================*/
216 int SAL_CALL main (int argc, char **argv)
218 #if (defined(WNT) && defined(PROFILE))
219 StartCAP();
220 #else
221 OTime aMainStartTime (OTime::getSystemTime());
222 #endif /* PROFILE */
224 store::OStoreFile aFile;
225 storeError eErrCode = store_E_None;
227 sal_uInt32 nOptions = 0;
228 for (int i = 1; i < argc; i++)
230 const char *opt = argv[i];
231 if (opt[0] == '-')
233 switch (ascii_toLowerCase(sal_Char(opt[1])))
235 case 'f':
236 nOptions |= OPTION_FILE;
237 break;
239 case 'd':
240 nOptions |= OPTION_DUMP;
241 break;
242 case 'i':
243 nOptions |= OPTION_ITER;
244 break;
245 case 'l':
246 nOptions |= OPTION_LINK;
247 break;
249 case 'r':
250 nOptions |= OPTION_READ;
251 break;
252 case 'w':
253 nOptions |= OPTION_WRITE;
254 break;
255 case 'c':
256 nOptions |= OPTION_CREAT;
257 break;
258 case 't':
259 nOptions |= OPTION_TRUNC;
260 break;
262 case 'p':
263 nOptions |= OPTION_PAUSE;
264 break;
266 case 'h':
267 default:
268 nOptions |= OPTION_HELP;
269 break;
272 else
274 if (nOptions & OPTION_FILE)
276 OUString aName (
277 argv[i], rtl_str_getLength(argv[i]),
278 osl_getThreadTextEncoding());
279 if ((nOptions & OPTION_CREAT) && (nOptions & OPTION_TRUNC))
280 eErrCode = aFile.create (aName, store_AccessCreate);
281 else if (nOptions & OPTION_CREAT)
282 eErrCode = aFile.create (aName, store_AccessReadCreate);
283 else if (nOptions & OPTION_WRITE)
284 eErrCode = aFile.create (aName, store_AccessReadWrite);
285 else
286 eErrCode = aFile.create (aName, store_AccessReadOnly);
287 if (eErrCode != store_E_None)
289 printf ("Error: can't open file: %s\n", argv[i]);
290 exit (0);
296 if ((nOptions == 0) || (nOptions & OPTION_HELP))
298 printf ("Usage:\tt_store "
299 "[[-c] [-t] [-r] [-w]] [[-i] [-d] [-h]] "
300 "[-f filename]\n");
302 printf ("\nOptions:\n");
303 printf ("-c\tcreate\n");
304 printf ("-t\ttruncate\n");
305 printf ("-r\tread\n");
306 printf ("-w\twrite\n");
307 printf ("-i\titerate\n");
308 printf ("-d\tdump\n");
309 printf ("-h\thelp\n");
310 printf ("-f\tfilename\n");
312 printf ("\nExamples:");
313 printf ("\nt_store -c -w -f t_store.rdb\n");
314 printf ("\tCreate file 't_store.rdb',\n"
315 "\twrite fixed number (1000) of streams.\n");
316 printf ("\nt_store -c -i -d -f t_store.rdb\n");
317 printf ("\tOpen file 't_store.rdb', "
318 "create '/' directory,\n"
319 "\titerate directory tree, "
320 "dump directory info.\n");
322 exit (0);
325 if (!aFile.isValid())
327 eErrCode = aFile.createInMemory();
328 if (eErrCode != store_E_None)
330 printf ("Error: can't create memory file\n");
331 exit (0);
335 // Stream Read/Write.
336 OUString aPath ("/");
337 if ((nOptions & OPTION_READ) || (nOptions & OPTION_WRITE))
339 // Mode.
340 storeAccessMode eMode = store_AccessReadOnly;
341 if (nOptions & OPTION_WRITE)
342 eMode = store_AccessReadWrite;
343 if (nOptions & OPTION_CREAT)
344 eMode = store_AccessCreate;
346 // Buffer.
347 char pBuffer[_DEMOSTOR_BUFSIZ] = "Hello World";
348 pBuffer[_DEMOSTOR_BUFSIZ - 2] = 'B';
349 pBuffer[_DEMOSTOR_BUFSIZ - 1] = '\0';
351 // Load/Save.
352 #ifndef PROFILE
353 OTime aStartTime (OTime::getSystemTime());
354 #endif /* PROFILE */
356 for (int i = 0; i < _DEMOSTOR_LOOPS; i++)
358 OUString aName ("demostor-");
359 aName += OUString::valueOf ((sal_Int32)(i + 1), 10);
360 aName += ".dat";
362 #if (_DEMOSTOR_REMOVE == 1)
363 eErrCode = aFile.remove (aPath, aName);
364 if ((eErrCode != store_E_None ) &&
365 (eErrCode != store_E_NotExists) )
366 break;
367 #endif /* _REMOVE */
369 store::OStoreStream aStream;
370 eErrCode = aStream.create (aFile, aPath, aName, eMode);
371 if (eErrCode != store_E_None)
373 OSL_TRACE("OStoreStream(%d)::create(): error: %d", i, eErrCode);
374 break;
377 if (nOptions & OPTION_TRUNC)
379 eErrCode = aStream.setSize(0);
380 if (eErrCode != store_E_None)
382 OSL_TRACE("OStoreStream(%d)::setSize(0): error: %d", i, eErrCode);
383 break;
387 sal_uInt32 nDone = 0;
388 if (nOptions & OPTION_WRITE)
390 eErrCode = aStream.writeAt (
391 0, pBuffer, sizeof(pBuffer), nDone);
392 if (eErrCode != store_E_None)
394 OSL_TRACE("OStoreStream(%d)::writeAt(): error: %d", i, eErrCode);
395 break;
399 if (nOptions & OPTION_READ)
401 sal_uInt32 nOffset = 0;
402 for (;;)
404 eErrCode = aStream.readAt (
405 nOffset, pBuffer, sizeof(pBuffer), nDone);
406 if (eErrCode != store_E_None)
408 OSL_TRACE("OStoreStream(%d)::readAt(): error: %d", i, eErrCode);
409 break;
411 if (nDone == 0)
412 break;
413 nOffset += nDone;
417 aStream.close();
419 #ifndef PROFILE
420 if (((i + 1) % (_DEMOSTOR_LOOPS/10)) == 0)
422 OTime aDelta (OTime::getSystemTime() - aStartTime);
424 sal_uInt32 nDelta = aDelta.Seconds * 1000000;
425 nDelta += (aDelta.Nanosec / 1000);
427 printf ("%d: %12.4g[usec]\n", (i+1),
428 (double)(nDelta)/(double)(i+1));
430 #endif /* PROFILE */
433 #ifndef PROFILE
434 OTime aDelta (OTime::getSystemTime() - aStartTime);
436 sal_uInt32 nDelta = aDelta.Seconds * 1000000;
437 nDelta += (aDelta.Nanosec / 1000);
439 printf ("Total(rd,wr): %d[usec]\n", (unsigned int)(nDelta));
440 #endif /* PROFILE */
443 // Link/Rename.
444 if (nOptions & OPTION_LINK)
446 // Create symlink to (root) directory.
447 eErrCode = aFile.symlink (
448 aPath, OUString("000000/"),
449 OUString(), aPath);
450 OSL_POSTCOND(
451 ((eErrCode == store_E_None ) ||
452 (eErrCode == store_E_AlreadyExists) ),
453 "t_store::main(): store_symlink() failed");
455 // Create symlink to file.
456 OUString aLinkName ("demostor-1.lnk");
458 eErrCode = aFile.symlink (
459 aPath, aLinkName,
460 aPath, OUString("demostor-1.dat"));
461 OSL_POSTCOND(
462 ((eErrCode == store_E_None ) ||
463 (eErrCode == store_E_AlreadyExists) ),
464 "t_store::main(): store_symlink() failed");
465 if ((eErrCode == store_E_None ) ||
466 (eErrCode == store_E_AlreadyExists) )
468 OUString aShortcut (
469 "Shortcut to demostor-1.dat");
470 eErrCode = aFile.rename (
471 aPath, aLinkName,
472 aPath, aShortcut);
473 OSL_POSTCOND(
474 ((eErrCode == store_E_None ) ||
475 (eErrCode == store_E_AlreadyExists) ),
476 "t_store::main(): store_rename() failed");
479 // Create directory.
480 OUString aDirName ("demostor-1.dir");
481 store::OStoreDirectory aDir;
483 eErrCode = aDir.create (
484 aFile, aPath, aDirName, store_AccessReadCreate);
485 OSL_POSTCOND(
486 (eErrCode == store_E_None),
487 "t_store::main(): store_createDirectory() failed");
488 if (eErrCode == store_E_None)
493 // Directory iteration.
494 if (nOptions & OPTION_ITER)
496 #ifndef PROFILE
497 OTime aStartTime (OTime::getSystemTime());
498 #endif /* PROFILE */
499 OUString aEmpty;
501 // Root directory.
502 store::OStoreDirectory aRootDir;
503 if (nOptions & OPTION_LINK)
505 // Open symlink entry.
506 eErrCode = aRootDir.create (
507 aFile, aPath, OUString("000000"),
508 store_AccessReadOnly);
510 else
512 // Open direct entry.
513 if (nOptions & OPTION_CREAT)
514 eErrCode = aRootDir.create (
515 aFile, aEmpty, aEmpty, store_AccessReadCreate);
516 else if (nOptions & OPTION_WRITE)
517 eErrCode = aRootDir.create (
518 aFile, aEmpty, aEmpty, store_AccessReadWrite);
519 else
520 eErrCode = aRootDir.create (
521 aFile, aEmpty, aEmpty, store_AccessReadOnly);
524 if (eErrCode == store_E_None)
526 // Traverse directory tree.
527 DirectoryTraveller aTraveller (
528 aFile, aEmpty, aEmpty, nOptions, 0);
529 aRootDir.travel (aTraveller);
531 else
533 // Failure.
534 printf ("Error: can't open directory: \"/\"\n");
537 #ifndef PROFILE
538 OTime aDelta (OTime::getSystemTime() - aStartTime);
540 sal_uInt32 nDelta = aDelta.Seconds * 1000000;
541 nDelta += (aDelta.Nanosec / 1000);
543 printf ("Total(iter): %d[usec]\n", (unsigned int)(nDelta));
544 #endif /* PROFILE */
547 if (nOptions & OPTION_PAUSE)
549 TimeValue tv;
550 tv.Seconds = 300;
551 tv.Nanosec = 0;
552 osl_waitThread (&tv);
555 // Size.
556 sal_uInt32 nSize = 0;
557 aFile.getSize (nSize);
559 // Done.
560 aFile.close();
562 #if (defined(WNT) && defined(PROFILE))
563 StopCAP();
564 DumpCAP();
565 #endif /* PROFILE */
566 #ifndef PROFILE
567 OTime aDelta (OTime::getSystemTime() - aMainStartTime);
569 sal_uInt32 nDelta = aDelta.Seconds * 1000000;
570 nDelta += (aDelta.Nanosec / 1000);
572 printf ("Total: %d[usec]\n", (unsigned int)(nDelta));
573 #endif /* PROFILE */
575 return 0;
578 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */