diff options
Diffstat (limited to 'src/main/java/org/onap/music/datastore/CassaLockStore.java')
-rw-r--r-- | src/main/java/org/onap/music/datastore/CassaLockStore.java | 61 |
1 files changed, 56 insertions, 5 deletions
diff --git a/src/main/java/org/onap/music/datastore/CassaLockStore.java b/src/main/java/org/onap/music/datastore/CassaLockStore.java index c1bf4784..100d93bc 100644 --- a/src/main/java/org/onap/music/datastore/CassaLockStore.java +++ b/src/main/java/org/onap/music/datastore/CassaLockStore.java @@ -1,5 +1,6 @@ package org.onap.music.datastore; +import java.util.ArrayList; import java.util.List; import org.onap.music.eelf.logging.EELFLoggerDelegate; @@ -16,6 +17,7 @@ import com.datastax.driver.core.Row; public class CassaLockStore { private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CassaLockStore.class); + private static String table_prepend_name = "lockQ_"; public class LockObject{ public String lockRef; @@ -50,7 +52,7 @@ public class CassaLockStore { public boolean createLockQueue(String keyspace, String table) throws MusicServiceException, MusicQueryException { logger.info(EELFLoggerDelegate.applicationLogger, "Create lock queue/table for " + keyspace+"."+table); - table = "lockQ_"+table; + table = table_prepend_name+table; String tabQuery = "CREATE TABLE IF NOT EXISTS "+keyspace+"."+table + " ( key text, lockReference bigint, createTime text, acquireTime text, guard bigint static, PRIMARY KEY ((key), lockReference) ) " + "WITH CLUSTERING ORDER BY (lockReference ASC);"; @@ -74,7 +76,7 @@ public class CassaLockStore { public String genLockRefandEnQueue(String keyspace, String table, String lockName) throws MusicServiceException, MusicQueryException { logger.info(EELFLoggerDelegate.applicationLogger, "Create lock reference for " + keyspace + "." + table + "." + lockName); - table = "lockQ_" + table; + table = table_prepend_name+table; PreparedQueryObject queryObject = new PreparedQueryObject(); @@ -119,6 +121,55 @@ public class CassaLockStore { return String.valueOf(lockRef); } + /** + * Returns a result set containing the list of clients waiting for a particular lock + * @param keyspace + * @param table + * @param key + * @return list of lockrefs in the queue + * @throws MusicServiceException + * @throws MusicQueryException + */ + public List<String> getLockQueue(String keyspace, String table, String key) + throws MusicServiceException, MusicQueryException { + logger.info(EELFLoggerDelegate.applicationLogger, + "Getting the queue for " + keyspace+"."+table+"."+key); + table = table_prepend_name+table; + String selectQuery = "select * from " + keyspace + "." + table + " where key='" + key + "';"; + PreparedQueryObject queryObject = new PreparedQueryObject(); + queryObject.appendQueryString(selectQuery); + ResultSet rs = dsHandle.executeOneConsistencyGet(queryObject); + ArrayList<String> lockQueue = new ArrayList<>(); + for (Row row: rs) { + lockQueue.add(Long.toString(row.getLong("lockReference"))); + } + return lockQueue; + } + + + /** + * Returns a result set containing the list of clients waiting for a particular lock + * @param keyspace + * @param table + * @param key + * @return size of lockrefs queue + * @throws MusicServiceException + * @throws MusicQueryException + */ + public long getLockQueueSize(String keyspace, String table, String key) + throws MusicServiceException, MusicQueryException { + logger.info(EELFLoggerDelegate.applicationLogger, + "Getting the queue size for " + keyspace+"."+table+"."+key); + table = table_prepend_name+table; + String selectQuery = "select count(*) from " + keyspace + "." + table + " where key='" + key + "';"; + PreparedQueryObject queryObject = new PreparedQueryObject(); + queryObject.appendQueryString(selectQuery); + ResultSet rs = dsHandle.executeOneConsistencyGet(queryObject); + System.err.println(rs); + + return rs.one().getLong("count"); + } + /** * This method returns the top of lock table/queue for the key. @@ -132,7 +183,7 @@ public class CassaLockStore { public LockObject peekLockQueue(String keyspace, String table, String key) throws MusicServiceException, MusicQueryException{ logger.info(EELFLoggerDelegate.applicationLogger, "Peek in lock table for " + keyspace+"."+table+"."+key); - table = "lockQ_"+table; + table = table_prepend_name+table; String selectQuery = "select * from "+keyspace+"."+table+" where key='"+key+"' LIMIT 1;"; PreparedQueryObject queryObject = new PreparedQueryObject(); queryObject.appendQueryString(selectQuery); @@ -156,7 +207,7 @@ public class CassaLockStore { * @throws MusicQueryException */ public void deQueueLockRef(String keyspace, String table, String key, String lockReference) throws MusicServiceException, MusicQueryException{ - table = "lockQ_"+table; + table = table_prepend_name+table; PreparedQueryObject queryObject = new PreparedQueryObject(); Long lockReferenceL = Long.parseLong(lockReference); String deleteQuery = "delete from "+keyspace+"."+table+" where key='"+key+"' AND lockReference ="+lockReferenceL+" IF EXISTS;"; @@ -166,7 +217,7 @@ public class CassaLockStore { public void updateLockAcquireTime(String keyspace, String table, String key, String lockReference) throws MusicServiceException, MusicQueryException{ - table = "lockQ_"+table; + table = table_prepend_name+table; PreparedQueryObject queryObject = new PreparedQueryObject(); Long lockReferenceL = Long.parseLong(lockReference); String updateQuery = "update "+keyspace+"."+table+" set acquireTime='"+ System.currentTimeMillis()+"' where key='"+key+"' AND lockReference = "+lockReferenceL+" IF EXISTS;"; |