Skip to content

Commit

Permalink
TransactionLocking improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sanketsarang committed Jun 1, 2019
1 parent 33f848e commit e3b50c4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.blobcity.db.locks;

import com.blobcity.db.exceptions.OperationException;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.scheduling.annotation.Scheduled;
Expand Down Expand Up @@ -56,6 +58,7 @@ public void acquireLock(String app, String table, String pk, LockType lockType)
return value;
});
map.get(key).acquireReadLock();
break;
case WRITE:
map.compute(key, (k, value)-> {
if(value == null) {
Expand All @@ -64,6 +67,7 @@ public void acquireLock(String app, String table, String pk, LockType lockType)
return value;
});
map.get(key).acquireWriteLock();
break;
}
}

Expand Down Expand Up @@ -96,9 +100,6 @@ private String generateKey(String app, String table, String pk) {
@Scheduled(cron = "0 * * * * *")
private void cleanUp() {
final long removeBefore = System.currentTimeMillis() - 30000; //30 seconds
map.entrySet().removeIf(key -> {
ReadWriteSemaphore rws = map.get(key);
return rws.getLockType() == LockType.NONE && rws.getLastOperatedAt() < removeBefore;
});
map.entrySet().removeIf(item -> item.getValue().getLockType() == LockType.NONE && item.getValue().getLastOperatedAt() < removeBefore);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,12 @@ public class BSqlFileManager {
*/
public String select(final String app, final String table, final String key) throws OperationException {
String result = null;
if (LicenseRules.DATA_CACHING && cacheRules.shouldCache(app, table)) {
if (app.equals(".systemdb") || (LicenseRules.DATA_CACHING && cacheRules.shouldCache(app, table))) {
result = dataCache.load(app, table, key);
if (result != null) {
return result;
}
}

transactionLocking.acquireLock(app, table, key, LockType.READ);
try {
Path path = Paths.get(PathUtil.dataFile(app, table, key));
Expand Down Expand Up @@ -332,7 +331,7 @@ public void save(final String app, final String table, final String key, final S
Path path = Paths.get(PathUtil.dataFile(app, table, key));
try {
Files.write(path, jsonString.getBytes("UTF-8"));
if (LicenseRules.DATA_CACHING && cacheRules.shouldCache(app, table)) {
if (app.equals(".systemdb") || (LicenseRules.DATA_CACHING && cacheRules.shouldCache(app, table))) {
dataCache.cache(app, table, key, jsonString);
}
} catch (IOException ex) {
Expand All @@ -354,7 +353,7 @@ public void insert(final String app, final String table, final String key, final
}
try {
Files.write(path, jsonString.getBytes("UTF-8"));
if (LicenseRules.DATA_CACHING && LicenseRules.CACHE_INSERTS && cacheRules.shouldCache(app, table)) {
if (app.equals(".systemdb") || (LicenseRules.DATA_CACHING && LicenseRules.CACHE_INSERTS && cacheRules.shouldCache(app, table))) {
dataCache.cache(app, table, key, jsonString);
}
} catch (IOException ex) {
Expand All @@ -380,7 +379,7 @@ public boolean rename(final String app, final String table, final String existin
if (file.renameTo(newFile)) {

/* Update cache */
if (LicenseRules.DATA_CACHING && cacheRules.shouldCache(app, table)) {
if (app.equals(".systemdb") || (LicenseRules.DATA_CACHING && cacheRules.shouldCache(app, table))) {
String cachedValue = dataCache.load(app, table, existingKey);
dataCache.invalidate(app, table, existingKey);
dataCache.cache(app, table, newKey, cachedValue);
Expand Down

0 comments on commit e3b50c4

Please sign in to comment.