forked from apache/geode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attempt to integrated Greg Green's PR apache#404 (GEODE-2469)
- This work optimizes and simplifies storage of hashes by NOT having a separate region for every hash. Authored-by: Jens Deppe <jdeppe@pivotal.io>
- Loading branch information
1 parent
779e5ce
commit c6303cc
Showing
48 changed files
with
1,916 additions
and
314 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
geode-core/src/main/java/org/apache/geode/redis/internal/AutoCloseableLock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license | ||
* agreements. See the NOTICE file distributed with this work for additional information regarding | ||
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance with the License. You may obtain a | ||
* copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
* | ||
*/ | ||
package org.apache.geode.redis.internal; | ||
|
||
|
||
/** | ||
* Created by gosullivan on 3/10/17. | ||
*/ | ||
public class AutoCloseableLock implements AutoCloseable { | ||
Runnable unlock; | ||
|
||
public AutoCloseableLock(Runnable unlockFunction) { | ||
unlock = unlockFunction; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
this.unlock.run(); | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
geode-core/src/main/java/org/apache/geode/redis/internal/RedisLockService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license | ||
* agreements. See the NOTICE file distributed with this work for additional information regarding | ||
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance with the License. You may obtain a | ||
* copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
* | ||
*/ | ||
package org.apache.geode.redis.internal; | ||
|
||
import java.util.WeakHashMap; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.locks.Lock; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
/** | ||
* | ||
* Locking mechanism to support Redis operations | ||
* | ||
*/ | ||
public class RedisLockService { | ||
|
||
|
||
private static final int DEFAULT_TIMEOUT = 1000; | ||
private final int timeoutMS; | ||
private WeakHashMap<Object, Lock> map = new WeakHashMap<Object, Lock>(); | ||
|
||
|
||
/** | ||
* Construct with the default 1000ms timeout setting | ||
*/ | ||
public RedisLockService() { | ||
this(DEFAULT_TIMEOUT); | ||
} | ||
|
||
/** | ||
* Construct with the timeout setting | ||
* | ||
* @param timeoutMS the default timeout to wait for lock | ||
*/ | ||
public RedisLockService(int timeoutMS) { | ||
this.timeoutMS = timeoutMS; | ||
} | ||
|
||
/** | ||
* Obtain a lock | ||
* | ||
* @param name the lock name/key | ||
* @return true if lock establish prior to timeout | ||
*/ | ||
public synchronized boolean lock(Object name) { | ||
if (name == null) | ||
return false; | ||
|
||
Lock lock = map.get(name); | ||
|
||
if (lock == null) { | ||
lock = new ReentrantLock(); | ||
map.put(name, lock); | ||
} | ||
|
||
try { | ||
return lock.tryLock(timeoutMS, TimeUnit.MILLISECONDS); | ||
} catch (InterruptedException e) { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Release the given lock | ||
* | ||
* @param name the lock name | ||
*/ | ||
public synchronized void unlock(Object name) { | ||
if (name == null) | ||
return; | ||
|
||
Lock lock = map.get(name); | ||
|
||
if (lock == null) { | ||
return; | ||
} | ||
|
||
try { | ||
lock.unlock(); | ||
} finally { | ||
map.remove(name); | ||
} | ||
} | ||
|
||
/** | ||
* Retrieve the managed lock | ||
* | ||
* @param name the name key for lock | ||
* @return the manage lock (null if does not exist) | ||
*/ | ||
Lock getLock(Object name) { | ||
return map.get(name); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.