Skip to content

Commit

Permalink
add generic parameter for RowLock (#42)
Browse files Browse the repository at this point in the history
Change-Id: I309b89846ca15d58ea39095d406a0b0b5de510e7
  • Loading branch information
javeme authored and zhoney committed Dec 31, 2019
1 parent d78b728 commit 8268b75
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
9 changes: 6 additions & 3 deletions src/main/java/com/baidu/hugegraph/concurrent/LockGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,14 @@ public KeyLock keyLock(String lockName, int size) {
return (KeyLock) this.locksMap.get(lockName);
}

public RowLock rowLock(String lockName) {
public <K extends Comparable<K>> RowLock<K> rowLock(String lockName) {
if (!this.locksMap.containsKey(lockName)) {
this.locksMap.putIfAbsent(lockName, new RowLock());
this.locksMap.putIfAbsent(lockName, new RowLock<>());
}
return (RowLock) this.locksMap.get(lockName);
Object value = this.locksMap.get(lockName);
@SuppressWarnings("unchecked")
RowLock<K> lock = (RowLock<K>) value;
return lock;
}

public String name() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@

import com.baidu.hugegraph.concurrent.AtomicLock;
import com.baidu.hugegraph.concurrent.KeyLock;
import com.baidu.hugegraph.concurrent.RowLock;
import com.baidu.hugegraph.concurrent.LockGroup;
import com.baidu.hugegraph.concurrent.RowLock;
import com.baidu.hugegraph.testutil.Assert;

public class LockGroupTest {
Expand Down Expand Up @@ -80,9 +80,9 @@ public void testKeyLockWithSize() {

@Test
public void testRowLock() {
RowLock lock = this.group.rowLock("lock");
RowLock<?> lock = this.group.rowLock("lock");
Assert.assertNotNull(lock);
RowLock lock1 = this.group.rowLock("lock");
RowLock<?> lock1 = this.group.rowLock("lock");
Assert.assertSame(lock, lock1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ public void testRowLockMultiRows() {
lock.unlockAll(ImmutableSet.of(1, 2, 3));
}

@SuppressWarnings("unchecked")
@Test
public void testRowLockWithMultiThreads() {
RowLock lock = new RowLock();
RowLock<Integer> lock = new RowLock<>();
Set<String> names = new HashSet<>(THREADS_NUM);
List<Integer> keys = new ArrayList<>(5);
Random random = new Random();
Expand All @@ -84,10 +83,9 @@ public void testRowLockWithMultiThreads() {
Assert.assertEquals(THREADS_NUM, names.size());
}

@SuppressWarnings("unchecked")
@Test
public void testRowLockWithMultiThreadsWithRandomKey() {
RowLock lock = new RowLock();
RowLock<Integer> lock = new RowLock<>();
Set<String> names = new HashSet<>(THREADS_NUM);

Assert.assertEquals(0, names.size());
Expand Down

0 comments on commit 8268b75

Please sign in to comment.