-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e08860d
commit 1254c30
Showing
6 changed files
with
243 additions
and
20 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
75 changes: 75 additions & 0 deletions
75
...main/java/com/github/samyuan1990/FabricJavaPool/cache/FabricConnectionImplCacheProxy.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,75 @@ | ||
package com.github.samyuan1990.FabricJavaPool.cache; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
|
||
import com.github.samyuan1990.FabricJavaPool.ExecuteResult; | ||
import org.hyperledger.fabric.protos.ledger.rwset.kvrwset.KvRwset; | ||
import org.hyperledger.fabric.sdk.ProposalResponse; | ||
import org.hyperledger.fabric.sdk.TxReadWriteSetInfo; | ||
|
||
public class FabricConnectionImplCacheProxy extends FabricContractConnectImplCacheProxy implements InvocationHandler { | ||
|
||
|
||
public FabricConnectionImplCacheProxy(Object obj, String cacheURL, String userName, String channelName, int timeout) { | ||
super(obj, cacheURL, userName, channelName, timeout); | ||
} | ||
|
||
public FabricConnectionImplCacheProxy(String cacheURL, String user, String mychannel, int timeout) { | ||
super(cacheURL, user, mychannel, timeout); | ||
} | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
Object result = null; | ||
if (method.getName().equals("query")) { | ||
String key = genericKey(userName, channelName, args); | ||
result = memcachedClient.get(key); | ||
if (result != null) { | ||
System.out.println("hit"); | ||
return result; | ||
} | ||
result = method.invoke(obj, args); | ||
ExecuteResult executeResult = (ExecuteResult) result; | ||
if (executeResult.getPropResp() == null) { | ||
return result; | ||
} | ||
for (ProposalResponse p : executeResult.getPropResp()) { | ||
TxReadWriteSetInfo txReadWriteSetInfo = p.getChaincodeActionResponseReadWriteSetInfo(); | ||
for (TxReadWriteSetInfo.NsRwsetInfo nsRwsetInfo : txReadWriteSetInfo.getNsRwsetInfos()) { | ||
KvRwset.KVRWSet rws = nsRwsetInfo.getRwset(); | ||
for (KvRwset.KVRead readList : rws.getReadsList()) { | ||
String blockKey = readList.getKey(); | ||
memcachedClient.set(blockKey, timeout, key); | ||
} | ||
} | ||
} | ||
memcachedClient.set(key, timeout, result); | ||
return result; | ||
} | ||
if (method.getName().equals("invoke")) { | ||
result = method.invoke(obj, args); | ||
ExecuteResult executeResult = (ExecuteResult) result; | ||
if (executeResult.getPropResp() == null) { | ||
return result; | ||
} | ||
for (ProposalResponse p : executeResult.getPropResp()) { | ||
TxReadWriteSetInfo txReadWriteSetInfo = p.getChaincodeActionResponseReadWriteSetInfo(); | ||
for (TxReadWriteSetInfo.NsRwsetInfo nsRwsetInfo : txReadWriteSetInfo.getNsRwsetInfos()) { | ||
KvRwset.KVRWSet rws = nsRwsetInfo.getRwset(); | ||
for (KvRwset.KVRead readList : rws.getReadsList()) { | ||
String blockKey = readList.getKey(); | ||
String blockCache = memcachedClient.get(blockKey); | ||
if (!blockCache.equals(null)) { | ||
memcachedClient.delete(blockCache); | ||
memcachedClient.delete(blockKey); | ||
} | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
result = method.invoke(obj, args); | ||
return result; | ||
} | ||
} |
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
72 changes: 72 additions & 0 deletions
72
.../java/com/github/samyuan1990/FabricJavaPool/cache/FabricConnectionImplCacheProxyTest.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,72 @@ | ||
package com.github.samyuan1990.FabricJavaPool.cache; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import com.github.samyuan1990.FabricJavaPool.ExecuteResult; | ||
import net.rubyeye.xmemcached.MemcachedClient; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class FabricConnectionImplCacheProxyTest { | ||
|
||
static String noQuery() { | ||
return "a"; | ||
} | ||
|
||
static ExecuteResult query(String a, String b) { | ||
return new ExecuteResult("a", null); | ||
} | ||
|
||
static ExecuteResult invoke(String a, String b) { | ||
return new ExecuteResult("a", null); | ||
} | ||
|
||
@Test | ||
public void invokeFunctionQuery() throws Throwable { | ||
FabricConnectionImplCacheProxy test = new FabricConnectionImplCacheProxy("test", "user", "mychannel", 10); | ||
Method method = FabricConnectionImplCacheProxyTest.class.getDeclaredMethod("query", String.class, String.class); | ||
MemcachedClient memcachedClient = mock(MemcachedClient.class); | ||
when(memcachedClient.get((String) any())).thenReturn(null); | ||
//when(memcachedClient.set((String) any(), (int) any(), (String) any())).thenReturn(true); | ||
test.setMemcachedClient(memcachedClient); | ||
ExecuteResult rs = (ExecuteResult) test.invoke(null, method, new Object[]{"1", "2"}); | ||
Assert.assertEquals("a", rs.getResult()); | ||
} | ||
|
||
@Test | ||
public void invokeFunctionQuery2() throws Throwable { | ||
FabricConnectionImplCacheProxy test = new FabricConnectionImplCacheProxy("test", "user", "mychannel", 10); | ||
Method method = FabricConnectionImplCacheProxyTest.class.getDeclaredMethod("query", String.class, String.class); | ||
MemcachedClient memcachedClient = mock(MemcachedClient.class); | ||
when(memcachedClient.get((String) any())).thenReturn(new ExecuteResult("b", null)); | ||
//when(memcachedClient.set((String) any(), (int) any(), (String) any())).thenReturn(true); | ||
test.setMemcachedClient(memcachedClient); | ||
ExecuteResult rs = (ExecuteResult) test.invoke(null, method, new Object[]{"1", "2"}); | ||
Assert.assertEquals("b", rs.getResult()); | ||
} | ||
|
||
@Test | ||
public void invokeFunctionOthers() throws Throwable { | ||
FabricConnectionImplCacheProxy test = new FabricConnectionImplCacheProxy("test", "test", "test", 10); | ||
Method method = FabricConnectionImplCacheProxyTest.class.getDeclaredMethod("noQuery"); | ||
String rs = (String) test.invoke(null, method, null); | ||
Assert.assertEquals("a", rs); | ||
} | ||
|
||
@Test | ||
public void invokeFunctionInvoke() throws Throwable { | ||
FabricConnectionImplCacheProxy test = new FabricConnectionImplCacheProxy("test", "user", "mychannel", 10); | ||
Method method = FabricConnectionImplCacheProxyTest.class.getDeclaredMethod("invoke", String.class, String.class); | ||
MemcachedClient memcachedClient = mock(MemcachedClient.class); | ||
when(memcachedClient.get((String) any())).thenReturn("b"); | ||
//when(memcachedClient.set((String) any(), (int) any(), (String) any())).thenReturn(true); | ||
test.setMemcachedClient(memcachedClient); | ||
ExecuteResult rs = (ExecuteResult) test.invoke(null, method, new Object[]{"1", "2"}); | ||
Assert.assertEquals("a", rs.getResult()); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
.../com/github/samyuan1990/FabricJavaPool/cache/FabricContractConnectImplCacheProxyTest.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,64 @@ | ||
package com.github.samyuan1990.FabricJavaPool.cache; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
|
||
import net.rubyeye.xmemcached.MemcachedClient; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class FabricContractConnectImplCacheProxyTest { | ||
|
||
static String noQuery() { | ||
return "a"; | ||
} | ||
|
||
static String query(String a, String b) { | ||
return "a"; | ||
} | ||
|
||
@Test | ||
public void genericKey() { | ||
FabricContractConnectImplCacheProxy test = new FabricContractConnectImplCacheProxy("test", "test", "test", 10); | ||
String rs = test.genericKey("user", "mychannel", new Object[]{"1", "2"}); | ||
Assert.assertEquals("12usermychannel", rs); | ||
} | ||
|
||
@Test | ||
public void invokeForQuery() throws Throwable { | ||
FabricContractConnectImplCacheProxy test = new FabricContractConnectImplCacheProxy("test", "user", "mychannel", 10); | ||
Method method = FabricContractConnectImplCacheProxyTest.class.getDeclaredMethod("query", String.class, String.class); | ||
MemcachedClient memcachedClient = mock(MemcachedClient.class); | ||
when(memcachedClient.get((String) any())).thenReturn(null); | ||
//when(memcachedClient.set((String) any(), (int) any(), (String) any())).thenReturn(true); | ||
test.setMemcachedClient(memcachedClient); | ||
String rs = (String) test.invoke(null, method, new Object[]{"1", "2"}); | ||
Assert.assertEquals("a", rs); | ||
} | ||
|
||
@Test | ||
public void invokeForQuery2() throws Throwable { | ||
FabricContractConnectImplCacheProxy test = new FabricContractConnectImplCacheProxy("test", "user", "mychannel", 10); | ||
Method method = FabricContractConnectImplCacheProxyTest.class.getDeclaredMethod("query", String.class, String.class); | ||
MemcachedClient memcachedClient = mock(MemcachedClient.class); | ||
when(memcachedClient.get((String) any())).thenReturn("b"); | ||
//when(memcachedClient.set((String) any(), (int) any(), (String) any())).thenReturn(true); | ||
test.setMemcachedClient(memcachedClient); | ||
String rs = (String) test.invoke(null, method, new Object[]{"1", "2"}); | ||
Assert.assertEquals("b", rs); | ||
} | ||
|
||
@Test | ||
public void invokeNonQuery() throws Throwable { | ||
FabricContractConnectImplCacheProxy test = new FabricContractConnectImplCacheProxy("test", "test", "test", 10); | ||
Method method = FabricContractConnectImplCacheProxyTest.class.getDeclaredMethod("noQuery"); | ||
String rs = (String) test.invoke(null, method, null); | ||
Assert.assertEquals("a", rs); | ||
} | ||
} |