Synchronization improved, deadlocks fixed.
1.1 --- a/mdr/src/org/netbeans/mdr/persistence/btreeimpl/btreestorage/BtreeDatabase.java Wed Sep 22 12:00:21 2004 +0000
1.2 +++ b/mdr/src/org/netbeans/mdr/persistence/btreeimpl/btreestorage/BtreeDatabase.java Wed Sep 22 13:51:08 2004 +0000
1.3 @@ -344,20 +344,22 @@
1.4 * deleting the current repository, and renaming the temporary repository
1.5 * to our name.
1.6 */
1.7 - public synchronized void compress() throws StorageException {
1.8 - if (cache.hasChanges()) {
1.9 - throw new StorageBadRequestException(
1.10 - MessageFormat.format(
1.11 - "There are changes to repository {0} that have not been committed",
1.12 - new Object[] {repositoryName} ));
1.13 - }
1.14 + public void compress() throws StorageException {
1.15 + synchronized (myStorage) {
1.16 + if (cache.hasChanges()) {
1.17 + throw new StorageBadRequestException(
1.18 + MessageFormat.format(
1.19 + "There are changes to repository {0} that have not been committed",
1.20 + new Object[] {repositoryName} ));
1.21 + }
1.22
1.23 - String tempName = "tmp" + (new Random().nextInt());
1.24 - copy(tempName);
1.25 - closeFiles();
1.26 - delete(repositoryName);
1.27 - rename(tempName, repositoryName);
1.28 - open(false);
1.29 + String tempName = "tmp" + (new Random().nextInt());
1.30 + copy(tempName);
1.31 + closeFiles();
1.32 + delete(repositoryName);
1.33 + rename(tempName, repositoryName);
1.34 + open(false);
1.35 + }
1.36 }
1.37
1.38 /** Rebuild the index file. Since we iterate through the data file
1.39 @@ -378,8 +380,10 @@
1.40 /** returns the number of objects in the repository
1.41 *
1.42 */
1.43 - public synchronized int size() {
1.44 - return dataFile.size() + cache.numberNew() - cache.numberDeleted();
1.45 + public int size() {
1.46 + synchronized (myStorage) {
1.47 + return dataFile.size() + cache.numberNew() - cache.numberDeleted();
1.48 + }
1.49 }
1.50
1.51 /** Returns the unique name of the index in the Storage.
1.52 @@ -405,15 +409,17 @@
1.53
1.54 /** Close repository
1.55 */
1.56 - synchronized void close() throws StorageException {
1.57 - closeFiles();
1.58 - cache = null;
1.59 - transactionCache = null;
1.60 + void close() throws StorageException {
1.61 + synchronized (myStorage) {
1.62 + closeFiles();
1.63 + cache = null;
1.64 + transactionCache = null;
1.65 + }
1.66 }
1.67
1.68 /** Close all files
1.69 */
1.70 - synchronized void closeFiles() throws StorageException {
1.71 + private void closeFiles() throws StorageException {
1.72 modificationLevel++;
1.73 fileCache.abort();
1.74 fileCache = null;
1.75 @@ -431,40 +437,45 @@
1.76 /** Commits changes to transaction cache,
1.77 * if cache treshold is reached, flushes cache to disk.
1.78 */
1.79 - public synchronized void commitChanges() throws StorageException {
1.80 - save(false);
1.81 -
1.82 - if (transactionCache.tresholdReached ()) {
1.83 - try {
1.84 - //System.err.println("Threshhold reached!");
1.85 - fileCache.commit();
1.86 - transactionCache.clear ();
1.87 - } catch (StorageException ex) {
1.88 - saveFailed = true;
1.89 - throw(ex);
1.90 - } // catch
1.91 - } else {
1.92 - transactionCache.commit ();
1.93 + public void commitChanges() throws StorageException {
1.94 + synchronized (myStorage) {
1.95 + save(false);
1.96 +
1.97 + if (transactionCache.tresholdReached ()) {
1.98 + try {
1.99 + //System.err.println("Threshhold reached!");
1.100 + fileCache.commit();
1.101 + transactionCache.clear ();
1.102 + } catch (StorageException ex) {
1.103 + saveFailed = true;
1.104 + throw(ex);
1.105 + } // catch
1.106 + } else {
1.107 + transactionCache.commit ();
1.108 + }
1.109 }
1.110 -
1.111 }
1.112
1.113 /**
1.114 * Called on exit, commited data cached in transaction cache need to be written to disk.
1.115 */
1.116 - public synchronized void shutDown() throws StorageException {
1.117 - try {
1.118 - fileCache.commit();
1.119 - } catch (StorageException ex) {
1.120 - saveFailed = true;
1.121 - throw(ex);
1.122 - } // catch
1.123 + public void shutDown() throws StorageException {
1.124 + synchronized (myStorage) {
1.125 + try {
1.126 + fileCache.commit();
1.127 + } catch (StorageException ex) {
1.128 + saveFailed = true;
1.129 + throw(ex);
1.130 + } // catch
1.131 + }
1.132 }
1.133
1.134 /** save all changes to disk without comitting
1.135 */
1.136 - public synchronized void saveChanges() throws StorageException {
1.137 - save(false);
1.138 + public void saveChanges() throws StorageException {
1.139 + synchronized (myStorage) {
1.140 + save(false);
1.141 + }
1.142 }
1.143
1.144 /* save all changes to disk, optionally comitting */
1.145 @@ -556,57 +567,59 @@
1.146
1.147 /** roll back all changes
1.148 */
1.149 - public synchronized void rollbackChanges() throws StorageException {
1.150 - modificationLevel++;
1.151 - Integer offset;
1.152 - int dataOffset;
1.153 -
1.154 - // iterator have to be obtained before closeFiles is called (it drops transitionCache)
1.155 - TransactionCache.CacheIterator iter = transactionCache.iterator ();
1.156 - long counter = dataFile.getMofIdCounter ();
1.157 - boolean dataCommited = transactionCache.containsCommitedData ();
1.158 -
1.159 - // throw away data in file cache
1.160 - close();
1.161 - open(false);
1.162 -
1.163 - // redo operations commited to transaction cache
1.164 - try {
1.165 - while (iter.hasNext ()) {
1.166 - TransactionCache.Record rec = iter.next ();
1.167 - switch (rec.op) {
1.168 - case TransactionCache.OP_DELETE:
1.169 - removeRecord(rec.id);
1.170 - break;
1.171 - case TransactionCache.OP_REPLACE:
1.172 - offset = (Integer)indexFile.get(rec.id);
1.173 - dataOffset = dataFile.replace(
1.174 - offset.intValue(), rec.id.getSerialNumber(), rec.value);
1.175 - indexFile.replace(rec.id, new Integer(dataOffset));
1.176 - break;
1.177 - case TransactionCache.OP_INSERT:
1.178 - dataOffset = dataFile.put(rec.id.getSerialNumber(), rec.value);
1.179 - indexFile.add(rec.id, new Integer(dataOffset));
1.180 - break;
1.181 - } // switch
1.182 -
1.183 - } // while
1.184 -
1.185 - // reset mof id generator to the value before rollback
1.186 - dataFile.setMofIdCounter (counter);
1.187 - fileCache.commit();
1.188 - } catch (StorageException ex) {
1.189 - // Record that commit has failed.
1.190 - saveFailed = true;
1.191 - throw(ex);
1.192 - }
1.193 + public void rollbackChanges() throws StorageException {
1.194 + synchronized (myStorage) {
1.195 + modificationLevel++;
1.196 + Integer offset;
1.197 + int dataOffset;
1.198 +
1.199 + // iterator have to be obtained before closeFiles is called (it drops transitionCache)
1.200 + TransactionCache.CacheIterator iter = transactionCache.iterator ();
1.201 + long counter = dataFile.getMofIdCounter ();
1.202 + boolean dataCommited = transactionCache.containsCommitedData ();
1.203 +
1.204 + // throw away data in file cache
1.205 + close();
1.206 + open(false);
1.207 +
1.208 + // redo operations commited to transaction cache
1.209 + try {
1.210 + while (iter.hasNext ()) {
1.211 + TransactionCache.Record rec = iter.next ();
1.212 + switch (rec.op) {
1.213 + case TransactionCache.OP_DELETE:
1.214 + removeRecord(rec.id);
1.215 + break;
1.216 + case TransactionCache.OP_REPLACE:
1.217 + offset = (Integer)indexFile.get(rec.id);
1.218 + dataOffset = dataFile.replace(
1.219 + offset.intValue(), rec.id.getSerialNumber(), rec.value);
1.220 + indexFile.replace(rec.id, new Integer(dataOffset));
1.221 + break;
1.222 + case TransactionCache.OP_INSERT:
1.223 + dataOffset = dataFile.put(rec.id.getSerialNumber(), rec.value);
1.224 + indexFile.add(rec.id, new Integer(dataOffset));
1.225 + break;
1.226 + } // switch
1.227 +
1.228 + } // while
1.229 +
1.230 + // reset mof id generator to the value before rollback
1.231 + dataFile.setMofIdCounter (counter);
1.232 + fileCache.commit();
1.233 + } catch (StorageException ex) {
1.234 + // Record that commit has failed.
1.235 + saveFailed = true;
1.236 + throw(ex);
1.237 + }
1.238 + }
1.239 }
1.240
1.241 /** Deletes a repository record
1.242 * @param key -- a String
1.243 * @return true if record exists
1.244 */
1.245 - public synchronized boolean remove(Object key) throws StorageException {
1.246 + public boolean remove(Object key) throws StorageException {
1.247 return remove(makeMOFID(key));
1.248 }
1.249
1.250 @@ -614,14 +627,16 @@
1.251 * @param mKey
1.252 * @return true if record exists
1.253 */
1.254 - public synchronized boolean remove(MOFID mKey) throws StorageException {
1.255 - modificationLevel++;
1.256 - if (!exists(mKey)) {
1.257 - return false;
1.258 - }
1.259 - else {
1.260 - cache.remove(mKey);
1.261 - return true;
1.262 + public boolean remove(MOFID mKey) throws StorageException {
1.263 + synchronized (myStorage) {
1.264 + modificationLevel++;
1.265 + if (!exists(mKey)) {
1.266 + return false;
1.267 + }
1.268 + else {
1.269 + cache.remove(mKey);
1.270 + return true;
1.271 + }
1.272 }
1.273 }
1.274
1.275 @@ -629,7 +644,7 @@
1.276 * @param key -- a String
1.277 * @param value
1.278 */
1.279 - public synchronized void add(Object key, Object value) throws StorageException {
1.280 + public void add(Object key, Object value) throws StorageException {
1.281 add(makeMOFID(key), value);
1.282 }
1.283
1.284 @@ -637,14 +652,16 @@
1.285 * @param mKey
1.286 * @param value
1.287 */
1.288 - public synchronized void add(MOFID mKey, Object value) throws StorageException {
1.289 - modificationLevel++;
1.290 - if (exists(mKey)) {
1.291 - throw new StorageBadRequestException(
1.292 - MessageFormat.format("Record with key {0} already exists",
1.293 - new Object[] {mKey} ) );
1.294 + public void add(MOFID mKey, Object value) throws StorageException {
1.295 + synchronized (myStorage) {
1.296 + modificationLevel++;
1.297 + if (exists(mKey)) {
1.298 + throw new StorageBadRequestException(
1.299 + MessageFormat.format("Record with key {0} already exists",
1.300 + new Object[] {mKey} ) );
1.301 + }
1.302 + addToCache(mKey, value);
1.303 }
1.304 - addToCache(mKey, value);
1.305 }
1.306
1.307 /* Add object to cache */
1.308 @@ -660,7 +677,7 @@
1.309 * @param value
1.310 * @throws StorageException
1.311 */
1.312 - public synchronized void replace(Object key, Object value) throws StorageException {
1.313 + public void replace(Object key, Object value) throws StorageException {
1.314 replace(makeMOFID(key), value);
1.315 }
1.316
1.317 @@ -671,13 +688,15 @@
1.318 * @param value
1.319 * @throws StorageException
1.320 */
1.321 - public synchronized void replace(MOFID mKey, Object value) throws StorageException {
1.322 - modificationLevel++;
1.323 - if (!exists(mKey)) {
1.324 - noSuchRecord(mKey);
1.325 + public void replace(MOFID mKey, Object value) throws StorageException {
1.326 + synchronized (myStorage) {
1.327 + modificationLevel++;
1.328 + if (!exists(mKey)) {
1.329 + noSuchRecord(mKey);
1.330 + }
1.331 +
1.332 + replaceInCache(mKey, value);
1.333 }
1.334 -
1.335 - replaceInCache(mKey, value);
1.336 }
1.337
1.338 /* Replace object in cache */
1.339 @@ -695,17 +714,19 @@
1.340 * @param value
1.341 * @return always null
1.342 */
1.343 - public synchronized boolean put(Object key, Object value) throws StorageException {
1.344 - modificationLevel++;
1.345 - MOFID mKey = makeMOFID(key);
1.346 + public boolean put(Object key, Object value) throws StorageException {
1.347 + synchronized (myStorage) {
1.348 + modificationLevel++;
1.349 + MOFID mKey = makeMOFID(key);
1.350
1.351 - if (!exists(mKey)) {
1.352 - addToCache(mKey, value);
1.353 - return false;
1.354 - }
1.355 - else {
1.356 - replaceInCache(mKey, value);
1.357 - return true;
1.358 + if (!exists(mKey)) {
1.359 + addToCache(mKey, value);
1.360 + return false;
1.361 + }
1.362 + else {
1.363 + replaceInCache(mKey, value);
1.364 + return true;
1.365 + }
1.366 }
1.367 }
1.368
1.369 @@ -714,12 +735,12 @@
1.370 * @return value associated with specified key,
1.371 * or null if there was no mapping for key
1.372 */
1.373 - public synchronized Object getIfExists(Object key)throws StorageException {
1.374 + public Object getIfExists(Object key)throws StorageException {
1.375 return getIfExists(makeMOFID(key));
1.376 }
1.377
1.378 /** Like getIfExists, since we don't return keys */
1.379 - public synchronized Object getObjectIfExists(Object key, SinglevaluedIndex dummy) throws StorageException {
1.380 + public Object getObjectIfExists(Object key, SinglevaluedIndex dummy) throws StorageException {
1.381 return getIfExists(key);
1.382 }
1.383
1.384 @@ -728,20 +749,22 @@
1.385 * @return value associated with specified key,
1.386 * or null if there was no mapping for key
1.387 */
1.388 - public synchronized Object getIfExists(MOFID mKey)throws StorageException {
1.389 - Object retval;
1.390 - retval = cache.get(mKey);
1.391 - if (retval == null) {
1.392 - if (cache.isDeleted(mKey)) {
1.393 - return null;
1.394 + public Object getIfExists(MOFID mKey)throws StorageException {
1.395 + synchronized (myStorage) {
1.396 + Object retval;
1.397 + retval = cache.get(mKey);
1.398 + if (retval == null) {
1.399 + if (cache.isDeleted(mKey)) {
1.400 + return null;
1.401 + }
1.402 + retval = getRecord(mKey);
1.403 + if (retval != null) {
1.404 + cache.put(mKey, retval);
1.405 + }
1.406 }
1.407 - retval = getRecord(mKey);
1.408 - if (retval != null) {
1.409 - cache.put(mKey, retval);
1.410 - }
1.411 +
1.412 + return retval;
1.413 }
1.414 -
1.415 - return retval;
1.416 }
1.417
1.418 /** Gets a record from the repository. Throws an exception if none exists.
1.419 @@ -749,17 +772,17 @@
1.420 * @return value associated with specified key,
1.421 * or null if there was no mapping for key
1.422 */
1.423 - public synchronized Object get(Object key)throws StorageException {
1.424 + public Object get(Object key)throws StorageException {
1.425 Object retval = getIfExists(key);
1.426 if (retval == null) {
1.427 - noSuchRecord(key);
1.428 + noSuchRecord(key);
1.429 }
1.430
1.431 return retval;
1.432 }
1.433
1.434 /** Like get, since we don't return keys */
1.435 - public synchronized Object getObject(Object key, SinglevaluedIndex dummy)
1.436 + public Object getObject(Object key, SinglevaluedIndex dummy)
1.437 throws StorageException {
1.438 return get(key);
1.439 }
1.440 @@ -769,16 +792,16 @@
1.441 * @return value associated with specified key,
1.442 * or null if there was no mapping for key
1.443 */
1.444 - public synchronized Object get(MOFID mKey)throws StorageException {
1.445 + public Object get(MOFID mKey)throws StorageException {
1.446 Object retval = getIfExists(mKey);
1.447 if (retval == null) {
1.448 - noSuchRecord(mKey);
1.449 + noSuchRecord(mKey);
1.450 }
1.451
1.452 return retval;
1.453 }
1.454
1.455 - public synchronized Collection queryByKeyPrefix (Object prefix, SinglevaluedIndex repos) {
1.456 + public Collection queryByKeyPrefix (Object prefix, SinglevaluedIndex repos) {
1.457 throw new UnsupportedOperationException ();
1.458 }
1.459
1.460 @@ -814,31 +837,33 @@
1.461 /** Mark that the object has changed, and so needs to be saved on commit
1.462 * @param key key of object whch changed (a String)
1.463 */
1.464 - public synchronized void objectStateChanged(Object key) throws StorageException {
1.465 + public void objectStateChanged(Object key) throws StorageException {
1.466 objectStateChanged(makeMOFID(key));
1.467 }
1.468
1.469 /** Mark that the object has changed, and so needs to be saved on commit
1.470 * @param mKey key of object whch changed
1.471 */
1.472 - public synchronized void objectStateChanged(MOFID mKey) throws StorageException {
1.473 - modificationLevel++;
1.474 - cache.setDirty(mKey);
1.475 + public void objectStateChanged(MOFID mKey) throws StorageException {
1.476 + synchronized (myStorage) {
1.477 + modificationLevel++;
1.478 + cache.setDirty(mKey);
1.479 + }
1.480 }
1.481
1.482 - /** Fetch an index by name
1.483 + /** Fetch an index by name. Must be called from a block synchronized on myStorage.
1.484 * @param name name of index
1.485 */
1.486 - synchronized Object fetchIndex(String name) throws StorageException {
1.487 + Object fetchIndex(String name) throws StorageException {
1.488 fetchIndexIndex();
1.489 MOFID indexID = indexIndex.get(name);
1.490 return (indexID == null) ? null : get(indexID);
1.491 }
1.492
1.493 - /** drop an index by name
1.494 + /** Drop an index by name. Must be called from a block synchronized on myStorage.
1.495 * @param name name of index
1.496 */
1.497 - synchronized void dropIndex(String name) throws StorageException {
1.498 + void dropIndex(String name) throws StorageException {
1.499 modificationLevel++;
1.500 fetchIndexIndex();
1.501 MOFID indexId = indexIndex.get(name);
1.502 @@ -850,11 +875,13 @@
1.503 /** List all index names
1.504 * @return an array of all the index names in alphabetical order
1.505 */
1.506 - public synchronized String [] listIndexes() throws StorageException {
1.507 - fetchIndexIndex();
1.508 - String retval[] = indexIndex.listNames();
1.509 - Arrays.sort(retval);
1.510 - return retval;
1.511 + public String [] listIndexes() throws StorageException {
1.512 + synchronized (myStorage) {
1.513 + fetchIndexIndex();
1.514 + String retval[] = indexIndex.listNames();
1.515 + Arrays.sort(retval);
1.516 + return retval;
1.517 + }
1.518 }
1.519
1.520 /** Add a new index
1.521 @@ -862,7 +889,7 @@
1.522 * @param index the index object
1.523 * @param mID the MOFID of the index
1.524 */
1.525 - synchronized void addIndex(String name, Object index, MOFID mID) throws StorageException {
1.526 + void addIndex(String name, Object index, MOFID mID) throws StorageException {
1.527 modificationLevel++;
1.528 add(mID, index);
1.529 fetchIndexIndex();
1.530 @@ -1064,108 +1091,116 @@
1.531 }
1.532
1.533 /** Set our logging stream */
1.534 - public synchronized void setLoggingStream(PrintStream strm) {
1.535 - loggingStream = strm;
1.536 + public void setLoggingStream(PrintStream strm) {
1.537 + synchronized (myStorage) {
1.538 + loggingStream = strm;
1.539 + }
1.540 }
1.541
1.542 /** Return the MOFID generator for this repository */
1.543 - public synchronized MofidGenerator getMofidGenerator() {
1.544 - return dataFile;
1.545 + public MofidGenerator getMofidGenerator() {
1.546 + synchronized (myStorage) {
1.547 + return dataFile;
1.548 + }
1.549 }
1.550
1.551 private Map mofidMap = null;
1.552
1.553 /** Return the map of MOFID UUIDs we know about
1.554 */
1.555 - public synchronized Map getMofidMap() {
1.556 - // When we federae repositories, we'll need to know about all
1.557 - // of the federates. For now, there's only us.
1.558 - if (mofidMap == null) {
1.559 - mofidMap = new HashMap();
1.560 - mofidMap.put(dataFile.getMofidPrefix(), dataFile);
1.561 - }
1.562 - return mofidMap;
1.563 + public Map getMofidMap() {
1.564 + synchronized (myStorage) {
1.565 + // When we federae repositories, we'll need to know about all
1.566 + // of the federates. For now, there's only us.
1.567 + if (mofidMap == null) {
1.568 + mofidMap = new HashMap();
1.569 + mofidMap.put(dataFile.getMofidPrefix(), dataFile);
1.570 + }
1.571 + return mofidMap;
1.572 + }
1.573 }
1.574
1.575 /** Check consistency of btree database
1.576 * @param strm where to write inconsistencies
1.577 * @return number of errors encountered
1.578 */
1.579 - public synchronized int checkConsistency(PrintWriter strm)
1.580 + public int checkConsistency(PrintWriter strm)
1.581 throws StorageException {
1.582
1.583 - /* Check consistency of data file */
1.584 - int numErrs =
1.585 - dataFile.dump(BtreeDataFile.DUMP_CONSISTENTCY_INFO, 0, false, strm);
1.586 + synchronized (myStorage) {
1.587 + /* Check consistency of data file */
1.588 + int numErrs =
1.589 + dataFile.dump(BtreeDataFile.DUMP_CONSISTENTCY_INFO, 0, false, strm);
1.590
1.591 - /* Check that each key in the data file is properly in the index file */
1.592 - Iterator recordIter = dataFile.iterator(
1.593 - BtreeDataFile.ITERATE_NORMAL_EXTENTS);
1.594 + /* Check that each key in the data file is properly in the index file */
1.595 + Iterator recordIter = dataFile.iterator(
1.596 + BtreeDataFile.ITERATE_NORMAL_EXTENTS);
1.597
1.598 - while (recordIter.hasNext()) {
1.599 - NormalBtreeExtent ext = (NormalBtreeExtent)recordIter.next();
1.600 - MOFID mKey = this.myStorage.readMOFIDData (new ByteArrayInputStream (ext.key));
1.601 - Integer offset = (Integer)indexFile.getIfExists(mKey);
1.602 - if (offset == null) {
1.603 - strm.println("ID " + mKey + " is not in the index file.");
1.604 - numErrs++;
1.605 - } else if (offset.intValue() != ext.myChunkNum) {
1.606 - strm.println("ID " + mKey + " has differring offsets: " +
1.607 - ext.myChunkNum + " and " + offset.intValue());
1.608 - numErrs++;
1.609 - }
1.610 - }
1.611 + while (recordIter.hasNext()) {
1.612 + NormalBtreeExtent ext = (NormalBtreeExtent)recordIter.next();
1.613 + MOFID mKey = this.myStorage.readMOFIDData (new ByteArrayInputStream (ext.key));
1.614 + Integer offset = (Integer)indexFile.getIfExists(mKey);
1.615 + if (offset == null) {
1.616 + strm.println("ID " + mKey + " is not in the index file.");
1.617 + numErrs++;
1.618 + } else if (offset.intValue() != ext.myChunkNum) {
1.619 + strm.println("ID " + mKey + " has differring offsets: " +
1.620 + ext.myChunkNum + " and " + offset.intValue());
1.621 + numErrs++;
1.622 + }
1.623 + }
1.624
1.625 - /* Check that cache is consistent with the files */
1.626 - Iterator actIter = cache.iterateActive();
1.627 - while (actIter.hasNext()) {
1.628 - MOFID key = (MOFID)actIter.next();
1.629 - if (cache.isDeleted(key)) {
1.630 - if (!cache.isNew(key)) {
1.631 - strm.println(
1.632 - "ID " + key + " is deleted and active but not new");
1.633 - numErrs++;
1.634 - }
1.635 - }
1.636 - else {
1.637 - if (cache.isNew(key)) {
1.638 - if (inIndexFile(key)) {
1.639 - strm.println(
1.640 - "ID " + key + " is new but in the index file");
1.641 - numErrs++;
1.642 - }
1.643 - }
1.644 - else {
1.645 - if (!inIndexFile(key)) {
1.646 - strm.println(
1.647 - "ID " + key +
1.648 - " exists but is not in the index file");
1.649 - numErrs++;
1.650 - }
1.651 - }
1.652 - }
1.653 - }
1.654 -
1.655 - Iterator delIter = cache.iterateDeleted();
1.656 - while (delIter.hasNext()) {
1.657 - MOFID key = (MOFID)delIter.next();
1.658 - if (!inIndexFile(key)) {
1.659 - strm.println(
1.660 - "ID " + key + " is deleted but not in the index file");
1.661 - numErrs++;
1.662 - }
1.663 - }
1.664 + /* Check that cache is consistent with the files */
1.665 + Iterator actIter = cache.iterateActive();
1.666 + while (actIter.hasNext()) {
1.667 + MOFID key = (MOFID)actIter.next();
1.668 + if (cache.isDeleted(key)) {
1.669 + if (!cache.isNew(key)) {
1.670 + strm.println(
1.671 + "ID " + key + " is deleted and active but not new");
1.672 + numErrs++;
1.673 + }
1.674 + }
1.675 + else {
1.676 + if (cache.isNew(key)) {
1.677 + if (inIndexFile(key)) {
1.678 + strm.println(
1.679 + "ID " + key + " is new but in the index file");
1.680 + numErrs++;
1.681 + }
1.682 + }
1.683 + else {
1.684 + if (!inIndexFile(key)) {
1.685 + strm.println(
1.686 + "ID " + key +
1.687 + " exists but is not in the index file");
1.688 + numErrs++;
1.689 + }
1.690 + }
1.691 + }
1.692 + }
1.693
1.694 - numErrs += ((Btree)indexFile).consistencyCheck(strm);
1.695 - strm.println("" + numErrs + " error(s) detected.");
1.696 - strm.println();
1.697 - cache.showStats(strm);
1.698 - strm.println();
1.699 - fileCache.showStats(strm);
1.700 - strm.println();
1.701 - strm.flush();
1.702 + Iterator delIter = cache.iterateDeleted();
1.703 + while (delIter.hasNext()) {
1.704 + MOFID key = (MOFID)delIter.next();
1.705 + if (!inIndexFile(key)) {
1.706 + strm.println(
1.707 + "ID " + key + " is deleted but not in the index file");
1.708 + numErrs++;
1.709 + }
1.710 + }
1.711
1.712 - return numErrs;
1.713 + numErrs += ((Btree)indexFile).consistencyCheck(strm);
1.714 + strm.println("" + numErrs + " error(s) detected.");
1.715 + strm.println();
1.716 + cache.showStats(strm);
1.717 + strm.println();
1.718 + fileCache.showStats(strm);
1.719 + strm.println();
1.720 + strm.flush();
1.721 +
1.722 + return numErrs;
1.723 + }
1.724 }
1.725
1.726
1.727 @@ -1213,7 +1248,7 @@
1.728 private void getNextKey() {
1.729 nextKey = null;
1.730
1.731 - synchronized(BtreeDatabase.this) {
1.732 + synchronized(myStorage) {
1.733 checkModLevel();
1.734 while (fileIter.hasNext()) {
1.735 MOFID fileKey = (MOFID)fileIter.next();
1.736 @@ -1266,7 +1301,7 @@
1.737 private Iterator keyIter;
1.738
1.739 ValueIterator() {
1.740 - synchronized (BtreeDatabase.this) {
1.741 + synchronized (myStorage) {
1.742 keyIter = new KeyIterator(true);
1.743 }
1.744 }
1.745 @@ -1312,7 +1347,7 @@
1.746 * @return iterator
1.747 */
1.748 public Iterator iterator() {
1.749 - synchronized (BtreeDatabase.this) {
1.750 + synchronized (myStorage) {
1.751 return new KeyIterator(false);
1.752 }
1.753 }