Changeset 2631


Ignore:
Timestamp:
22/02/2010 22:10:04 (2 years ago)
Author:
chris
Message:

Remove all references to bdb databases, use QDBM instead.

Location:
box/trunk/bin/bbackupd
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • box/trunk/bin/bbackupd/BackupClientInodeToIDMap.cpp

    r2314 r2631  
    1010#include "Box.h" 
    1111 
    12 #ifdef HAVE_DB 
    13         // Include db headers and other OS files if they're needed for the disc implementation 
    14         #include <sys/types.h> 
    15         #include <fcntl.h> 
    16         #include <limits.h> 
    17         #include <db.h> 
    18         #include <sys/stat.h> 
    19 #endif 
     12#include <stdlib.h> 
     13#include <depot.h> 
    2014 
    2115#define BACKIPCLIENTINODETOIDMAP_IMPLEMENTATION 
    2216#include "BackupClientInodeToIDMap.h" 
     17#undef BACKIPCLIENTINODETOIDMAP_IMPLEMENTATION 
    2318 
    2419#include "BackupStoreException.h" 
    2520 
    26  
    2721#include "MemLeakFindOn.h" 
    28  
    29 // What type of Berkeley DB shall we use? 
    30 #define TABLE_DATABASE_TYPE DB_HASH 
    3122 
    3223typedef struct 
     
    3627} IDBRecord; 
    3728 
     29#define BOX_DBM_MESSAGE(stuff) stuff << " (qdbm): " << dperrmsg(dpecode) 
     30 
     31#define BOX_LOG_DBM_ERROR(stuff) \ 
     32        BOX_ERROR(BOX_DBM_MESSAGE(stuff)) 
     33 
     34#define THROW_DBM_ERROR(message, filename, exception, subtype) \ 
     35        BOX_LOG_DBM_ERROR(message << ": " << filename); \ 
     36        THROW_EXCEPTION_MESSAGE(exception, subtype, \ 
     37                BOX_DBM_MESSAGE(message << ": " << filename)); 
     38 
     39#define ASSERT_DBM_OK(operation, message, filename, exception, subtype) \ 
     40        if(!(operation)) \ 
     41        { \ 
     42                THROW_DBM_ERROR(message, filename, exception, subtype); \ 
     43        } 
     44 
     45#define ASSERT_DBM_OPEN() \ 
     46        if(mpDepot == 0) \ 
     47        { \ 
     48                THROW_EXCEPTION_MESSAGE(BackupStoreException, InodeMapNotOpen, \ 
     49                        "Inode database not open"); \ 
     50        } 
     51 
     52#define ASSERT_DBM_CLOSED() \ 
     53        if(mpDepot != 0) \ 
     54        { \ 
     55                THROW_EXCEPTION_MESSAGE(CommonException, Internal, \ 
     56                        "Inode database already open: " << mFilename); \ 
     57        }  
     58 
    3859// -------------------------------------------------------------------------- 
    3960// 
     
    4566// -------------------------------------------------------------------------- 
    4667BackupClientInodeToIDMap::BackupClientInodeToIDMap() 
    47 #ifndef BACKIPCLIENTINODETOIDMAP_IN_MEMORY_IMPLEMENTATION 
    4868        : mReadOnly(true), 
    4969          mEmpty(false), 
    50           dbp(0) 
    51 #endif 
     70          mpDepot(0) 
    5271{ 
    5372} 
     
    6382BackupClientInodeToIDMap::~BackupClientInodeToIDMap() 
    6483{ 
    65 #ifndef BACKIPCLIENTINODETOIDMAP_IN_MEMORY_IMPLEMENTATION 
    66         if(dbp != 0) 
    67         { 
    68 #if BDB_VERSION_MAJOR >= 3 
    69                 dbp->close(0); 
    70 #else 
    71                 dbp->close(dbp); 
    72 #endif 
    73         } 
    74 #endif 
    75 } 
    76  
     84        if(mpDepot != 0) 
     85        { 
     86                Close(); 
     87        } 
     88} 
    7789 
    7890// -------------------------------------------------------------------------- 
     
    8496// 
    8597// -------------------------------------------------------------------------- 
    86 void BackupClientInodeToIDMap::Open(const char *Filename, bool ReadOnly, bool CreateNew) 
    87 { 
    88 #ifndef BACKIPCLIENTINODETOIDMAP_IN_MEMORY_IMPLEMENTATION 
     98void BackupClientInodeToIDMap::Open(const char *Filename, bool ReadOnly, 
     99        bool CreateNew) 
     100{ 
     101        mFilename = Filename; 
     102 
    89103        // Correct arguments? 
    90104        ASSERT(!(CreateNew && ReadOnly)); 
    91105         
    92106        // Correct usage? 
    93         ASSERT(dbp == 0); 
     107        ASSERT_DBM_CLOSED(); 
    94108        ASSERT(!mEmpty); 
    95109         
    96110        // Open the database file 
    97 #if BDB_VERSION_MAJOR >= 3 
    98         dbp = new Db(0,0); 
    99         dbp->set_pagesize(1024);                /* Page size: 1K. */ 
    100         dbp->set_cachesize(0, 32 * 1024, 0); 
    101         dbp->open(NULL, Filename, NULL, DB_HASH, DB_CREATE, 0664); 
    102 #else 
    103         dbp = dbopen(Filename, (CreateNew?O_CREAT:0) | (ReadOnly?O_RDONLY:O_RDWR), S_IRUSR | S_IWUSR | S_IRGRP, TABLE_DATABASE_TYPE, NULL); 
    104 #endif 
    105         if(dbp == NULL) 
    106         { 
    107                 THROW_EXCEPTION(BackupStoreException, BerkelyDBFailure); 
    108         } 
     111        int mode = ReadOnly ? DP_OREADER : DP_OWRITER; 
     112        if(CreateNew) 
     113        { 
     114                mode |= DP_OCREAT; 
     115        } 
     116         
     117        mpDepot = dpopen(Filename, mode, 0); 
     118         
     119        ASSERT_DBM_OK(mpDepot, "Failed to open inode database", mFilename, 
     120                BackupStoreException, BerkelyDBFailure); 
    109121         
    110122        // Read only flag 
    111123        mReadOnly = ReadOnly; 
    112 #endif 
    113124} 
    114125 
     
    117128// Function 
    118129//              Name:    BackupClientInodeToIDMap::OpenEmpty() 
    119 //              Purpose: 'Open' this map. Not associated with a disc file. Useful for when a map 
    120 //                               is required, but is against an empty file on disc which shouldn't be created. 
    121 //                               Implies read only. 
     130//              Purpose: 'Open' this map. Not associated with a disc file. 
     131//                       Useful for when a map is required, but is against 
     132//                       an empty file on disc which shouldn't be created. 
     133//                       Implies read only. 
    122134//              Created: 20/11/03 
    123135// 
     
    125137void BackupClientInodeToIDMap::OpenEmpty() 
    126138{ 
    127 #ifndef BACKIPCLIENTINODETOIDMAP_IN_MEMORY_IMPLEMENTATION 
    128         ASSERT(dbp == 0); 
     139        ASSERT_DBM_CLOSED(); 
     140        ASSERT(mpDepot == 0); 
    129141        mEmpty = true; 
    130142        mReadOnly = true; 
    131 #endif 
    132 } 
    133  
    134  
     143} 
    135144 
    136145// -------------------------------------------------------------------------- 
     
    144153void BackupClientInodeToIDMap::Close() 
    145154{ 
    146 #ifndef BACKIPCLIENTINODETOIDMAP_IN_MEMORY_IMPLEMENTATION 
    147         if(dbp != 0) 
    148         { 
    149 #if BDB_VERSION_MAJOR >= 3 
    150                 if(dbp->close(0) != 0) 
    151 #else 
    152                 if(dbp->close(dbp) != 0) 
    153 #endif 
    154                 { 
    155                         THROW_EXCEPTION(BackupStoreException, BerkelyDBFailure); 
    156                 } 
    157                 dbp = 0; 
    158         } 
    159 #endif 
    160 } 
    161  
    162  
    163 // -------------------------------------------------------------------------- 
    164 // 
    165 // Function 
    166 //              Name:    BackupClientInodeToIDMap::AddToMap(InodeRefType, int64_t, int64_t) 
    167 //              Purpose: Adds an entry to the map. Overwrites any existing entry. 
    168 //              Created: 11/11/03 
    169 // 
    170 // -------------------------------------------------------------------------- 
    171 void BackupClientInodeToIDMap::AddToMap(InodeRefType InodeRef, int64_t ObjectID, int64_t InDirectory) 
    172 { 
    173 #ifdef BACKIPCLIENTINODETOIDMAP_IN_MEMORY_IMPLEMENTATION 
    174         mMap[InodeRef] = std::pair<int64_t, int64_t>(ObjectID, InDirectory); 
    175 #else 
     155        ASSERT_DBM_OPEN(); 
     156        ASSERT_DBM_OK(dpclose(mpDepot), "Failed to close inode database", 
     157                mFilename, BackupStoreException, BerkelyDBFailure); 
     158        mpDepot = 0; 
     159} 
     160 
     161// -------------------------------------------------------------------------- 
     162// 
     163// Function 
     164//              Name:    BackupClientInodeToIDMap::AddToMap(InodeRefType, 
     165//                       int64_t, int64_t) 
     166//              Purpose: Adds an entry to the map. Overwrites any existing 
     167//                       entry. 
     168//              Created: 11/11/03 
     169// 
     170// -------------------------------------------------------------------------- 
     171void BackupClientInodeToIDMap::AddToMap(InodeRefType InodeRef, int64_t ObjectID, 
     172        int64_t InDirectory) 
     173{ 
    176174        if(mReadOnly) 
    177175        { 
     
    179177        } 
    180178 
    181         if(dbp == 0) 
     179        if(mpDepot == 0) 
    182180        { 
    183181                THROW_EXCEPTION(BackupStoreException, InodeMapNotOpen); 
    184182        } 
     183 
     184        ASSERT_DBM_OPEN(); 
    185185 
    186186        // Setup structures 
     
    189189        rec.mInDirectory = InDirectory; 
    190190 
    191 #if BDB_VERSION_MAJOR >= 3 
    192         Dbt key(&InodeRef, sizeof(InodeRef)); 
    193         Dbt data(&rec, sizeof(rec)); 
    194  
    195         if (dbp->put(0, &key, &data, 0) != 0) { 
    196                 THROW_EXCEPTION(BackupStoreException, BerkelyDBFailure); 
    197         } 
    198 #else 
    199          
    200         DBT key; 
    201         key.data = &InodeRef; 
    202         key.size = sizeof(InodeRef); 
    203          
    204         DBT data; 
    205         data.data = &rec; 
    206         data.size = sizeof(rec); 
    207          
    208         // Add to map (or replace existing entry) 
    209         if(dbp->put(dbp, &key, &data, 0) != 0) 
    210         { 
    211                 THROW_EXCEPTION(BackupStoreException, BerkelyDBFailure); 
    212         } 
    213 #endif 
    214 #endif 
     191        ASSERT_DBM_OK(dpput(mpDepot, (const char *)&InodeRef, sizeof(InodeRef), 
     192                (const char *)&rec, sizeof(rec), DP_DOVER), 
     193                "Failed to add record to inode database", mFilename, 
     194                BackupStoreException, BerkelyDBFailure); 
    215195} 
    216196 
     
    229209        int64_t &rObjectIDOut, int64_t &rInDirectoryOut) const 
    230210{ 
    231 #ifdef BACKIPCLIENTINODETOIDMAP_IN_MEMORY_IMPLEMENTATION 
    232         std::map<InodeRefType, std::pair<int64_t, int64_t> >::const_iterator i(mMap.find(InodeRef)); 
    233          
    234         // Found? 
    235         if(i == mMap.end()) 
    236         { 
    237                 return false; 
    238         } 
    239  
    240         // Yes. Return the details 
    241         rObjectIDOut = i->second.first; 
    242         rInDirectoryOut = i->second.second; 
    243         return true; 
    244 #else 
    245211        if(mEmpty) 
    246212        { 
     
    249215        } 
    250216 
    251         if(dbp == 0) 
     217        if(mpDepot == 0) 
    252218        { 
    253219                THROW_EXCEPTION(BackupStoreException, InodeMapNotOpen); 
    254220        } 
    255  
    256 #if BDB_VERSION_MAJOR >= 3 
    257         Dbt key(&InodeRef, sizeof(InodeRef)); 
    258         Dbt data(0, 0); 
    259         switch(dbp->get(NULL, &key, &data, 0)) 
    260 #else 
    261         DBT key; 
    262         key.data = &InodeRef; 
    263         key.size = sizeof(InodeRef); 
    264          
    265         DBT data; 
    266         data.data = 0; 
    267         data.size = 0; 
    268  
    269         switch(dbp->get(dbp, &key, &data, 0)) 
    270 #endif 
    271          
    272         { 
    273         case 1: // key not in file 
     221         
     222        ASSERT_DBM_OPEN(); 
     223 
     224        IDBRecord rec; 
     225         
     226        if(dpgetwb(mpDepot, (const char *)&InodeRef, sizeof(InodeRef), 
     227                0, sizeof(IDBRecord), (char *)&rec) == -1) 
     228        { 
     229                // key not in file 
    274230                return false; 
    275          
    276         case -1:        // error 
    277         default:        // not specified in docs 
    278                 THROW_EXCEPTION(BackupStoreException, BerkelyDBFailure); 
    279                 return false; 
    280          
    281         case 0:         // success, found it 
    282                 break;           
    283         } 
    284  
    285         // Check for sensible return 
    286 #if BDB_VERSION_MAJOR >= 3 
    287         if(key.get_data() == 0 || data.get_size() != sizeof(IDBRecord)) 
    288         { 
    289                 // Assert in debug version 
    290                 ASSERT(key.get_data() == 0 || data.get_size() != sizeof(IDBRecord)); 
     231        } 
    291232                 
    292                 // Invalid entries mean it wasn't found 
    293                 return false; 
    294         } 
    295  
    296         // Data alignment isn't guaranteed to be on a suitable boundary 
    297         IDBRecord rec; 
    298  
    299         ::memcpy(&rec, data.get_data(), sizeof(rec)); 
    300 #else 
    301         if(key.data == 0 || data.size != sizeof(IDBRecord)) 
    302         { 
    303                 // Assert in debug version 
    304                 ASSERT(key.data == 0 || data.size != sizeof(IDBRecord)); 
    305                  
    306                 // Invalid entries mean it wasn't found 
    307                 return false; 
    308         } 
    309          
    310         // Data alignment isn't guaranteed to be on a suitable boundary 
    311         IDBRecord rec; 
    312  
    313         ::memcpy(&rec, data.data, sizeof(rec)); 
    314 #endif 
    315          
    316233        // Return data 
    317234        rObjectIDOut = rec.mObjectID; 
    318235        rInDirectoryOut = rec.mInDirectory; 
    319236 
    320         // Don't have to worry about freeing the returned data 
    321  
    322237        // Found 
    323238        return true; 
    324 #endif 
    325 } 
    326  
    327  
     239} 
  • box/trunk/bin/bbackupd/BackupClientInodeToIDMap.h

    r217 r2631  
    99 
    1010#ifndef BACKUPCLIENTINODETOIDMAP_H 
    11 #define BACKUPCLIENTINODETOIDMAP__H 
     11#define BACKUPCLIENTINODETOIDMAP_H 
    1212 
    1313#include <sys/types.h> 
     
    1616#include <utility> 
    1717 
    18 // Use in memory implementation if there isn't access to the Berkely DB on this platform 
    19 #ifndef HAVE_DB 
    20         #define BACKIPCLIENTINODETOIDMAP_IN_MEMORY_IMPLEMENTATION 
    21 #endif 
    22  
    2318// avoid having to include the DB files when not necessary 
    2419#ifndef BACKIPCLIENTINODETOIDMAP_IMPLEMENTATION 
    25 #ifdef BERKELY_V4 
    26         class Db; 
    27 #else 
    28         class DB; 
    29 #endif 
     20        class DEPOT; 
    3021#endif 
    3122 
     
    5647 
    5748private: 
    58 #ifdef BACKIPCLIENTINODETOIDMAP_IN_MEMORY_IMPLEMENTATION 
    59         std::map<InodeRefType, std::pair<int64_t, int64_t> > mMap; 
    60 #else 
    6149        bool mReadOnly; 
    6250        bool mEmpty; 
    63 #ifdef BERKELY_V4 
    64         Db *dbp;        // c++ style implimentation 
    65 #else 
    66         DB *dbp;        // C style interface, use notation from documentation 
    67 #endif // BERKELY_V4 
    68 #endif // BACKIPCLIENTINODETOIDMAP_IN_MEMORY_IMPLEMENTATION 
     51        std::string mFilename; 
     52        DEPOT *mpDepot; 
    6953}; 
    7054 
    71 #endif // BACKUPCLIENTINODETOIDMAP__H 
     55#endif // BACKUPCLIENTINODETOIDMAP_H 
    7256 
    7357 
  • box/trunk/bin/bbackupd/BackupDaemon.cpp

    r2597 r2631  
    17851785void BackupDaemon::SetupIDMapsForSync() 
    17861786{ 
    1787         // Need to do different things depending on whether it's an 
    1788         // in memory implementation, or whether it's all stored on disc. 
    1789          
    1790 #ifdef BACKIPCLIENTINODETOIDMAP_IN_MEMORY_IMPLEMENTATION 
    1791  
    1792         // Make sure we have some blank, empty ID maps 
    1793         DeleteIDMapVector(mNewIDMaps); 
    1794         FillIDMapVector(mNewIDMaps, true /* new maps */); 
    1795  
    1796         // Then make sure that the current maps have objects, 
    1797         // even if they are empty (for the very first run) 
    1798         if(mCurrentIDMaps.empty()) 
    1799         { 
    1800                 FillIDMapVector(mCurrentIDMaps, false /* current maps */); 
    1801         } 
    1802  
    1803 #else 
    1804  
    18051787        // Make sure we have some blank, empty ID maps 
    18061788        DeleteIDMapVector(mNewIDMaps); 
     
    18081790        DeleteIDMapVector(mCurrentIDMaps); 
    18091791        FillIDMapVector(mCurrentIDMaps, false /* new maps */); 
    1810  
    1811 #endif 
    18121792} 
    18131793 
     
    19361916void BackupDaemon::CommitIDMapsAfterSync() 
    19371917{ 
    1938         // Need to do different things depending on whether it's an in memory implementation, 
    1939         // or whether it's all stored on disc. 
    1940          
    1941 #ifdef BACKIPCLIENTINODETOIDMAP_IN_MEMORY_IMPLEMENTATION 
    1942         // Remove the current ID maps 
    1943         DeleteIDMapVector(mCurrentIDMaps); 
    1944  
    1945         // Copy the (pointers to) "new" maps over to be the new "current" maps 
    1946         mCurrentIDMaps = mNewIDMaps; 
    1947          
    1948         // Clear the new ID maps vector (not delete them!) 
    1949         mNewIDMaps.clear(); 
    1950  
    1951 #else 
    1952  
    19531918        // Get rid of the maps in memory (leaving them on disc of course) 
    19541919        DeleteIDMapVector(mCurrentIDMaps); 
     
    19741939                } 
    19751940        } 
    1976  
    1977 #endif 
    19781941} 
    19791942 
     
    19971960                 
    19981961                // Close and delete 
    1999                 toDel->Close(); 
    2000                 delete toDel; 
     1962win32 rename            delete toDel; 
    20011963        } 
    20021964        ASSERT(rVector.size() == 0); 
Note: See TracChangeset for help on using the changeset viewer.