1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include <sal/types.h>
23 #include <osl/diagnose.h>
24 #include <osl/thread.h>
26 #include <rtl/ustring.hxx>
27 #include <store/store.hxx>
31 #if (defined(WNT) && defined(PROFILE))
41 /*========================================================================
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
56 OPTION_PAUSE
= 0x0004,
57 OPTION_REBUILD
= 0x0008,
64 OPTION_WRITE
= 0x0200,
65 OPTION_CREAT
= 0x0400,
69 inline sal_Char
ascii_toLowerCase (sal_Char ch
)
71 if ((ch
>= 0x41) && (ch
<= 0x5A))
77 /*========================================================================
81 *======================================================================*/
82 struct OTime
: public TimeValue
90 static OTime
getSystemTime (void)
93 osl_getSystemTime (&tv
);
97 OTime
& operator-= (const OTime
& rPast
)
99 Seconds
-= rPast
.Seconds
;
100 if (Nanosec
< rPast
.Nanosec
)
103 Nanosec
+= 1000000000;
105 Nanosec
-= rPast
.Nanosec
;
109 friend OTime
operator- (const OTime
& rTimeA
, const OTime
& rTimeB
)
111 OTime
aTimeC (rTimeA
);
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
;
132 sal_uInt32 m_nOptions
;
133 unsigned int m_nLevel
;
134 unsigned int m_nCount
;
139 const OUString
&rPath
,
140 const OUString
&rName
,
142 unsigned int nLevel
= 0);
144 virtual ~DirectoryTraveller (void);
146 virtual sal_Bool
visit (const iter
& it
);
150 * DirectoryTraveller.
152 DirectoryTraveller::DirectoryTraveller (
154 const OUString
&rPath
,
155 const OUString
&rName
,
160 m_nOptions (nOptions
),
164 m_aPath
+= rName
+ "/";
168 * ~DirectoryTraveller.
170 DirectoryTraveller::~DirectoryTraveller (void)
177 sal_Bool
DirectoryTraveller::visit (const iter
& it
)
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",
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)
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
);
211 /*========================================================================
215 *======================================================================*/
216 int SAL_CALL
main (int argc
, char **argv
)
218 #if (defined(WNT) && defined(PROFILE))
221 OTime
aMainStartTime (OTime::getSystemTime());
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
];
233 switch (ascii_toLowerCase(sal_Char(opt
[1])))
236 nOptions
|= OPTION_FILE
;
240 nOptions
|= OPTION_DUMP
;
243 nOptions
|= OPTION_ITER
;
246 nOptions
|= OPTION_LINK
;
250 nOptions
|= OPTION_READ
;
253 nOptions
|= OPTION_WRITE
;
256 nOptions
|= OPTION_CREAT
;
259 nOptions
|= OPTION_TRUNC
;
263 nOptions
|= OPTION_PAUSE
;
268 nOptions
|= OPTION_HELP
;
274 if (nOptions
& OPTION_FILE
)
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
);
286 eErrCode
= aFile
.create (aName
, store_AccessReadOnly
);
287 if (eErrCode
!= store_E_None
)
289 printf ("Error: can't open file: %s\n", argv
[i
]);
296 if ((nOptions
== 0) || (nOptions
& OPTION_HELP
))
298 printf ("Usage:\tt_store "
299 "[[-c] [-t] [-r] [-w]] [[-i] [-d] [-h]] "
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");
325 if (!aFile
.isValid())
327 eErrCode
= aFile
.createInMemory();
328 if (eErrCode
!= store_E_None
)
330 printf ("Error: can't create memory file\n");
335 // Stream Read/Write.
336 OUString
aPath ("/");
337 if ((nOptions
& OPTION_READ
) || (nOptions
& OPTION_WRITE
))
340 storeAccessMode eMode
= store_AccessReadOnly
;
341 if (nOptions
& OPTION_WRITE
)
342 eMode
= store_AccessReadWrite
;
343 if (nOptions
& OPTION_CREAT
)
344 eMode
= store_AccessCreate
;
347 char pBuffer
[_DEMOSTOR_BUFSIZ
] = "Hello World";
348 pBuffer
[_DEMOSTOR_BUFSIZ
- 2] = 'B';
349 pBuffer
[_DEMOSTOR_BUFSIZ
- 1] = '\0';
353 OTime
aStartTime (OTime::getSystemTime());
356 for (int i
= 0; i
< _DEMOSTOR_LOOPS
; i
++)
358 OUString
aName ("demostor-");
359 aName
+= OUString::valueOf ((sal_Int32
)(i
+ 1), 10);
362 #if (_DEMOSTOR_REMOVE == 1)
363 eErrCode
= aFile
.remove (aPath
, aName
);
364 if ((eErrCode
!= store_E_None
) &&
365 (eErrCode
!= store_E_NotExists
) )
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
);
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
);
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
);
399 if (nOptions
& OPTION_READ
)
401 sal_uInt32 nOffset
= 0;
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
);
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));
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
));
444 if (nOptions
& OPTION_LINK
)
446 // Create symlink to (root) directory.
447 eErrCode
= aFile
.symlink (
448 aPath
, OUString("000000/"),
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 (
460 aPath
, OUString("demostor-1.dat"));
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
) )
469 "Shortcut to demostor-1.dat");
470 eErrCode
= aFile
.rename (
474 ((eErrCode
== store_E_None
) ||
475 (eErrCode
== store_E_AlreadyExists
) ),
476 "t_store::main(): store_rename() failed");
480 OUString
aDirName ("demostor-1.dir");
481 store::OStoreDirectory aDir
;
483 eErrCode
= aDir
.create (
484 aFile
, aPath
, aDirName
, store_AccessReadCreate
);
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
)
497 OTime
aStartTime (OTime::getSystemTime());
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
);
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
);
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
);
534 printf ("Error: can't open directory: \"/\"\n");
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
));
547 if (nOptions
& OPTION_PAUSE
)
552 osl_waitThread (&tv
);
556 sal_uInt32 nSize
= 0;
557 aFile
.getSize (nSize
);
562 #if (defined(WNT) && defined(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
));
578 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */