Datafile.cc

00001 /* 
00002    EyeDB Object Database Management System
00003    Copyright (C) 1994-2008 SYSRA
00004    
00005    EyeDB is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009    
00010    EyeDB is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014    
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA 
00018 */
00019 
00020 /*
00021    Author: Eric Viara <viara@sysra.com>
00022 */
00023 
00024 
00025 #include <assert.h>
00026 #include <eyedb_p.h>
00027 
00028 using namespace std;
00029 
00030 namespace eyedb {
00031 
00032   Status
00033   Datafile::remove() const
00034   {
00035     RPCStatus rpc_status = deleteDatafile(db->getDbHandle(), id);
00036     return StatusMake(rpc_status);
00037   }
00038 
00039   Status
00040   Datafile::resize(unsigned int size) const
00041   {
00042     RPCStatus rpc_status =
00043       resizeDatafile(db->getDbHandle(), id, size);
00044     return StatusMake(rpc_status);
00045   }
00046 
00047   Status
00048   Datafile::getInfo(DatafileInfo &datinfo) const
00049   {
00050     DatafileInfo::Info info;
00051     RPCStatus rpc_status =
00052       getDatafileInfo(db->getDbHandle(), id, &info);
00053     if (!rpc_status) datinfo.set(this, info);
00054     return StatusMake(rpc_status);
00055   }
00056 
00057   Status
00058   Datafile::rename(const char *newname) const
00059   {
00060     RPCStatus rpc_status =
00061       renameDatafile(db->getDbHandle(), id, newname);
00062     return StatusMake(rpc_status);
00063   }
00064 
00065   Status
00066   Datafile::defragment() const
00067   {
00068     RPCStatus rpc_status = defragmentDatafile(db->getDbHandle(), id);
00069     return StatusMake(rpc_status);
00070   }
00071 
00072   Status
00073   Datafile::move(const char *filedir, const char *filename) const
00074   {
00075     std::string s;
00076     if (filedir)
00077       s = std::string(filedir) + "/" + filename;
00078     else
00079       s = filename;
00080     RPCStatus rpc_status = moveDatafile(db->getDbHandle(), id, s.c_str());
00081     return StatusMake(rpc_status);
00082   }
00083 
00084 #define ONE_K 1024
00085 #define ONE_M (ONE_K*ONE_K)
00086 #define ONE_G (ONE_M*ONE_K)
00087 
00088   void
00089   display_datsize(std::ostream &os, unsigned long long _sz)
00090   {
00091     unsigned long long sz1, sz2;
00092     unsigned long long sz;
00093 
00094     sz = _sz;
00095     os << sz << "b";
00096 
00097     sz = _sz / ONE_K;
00098     if (sz)
00099       {
00100         os << ", ~" << sz << "Kb";
00101 
00102         sz = _sz / ONE_M;
00103         if (sz)
00104           {
00105             sz1 = sz * ONE_M;
00106             sz2 = (sz+1) * ONE_M;
00107             if ((sz2 - _sz) < (_sz - sz1))
00108               sz = sz+1;
00109             os << ", ~" << sz << "Mb";
00110 
00111             sz = _sz / ONE_G;
00112             if (sz) {
00113               sz1 = sz * ONE_G;
00114               sz2 = (sz+1) * ONE_G;
00115               if ((sz2 - _sz) < (_sz - sz1))
00116                 sz = sz+1;
00117               os << ", ~" << sz << "Gb";
00118               /*
00119                 printf("\n{_sz %llu, sz %llu, sz1 %llu, sz2 %llu, "
00120                 "(sz2 - _sz) %llu, (sz1 - _sz) %llu}\n",
00121                 _sz, sz, sz1, sz2, (sz2 - _sz), (_sz - sz1));
00122               */
00123             }
00124           }
00125       }
00126 
00127     os << '\n';
00128   }
00129 
00130   std::ostream& operator<<(std::ostream &os, const Datafile &dat)
00131   {
00132     os << "Datafile #" << dat.getId() << '\n';
00133 
00134     if (!dat.isValid()) {
00135       os << "  Invalid datafile\n";
00136       return os;
00137     }
00138 
00139     os << "  Name      " << (*dat.getName() ? dat.getName() : "<unnamed>") <<
00140       '\n';
00141     if (dat.getDataspace())
00142       os << "  Dataspace #" << dat.getDataspace()->getId() << " " <<
00143         dat.getDataspace()->getName() << '\n';
00144     os << "  File      " << dat.getFile() << '\n';
00145     os << "  Maxsize   ";
00146     display_datsize(os, (eyedblib::uint64)dat.getMaxsize()*1024);
00147 
00148     if (dat.getMaptype() == eyedbsm::BitmapType)
00149       os << "  Slotsize  " << dat.getSlotsize() << '\n';
00150     else
00151       os << "  Linkmap allocator\n";
00152 
00153     os << "  Oid Type  " << (dat.isPhysical() ? "Physical" : "Logical") << '\n';
00154     return os;
00155   }
00156 
00157   void
00158   DatafileInfo::set(const Datafile *_datafile,
00159                     DatafileInfo::Info &_info)
00160   {
00161     datafile = _datafile;
00162     info = _info;
00163   }
00164 
00165   std::ostream& operator<<(std::ostream& os, const DatafileInfo &datinfo)
00166   {
00167     if (!datinfo.getDatafile()) {
00168       os << "Null Datafile";
00169       return os;
00170     }
00171     const DatafileInfo::Info &info = datinfo.getInfo();
00172 
00173     os << *datinfo.getDatafile();
00174     os << '\n';
00175     os << "  Object Number        " << info.objcnt << '\n';
00176     os << "  Total Busy Size      ";
00177     display_datsize(os, info.totalsize);
00178     os << "  Average Size         ";
00179     display_datsize(os, info.avgsize);
00180     os << '\n';
00181     os << "  Slot Count           " << info.slotcnt << '\n';
00182     os << "  Busy Slot Count      " << info.busyslotcnt << '\n';
00183     os << "  Last Busy Slot       " << info.lastbusyslot << '\n';
00184     os << "  Last Slot            " << info.lastslot << '\n';
00185     os << "  Busy Slot Size       ";
00186     display_datsize(os, info.busyslotsize);
00187 
00188     os << "  .dat File Size       ";
00189     display_datsize(os, info.datfilesize);
00190     os << "  .dat File Block Size ";
00191     display_datsize(os, info.datfileblksize);
00192 
00193     os << "  .dmp File Size       ";
00194     display_datsize(os, info.dmpfilesize);
00195     os << "  .dmp File Block Size ";
00196     display_datsize(os, info.dmpfileblksize);
00197 
00198     os << "  Current Slot         " << info.curslot << '\n';
00199     os << "  Defragmentable Size  ";
00200     display_datsize(os, info.defragmentablesize);
00201 
00202     char buf[16];
00203     sprintf(buf, "%2.2f",
00204             (info.lastbusyslot ? (double)(100.*info.slotfragcnt)/info.lastbusyslot : 0));
00205 
00206     os << "  Slot Fragmentation   " << info.slotfragcnt << "/" <<
00207       info.lastbusyslot << " slots [" << buf << "%]\n";
00208 
00209     sprintf(buf, "%2.2f", info.used);
00210     os << "  Used                 " << buf << "%\n";
00211 
00212     return os;
00213   }
00214 
00215 }
00216   

Generated on Mon Dec 22 18:15:53 2008 for eyedb by  doxygen 1.5.3