LCOV - code coverage report
Current view: top level - xmlhelp/source/cxxhelp/provider - db.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 24 24 100.0 %
Date: 2012-08-25 Functions: 12 12 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 5 8 62.5 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*************************************************************************
       3                 :            :  *
       4                 :            :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5                 :            :  *
       6                 :            :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7                 :            :  *
       8                 :            :  * OpenOffice.org - a multi-platform office productivity suite
       9                 :            :  *
      10                 :            :  * This file is part of OpenOffice.org.
      11                 :            :  *
      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.
      15                 :            :  *
      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).
      21                 :            :  *
      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.
      26                 :            :  *
      27                 :            :  ************************************************************************/
      28                 :            : #ifndef BERKELEYDBPROXY_DB_HXX_
      29                 :            : #define BERKELEYDBPROXY_DB_HXX_
      30                 :            : 
      31                 :            : #ifdef SYSTEM_DB_HEADER
      32                 :            : #include SYSTEM_DB_HEADER
      33                 :            : #else
      34                 :            : #include <berkeleydb/db.h>
      35                 :            : #endif
      36                 :            : 
      37                 :            : #include "com/sun/star/ucb/XSimpleFileAccess2.hpp"
      38                 :            : 
      39                 :            : #include <boost/unordered_map.hpp>
      40                 :            : #include <rtl/string.hxx>
      41                 :            : 
      42                 :            : extern "C" {
      43                 :            :   typedef void *(*db_malloc_fcn_type)(size_t);
      44                 :            :   typedef void *(*db_realloc_fcn_type)(void *, size_t);
      45                 :            :   typedef void (*db_free_fcn_type)(void *);
      46                 :            : }
      47                 :            : 
      48                 :            : 
      49                 :            : namespace berkeleydbproxy {
      50                 :            : 
      51                 :            :     class Dbc;
      52                 :            :     class Dbt;
      53                 :            : 
      54                 :            :     namespace db_internal
      55                 :            :     {
      56                 :            :         class Noncopyable
      57                 :            :         {
      58                 :            :             // not implemented
      59                 :            :             Noncopyable(const Noncopyable&);
      60                 :            :             void operator=(const Noncopyable&);
      61                 :            :         protected:
      62                 :         12 :             Noncopyable() {}
      63                 :         12 :             ~Noncopyable() {}
      64                 :            :         };
      65                 :            :     }
      66                 :            : 
      67                 :            :     class DbException
      68                 :            :     {
      69                 :            :         rtl::OString what_;
      70                 :            :     public:
      71                 :            :         explicit DbException(rtl::OString const & whatparam)
      72                 :            :         : what_(whatparam)
      73                 :            :         {}
      74                 :            : 
      75                 :            :         const char *what() const
      76                 :            :         { return what_.getStr(); }
      77                 :            :     };
      78                 :            : 
      79                 :            :     struct eq
      80                 :            :     {
      81                 :       5886 :         bool operator()( const rtl::OString& rKey1, const rtl::OString& rKey2 ) const
      82                 :       5886 :             { return rKey1.compareTo( rKey2 ) == 0; }
      83                 :            :     };
      84                 :            : 
      85                 :            :     struct ha
      86                 :            :     {
      87                 :      54300 :         size_t operator()( const rtl::OString& rName ) const
      88                 :      54300 :             { return rName.hashCode(); }
      89                 :            :     };
      90                 :            : 
      91                 :            : 
      92                 :            :     class DBData
      93                 :            :     {
      94                 :            :         friend class        DBHelp;
      95                 :            : 
      96                 :            :         int                 m_nSize;
      97                 :            :         char*               m_pBuffer;
      98                 :            : 
      99                 :            :         void copyToBuffer( const char* pSrcData, int nSize );
     100                 :            : 
     101                 :            :     public:
     102                 :      54484 :         DBData( void )
     103                 :            :             : m_nSize( 0 )
     104                 :      54484 :             , m_pBuffer( NULL )
     105                 :      54484 :         {}
     106                 :      54484 :         ~DBData()
     107         [ +  + ]:      54484 :             { delete [] m_pBuffer; }
     108                 :            : 
     109                 :        118 :           int getSize() const
     110                 :        118 :             { return m_nSize; }
     111                 :      54120 :           const char* getData() const
     112                 :      54120 :             { return m_pBuffer; }
     113                 :            :     };
     114                 :            : 
     115                 :            :     typedef boost::unordered_map< rtl::OString,std::pair<int,int>,ha,eq >   StringToValPosMap;
     116                 :            :     typedef boost::unordered_map< rtl::OString,rtl::OString,ha,eq >     StringToDataMap;
     117                 :            : 
     118                 :            :     class DBHelp
     119                 :            :     {
     120                 :            :         rtl::OUString       m_aFileURL;
     121                 :            :         StringToDataMap*    m_pStringToDataMap;
     122                 :            :         StringToValPosMap*  m_pStringToValPosMap;
     123                 :            :         com::sun::star::uno::Reference< com::sun::star::ucb::XSimpleFileAccess2 >
     124                 :            :                             m_xSFA;
     125                 :            : 
     126                 :            :         com::sun::star::uno::Sequence< sal_Int8 >
     127                 :            :                             m_aItData;
     128                 :            :         const char*         m_pItData;
     129                 :            :         int                 m_nItRead;
     130                 :            :         int                 m_iItPos;
     131                 :            : 
     132                 :            :         bool implReadLenAndData( const char* pData, int& riPos, DBData& rValue );
     133                 :            : 
     134                 :            :     public:
     135                 :            :         //DBHelp must get a fileURL which can then directly be used by simple file access.
     136                 :            :         //SimpleFileAccess requires file URLs as arguments. Passing file path may work but fails
     137                 :            :         //for example when using long file paths on Windows, which start with "\\?\"
     138                 :         12 :         DBHelp( const rtl::OUString& rFileURL,
     139                 :            :             com::sun::star::uno::Reference< com::sun::star::ucb::XSimpleFileAccess2 > xSFA )
     140                 :            :                 : m_aFileURL( rFileURL )
     141                 :            :                 , m_pStringToDataMap( NULL )
     142                 :            :                 , m_pStringToValPosMap( NULL )
     143                 :            :                 , m_xSFA( xSFA )
     144                 :            :                 , m_pItData( NULL )
     145                 :            :                 , m_nItRead( -1 )
     146         [ +  - ]:         12 :                 , m_iItPos( -1 )
     147                 :            :         {
     148                 :            :             OSL_ASSERT(!rFileURL.compareTo(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("file:")), 5));
     149                 :         12 :         }
     150                 :         12 :         ~DBHelp()
     151 [ +  - ][ +  - ]:         12 :             { releaseHashMap(); }
     152                 :            : 
     153                 :            :         void createHashMap( bool bOptimizeForPerformance = false );
     154                 :            :         void releaseHashMap( void );
     155                 :            : 
     156                 :            :         bool getValueForKey( const rtl::OString& rKey, DBData& rValue );
     157                 :            : 
     158                 :            :         bool startIteration( void );
     159                 :            :         bool getNextKeyAndValue( DBData& rKey, DBData& rValue );
     160                 :            :         void stopIteration( void );
     161                 :            :     };
     162                 :            : 
     163                 :            :     class Db : db_internal::Noncopyable
     164                 :            :     {
     165                 :            :     private:
     166                 :            :         DB* m_pDBP;
     167                 :            :         DBHelp* m_pDBHelp;
     168                 :            : 
     169                 :            :     public:
     170                 :            :         Db();
     171                 :            :         ~Db();
     172                 :            : 
     173                 :         12 :         void setDBHelp( DBHelp* pDBHelp )
     174                 :         12 :             { m_pDBHelp = pDBHelp; }
     175                 :        404 :         DBHelp* getDBHelp( void )
     176                 :        404 :             { return m_pDBHelp; }
     177                 :            : 
     178                 :            :         int close(u_int32_t flags);
     179                 :            : 
     180                 :            :         int open(DB_TXN *txnid,
     181                 :            :                  const char *file,
     182                 :            :                  const char *database,
     183                 :            :                  DBTYPE type,
     184                 :            :                  u_int32_t flags,
     185                 :            :                  int mode);
     186                 :            : 
     187                 :            :         int open(DB_TXN *txnid,
     188                 :            :                  ::rtl::OUString const & fileURL,
     189                 :            :                  DBTYPE type,
     190                 :            :                  u_int32_t flags,
     191                 :            :                  int mode);
     192                 :            : 
     193                 :            : 
     194                 :            :         int get(DB_TXN* txnid, Dbt *key, Dbt *data, u_int32_t flags);
     195                 :            : 
     196                 :            :         int cursor(DB_TXN *txnid, Dbc **cursorp, u_int32_t flags);
     197                 :            :     };
     198                 :            : 
     199                 :            :     class Dbc : db_internal::Noncopyable
     200                 :            :     {
     201                 :            :         friend class Db;
     202                 :            :         friend class Dbt;
     203                 :            : 
     204                 :            :     private:
     205                 :            :         DBC* m_pDBC;
     206                 :            : 
     207                 :            :         explicit Dbc(DBC* pDBC);
     208                 :            :         ~Dbc();
     209                 :            : 
     210                 :            :     public:
     211                 :            :         int close();
     212                 :            : 
     213                 :            :         int get(Dbt *key, Dbt *data, u_int32_t flags);
     214                 :            :     };
     215                 :            : 
     216                 :            :     class Dbt: private DBT
     217                 :            :     {
     218                 :            :         friend class Db;
     219                 :            :         friend class Dbc;
     220                 :            : 
     221                 :            :     public:
     222                 :            :         Dbt(void *data_arg, u_int32_t size_arg);
     223                 :            : 
     224                 :            :         Dbt();
     225                 :            : 
     226                 :            :         ~Dbt();
     227                 :            : 
     228                 :            :           void *get_data() const;
     229                 :            :         void set_data(void *value);
     230                 :            : 
     231                 :            :           u_int32_t get_size() const;
     232                 :            :         void set_size(u_int32_t value);
     233                 :            : 
     234                 :            :         void set_flags(u_int32_t);
     235                 :            :     };
     236                 :            : }
     237                 :            : 
     238                 :            : #endif
     239                 :            : 
     240                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10