-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENHANCE: Modify decoding logic in collection get api.
- Loading branch information
Showing
6 changed files
with
226 additions
and
25 deletions.
There are no files selected for viewing
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
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
180 changes: 180 additions & 0 deletions
180
src/main/java/net/spy/memcached/internal/CollectionGetFuture.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,180 @@ | ||
package net.spy.memcached.internal; | ||
|
||
import net.spy.memcached.CachedData; | ||
import net.spy.memcached.OperationTimeoutException; | ||
import net.spy.memcached.collection.ByteArrayBKey; | ||
import net.spy.memcached.collection.Element; | ||
import net.spy.memcached.protocol.ascii.CollectionGetOperationImpl; | ||
import net.spy.memcached.transcoders.Transcoder; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.LinkedHashMap; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
public abstract class CollectionGetFuture<T, R> extends CollectionFuture<T> { | ||
|
||
protected final Transcoder<R> tc; | ||
|
||
protected T result = null; | ||
|
||
public CollectionGetFuture(CountDownLatch l, long opTimeout, Transcoder<R> tc) { | ||
super(l, opTimeout); | ||
this.tc = tc; | ||
} | ||
|
||
public static class ListGetFuture<T> extends CollectionGetFuture<List<T>, T> { | ||
private final List<CachedData> cachedDataList = new ArrayList<CachedData>(); | ||
|
||
public ListGetFuture(CountDownLatch l, long opTimeout, Transcoder<T> tc) { | ||
super(l, opTimeout, tc); | ||
} | ||
|
||
@Override | ||
public List<T> get() throws InterruptedException, ExecutionException { | ||
try { | ||
return get(timeout, TimeUnit.MILLISECONDS); | ||
} catch (TimeoutException e) { | ||
throw new OperationTimeoutException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public List<T> get(long duration, TimeUnit units) | ||
throws InterruptedException, TimeoutException, ExecutionException { | ||
|
||
if (result == null) { | ||
result = super.get(duration, units); | ||
for (CachedData cachedData : this.cachedDataList) { | ||
result.add(tc.decode(cachedData)); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
public void addCachedData(CachedData data) { | ||
cachedDataList.add(data); | ||
} | ||
} | ||
|
||
public static class SetGetFuture<T> extends CollectionGetFuture<Set<T>, T> { | ||
private final List<CachedData> cachedDataList = new ArrayList<CachedData>(); | ||
|
||
public SetGetFuture(CountDownLatch l, long opTimeout, Transcoder<T> tc) { | ||
super(l, opTimeout, tc); | ||
} | ||
|
||
@Override | ||
public Set<T> get() throws InterruptedException, ExecutionException { | ||
try { | ||
return get(timeout, TimeUnit.MILLISECONDS); | ||
} catch (TimeoutException e) { | ||
throw new OperationTimeoutException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Set<T> get(long duration, TimeUnit units) | ||
throws InterruptedException, TimeoutException, ExecutionException { | ||
|
||
if (result == null) { | ||
result = super.get(duration, units); | ||
for (CachedData cachedData : this.cachedDataList) { | ||
result.add(tc.decode(cachedData)); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
public void addCachedData(CachedData data) { | ||
cachedDataList.add(data); | ||
} | ||
} | ||
|
||
public static class BTreeGetFuture<K, V> extends CollectionGetFuture<Map<K, Element<V>>, V> { | ||
private final HashMap<K, CachedData> cachedDataMap | ||
= new LinkedHashMap<K, CachedData>(); | ||
|
||
public BTreeGetFuture(CountDownLatch l, long opTimeout, Transcoder<V> tc) { | ||
super(l, opTimeout, tc); | ||
} | ||
|
||
@Override | ||
public Map<K, Element<V>> get() throws InterruptedException, ExecutionException { | ||
try { | ||
return get(timeout, TimeUnit.MILLISECONDS); | ||
} catch (TimeoutException e) { | ||
throw new OperationTimeoutException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Map<K, Element<V>> get(long duration, TimeUnit units) | ||
throws InterruptedException, TimeoutException, ExecutionException { | ||
|
||
if (result == null) { | ||
result = super.get(duration, units); | ||
CollectionGetOperationImpl collectionGetOperation = (CollectionGetOperationImpl) op; | ||
List<byte[]> elementFlag = collectionGetOperation.getElementFlag(); | ||
int index = 0; | ||
|
||
for (Map.Entry<K, CachedData> entry : cachedDataMap.entrySet()) { | ||
K bKey = entry.getKey(); | ||
CachedData cachedData = entry.getValue(); | ||
V decodeValue = tc.decode(cachedData); | ||
Element<V> elem = bKey instanceof Long ? | ||
new Element<V>((Long) bKey, decodeValue, elementFlag.get(index)) : | ||
new Element<V>((ByteArrayBKey) bKey, decodeValue, elementFlag.get(index)); | ||
result.put(bKey, elem); | ||
index++; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
public void putCachedData(K key, CachedData data) { | ||
cachedDataMap.put(key, data); | ||
} | ||
} | ||
|
||
public static class MapGetFuture<T> extends CollectionGetFuture<Map<String, T>, T> { | ||
private final HashMap<String, CachedData> cachedDataMap | ||
= new HashMap<String, CachedData>(); | ||
|
||
public MapGetFuture(CountDownLatch l, long opTimeout, Transcoder<T> tc) { | ||
super(l, opTimeout, tc); | ||
} | ||
|
||
@Override | ||
public Map<String, T> get() throws InterruptedException, ExecutionException { | ||
try { | ||
return get(timeout, TimeUnit.MILLISECONDS); | ||
} catch (TimeoutException e) { | ||
throw new OperationTimeoutException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Map<String, T> get(long duration, TimeUnit units) | ||
throws InterruptedException, TimeoutException, ExecutionException { | ||
|
||
if (result == null) { | ||
result = super.get(duration, units); | ||
for (Map.Entry<String, CachedData> entry : cachedDataMap.entrySet()) { | ||
result.put(entry.getKey(), tc.decode(entry.getValue())); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
public void putCachedData(String key, CachedData data) { | ||
cachedDataMap.put(key, data); | ||
} | ||
} | ||
} |
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