Skip to content

Commit 2ae0107

Browse files
committed
Made the file-based chache more robust (ref #382).
1 parent c46647e commit 2ae0107

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

Diff for: src/main/java/org/aksw/gerbil/semantic/sameas/impl/cache/FileBasedCachingSameAsRetriever.java

+29-25
Original file line numberDiff line numberDiff line change
@@ -136,34 +136,37 @@ public Set<String> retrieveSameURIs(String uri) {
136136
LOGGER.error("Exception while waiting for read mutex. Returning null.", e);
137137
return null;
138138
}
139-
// Check again that nobody already added the uri
140-
if (uriSetIdMapping.containsKey(uri)) {
141-
// use the cached result
142-
int setId = uriSetIdMapping.get(uri);
143-
if (setId != ENTITY_NOT_FOUND) {
144-
result = sets.get(setId);
145-
} else {
146-
result = null;
147-
}
148-
} else {
149-
if (result != null) {
150-
mergeSetIntoCache(result);
139+
try {
140+
// Check again that nobody already added the uri
141+
if (uriSetIdMapping.containsKey(uri)) {
142+
// use the cached result
143+
int setId = uriSetIdMapping.get(uri);
144+
if (setId != ENTITY_NOT_FOUND) {
145+
result = sets.get(setId);
146+
} else {
147+
result = null;
148+
}
151149
} else {
152-
uriSetIdMapping.put(uri, ENTITY_NOT_FOUND);
153-
}
154-
++cacheChanges;
155-
if ((forceStorageAfterChanges > 0) && (cacheChanges >= forceStorageAfterChanges)) {
156-
LOGGER.info("Storing the cache has been forced...");
157-
try {
158-
performCacheStorage();
159-
} catch (IOException e) {
160-
LOGGER.error("Exception while writing cache to file. Aborting.", e);
150+
if (result != null) {
151+
mergeSetIntoCache(result);
152+
} else {
153+
uriSetIdMapping.put(uri, ENTITY_NOT_FOUND);
154+
}
155+
++cacheChanges;
156+
if ((forceStorageAfterChanges > 0) && (cacheChanges >= forceStorageAfterChanges)) {
157+
LOGGER.info("Storing the cache has been forced...");
158+
try {
159+
performCacheStorage();
160+
} catch (IOException e) {
161+
LOGGER.error("Exception while writing cache to file. Aborting.", e);
162+
}
161163
}
162164
}
165+
} finally {
166+
// The last one will be released at the end
167+
cacheReadMutex.release(MAX_CONCURRENT_READERS - 1);
168+
cacheWriteMutex.release();
163169
}
164-
// The last one will be released at the end
165-
cacheReadMutex.release(MAX_CONCURRENT_READERS - 1);
166-
cacheWriteMutex.release();
167170
}
168171
cacheReadMutex.release();
169172
return result;
@@ -211,8 +214,9 @@ public void storeCache() {
211214
performCacheStorage();
212215
} catch (IOException e) {
213216
LOGGER.error("Exception while writing cache to file. Aborting.", e);
217+
} finally {
218+
cacheWriteMutex.release();
214219
}
215-
cacheWriteMutex.release();
216220
}
217221

218222
private void performCacheStorage() throws IOException {

Diff for: src/main/java/org/aksw/gerbil/semantic/sameas/impl/cache/IndexFileBasedCachingSameAsRetriever.java

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
import com.carrotsearch.hppc.IntOpenHashSet;
3939
import com.carrotsearch.hppc.ObjectIntOpenHashMap;
4040

41+
/**
42+
* @deprecated MR: marked it as deprecated as it is simply a copy of the FileBasedCachineSameAsRetriever.
43+
*/
44+
@Deprecated
4145
public class IndexFileBasedCachingSameAsRetriever extends AbstractSameAsRetrieverDecorator {
4246

4347
private static final Logger LOGGER = LoggerFactory.getLogger(IndexFileBasedCachingSameAsRetriever.class);

0 commit comments

Comments
 (0)