00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
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
00124 os << *this;
00125 } else {
00126
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 }