1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_xmlhelp.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include "db.hxx" 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir #include <rtl/alloc.h> 34*cdf0e10cSrcweir #include <cstring> 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir #include "com/sun/star/io/XSeekable.hpp" 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir #include "osl/file.hxx" 39*cdf0e10cSrcweir #include "osl/thread.hxx" 40*cdf0e10cSrcweir #ifdef TEST_DBHELP 41*cdf0e10cSrcweir #include <osl/time.h> 42*cdf0e10cSrcweir #endif 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir using namespace com::sun::star; 45*cdf0e10cSrcweir using namespace com::sun::star::uno; 46*cdf0e10cSrcweir using namespace com::sun::star::io; 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir namespace berkeleydbproxy { 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir //---------------------------------------------------------------------------- 51*cdf0e10cSrcweir namespace db_internal 52*cdf0e10cSrcweir { 53*cdf0e10cSrcweir // static void raise_error(int dberr, const char * where); 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir static inline int check_error(int dberr, const char * where) 56*cdf0e10cSrcweir { 57*cdf0e10cSrcweir (void)where; 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir // if (dberr) raise_error(dberr,where); 60*cdf0e10cSrcweir return dberr; 61*cdf0e10cSrcweir } 62*cdf0e10cSrcweir } 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir void DBData::copyToBuffer( const char* pSrcData, int nSize ) 65*cdf0e10cSrcweir { 66*cdf0e10cSrcweir m_nSize = nSize; 67*cdf0e10cSrcweir delete [] m_pBuffer; 68*cdf0e10cSrcweir m_pBuffer = new char[m_nSize+1]; 69*cdf0e10cSrcweir memcpy( m_pBuffer, pSrcData, m_nSize ); 70*cdf0e10cSrcweir m_pBuffer[m_nSize] = 0; 71*cdf0e10cSrcweir } 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir // DBHelp 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir bool DBHelp::implReadLenAndData( const char* pData, int& riPos, DBData& rValue ) 77*cdf0e10cSrcweir { 78*cdf0e10cSrcweir bool bSuccess = false; 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir // Read key len 81*cdf0e10cSrcweir const char* pStartPtr = pData + riPos; 82*cdf0e10cSrcweir char* pEndPtr; 83*cdf0e10cSrcweir sal_Int32 nKeyLen = strtol( pStartPtr, &pEndPtr, 16 ); 84*cdf0e10cSrcweir if( pEndPtr == pStartPtr ) 85*cdf0e10cSrcweir return bSuccess; 86*cdf0e10cSrcweir riPos += (pEndPtr - pStartPtr) + 1; 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir const char* pKeySrc = pData + riPos; 89*cdf0e10cSrcweir rValue.copyToBuffer( pKeySrc, nKeyLen ); 90*cdf0e10cSrcweir riPos += nKeyLen + 1; 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir bSuccess = true; 93*cdf0e10cSrcweir return bSuccess; 94*cdf0e10cSrcweir } 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir #ifdef TEST_DBHELP 97*cdf0e10cSrcweir 98*cdf0e10cSrcweir typedef std::pair< rtl::OString, rtl::OString > KeyValPair; 99*cdf0e10cSrcweir typedef std::vector< KeyValPair > KeyValPairVector; 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir void testWriteKeyValue( FILE* pFile, const KeyValPair& rKeyValPair ) 102*cdf0e10cSrcweir { 103*cdf0e10cSrcweir if( pFile == NULL ) 104*cdf0e10cSrcweir return; 105*cdf0e10cSrcweir char cLF = 10; 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir const rtl::OString& aKeyStr = rKeyValPair.first; 108*cdf0e10cSrcweir const rtl::OString& aValueStr = rKeyValPair.second; 109*cdf0e10cSrcweir int nKeyLen = aKeyStr.getLength(); 110*cdf0e10cSrcweir int nValueLen = aValueStr.getLength(); 111*cdf0e10cSrcweir fprintf( pFile, "%x ", nKeyLen ); 112*cdf0e10cSrcweir if( nKeyLen > 0 ) 113*cdf0e10cSrcweir fwrite( aKeyStr.getStr(), 1, nKeyLen, pFile ); 114*cdf0e10cSrcweir fprintf( pFile, " %x ", nValueLen ); 115*cdf0e10cSrcweir if( nValueLen > 0 ) 116*cdf0e10cSrcweir fwrite( aValueStr.getStr(), 1, nValueLen, pFile ); 117*cdf0e10cSrcweir fprintf( pFile, "%c", cLF ); 118*cdf0e10cSrcweir } 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir bool DBHelp::testAgainstDb( const rtl::OUString& fileURL, bool bOldDbAccess ) 121*cdf0e10cSrcweir { 122*cdf0e10cSrcweir bool bSuccess = true; 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir KeyValPairVector avKeyValPair; 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir rtl::OUString aOutFileName = fileURL; 127*cdf0e10cSrcweir aOutFileName += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("_TestOut")); 128*cdf0e10cSrcweir if( bOldDbAccess ) 129*cdf0e10cSrcweir aOutFileName += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("_Old")); 130*cdf0e10cSrcweir #ifdef WNT 131*cdf0e10cSrcweir FILE* pFile = _wfopen( aOutFileName.getStr(), L"wb" ); 132*cdf0e10cSrcweir #else 133*cdf0e10cSrcweir rtl::OString sFile = rtl::OUStringToOString(aOutFileName, osl_getThreadTextEncoding()); 134*cdf0e10cSrcweir FILE* pFile = fopen( sFile.getStr(), "wb" ); 135*cdf0e10cSrcweir #endif 136*cdf0e10cSrcweir // Get all values 137*cdf0e10cSrcweir Db table; 138*cdf0e10cSrcweir if( 0 == table.open( 0,fileURL,DB_BTREE,DB_RDONLY,0644 ) ) 139*cdf0e10cSrcweir { 140*cdf0e10cSrcweir bool first = true; 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir Dbc* cursor = 0; 143*cdf0e10cSrcweir table.cursor( 0,&cursor,0 ); 144*cdf0e10cSrcweir Dbt key_,data; 145*cdf0e10cSrcweir key_.set_flags( DB_DBT_MALLOC ); // Initially the cursor must allocate the necessary memory 146*cdf0e10cSrcweir data.set_flags( DB_DBT_MALLOC ); 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir while( cursor && DB_NOTFOUND != cursor->get( &key_,&data,DB_NEXT ) ) 149*cdf0e10cSrcweir { 150*cdf0e10cSrcweir rtl::OString keyword( static_cast<sal_Char*>(key_.get_data()), 151*cdf0e10cSrcweir key_.get_size() ); 152*cdf0e10cSrcweir rtl::OString value( static_cast<sal_Char*>(data.get_data()), 153*cdf0e10cSrcweir data.get_size() ); 154*cdf0e10cSrcweir 155*cdf0e10cSrcweir KeyValPair aPair( keyword, value ); 156*cdf0e10cSrcweir avKeyValPair.push_back( aPair ); 157*cdf0e10cSrcweir if( pFile != NULL ) 158*cdf0e10cSrcweir testWriteKeyValue( pFile, aPair ); 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir if( first ) 161*cdf0e10cSrcweir { 162*cdf0e10cSrcweir key_.set_flags( DB_DBT_REALLOC ); 163*cdf0e10cSrcweir data.set_flags( DB_DBT_REALLOC ); 164*cdf0e10cSrcweir first = false; 165*cdf0e10cSrcweir } 166*cdf0e10cSrcweir } 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir if( cursor ) cursor->close(); 169*cdf0e10cSrcweir } 170*cdf0e10cSrcweir table.close( 0 ); 171*cdf0e10cSrcweir 172*cdf0e10cSrcweir // TEST 173*cdf0e10cSrcweir DBData aDBData; 174*cdf0e10cSrcweir Db tableTest; 175*cdf0e10cSrcweir Dbt data; 176*cdf0e10cSrcweir 177*cdf0e10cSrcweir int nOkCount = 0; 178*cdf0e10cSrcweir int nErrCount = 0; 179*cdf0e10cSrcweir 180*cdf0e10cSrcweir bool bTestSuccess; 181*cdf0e10cSrcweir const char* pTestReadData = NULL; 182*cdf0e10cSrcweir int nTestReadDataSize = 0; 183*cdf0e10cSrcweir 184*cdf0e10cSrcweir sal_uInt32 starttime = osl_getGlobalTimer(); 185*cdf0e10cSrcweir sal_uInt32 afterfirsttime = starttime; 186*cdf0e10cSrcweir 187*cdf0e10cSrcweir if( pFile != NULL ) 188*cdf0e10cSrcweir { 189*cdf0e10cSrcweir if( bOldDbAccess ) 190*cdf0e10cSrcweir fprintf( pFile, "\nTesting old access:\n" ); 191*cdf0e10cSrcweir else 192*cdf0e10cSrcweir fprintf( pFile, "\nTesting new access:\n" ); 193*cdf0e10cSrcweir } 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir KeyValPairVector::const_iterator it; 196*cdf0e10cSrcweir bool bFirst = true; 197*cdf0e10cSrcweir for( it = avKeyValPair.begin() ; it != avKeyValPair.end() ; ++it ) 198*cdf0e10cSrcweir { 199*cdf0e10cSrcweir const KeyValPair& rKeyValPair = *it; 200*cdf0e10cSrcweir 201*cdf0e10cSrcweir const rtl::OString& aKeyStr = rKeyValPair.first; 202*cdf0e10cSrcweir const rtl::OString& aValueStr = rKeyValPair.second; 203*cdf0e10cSrcweir int nKeyLen = aKeyStr.getLength(); 204*cdf0e10cSrcweir int nValueLen = aValueStr.getLength(); 205*cdf0e10cSrcweir 206*cdf0e10cSrcweir const sal_Char* ptr = aValueStr.getStr(); 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir bTestSuccess = false; 209*cdf0e10cSrcweir pTestReadData = NULL; 210*cdf0e10cSrcweir nTestReadDataSize = 0; 211*cdf0e10cSrcweir if( bOldDbAccess ) 212*cdf0e10cSrcweir { 213*cdf0e10cSrcweir if( bFirst ) 214*cdf0e10cSrcweir { 215*cdf0e10cSrcweir if( tableTest.open( 0,fileURL, DB_BTREE,DB_RDONLY,0644 ) ) 216*cdf0e10cSrcweir { 217*cdf0e10cSrcweir if( pFile != NULL ) 218*cdf0e10cSrcweir fprintf( pFile, "Cannot open database\n" ); 219*cdf0e10cSrcweir 220*cdf0e10cSrcweir break; 221*cdf0e10cSrcweir } 222*cdf0e10cSrcweir } 223*cdf0e10cSrcweir 224*cdf0e10cSrcweir Dbt key( static_cast< void* >( const_cast< sal_Char* >( aKeyStr.getStr() ) ), aKeyStr.getLength() ); 225*cdf0e10cSrcweir int err = tableTest.get( 0, &key, &data, 0 ); 226*cdf0e10cSrcweir if( err == 0 ) 227*cdf0e10cSrcweir { 228*cdf0e10cSrcweir bTestSuccess = true; 229*cdf0e10cSrcweir pTestReadData = static_cast< sal_Char* >( data.get_data() ); 230*cdf0e10cSrcweir nTestReadDataSize = data.get_size(); 231*cdf0e10cSrcweir } 232*cdf0e10cSrcweir } 233*cdf0e10cSrcweir else 234*cdf0e10cSrcweir { 235*cdf0e10cSrcweir bTestSuccess = getValueForKey( aKeyStr, aDBData ); 236*cdf0e10cSrcweir if( bTestSuccess ) 237*cdf0e10cSrcweir { 238*cdf0e10cSrcweir pTestReadData = aDBData.getData(); 239*cdf0e10cSrcweir nTestReadDataSize = aDBData.getSize(); 240*cdf0e10cSrcweir } 241*cdf0e10cSrcweir } 242*cdf0e10cSrcweir if( bFirst ) 243*cdf0e10cSrcweir { 244*cdf0e10cSrcweir afterfirsttime = osl_getGlobalTimer(); 245*cdf0e10cSrcweir bFirst = false; 246*cdf0e10cSrcweir } 247*cdf0e10cSrcweir int nError = 0; 248*cdf0e10cSrcweir if( bTestSuccess && pTestReadData != NULL ) 249*cdf0e10cSrcweir { 250*cdf0e10cSrcweir int nCmp = memcmp( ptr, pTestReadData, nValueLen ); 251*cdf0e10cSrcweir if( nCmp == 0 ) 252*cdf0e10cSrcweir ++nOkCount; 253*cdf0e10cSrcweir else 254*cdf0e10cSrcweir nError = 1; 255*cdf0e10cSrcweir 256*cdf0e10cSrcweir if( nValueLen != nTestReadDataSize ) 257*cdf0e10cSrcweir nError = 2; 258*cdf0e10cSrcweir } 259*cdf0e10cSrcweir else 260*cdf0e10cSrcweir nError = 3; 261*cdf0e10cSrcweir 262*cdf0e10cSrcweir if( nError != 0 ) 263*cdf0e10cSrcweir { 264*cdf0e10cSrcweir bSuccess = false; 265*cdf0e10cSrcweir ++nErrCount; 266*cdf0e10cSrcweir 267*cdf0e10cSrcweir if( pFile != NULL ) 268*cdf0e10cSrcweir { 269*cdf0e10cSrcweir fprintf( pFile, "ERROR, not found:\n" ); 270*cdf0e10cSrcweir testWriteKeyValue( pFile, rKeyValPair ); 271*cdf0e10cSrcweir fprintf( pFile, "\nError Code: %d\n", nError ); 272*cdf0e10cSrcweir } 273*cdf0e10cSrcweir } 274*cdf0e10cSrcweir } 275*cdf0e10cSrcweir tableTest.close( 0 ); 276*cdf0e10cSrcweir 277*cdf0e10cSrcweir sal_uInt32 endtime = osl_getGlobalTimer(); 278*cdf0e10cSrcweir double dDiffTime = (endtime-starttime) / 1000.0; 279*cdf0e10cSrcweir double dDiffFirstTime = (afterfirsttime-starttime) / 1000.0; 280*cdf0e10cSrcweir if( pFile != NULL ) 281*cdf0e10cSrcweir { 282*cdf0e10cSrcweir int nCount = avKeyValPair.size(); 283*cdf0e10cSrcweir fprintf( pFile, "%d key/values in total, read %d correctly, %d errors\n", 284*cdf0e10cSrcweir nCount, nOkCount, nErrCount ); 285*cdf0e10cSrcweir fprintf( pFile, "Time taken: %g s (First access %g s)\n", dDiffTime, dDiffFirstTime ); 286*cdf0e10cSrcweir fprintf( pFile, "Average time per access: %g s\n", dDiffTime / nCount ); 287*cdf0e10cSrcweir } 288*cdf0e10cSrcweir 289*cdf0e10cSrcweir if( pFile != NULL ) 290*cdf0e10cSrcweir fclose( pFile ); 291*cdf0e10cSrcweir 292*cdf0e10cSrcweir return bSuccess; 293*cdf0e10cSrcweir } 294*cdf0e10cSrcweir 295*cdf0e10cSrcweir #endif 296*cdf0e10cSrcweir 297*cdf0e10cSrcweir 298*cdf0e10cSrcweir void DBHelp::createHashMap( bool bOptimizeForPerformance ) 299*cdf0e10cSrcweir { 300*cdf0e10cSrcweir releaseHashMap(); 301*cdf0e10cSrcweir if( bOptimizeForPerformance ) 302*cdf0e10cSrcweir { 303*cdf0e10cSrcweir if( m_pStringToDataMap != NULL ) 304*cdf0e10cSrcweir return; 305*cdf0e10cSrcweir m_pStringToDataMap = new StringToDataMap(); 306*cdf0e10cSrcweir } 307*cdf0e10cSrcweir else 308*cdf0e10cSrcweir { 309*cdf0e10cSrcweir if( m_pStringToValPosMap != NULL ) 310*cdf0e10cSrcweir return; 311*cdf0e10cSrcweir m_pStringToValPosMap = new StringToValPosMap(); 312*cdf0e10cSrcweir } 313*cdf0e10cSrcweir 314*cdf0e10cSrcweir Reference< XInputStream > xIn = m_xSFA->openFileRead( m_aFileURL ); 315*cdf0e10cSrcweir if( xIn.is() ) 316*cdf0e10cSrcweir { 317*cdf0e10cSrcweir Sequence< sal_Int8 > aData; 318*cdf0e10cSrcweir sal_Int32 nSize = m_xSFA->getSize( m_aFileURL ); 319*cdf0e10cSrcweir sal_Int32 nRead = xIn->readBytes( aData, nSize ); 320*cdf0e10cSrcweir 321*cdf0e10cSrcweir const char* pData = (const char*)aData.getConstArray(); 322*cdf0e10cSrcweir int iPos = 0; 323*cdf0e10cSrcweir while( iPos < nRead ) 324*cdf0e10cSrcweir { 325*cdf0e10cSrcweir DBData aDBKey; 326*cdf0e10cSrcweir if( !implReadLenAndData( pData, iPos, aDBKey ) ) 327*cdf0e10cSrcweir break; 328*cdf0e10cSrcweir 329*cdf0e10cSrcweir rtl::OString aOKeyStr = aDBKey.getData(); 330*cdf0e10cSrcweir 331*cdf0e10cSrcweir // Read val len 332*cdf0e10cSrcweir const char* pStartPtr = pData + iPos; 333*cdf0e10cSrcweir char* pEndPtr; 334*cdf0e10cSrcweir sal_Int32 nValLen = strtol( pStartPtr, &pEndPtr, 16 ); 335*cdf0e10cSrcweir if( pEndPtr == pStartPtr ) 336*cdf0e10cSrcweir break; 337*cdf0e10cSrcweir 338*cdf0e10cSrcweir iPos += (pEndPtr - pStartPtr) + 1; 339*cdf0e10cSrcweir 340*cdf0e10cSrcweir if( bOptimizeForPerformance ) 341*cdf0e10cSrcweir { 342*cdf0e10cSrcweir const char* pValSrc = pData + iPos; 343*cdf0e10cSrcweir rtl::OString aValStr( pValSrc, nValLen ); 344*cdf0e10cSrcweir (*m_pStringToDataMap)[aOKeyStr] = aValStr; 345*cdf0e10cSrcweir } 346*cdf0e10cSrcweir else 347*cdf0e10cSrcweir { 348*cdf0e10cSrcweir // store value start position 349*cdf0e10cSrcweir (*m_pStringToValPosMap)[aOKeyStr] = std::pair<int,int>( iPos, nValLen ); 350*cdf0e10cSrcweir } 351*cdf0e10cSrcweir iPos += nValLen + 1; 352*cdf0e10cSrcweir } 353*cdf0e10cSrcweir 354*cdf0e10cSrcweir xIn->closeInput(); 355*cdf0e10cSrcweir } 356*cdf0e10cSrcweir } 357*cdf0e10cSrcweir 358*cdf0e10cSrcweir void DBHelp::releaseHashMap( void ) 359*cdf0e10cSrcweir { 360*cdf0e10cSrcweir if( m_pStringToDataMap != NULL ) 361*cdf0e10cSrcweir { 362*cdf0e10cSrcweir delete m_pStringToDataMap; 363*cdf0e10cSrcweir m_pStringToDataMap = NULL; 364*cdf0e10cSrcweir } 365*cdf0e10cSrcweir if( m_pStringToValPosMap != NULL ) 366*cdf0e10cSrcweir { 367*cdf0e10cSrcweir delete m_pStringToValPosMap; 368*cdf0e10cSrcweir m_pStringToValPosMap = NULL; 369*cdf0e10cSrcweir } 370*cdf0e10cSrcweir } 371*cdf0e10cSrcweir 372*cdf0e10cSrcweir 373*cdf0e10cSrcweir bool DBHelp::getValueForKey( const rtl::OString& rKey, DBData& rValue ) 374*cdf0e10cSrcweir { 375*cdf0e10cSrcweir bool bSuccess = false; 376*cdf0e10cSrcweir if( !m_xSFA.is() ) 377*cdf0e10cSrcweir return bSuccess; 378*cdf0e10cSrcweir 379*cdf0e10cSrcweir try 380*cdf0e10cSrcweir { 381*cdf0e10cSrcweir 382*cdf0e10cSrcweir if( m_pStringToDataMap == NULL && m_pStringToValPosMap == NULL ) 383*cdf0e10cSrcweir { 384*cdf0e10cSrcweir bool bOptimizeForPerformance = false; 385*cdf0e10cSrcweir createHashMap( bOptimizeForPerformance ); 386*cdf0e10cSrcweir } 387*cdf0e10cSrcweir 388*cdf0e10cSrcweir if( m_pStringToValPosMap != NULL ) 389*cdf0e10cSrcweir { 390*cdf0e10cSrcweir StringToValPosMap::const_iterator it = m_pStringToValPosMap->find( rKey ); 391*cdf0e10cSrcweir if( it != m_pStringToValPosMap->end() ) 392*cdf0e10cSrcweir { 393*cdf0e10cSrcweir const std::pair<int,int>& rValPair = it->second; 394*cdf0e10cSrcweir int iValuePos = rValPair.first; 395*cdf0e10cSrcweir int nValueLen = rValPair.second; 396*cdf0e10cSrcweir 397*cdf0e10cSrcweir Reference< XInputStream > xIn = m_xSFA->openFileRead( m_aFileURL ); 398*cdf0e10cSrcweir if( xIn.is() ) 399*cdf0e10cSrcweir { 400*cdf0e10cSrcweir Reference< XSeekable > xXSeekable( xIn, UNO_QUERY ); 401*cdf0e10cSrcweir if( xXSeekable.is() ) 402*cdf0e10cSrcweir { 403*cdf0e10cSrcweir xXSeekable->seek( iValuePos ); 404*cdf0e10cSrcweir 405*cdf0e10cSrcweir Sequence< sal_Int8 > aData; 406*cdf0e10cSrcweir sal_Int32 nRead = xIn->readBytes( aData, nValueLen ); 407*cdf0e10cSrcweir if( nRead == nValueLen ) 408*cdf0e10cSrcweir { 409*cdf0e10cSrcweir const char* pData = (const sal_Char*)aData.getConstArray(); 410*cdf0e10cSrcweir rValue.copyToBuffer( pData, nValueLen ); 411*cdf0e10cSrcweir bSuccess = true; 412*cdf0e10cSrcweir } 413*cdf0e10cSrcweir } 414*cdf0e10cSrcweir xIn->closeInput(); 415*cdf0e10cSrcweir } 416*cdf0e10cSrcweir } 417*cdf0e10cSrcweir } 418*cdf0e10cSrcweir 419*cdf0e10cSrcweir else if( m_pStringToDataMap != NULL ) 420*cdf0e10cSrcweir { 421*cdf0e10cSrcweir StringToDataMap::const_iterator it = m_pStringToDataMap->find( rKey ); 422*cdf0e10cSrcweir if( it != m_pStringToDataMap->end() ) 423*cdf0e10cSrcweir { 424*cdf0e10cSrcweir const rtl::OString& rValueStr = it->second; 425*cdf0e10cSrcweir int nValueLen = rValueStr.getLength(); 426*cdf0e10cSrcweir const char* pData = rValueStr.getStr(); 427*cdf0e10cSrcweir rValue.copyToBuffer( pData, nValueLen ); 428*cdf0e10cSrcweir bSuccess = true; 429*cdf0e10cSrcweir } 430*cdf0e10cSrcweir } 431*cdf0e10cSrcweir 432*cdf0e10cSrcweir } 433*cdf0e10cSrcweir catch( Exception & ) 434*cdf0e10cSrcweir { 435*cdf0e10cSrcweir bSuccess = false; 436*cdf0e10cSrcweir } 437*cdf0e10cSrcweir 438*cdf0e10cSrcweir return bSuccess; 439*cdf0e10cSrcweir } 440*cdf0e10cSrcweir 441*cdf0e10cSrcweir bool DBHelp::startIteration( void ) 442*cdf0e10cSrcweir { 443*cdf0e10cSrcweir bool bSuccess = false; 444*cdf0e10cSrcweir 445*cdf0e10cSrcweir sal_Int32 nSize = m_xSFA->getSize( m_aFileURL ); 446*cdf0e10cSrcweir 447*cdf0e10cSrcweir Reference< XInputStream > xIn = m_xSFA->openFileRead( m_aFileURL ); 448*cdf0e10cSrcweir if( xIn.is() ) 449*cdf0e10cSrcweir { 450*cdf0e10cSrcweir m_nItRead = xIn->readBytes( m_aItData, nSize ); 451*cdf0e10cSrcweir if( m_nItRead == nSize ) 452*cdf0e10cSrcweir { 453*cdf0e10cSrcweir bSuccess = true; 454*cdf0e10cSrcweir m_pItData = (const char*)m_aItData.getConstArray(); 455*cdf0e10cSrcweir m_iItPos = 0; 456*cdf0e10cSrcweir } 457*cdf0e10cSrcweir else 458*cdf0e10cSrcweir { 459*cdf0e10cSrcweir stopIteration(); 460*cdf0e10cSrcweir } 461*cdf0e10cSrcweir } 462*cdf0e10cSrcweir 463*cdf0e10cSrcweir return bSuccess; 464*cdf0e10cSrcweir } 465*cdf0e10cSrcweir 466*cdf0e10cSrcweir bool DBHelp::getNextKeyAndValue( DBData& rKey, DBData& rValue ) 467*cdf0e10cSrcweir { 468*cdf0e10cSrcweir bool bSuccess = false; 469*cdf0e10cSrcweir 470*cdf0e10cSrcweir if( m_iItPos < m_nItRead ) 471*cdf0e10cSrcweir { 472*cdf0e10cSrcweir if( implReadLenAndData( m_pItData, m_iItPos, rKey ) ) 473*cdf0e10cSrcweir { 474*cdf0e10cSrcweir if( implReadLenAndData( m_pItData, m_iItPos, rValue ) ) 475*cdf0e10cSrcweir bSuccess = true; 476*cdf0e10cSrcweir } 477*cdf0e10cSrcweir } 478*cdf0e10cSrcweir 479*cdf0e10cSrcweir return bSuccess; 480*cdf0e10cSrcweir } 481*cdf0e10cSrcweir 482*cdf0e10cSrcweir void DBHelp::stopIteration( void ) 483*cdf0e10cSrcweir { 484*cdf0e10cSrcweir m_aItData = Sequence<sal_Int8>(); 485*cdf0e10cSrcweir m_pItData = NULL; 486*cdf0e10cSrcweir m_nItRead = -1; 487*cdf0e10cSrcweir m_iItPos = -1; 488*cdf0e10cSrcweir } 489*cdf0e10cSrcweir 490*cdf0e10cSrcweir 491*cdf0e10cSrcweir Db::Db() 492*cdf0e10cSrcweir { 493*cdf0e10cSrcweir db_internal::check_error( db_create(&m_pDBP,0,0),"Db::Db" ); 494*cdf0e10cSrcweir m_pDBHelp = NULL; 495*cdf0e10cSrcweir } 496*cdf0e10cSrcweir 497*cdf0e10cSrcweir 498*cdf0e10cSrcweir Db::~Db() 499*cdf0e10cSrcweir { 500*cdf0e10cSrcweir if (m_pDBP) 501*cdf0e10cSrcweir { 502*cdf0e10cSrcweir // should not happen 503*cdf0e10cSrcweir // TODO: add assert 504*cdf0e10cSrcweir } 505*cdf0e10cSrcweir 506*cdf0e10cSrcweir delete m_pDBHelp; 507*cdf0e10cSrcweir } 508*cdf0e10cSrcweir 509*cdf0e10cSrcweir 510*cdf0e10cSrcweir int Db::close(u_int32_t flags) 511*cdf0e10cSrcweir { 512*cdf0e10cSrcweir int error = m_pDBP->close(m_pDBP,flags); 513*cdf0e10cSrcweir m_pDBP = 0; 514*cdf0e10cSrcweir return db_internal::check_error(error,"Db::close"); 515*cdf0e10cSrcweir } 516*cdf0e10cSrcweir 517*cdf0e10cSrcweir int Db::open(DB_TXN *txnid, 518*cdf0e10cSrcweir const char *file, 519*cdf0e10cSrcweir const char *database, 520*cdf0e10cSrcweir DBTYPE type, 521*cdf0e10cSrcweir u_int32_t flags, 522*cdf0e10cSrcweir int mode) 523*cdf0e10cSrcweir { 524*cdf0e10cSrcweir int err = m_pDBP->open(m_pDBP,txnid,file,database,type,flags,mode); 525*cdf0e10cSrcweir return db_internal::check_error( err,"Db::open" ); 526*cdf0e10cSrcweir } 527*cdf0e10cSrcweir 528*cdf0e10cSrcweir int Db::open(DB_TXN *txnid, 529*cdf0e10cSrcweir ::rtl::OUString const & fileURL, 530*cdf0e10cSrcweir DBTYPE type, 531*cdf0e10cSrcweir u_int32_t flags, 532*cdf0e10cSrcweir int mode) 533*cdf0e10cSrcweir { 534*cdf0e10cSrcweir ::rtl::OUString ouPath; 535*cdf0e10cSrcweir ::osl::FileBase::getSystemPathFromFileURL(fileURL, ouPath); 536*cdf0e10cSrcweir const ::rtl::OString sPath = ::rtl::OUStringToOString(ouPath, osl_getThreadTextEncoding()); 537*cdf0e10cSrcweir return open(txnid, sPath.getStr(), 0, type, flags, mode); 538*cdf0e10cSrcweir } 539*cdf0e10cSrcweir 540*cdf0e10cSrcweir 541*cdf0e10cSrcweir 542*cdf0e10cSrcweir int Db::get(DB_TXN *txnid, Dbt *key, Dbt *data, u_int32_t flags) 543*cdf0e10cSrcweir { 544*cdf0e10cSrcweir int err = m_pDBP->get(m_pDBP,txnid,key,data,flags); 545*cdf0e10cSrcweir 546*cdf0e10cSrcweir // these are non-exceptional outcomes 547*cdf0e10cSrcweir if (err != DB_NOTFOUND && err != DB_KEYEMPTY) 548*cdf0e10cSrcweir db_internal::check_error( err,"Db::get" ); 549*cdf0e10cSrcweir 550*cdf0e10cSrcweir return err; 551*cdf0e10cSrcweir } 552*cdf0e10cSrcweir 553*cdf0e10cSrcweir int Db::cursor(DB_TXN *txnid, Dbc **cursorp, u_int32_t flags) 554*cdf0e10cSrcweir { 555*cdf0e10cSrcweir DBC * dbc = 0; 556*cdf0e10cSrcweir int error = m_pDBP->cursor(m_pDBP,txnid,&dbc,flags); 557*cdf0e10cSrcweir 558*cdf0e10cSrcweir if (!db_internal::check_error(error,"Db::cursor")) 559*cdf0e10cSrcweir *cursorp = new Dbc(dbc); 560*cdf0e10cSrcweir 561*cdf0e10cSrcweir return error; 562*cdf0e10cSrcweir } 563*cdf0e10cSrcweir 564*cdf0e10cSrcweir //---------------------------------------------------------------------------- 565*cdf0e10cSrcweir 566*cdf0e10cSrcweir Dbc::Dbc(DBC * dbc) 567*cdf0e10cSrcweir : m_pDBC(dbc) 568*cdf0e10cSrcweir { 569*cdf0e10cSrcweir } 570*cdf0e10cSrcweir 571*cdf0e10cSrcweir Dbc::~Dbc() 572*cdf0e10cSrcweir { 573*cdf0e10cSrcweir } 574*cdf0e10cSrcweir 575*cdf0e10cSrcweir int Dbc::close() 576*cdf0e10cSrcweir { 577*cdf0e10cSrcweir int err = m_pDBC->c_close(m_pDBC); 578*cdf0e10cSrcweir delete this; 579*cdf0e10cSrcweir return db_internal::check_error( err,"Dbcursor::close" ); 580*cdf0e10cSrcweir } 581*cdf0e10cSrcweir 582*cdf0e10cSrcweir int Dbc::get(Dbt *key, Dbt *data, u_int32_t flags) 583*cdf0e10cSrcweir { 584*cdf0e10cSrcweir int err = m_pDBC->c_get(m_pDBC,key,data,flags); 585*cdf0e10cSrcweir 586*cdf0e10cSrcweir // these are non-exceptional outcomes 587*cdf0e10cSrcweir if (err != DB_NOTFOUND && err != DB_KEYEMPTY) 588*cdf0e10cSrcweir db_internal::check_error( err, "Dbcursor::get" ); 589*cdf0e10cSrcweir 590*cdf0e10cSrcweir return err; 591*cdf0e10cSrcweir } 592*cdf0e10cSrcweir 593*cdf0e10cSrcweir //---------------------------------------------------------------------------- 594*cdf0e10cSrcweir 595*cdf0e10cSrcweir 596*cdf0e10cSrcweir Dbt::Dbt() 597*cdf0e10cSrcweir { 598*cdf0e10cSrcweir using namespace std; 599*cdf0e10cSrcweir DBT * thispod = this; 600*cdf0e10cSrcweir memset(thispod, 0, sizeof *thispod); 601*cdf0e10cSrcweir } 602*cdf0e10cSrcweir 603*cdf0e10cSrcweir 604*cdf0e10cSrcweir Dbt::Dbt(void *data_arg, u_int32_t size_arg) 605*cdf0e10cSrcweir { 606*cdf0e10cSrcweir using namespace std; 607*cdf0e10cSrcweir DBT * thispod = this; 608*cdf0e10cSrcweir memset(thispod, 0, sizeof *thispod); 609*cdf0e10cSrcweir this->set_data(data_arg); 610*cdf0e10cSrcweir this->set_size(size_arg); 611*cdf0e10cSrcweir } 612*cdf0e10cSrcweir 613*cdf0e10cSrcweir /* 614*cdf0e10cSrcweir Dbt::Dbt(const Dbt & other) 615*cdf0e10cSrcweir { 616*cdf0e10cSrcweir using namespace std; 617*cdf0e10cSrcweir const DBT *otherpod = &other; 618*cdf0e10cSrcweir DBT *thispod = this; 619*cdf0e10cSrcweir memcpy(thispod, otherpod, sizeof *thispod); 620*cdf0e10cSrcweir } 621*cdf0e10cSrcweir 622*cdf0e10cSrcweir Dbt& Dbt::operator = (const Dbt & other) 623*cdf0e10cSrcweir { 624*cdf0e10cSrcweir if (this != &other) 625*cdf0e10cSrcweir { 626*cdf0e10cSrcweir using namespace std; 627*cdf0e10cSrcweir const DBT *otherpod = &other; 628*cdf0e10cSrcweir DBT *thispod = this; 629*cdf0e10cSrcweir memcpy(thispod, otherpod, sizeof *thispod); 630*cdf0e10cSrcweir } 631*cdf0e10cSrcweir return *this; 632*cdf0e10cSrcweir } 633*cdf0e10cSrcweir */ 634*cdf0e10cSrcweir 635*cdf0e10cSrcweir Dbt::~Dbt() 636*cdf0e10cSrcweir { 637*cdf0e10cSrcweir } 638*cdf0e10cSrcweir 639*cdf0e10cSrcweir void * Dbt::get_data() const 640*cdf0e10cSrcweir { 641*cdf0e10cSrcweir return this->data; 642*cdf0e10cSrcweir } 643*cdf0e10cSrcweir 644*cdf0e10cSrcweir void Dbt::set_data(void *value) 645*cdf0e10cSrcweir { 646*cdf0e10cSrcweir this->data = value; 647*cdf0e10cSrcweir } 648*cdf0e10cSrcweir 649*cdf0e10cSrcweir u_int32_t Dbt::get_size() const 650*cdf0e10cSrcweir { 651*cdf0e10cSrcweir return this->size; 652*cdf0e10cSrcweir } 653*cdf0e10cSrcweir 654*cdf0e10cSrcweir void Dbt::set_size(u_int32_t value) 655*cdf0e10cSrcweir { 656*cdf0e10cSrcweir this->size = value; 657*cdf0e10cSrcweir } 658*cdf0e10cSrcweir 659*cdf0e10cSrcweir void Dbt::set_flags(u_int32_t value) 660*cdf0e10cSrcweir { 661*cdf0e10cSrcweir this->flags = value; 662*cdf0e10cSrcweir } 663*cdf0e10cSrcweir 664*cdf0e10cSrcweir //---------------------------------------------------------------------------- 665*cdf0e10cSrcweir /* 666*cdf0e10cSrcweir void db_internal::raise_error(int dberr, const char * where) 667*cdf0e10cSrcweir { 668*cdf0e10cSrcweir if (!where) where = "<unknown>"; 669*cdf0e10cSrcweir 670*cdf0e10cSrcweir const char * dberrmsg = db_strerror(dberr); 671*cdf0e10cSrcweir if (!dberrmsg || !*dberrmsg) dberrmsg = "<unknown DB error>"; 672*cdf0e10cSrcweir 673*cdf0e10cSrcweir rtl::OString msg = where; 674*cdf0e10cSrcweir msg += ": "; 675*cdf0e10cSrcweir msg += dberrmsg; 676*cdf0e10cSrcweir 677*cdf0e10cSrcweir throw DbException(msg); 678*cdf0e10cSrcweir } 679*cdf0e10cSrcweir */ 680*cdf0e10cSrcweir 681*cdf0e10cSrcweir //---------------------------------------------------------------------------- 682*cdf0e10cSrcweir } // namespace ecomp 683*cdf0e10cSrcweir 684