1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: t_store.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_store.hxx"
34 #define _T_STORE_CXX "$Revision: 1.7.8.2 $"
35 #include <sal/types.h>
36 #include <osl/diagnose.h>
37 #include <osl/thread.h>
39 #include <rtl/ustring.hxx>
40 #include <store/store.hxx>
44 #if (defined(WNT) && defined(PROFILE))
55 /*========================================================================
59 *======================================================================*/
60 #define _DEMOSTOR_BUFSIZ 512 /* 4096, 1024, 512 */
61 #define _DEMOSTOR_LOOPS 1000 /* 1000, 2000 */
63 #define _DEMOSTOR_REMOVE 0
64 #define _DEMOSTOR_REBUILD 0
70 OPTION_PAUSE
= 0x0004,
71 OPTION_REBUILD
= 0x0008,
78 OPTION_WRITE
= 0x0200,
79 OPTION_CREAT
= 0x0400,
83 inline sal_Char
ascii_toLowerCase (sal_Char ch
)
85 if ((ch
>= 0x41) && (ch
<= 0x5A))
91 /*========================================================================
95 *======================================================================*/
96 struct OTime
: public TimeValue
104 static OTime
getSystemTime (void)
107 osl_getSystemTime (&tv
);
111 OTime
& operator-= (const OTime
& rPast
)
113 Seconds
-= rPast
.Seconds
;
114 if (Nanosec
< rPast
.Nanosec
)
117 Nanosec
+= 1000000000;
119 Nanosec
-= rPast
.Nanosec
;
123 friend OTime
operator- (const OTime
& rTimeA
, const OTime
& rTimeB
)
125 OTime
aTimeC (rTimeA
);
131 /*========================================================================
133 * DirectoryTraveller.
135 *======================================================================*/
136 typedef store::OStoreDirectory Directory
;
138 class DirectoryTraveller
: public Directory::traveller
140 typedef store::OStoreFile file
;
141 typedef Directory::iterator iter
;
143 store::OStoreFile m_aFile
;
146 sal_uInt32 m_nOptions
;
147 unsigned int m_nLevel
;
148 unsigned int m_nCount
;
153 const OUString
&rPath
,
154 const OUString
&rName
,
156 unsigned int nLevel
= 0);
158 virtual ~DirectoryTraveller (void);
160 virtual sal_Bool
visit (const iter
& it
);
164 * DirectoryTraveller.
166 DirectoryTraveller::DirectoryTraveller (
168 const OUString
&rPath
,
169 const OUString
&rName
,
174 m_nOptions (nOptions
),
179 m_aPath
+= OUString::createFromAscii("/");
183 * ~DirectoryTraveller.
185 DirectoryTraveller::~DirectoryTraveller (void)
192 sal_Bool
DirectoryTraveller::visit (const iter
& it
)
195 if (m_nOptions
& OPTION_DUMP
)
197 rtl::OString
aName (it
.m_pszName
, it
.m_nLength
, RTL_TEXTENCODING_UTF8
);
198 printf ("Visit(%d,%d): %s [0x%08x] %d [Bytes]\n",
200 aName
.pData
->buffer
, (unsigned int)(it
.m_nAttrib
), (unsigned int)(it
.m_nSize
));
202 if (it
.m_nAttrib
& STORE_ATTRIB_ISDIR
)
204 OUString
aName (it
.m_pszName
, it
.m_nLength
);
205 if (aName
.compareToAscii ("XTextViewCursorSupplier") == 0)
211 storeError eErrCode
= aSubDir
.create (
212 m_aFile
, m_aPath
, aName
, store_AccessReadOnly
);
213 if (eErrCode
== store_E_None
)
215 sal_uInt32 nRefCount
= 0;
216 m_aFile
.getRefererCount (nRefCount
);
218 DirectoryTraveller
aSubTraveller (
219 m_aFile
, m_aPath
, aName
, m_nOptions
, m_nLevel
+ 1);
220 aSubDir
.travel (aSubTraveller
);
226 /*========================================================================
230 *======================================================================*/
231 int SAL_CALL
main (int argc
, char **argv
)
233 #if (defined(WNT) && defined(PROFILE))
236 OTime
aMainStartTime (OTime::getSystemTime());
239 store::OStoreFile aFile
;
240 storeError eErrCode
= store_E_None
;
242 sal_uInt32 nOptions
= 0;
243 for (int i
= 1; i
< argc
; i
++)
245 const char *opt
= argv
[i
];
248 switch (ascii_toLowerCase(sal_Char(opt
[1])))
251 nOptions
|= OPTION_FILE
;
255 nOptions
|= OPTION_DUMP
;
258 nOptions
|= OPTION_ITER
;
261 nOptions
|= OPTION_LINK
;
265 nOptions
|= OPTION_READ
;
268 nOptions
|= OPTION_WRITE
;
271 nOptions
|= OPTION_CREAT
;
274 nOptions
|= OPTION_TRUNC
;
278 nOptions
|= OPTION_PAUSE
;
283 nOptions
|= OPTION_HELP
;
289 if (nOptions
& OPTION_FILE
)
292 argv
[i
], rtl_str_getLength(argv
[i
]),
293 osl_getThreadTextEncoding());
294 if ((nOptions
& OPTION_CREAT
) && (nOptions
& OPTION_TRUNC
))
295 eErrCode
= aFile
.create (aName
, store_AccessCreate
);
296 else if (nOptions
& OPTION_CREAT
)
297 eErrCode
= aFile
.create (aName
, store_AccessReadCreate
);
298 else if (nOptions
& OPTION_WRITE
)
299 eErrCode
= aFile
.create (aName
, store_AccessReadWrite
);
301 eErrCode
= aFile
.create (aName
, store_AccessReadOnly
);
302 if (eErrCode
!= store_E_None
)
304 printf ("Error: can't open file: %s\n", argv
[i
]);
311 if ((nOptions
== 0) || (nOptions
& OPTION_HELP
))
313 printf ("Usage:\tt_store "
314 "[[-c] [-t] [-r] [-w]] [[-i] [-d] [-h]] "
317 printf ("\nOptions:\n");
318 printf ("-c\tcreate\n");
319 printf ("-t\ttruncate\n");
320 printf ("-r\tread\n");
321 printf ("-w\twrite\n");
322 printf ("-i\titerate\n");
323 printf ("-d\tdump\n");
324 printf ("-h\thelp\n");
325 printf ("-f\tfilename\n");
327 printf ("\nExamples:");
328 printf ("\nt_store -c -w -f t_store.rdb\n");
329 printf ("\tCreate file 't_store.rdb',\n"
330 "\twrite fixed number (1000) of streams.\n");
331 printf ("\nt_store -c -i -d -f t_store.rdb\n");
332 printf ("\tOpen file 't_store.rdb', "
333 "create '/' directory,\n"
334 "\titerate directory tree, "
335 "dump directory info.\n");
340 if (!aFile
.isValid())
342 eErrCode
= aFile
.createInMemory();
343 if (eErrCode
!= store_E_None
)
345 printf ("Error: can't create memory file\n");
350 // Stream Read/Write.
351 OUString
aPath (RTL_CONSTASCII_USTRINGPARAM("/"));
352 if ((nOptions
& OPTION_READ
) || (nOptions
& OPTION_WRITE
))
355 storeAccessMode eMode
= store_AccessReadOnly
;
356 if (nOptions
& OPTION_WRITE
)
357 eMode
= store_AccessReadWrite
;
358 if (nOptions
& OPTION_CREAT
)
359 eMode
= store_AccessCreate
;
362 char pBuffer
[_DEMOSTOR_BUFSIZ
] = "Hello World";
363 pBuffer
[_DEMOSTOR_BUFSIZ
- 2] = 'B';
364 pBuffer
[_DEMOSTOR_BUFSIZ
- 1] = '\0';
368 OTime
aStartTime (OTime::getSystemTime());
371 for (int i
= 0; i
< _DEMOSTOR_LOOPS
; i
++)
373 OUString
aName (RTL_CONSTASCII_USTRINGPARAM("demostor-"));
374 aName
+= OUString::valueOf ((sal_Int32
)(i
+ 1), 10);
375 aName
+= OUString::createFromAscii (".dat");
377 #if (_DEMOSTOR_REMOVE == 1)
378 eErrCode
= aFile
.remove (aPath
, aName
);
379 if ((eErrCode
!= store_E_None
) &&
380 (eErrCode
!= store_E_NotExists
) )
384 store::OStoreStream aStream
;
385 eErrCode
= aStream
.create (aFile
, aPath
, aName
, eMode
);
386 if (eErrCode
!= store_E_None
)
388 OSL_TRACE("OStoreStream(%d)::create(): error: %d", i
, eErrCode
);
392 if (nOptions
& OPTION_TRUNC
)
394 eErrCode
= aStream
.setSize(0);
395 if (eErrCode
!= store_E_None
)
397 OSL_TRACE("OStoreStream(%d)::setSize(0): error: %d", i
, eErrCode
);
402 sal_uInt32 nDone
= 0;
403 if (nOptions
& OPTION_WRITE
)
405 eErrCode
= aStream
.writeAt (
406 0, pBuffer
, sizeof(pBuffer
), nDone
);
407 if (eErrCode
!= store_E_None
)
409 OSL_TRACE("OStoreStream(%d)::writeAt(): error: %d", i
, eErrCode
);
414 if (nOptions
& OPTION_READ
)
416 sal_uInt32 nOffset
= 0;
419 eErrCode
= aStream
.readAt (
420 nOffset
, pBuffer
, sizeof(pBuffer
), nDone
);
421 if (eErrCode
!= store_E_None
)
423 OSL_TRACE("OStoreStream(%d)::readAt(): error: %d", i
, eErrCode
);
435 if (((i
+ 1) % (_DEMOSTOR_LOOPS
/10)) == 0)
437 OTime
aDelta (OTime::getSystemTime() - aStartTime
);
439 sal_uInt32 nDelta
= aDelta
.Seconds
* 1000000;
440 nDelta
+= (aDelta
.Nanosec
/ 1000);
442 printf ("%d: %12.4g[usec]\n", (i
+1),
443 (double)(nDelta
)/(double)(i
+1));
449 OTime
aDelta (OTime::getSystemTime() - aStartTime
);
451 sal_uInt32 nDelta
= aDelta
.Seconds
* 1000000;
452 nDelta
+= (aDelta
.Nanosec
/ 1000);
454 printf ("Total(rd,wr): %d[usec]\n", (unsigned int)(nDelta
));
459 if (nOptions
& OPTION_LINK
)
461 // Create symlink to (root) directory.
462 eErrCode
= aFile
.symlink (
463 aPath
, OUString::createFromAscii("000000/"),
466 ((eErrCode
== store_E_None
) ||
467 (eErrCode
== store_E_AlreadyExists
) ),
468 "t_store::main(): store_symlink() failed");
470 // Create symlink to file.
471 OUString
aLinkName (RTL_CONSTASCII_USTRINGPARAM("demostor-1.lnk"));
473 eErrCode
= aFile
.symlink (
475 aPath
, OUString::createFromAscii("demostor-1.dat"));
477 ((eErrCode
== store_E_None
) ||
478 (eErrCode
== store_E_AlreadyExists
) ),
479 "t_store::main(): store_symlink() failed");
480 if ((eErrCode
== store_E_None
) ||
481 (eErrCode
== store_E_AlreadyExists
) )
484 RTL_CONSTASCII_USTRINGPARAM("Shortcut to demostor-1.dat"));
485 eErrCode
= aFile
.rename (
489 ((eErrCode
== store_E_None
) ||
490 (eErrCode
== store_E_AlreadyExists
) ),
491 "t_store::main(): store_rename() failed");
495 OUString
aDirName (RTL_CONSTASCII_USTRINGPARAM("demostor-1.dir"));
496 store::OStoreDirectory aDir
;
498 eErrCode
= aDir
.create (
499 aFile
, aPath
, aDirName
, store_AccessReadCreate
);
501 (eErrCode
== store_E_None
),
502 "t_store::main(): store_createDirectory() failed");
503 if (eErrCode
== store_E_None
)
507 eErrCode
= aFile
.rename (
508 aPath
, "demostor-1.dir/",
509 aPath
, "Renamed demostor-1.dir");
511 ((eErrCode
== store_E_None
) ||
512 (eErrCode
== store_E_AlreadyExists
) ),
513 "t_store::main(): store_rename() failed");
515 eErrCode
= aFile
.rename (
516 aPath
, "Renamed demostor-1.dir/",
517 aPath
, "demostor-1.dir");
519 (eErrCode
== store_E_None
),
520 "t_store::main(): store_rename() failed");
525 // Directory iteration.
526 if (nOptions
& OPTION_ITER
)
529 OTime
aStartTime (OTime::getSystemTime());
534 store::OStoreDirectory aRootDir
;
535 if (nOptions
& OPTION_LINK
)
537 // Open symlink entry.
538 eErrCode
= aRootDir
.create (
539 aFile
, aPath
, OUString::createFromAscii("000000"),
540 store_AccessReadOnly
);
544 // Open direct entry.
545 if (nOptions
& OPTION_CREAT
)
546 eErrCode
= aRootDir
.create (
547 aFile
, aEmpty
, aEmpty
, store_AccessReadCreate
);
548 else if (nOptions
& OPTION_WRITE
)
549 eErrCode
= aRootDir
.create (
550 aFile
, aEmpty
, aEmpty
, store_AccessReadWrite
);
552 eErrCode
= aRootDir
.create (
553 aFile
, aEmpty
, aEmpty
, store_AccessReadOnly
);
556 if (eErrCode
== store_E_None
)
558 // Traverse directory tree.
559 DirectoryTraveller
aTraveller (
560 aFile
, aEmpty
, aEmpty
, nOptions
, 0);
561 aRootDir
.travel (aTraveller
);
566 printf ("Error: can't open directory: \"/\"\n");
570 OTime
aDelta (OTime::getSystemTime() - aStartTime
);
572 sal_uInt32 nDelta
= aDelta
.Seconds
* 1000000;
573 nDelta
+= (aDelta
.Nanosec
/ 1000);
575 printf ("Total(iter): %d[usec]\n", (unsigned int)(nDelta
));
579 if (nOptions
& OPTION_PAUSE
)
584 osl_waitThread (&tv
);
588 sal_uInt32 nSize
= 0;
589 aFile
.getSize (nSize
);
594 #if (defined(WNT) && defined(PROFILE))
599 OTime
aDelta (OTime::getSystemTime() - aMainStartTime
);
601 sal_uInt32 nDelta
= aDelta
.Seconds
* 1000000;
602 nDelta
+= (aDelta
.Nanosec
/ 1000);
604 printf ("Total: %d[usec]\n", (unsigned int)(nDelta
));