Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add generic parameter for RowLock #42

Merged
merged 1 commit into from
Dec 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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