Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

CacheIndex.C

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 // 
00003 // This file is part of Vesta.
00004 // 
00005 // Vesta 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 // Vesta 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 Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // Created on Fri Nov  7 09:59:07 PST 1997 by heydon
00020 // Last modified on Fri Apr 22 17:08:52 EDT 2005 by ken@xorian.net        
00021 //      modified on Sun Jul 28 12:07:33 EDT 2002 by lken@remote.xorian.net
00022 //      modified on Mon Apr 12 09:01:33 PDT 1999 by heydon
00023 
00024 #include <Basics.H>
00025 #include <FS.H>
00026 #include <VestaLog.H>
00027 #include <Recovery.H>
00028 #include "ImmutableVal.H"
00029 #include "CacheIndex.H"
00030 
00031 using std::ostream;
00032 using std::istream;
00033 using std::ios;
00034 
00035 inline void Indent(ostream &os, int indent) throw ()
00036 {
00037     for (int i = 0; i < indent; i++) os << ' ';
00038 }
00039 
00040 // CacheEntry::Indices --------------------------------------------------------
00041 
00042 void CacheEntry::Indices::Log(VestaLog &log) const
00043   throw (VestaLog::Error)
00044 {
00045     log.write((char *)(&(this->len)), sizeof_assert(this->len, 4));
00046     if (this->len > 0) {
00047         log.write((char *)(this->index),
00048           this->len * sizeof_assert(*(this->index), 4));
00049     }
00050 }
00051 
00052 void CacheEntry::Indices::Recover(RecoveryReader &rd)
00053   throw (VestaLog::Error, VestaLog::Eof)
00054 {
00055     rd.readAll((char *)(&(this->len)), sizeof_assert(this->len, 4));
00056     if (this->len > 0) {
00057         this->index = NEW_PTRFREE_ARRAY(CacheEntry::Index, this->len);
00058         rd.readAll((char *)(this->index),
00059           this->len * sizeof_assert(*(this->index), 4));
00060     } else {
00061         this->index = (CacheEntry::Index *)NULL;
00062     }
00063 }
00064 
00065 void CacheEntry::Indices::Write(ostream &ofs) const
00066   throw (FS::Failure)
00067 {
00068     FS::Write(ofs, (char *)(&(this->len)), sizeof_assert(this->len, 4));
00069     if (this->len > 0) {
00070         FS::Write(ofs, (char *)(this->index),
00071           this->len * sizeof_assert(*(this->index), 4));
00072     }
00073 }
00074 
00075 void CacheEntry::Indices::Read(istream &ifs)
00076   throw (FS::EndOfFile, FS::Failure)
00077 {
00078     FS::Read(ifs, (char *)(&(this->len)), sizeof_assert(this->len, 4));
00079     if (this->len > 0) {
00080         this->index = NEW_PTRFREE_ARRAY(CacheEntry::Index, this->len);
00081         FS::Read(ifs, (char *)(this->index),
00082           this->len * sizeof_assert(*(this->index), 4));
00083     } else {
00084         this->index = (CacheEntry::Index *)NULL;
00085     }
00086 }
00087 
00088 ImmutableVal* CacheEntry::Indices::ReadImmutable(istream &ifs)
00089   throw (FS::EndOfFile, FS::Failure)
00090 {
00091     int start = FS::Posn(ifs);
00092     Basics::int32 listLen;
00093     FS::Read(ifs, (char *)(&listLen), sizeof_assert(listLen, 4));
00094     int dataLen = listLen * sizeof_assert(CacheEntry::Index, 4);
00095     FS::Seek(ifs, dataLen, ios::cur);
00096     return NEW_CONSTR(ImmutableVal, (start, sizeof(listLen) + dataLen));
00097 }
00098 
00099 void CacheEntry::Indices::Send(SRPC &srpc) const throw (SRPC::failure)
00100 {
00101     srpc.send_int(this->len);
00102     if (this->len > 0) {
00103       srpc.send_int32_array((const Basics::int32 *) this->index, this->len);
00104     }
00105 }
00106 
00107 void CacheEntry::Indices::Recv(SRPC &srpc) throw (SRPC::failure)
00108 {
00109   this->len = srpc.recv_int();
00110   if (this->len > 0) {
00111     int len;
00112     this->index = (CacheEntry::Index *) srpc.recv_int32_array(len);
00113     assert(this->len == len);
00114   } else {
00115     this->index = (CacheEntry::Index *)NULL;
00116   }
00117 }
00118 
00119 void CacheEntry::Indices::Print(ostream &os, int indent) const throw ()
00120 {
00121     const int NumCIsPerLine = 8;
00122     if (this->len < NumCIsPerLine) {
00123         // print in 1-line format
00124         os << *this;
00125     } else {
00126         // multi-line format
00127         os << '{';
00128         for (int i = 0; i < this->len; i++) {
00129             if (i % NumCIsPerLine == 0) {
00130                 os << '\n'; Indent(os, indent);
00131             }
00132             os << this->index[i];
00133             if (i < this->len - 1) os << ", ";
00134         }
00135         os << " }";
00136     }
00137     os << '\n';
00138 }
00139 
00140 ostream& operator << (ostream &os, const CacheEntry::Indices& cis) throw ()
00141 {
00142     os << "{ ";
00143     for (int i = 0; i < cis.len; i++) {
00144         os << cis.index[i];
00145         if (i < cis.len - 1) os << ", ";
00146     }
00147     os << " }";
00148     return os;
00149 }
00150     
00151 void CacheEntry::IndicesApp::Append(CacheEntry::Index ci) throw ()
00152 {
00153     if (this->len == this->maxLen) {
00154         this->maxLen = max(2, 2 * this->maxLen);
00155         assert(this->maxLen > this->len);
00156         CacheEntry::Index *newIndex =
00157           NEW_PTRFREE_ARRAY(CacheEntry::Index, this->maxLen);
00158         if (this->index != (CacheEntry::Index *)NULL) {
00159             for (int i = 0; i < this->len; i++) {
00160                 newIndex[i] = this->index[i];
00161             }
00162         }
00163         this->index = newIndex;
00164     }
00165     this->index[this->len++] = ci;
00166 }

Generated on Mon May 8 00:48:31 2006 for Vesta by  doxygen 1.4.2