| 112 | | int e1 = access(pIndexFile, F_OK); |
|---|
| 113 | | int e2 = access(pDataFile, F_OK); |
|---|
| 114 | | if (e1 != 0 || e2 != 0) bFileExists = false; |
|---|
| 115 | | |
|---|
| 116 | | if (! bFileExists) bOverwrite = true; |
|---|
| | 104 | std::ifstream fin1(sIndexFile.c_str(), std::ios::in | std::ios::binary); |
|---|
| | 105 | std::ifstream fin2(sDataFile.c_str(), std::ios::in | std::ios::binary); |
|---|
| | 106 | if (fin1.fail() || fin2.fail()) bFileExists = false; |
|---|
| | 107 | fin1.close(); fin2.close(); |
|---|
| 119 | | if (bOverwrite == false) |
|---|
| 120 | | { |
|---|
| 121 | | int e1 = access(pIndexFile, R_OK | W_OK); |
|---|
| 122 | | int e2 = access(pDataFile, R_OK | W_OK); |
|---|
| 123 | | |
|---|
| 124 | | if ((e1 != 0 || e2 != 0) && bFileExists) |
|---|
| 125 | | { |
|---|
| 126 | | delete[] pIndexFile; |
|---|
| 127 | | delete[] pDataFile; |
|---|
| 128 | | throw Tools::IllegalArgumentException("Index file cannot be read/writen."); |
|---|
| 129 | | } |
|---|
| 130 | | } |
|---|
| 131 | | |
|---|
| 132 | | int cMode = (bOverwrite) ? O_CREAT | O_RDWR | O_TRUNC : O_RDWR; |
|---|
| 133 | | |
|---|
| 134 | | m_indexFile = open(pIndexFile, cMode, 0644); |
|---|
| 135 | | if (m_indexFile < 0) |
|---|
| 136 | | { |
|---|
| 137 | | delete[] pIndexFile; |
|---|
| 138 | | delete[] pDataFile; |
|---|
| 139 | | throw Tools::IllegalArgumentException("Index file cannot be opened."); |
|---|
| 140 | | } |
|---|
| 141 | | |
|---|
| 142 | | m_dataFile = open(pDataFile, cMode, 0644); |
|---|
| 143 | | if (m_dataFile < 0) |
|---|
| 144 | | { |
|---|
| 145 | | delete[] pIndexFile; |
|---|
| 146 | | delete[] pDataFile; |
|---|
| 147 | | throw Tools::IllegalArgumentException("Data file cannot be opened."); |
|---|
| 148 | | } |
|---|
| 149 | | |
|---|
| 150 | | delete[] pIndexFile; |
|---|
| 151 | | delete[] pDataFile; |
|---|
| | 110 | if (bFileExists == true && bOverwrite == false) |
|---|
| | 111 | { |
|---|
| | 112 | m_indexFile.open(sIndexFile.c_str(), std::ios::in | std::ios::out | std::ios::binary); |
|---|
| | 113 | m_dataFile.open(sDataFile.c_str(), std::ios::in | std::ios::out | std::ios::binary); |
|---|
| | 114 | |
|---|
| | 115 | if (m_indexFile.fail() || m_dataFile.fail()) |
|---|
| | 116 | throw Tools::IllegalArgumentException("SpatialIndex::DiskStorageManager: Index/Data file cannot be read/writen."); |
|---|
| | 117 | } |
|---|
| | 118 | else |
|---|
| | 119 | { |
|---|
| | 120 | m_indexFile.open(sIndexFile.c_str(), std::ios::in | std::ios::out | std::ios::binary | std::ios::trunc); |
|---|
| | 121 | m_dataFile.open(sDataFile.c_str(), std::ios::in | std::ios::out | std::ios::binary | std::ios::trunc); |
|---|
| | 122 | |
|---|
| | 123 | if (m_indexFile.fail() || m_dataFile.fail()) |
|---|
| | 124 | throw Tools::IllegalArgumentException("SpatialIndex::DiskStorageManager: Index/Data file cannot be created."); |
|---|
| | 125 | |
|---|
| | 126 | } |
|---|
| 176 | | ssize_t bytesread = read(m_indexFile, &m_pageSize, sizeof(size_t)); |
|---|
| 177 | | if (bytesread != sizeof(size_t)) throw Tools::IllegalStateException("Failed reading pageSize."); |
|---|
| 178 | | |
|---|
| 179 | | bytesread = read(m_indexFile, &m_nextPage, sizeof(id_type)); |
|---|
| 180 | | if (bytesread != sizeof(id_type)) |
|---|
| 181 | | throw Tools::IllegalStateException("Failed reading nextPage."); |
|---|
| | 152 | m_indexFile.read(reinterpret_cast<char*>(&m_pageSize), sizeof(size_t)); |
|---|
| | 153 | if (m_indexFile.fail()) |
|---|
| | 154 | throw Tools::IllegalStateException("SpatialIndex::DiskStorageManager: Failed reading pageSize."); |
|---|
| | 155 | |
|---|
| | 156 | m_indexFile.read(reinterpret_cast<char*>(&m_nextPage), sizeof(id_type)); |
|---|
| | 157 | if (m_indexFile.fail()) |
|---|
| | 158 | throw Tools::IllegalStateException("SpatialIndex::DiskStorageManager: Failed reading nextPage."); |
|---|
| 195 | | bytesread = read(m_indexFile, &count, sizeof(size_t)); |
|---|
| 196 | | if (bytesread != sizeof(size_t)) |
|---|
| 197 | | throw Tools::IllegalStateException("Corrupted storage manager index file."); |
|---|
| 198 | | |
|---|
| 199 | | for (size_t cCount = 0; cCount < count; cCount++) |
|---|
| 200 | | { |
|---|
| 201 | | bytesread = read(m_indexFile, &page, sizeof(id_type)); |
|---|
| 202 | | if (bytesread != sizeof(id_type)) |
|---|
| 203 | | throw Tools::IllegalStateException("Corrupted storage manager index file."); |
|---|
| | 171 | m_indexFile.read(reinterpret_cast<char*>(&count), sizeof(size_t)); |
|---|
| | 172 | if (m_indexFile.fail()) |
|---|
| | 173 | throw Tools::IllegalStateException("SpatialIndex::DiskStorageManager: Corrupted storage manager index file."); |
|---|
| | 174 | |
|---|
| | 175 | for (size_t cCount = 0; cCount < count; ++cCount) |
|---|
| | 176 | { |
|---|
| | 177 | m_indexFile.read(reinterpret_cast<char*>(&page), sizeof(id_type)); |
|---|
| | 178 | if (m_indexFile.fail()) |
|---|
| | 179 | throw Tools::IllegalStateException("SpatialIndex::DiskStorageManager: Corrupted storage manager index file."); |
|---|
| 208 | | bytesread = read(m_indexFile, &count, sizeof(size_t)); |
|---|
| 209 | | if (bytesread != sizeof(size_t)) |
|---|
| 210 | | throw Tools::IllegalStateException("Corrupted storage manager index file."); |
|---|
| 211 | | |
|---|
| 212 | | for (size_t cCount = 0; cCount < count; cCount++) |
|---|
| | 184 | m_indexFile.read(reinterpret_cast<char*>(&count), sizeof(size_t)); |
|---|
| | 185 | if (m_indexFile.fail()) |
|---|
| | 186 | throw Tools::IllegalStateException("SpatialIndex::DiskStorageManager: Corrupted storage manager index file."); |
|---|
| | 187 | |
|---|
| | 188 | for (size_t cCount = 0; cCount < count; ++cCount) |
|---|
| 216 | | bytesread = read(m_indexFile, &id, sizeof(id_type)); |
|---|
| 217 | | if (bytesread != sizeof(id_type)) |
|---|
| 218 | | throw Tools::IllegalStateException("Corrupted storage manager index file."); |
|---|
| 219 | | |
|---|
| 220 | | bytesread = read(m_indexFile, &(e->m_length), sizeof(size_t)); |
|---|
| 221 | | if (bytesread != sizeof(size_t)) |
|---|
| 222 | | throw Tools::IllegalStateException("Corrupted storage manager index file."); |
|---|
| | 192 | m_indexFile.read(reinterpret_cast<char*>(&id), sizeof(id_type)); |
|---|
| | 193 | if (m_indexFile.fail()) |
|---|
| | 194 | throw Tools::IllegalStateException("SpatialIndex::DiskStorageManager: Corrupted storage manager index file."); |
|---|
| | 195 | |
|---|
| | 196 | m_indexFile.read(reinterpret_cast<char*>(&(e->m_length)), sizeof(size_t)); |
|---|
| | 197 | if (m_indexFile.fail()) |
|---|
| | 198 | throw Tools::IllegalStateException("SpatialIndex::DiskStorageManager: Corrupted storage manager index file."); |
|---|
| 225 | | bytesread = read(m_indexFile, &count2, sizeof(size_t)); |
|---|
| 226 | | if (bytesread != sizeof(size_t)) |
|---|
| 227 | | throw Tools::IllegalStateException("Corrupted storage manager index file."); |
|---|
| 228 | | |
|---|
| 229 | | for (size_t cCount2 = 0; cCount2 < count2; cCount2++) |
|---|
| 230 | | { |
|---|
| 231 | | bytesread = read(m_indexFile, &page, sizeof(id_type)); |
|---|
| 232 | | if (bytesread != sizeof(id_type)) |
|---|
| 233 | | throw Tools::IllegalStateException("Corrupted storage manager index file."); |
|---|
| | 201 | m_indexFile.read(reinterpret_cast<char*>(&count2), sizeof(size_t)); |
|---|
| | 202 | if (m_indexFile.fail()) |
|---|
| | 203 | throw Tools::IllegalStateException("SpatialIndex::DiskStorageManager: Corrupted storage manager index file."); |
|---|
| | 204 | |
|---|
| | 205 | for (size_t cCount2 = 0; cCount2 < count2; ++cCount2) |
|---|
| | 206 | { |
|---|
| | 207 | m_indexFile.read(reinterpret_cast<char*>(&page), sizeof(id_type)); |
|---|
| | 208 | if (m_indexFile.fail()) |
|---|
| | 209 | throw Tools::IllegalStateException("SpatialIndex::DiskStorageManager: Corrupted storage manager index file."); |
|---|
| 259 | | ssize_t byteswritten; |
|---|
| 260 | | |
|---|
| 261 | | off_t seek = lseek(m_indexFile, 0, SEEK_SET); |
|---|
| 262 | | if (seek < 0) throw Tools::IllegalStateException("Corrupted storage manager index file."); |
|---|
| 263 | | |
|---|
| 264 | | byteswritten = write(m_indexFile, &m_pageSize, sizeof(size_t)); |
|---|
| 265 | | if (byteswritten != sizeof(size_t)) throw Tools::IllegalStateException("Corrupted storage manager index file."); |
|---|
| 266 | | byteswritten = write(m_indexFile, &m_nextPage, sizeof(id_type)); |
|---|
| 267 | | if (byteswritten != sizeof(id_type)) |
|---|
| 268 | | throw Tools::IllegalStateException("Corrupted storage manager index file."); |
|---|
| | 230 | m_indexFile.seekp(0, std::ios_base::beg); |
|---|
| | 231 | if (m_indexFile.fail()) |
|---|
| | 232 | throw Tools::IllegalStateException("SpatialIndex::DiskStorageManager: Corrupted storage manager index file."); |
|---|
| | 233 | |
|---|
| | 234 | m_indexFile.write(reinterpret_cast<const char*>(&m_pageSize), sizeof(size_t)); |
|---|
| | 235 | if (m_indexFile.fail()) |
|---|
| | 236 | throw Tools::IllegalStateException("SpatialIndex::DiskStorageManager: Corrupted storage manager index file."); |
|---|
| | 237 | |
|---|
| | 238 | m_indexFile.write(reinterpret_cast<const char*>(&m_nextPage), sizeof(id_type)); |
|---|
| | 239 | if (m_indexFile.fail()) |
|---|
| | 240 | throw Tools::IllegalStateException("SpatialIndex::DiskStorageManager: Corrupted storage manager index file."); |
|---|
| 287 | | byteswritten = write(m_indexFile, &count, sizeof(size_t)); |
|---|
| 288 | | if (byteswritten != sizeof(size_t)) |
|---|
| 289 | | throw Tools::IllegalStateException("Corrupted storage manager index file."); |
|---|
| 290 | | |
|---|
| 291 | | std::map<id_type, Entry*>::iterator it = m_pageIndex.begin(); |
|---|
| 292 | | |
|---|
| 293 | | while (it != m_pageIndex.end()) |
|---|
| | 259 | m_indexFile.write(reinterpret_cast<const char*>(&count), sizeof(size_t)); |
|---|
| | 260 | if (m_indexFile.fail()) |
|---|
| | 261 | throw Tools::IllegalStateException("SpatialIndex::DiskStorageManager: Corrupted storage manager index file."); |
|---|
| | 262 | |
|---|
| | 263 | std::map<id_type, Entry*>::iterator it; |
|---|
| | 264 | |
|---|
| | 265 | for (it = m_pageIndex.begin(); it != m_pageIndex.end(); ++it) |
|---|
| 306 | | byteswritten = write(m_indexFile, &count, sizeof(size_t)); |
|---|
| 307 | | if (byteswritten != sizeof(size_t)) |
|---|
| 308 | | throw Tools::IllegalStateException("Corrupted storage manager index file."); |
|---|
| 309 | | |
|---|
| 310 | | for (size_t cIndex = 0; cIndex < count; cIndex++) |
|---|
| | 278 | m_indexFile.write(reinterpret_cast<const char*>(&count), sizeof(size_t)); |
|---|
| | 279 | if (m_indexFile.fail()) |
|---|
| | 280 | throw Tools::IllegalStateException("SpatialIndex::DiskStorageManager: Corrupted storage manager index file."); |
|---|
| | 281 | |
|---|
| | 282 | for (size_t cIndex = 0; cIndex < count; ++cIndex) |
|---|
| 343 | | off_t seek = lseek(m_dataFile, pages[cNext] * m_pageSize, SEEK_SET); |
|---|
| 344 | | if (seek < 0) throw Tools::IllegalStateException("Corrupted data file."); |
|---|
| 345 | | |
|---|
| 346 | | ssize_t bytesread = read(m_dataFile, m_buffer, m_pageSize); |
|---|
| 347 | | if (bytesread <= 0) throw Tools::IllegalStateException("Corrupted data file."); |
|---|
| | 315 | m_dataFile.seekg(pages[cNext] * m_pageSize, std::ios_base::beg); |
|---|
| | 316 | if (m_dataFile.fail()) |
|---|
| | 317 | throw Tools::IllegalStateException("SpatialIndex::DiskStorageManager: Corrupted data file."); |
|---|
| | 318 | |
|---|
| | 319 | m_dataFile.read(reinterpret_cast<char*>(m_buffer), m_pageSize); |
|---|
| | 320 | if (m_dataFile.fail()) |
|---|
| | 321 | throw Tools::IllegalStateException("SpatialIndex::DiskStorageManager: Corrupted data file."); |
|---|
| 386 | | off_t seek = lseek(m_dataFile, cPage * m_pageSize, SEEK_SET); |
|---|
| 387 | | if (seek < 0) throw Tools::IllegalStateException("Corrupted data file."); |
|---|
| 388 | | ssize_t byteswritten = write(m_dataFile, m_buffer, m_pageSize); |
|---|
| 389 | | if (byteswritten <= 0) throw Tools::IllegalStateException("Corrupted data file."); |
|---|
| | 360 | m_dataFile.seekp(cPage * m_pageSize, std::ios_base::beg); |
|---|
| | 361 | if (m_dataFile.fail()) |
|---|
| | 362 | throw Tools::IllegalStateException("SpatialIndex::DiskStorageManager: Corrupted data file."); |
|---|
| | 363 | |
|---|
| | 364 | m_dataFile.write(reinterpret_cast<const char*>(m_buffer), m_pageSize); |
|---|
| | 365 | if (m_dataFile.fail()) |
|---|
| | 366 | throw Tools::IllegalStateException("SpatialIndex::DiskStorageManager: Corrupted data file."); |
|---|
| 439 | | off_t seek = lseek(m_dataFile, cPage * m_pageSize, SEEK_SET); |
|---|
| 440 | | if (seek < 0) throw Tools::IllegalStateException("Corrupted data file."); |
|---|
| 441 | | ssize_t byteswritten = write(m_dataFile, m_buffer, m_pageSize); |
|---|
| 442 | | if (byteswritten <= 0) throw Tools::IllegalStateException("Corrupted data file."); |
|---|
| | 417 | m_dataFile.seekp(cPage * m_pageSize, std::ios_base::beg); |
|---|
| | 418 | if (m_dataFile.fail()) |
|---|
| | 419 | throw Tools::IllegalStateException("SpatialIndex::DiskStorageManager: Corrupted data file."); |
|---|
| | 420 | |
|---|
| | 421 | m_dataFile.write(reinterpret_cast<const char*>(m_buffer), m_pageSize); |
|---|
| | 422 | if (m_dataFile.fail()) |
|---|
| | 423 | throw Tools::IllegalStateException("SpatialIndex::DiskStorageManager: Corrupted data file."); |
|---|