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 ************************************************************************/
31 #include <sal/types.h>
32 #include <osl/diagnose.h>
33 #include <osl/thread.h>
35 #include <rtl/ustring.hxx>
36 #include <store/store.hxx>
40 #if (defined(WNT) && defined(PROFILE))
51 /*========================================================================
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
66 OPTION_PAUSE
= 0x0004,
67 OPTION_REBUILD
= 0x0008,
74 OPTION_WRITE
= 0x0200,
75 OPTION_CREAT
= 0x0400,
79 inline sal_Char
ascii_toLowerCase (sal_Char ch
)
81 if ((ch
>= 0x41) && (ch
<= 0x5A))
87 /*========================================================================
91 *======================================================================*/
92 struct OTime
: public TimeValue
100 static OTime
getSystemTime (void)
103 osl_getSystemTime (&tv
);
107 OTime
& operator-= (const OTime
& rPast
)
109 Seconds
-= rPast
.Seconds
;
110 if (Nanosec
< rPast
.Nanosec
)
113 Nanosec
+= 1000000000;
115 Nanosec
-= rPast
.Nanosec
;
119 friend OTime
operator- (const OTime
& rTimeA
, const OTime
& rTimeB
)
121 OTime
aTimeC (rTimeA
);
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
;
142 sal_uInt32 m_nOptions
;
143 unsigned int m_nLevel
;
144 unsigned int m_nCount
;
149 const OUString
&rPath
,
150 const OUString
&rName
,
152 unsigned int nLevel
= 0);
154 virtual ~DirectoryTraveller (void);
156 virtual sal_Bool
visit (const iter
& it
);
160 * DirectoryTraveller.
162 DirectoryTraveller::DirectoryTraveller (
164 const OUString
&rPath
,
165 const OUString
&rName
,
170 m_nOptions (nOptions
),
175 m_aPath
+= OUString("/");
179 * ~DirectoryTraveller.
181 DirectoryTraveller::~DirectoryTraveller (void)
188 sal_Bool
DirectoryTraveller::visit (const iter
& it
)
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",
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)
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
);
222 /*========================================================================
226 *======================================================================*/
227 int SAL_CALL
main (int argc
, char **argv
)
229 #if (defined(WNT) && defined(PROFILE))
232 OTime
aMainStartTime (OTime::getSystemTime());
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
];
244 switch (ascii_toLowerCase(sal_Char(opt
[1])))
247 nOptions
|= OPTION_FILE
;
251 nOptions
|= OPTION_DUMP
;
254 nOptions
|= OPTION_ITER
;
257 nOptions
|= OPTION_LINK
;
261 nOptions
|= OPTION_READ
;
264 nOptions
|= OPTION_WRITE
;
267 nOptions
|= OPTION_CREAT
;
270 nOptions
|= OPTION_TRUNC
;
274 nOptions
|= OPTION_PAUSE
;
279 nOptions
|= OPTION_HELP
;
285 if (nOptions
& OPTION_FILE
)
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
);
297 eErrCode
= aFile
.create (aName
, store_AccessReadOnly
);
298 if (eErrCode
!= store_E_None
)
300 printf ("Error: can't open file: %s\n", argv
[i
]);
307 if ((nOptions
== 0) || (nOptions
& OPTION_HELP
))
309 printf ("Usage:\tt_store "
310 "[[-c] [-t] [-r] [-w]] [[-i] [-d] [-h]] "
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");
336 if (!aFile
.isValid())
338 eErrCode
= aFile
.createInMemory();
339 if (eErrCode
!= store_E_None
)
341 printf ("Error: can't create memory file\n");
346 // Stream Read/Write.
347 OUString
aPath ("/");
348 if ((nOptions
& OPTION_READ
) || (nOptions
& OPTION_WRITE
))
351 storeAccessMode eMode
= store_AccessReadOnly
;
352 if (nOptions
& OPTION_WRITE
)
353 eMode
= store_AccessReadWrite
;
354 if (nOptions
& OPTION_CREAT
)
355 eMode
= store_AccessCreate
;
358 char pBuffer
[_DEMOSTOR_BUFSIZ
] = "Hello World";
359 pBuffer
[_DEMOSTOR_BUFSIZ
- 2] = 'B';
360 pBuffer
[_DEMOSTOR_BUFSIZ
- 1] = '\0';
364 OTime
aStartTime (OTime::getSystemTime());
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
) )
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
);
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
);
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
);
410 if (nOptions
& OPTION_READ
)
412 sal_uInt32 nOffset
= 0;
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
);
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));
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
));
455 if (nOptions
& OPTION_LINK
)
457 // Create symlink to (root) directory.
458 eErrCode
= aFile
.symlink (
459 aPath
, OUString("000000/"),
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 (
471 aPath
, OUString("demostor-1.dat"));
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
) )
480 "Shortcut to demostor-1.dat");
481 eErrCode
= aFile
.rename (
485 ((eErrCode
== store_E_None
) ||
486 (eErrCode
== store_E_AlreadyExists
) ),
487 "t_store::main(): store_rename() failed");
491 OUString
aDirName ("demostor-1.dir");
492 store::OStoreDirectory aDir
;
494 eErrCode
= aDir
.create (
495 aFile
, aPath
, aDirName
, store_AccessReadCreate
);
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
)
508 OTime
aStartTime (OTime::getSystemTime());
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
);
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
);
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
);
545 printf ("Error: can't open directory: \"/\"\n");
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
));
558 if (nOptions
& OPTION_PAUSE
)
563 osl_waitThread (&tv
);
567 sal_uInt32 nSize
= 0;
568 aFile
.getSize (nSize
);
573 #if (defined(WNT) && defined(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
));
589 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */