refactor
This commit is contained in:
coderfengyun 2014-07-01 08:47:03 +08:00
parent 49db611739
commit a490260d05
7 changed files with 322 additions and 3 deletions

View File

@ -0,0 +1,77 @@
package org.bench4q.agent.plugin.basic.HBasePlugin;
import java.util.UUID;
import org.bench4q.agent.plugin.Constructor;
import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.behavior.Behavior;
import org.bench4q.agent.plugin.behavior.BehaviorType;
import org.bench4q.agent.utils.Type.SupportTypes;
import org.bench4q.share.exception.Bench4QRunTimeException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Logger;
@Plugin("HBase")
public class HBasePlugin {
private final Configuration conf;
private final HTable tableUnderTest;
@Constructor
public HBasePlugin() {
conf = HBaseConfiguration.create();
try {
@SuppressWarnings({ "unused", "resource" })
HBaseAdmin admin = new HBaseAdmin(conf);
tableUnderTest = new HTable(conf, "users");
} catch (Exception e) {
e.printStackTrace();
throw new Bench4QRunTimeException("Construct HBasePlugin fails!", e);
}
}
@Behavior(value = "Insert", type = BehaviorType.USER_BEHAVIOR)
public HBaseReturn insert(
@Parameter(value = "key", type = SupportTypes.Field) String key,
@Parameter(value = "value", type = SupportTypes.Field) String value) {
try {
Put put = new Put(UUID.randomUUID().toString().getBytes());
put.add("key".getBytes(), "key".getBytes(), key.getBytes());
put.add("value".getBytes(), "value".getBytes(), value.getBytes());
this.tableUnderTest.put(put);
} catch (Exception ex) {
Logger.getLogger(HBasePlugin.class).info(ex, ex);
return new HBaseReturn(false);
}
return new HBaseReturn(true);
}
public HBaseReturn Query(
@Parameter(value = "key", type = SupportTypes.Field) String key) {
try {
Filter filter = new SingleColumnValueFilter(Bytes.toBytes("key"),
Bytes.toBytes("key"), CompareOp.EQUAL, Bytes.toBytes("aaa")); // µ±ÁÐcolumn1µÄֵΪaaaʱ½øÐвéѯ
Scan s = new Scan();
s.setFilter(filter);
ResultScanner resultScanner = this.tableUnderTest.getScanner(s);
for (Result r : resultScanner) {
System.out.println("get the row key:" + new String(r.getRow()));
}
} catch (Exception ex) {
Logger.getLogger(HBasePlugin.class).info(ex, ex);
return new HBaseReturn(false);
}
return new HBaseReturn(true);
}
}

View File

@ -0,0 +1,9 @@
package org.bench4q.agent.plugin.basic.HBasePlugin;
import org.bench4q.agent.plugin.basic.PluginReturn;
public class HBaseReturn extends PluginReturn {
public HBaseReturn(boolean success) {
super(success);
}
}

View File

@ -0,0 +1,113 @@
package org.bench4q.agent.test;
import static org.junit.Assert.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import org.junit.Test;
public class LRUCache {
private int capacity;
private ListNode vHead;
private int last;
private Map<Integer, ListNode> keyNodes;
public LRUCache(int capacity) {
vHead = new ListNode(-1, -1, null, null);
this.keyNodes = new HashMap<Integer, ListNode>(capacity);
this.capacity = capacity;
}
public int get(int key) {
if (this.keyNodes.containsKey(key)) {
ListNode node = this.keyNodes.get(key);
delete(node);
offerFirst(node);
return node.val;
}
return -1;
}
private void delete(ListNode node) {
if (node.key == this.last) {
if (this.keyNodes.size() <= 1) {
this.last = Integer.MAX_VALUE;
} else {
this.last = node.previous.key;
}
}
node.previous.next = node.next;
if (node.next != null) {
node.next.previous = node.previous;
}
node.previous = null;
node.next = null;
this.keyNodes.remove(node.key);
}
private void offerFirst(ListNode node) {
node.next = vHead.next;
if (vHead.next != null) {
vHead.next.previous = node;
}
node.previous = vHead;
vHead.next = node;
if (this.keyNodes.size() == 0) {
this.last = node.key;
}
this.keyNodes.put(node.key, node);
}
public void set(int key, int value) {
ListNode valNode = null;
if (this.keyNodes.containsKey(key)) {
valNode = this.keyNodes.get(key);
valNode.val = value;
delete(valNode);
} else {
if (isFull()) {
delete(this.keyNodes.get(this.last));
}
valNode = new ListNode(key, value, null, null);
}
offerFirst(valNode);
}
private boolean isFull() {
return this.keyNodes.size() == this.capacity;
}
private class ListNode {
int key;
int val;
ListNode previous;
ListNode next;
public ListNode(int key, int val, ListNode previous, ListNode next) {
this.key = key;
this.val = val;
this.previous = previous;
this.next = next;
}
}
public static void main(String[] args) {
LRUCache cache = new LRUCache(3);
cache.set(1, 1);
cache.set(2, 2);
cache.set(3, 3);
cache.set(4, 4);
System.out.println(cache.get(4));
System.out.println(cache.get(3));
System.out.println(cache.get(2));
System.out.println(cache.get(1));
cache.set(5, 5);
System.out.println(cache.get(1));
System.out.println(cache.get(2));
System.out.println(cache.get(3));
System.out.println(cache.get(4));
System.out.println(cache.get(5));
}
}

View File

@ -0,0 +1,110 @@
package org.bench4q.agent.test;
import org.junit.Test;
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public class Solution {
private static boolean FIRST = true;
private static boolean SECOND = false;
public void reorderList(ListNode head) {
if (head == null || head.next == null || head.next.next == null) {
return;
}
ListNode startOfSecondHalf = findStartNodeOfSecondHalf(head);
startOfSecondHalf = reverse(startOfSecondHalf);
merge(head, startOfSecondHalf);
}
private ListNode findStartNodeOfSecondHalf(ListNode head) {
ListNode vHead = new ListNode(-1);
vHead.next = head;
ListNode runner = head, walker = head, walkerPre = vHead;
while (runner != null && runner.next != null) {
runner = runner.next;
runner = runner.next;
walkerPre = walkerPre.next;
walker = walker.next;
}
walkerPre.next = null;
return walker;
}
private ListNode reverse(ListNode start) {
ListNode vHead = new ListNode(-1), current = start, next = start.next;
while (current != null) {
current.next = vHead.next;
vHead.next = current;
current = next;
if (next == null) {
break;
}
next = next.next;
}
return vHead.next;
}
private void merge(ListNode head, ListNode head2) {
ListNode current = head, firstCurrent = head.next, secondCurrent = head2, firstNext = firstCurrent != null ? firstCurrent.next
: null, secondNext = head2.next;
boolean currentTurn = SECOND;
while (firstCurrent != null && secondCurrent != null) {
if (currentTurn == FIRST) {
firstCurrent.next = null;
current.next = firstCurrent;
firstCurrent = firstNext;
if (firstNext != null) {
firstNext = firstNext.next;
}
currentTurn = SECOND;
} else {
secondCurrent.next = null;
current.next = secondCurrent;
secondCurrent = secondNext;
if (secondNext != null) {
secondNext = secondNext.next;
}
currentTurn = FIRST;
}
current = current.next;
}
current.next = firstCurrent == null ? secondCurrent : firstCurrent;
}
@Test
public void test() {
ListNode list = new ListNode(1);
list.next = new ListNode(2);
list.next.next = new ListNode(3);
list.next.next.next = new ListNode(4);
list.next.next.next.next = new ListNode(5);
reorderList(list);
print(list);
}
private void print(ListNode list) {
while (list != null) {
System.out.println(list.val);
list = list.next;
}
}
@Test
public void test1() {
ListNode list = new ListNode(1);
list.next = new ListNode(2);
list.next.next = new ListNode(3);
reorderList(list);
print(list);
}
}

View File

@ -80,6 +80,17 @@ public class Agent {
this.currentEnumStatus = currentEnumStatus; this.currentEnumStatus = currentEnumStatus;
} }
public static Agent createAgentWithoutId(Agent agentWithoutInitial) {
if (agentWithoutInitial.getMaxLoad() > 0) {
return createAgentWithoutId(agentWithoutInitial.getHostName(),
agentWithoutInitial.getPort(),
agentWithoutInitial.getMaxLoad());
} else {
return createAgentWithoutId(agentWithoutInitial.getHostName(),
agentWithoutInitial.getPort());
}
}
public static Agent createAgentWithoutId(String hostName, int port) { public static Agent createAgentWithoutId(String hostName, int port) {
Agent agent = new Agent(); Agent agent = new Agent();
agent.setCurrentEnumStatus(AgentStatus.InIdle); agent.setCurrentEnumStatus(AgentStatus.InIdle);

View File

@ -51,8 +51,7 @@ public class AgentService {
public boolean addAgentToPool(Agent agentWithoutId) { public boolean addAgentToPool(Agent agentWithoutId) {
synchronized (this.getAgentRepository().getAddDeleteLock()) { synchronized (this.getAgentRepository().getAddDeleteLock()) {
if (!this.getAgentRepository().attach( if (!this.getAgentRepository().attach(
Agent.createAgentWithoutId(agentWithoutId.getHostName(), Agent.createAgentWithoutId(agentWithoutId))) {
agentWithoutId.getPort()))) {
return false; return false;
} }
Agent agent = this.getAgentRepository().getAgentBy( Agent agent = this.getAgentRepository().getAgentBy(

View File

@ -7,7 +7,7 @@
</property> </property>
<property name="hibernate.connection.username">root</property> <property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456 </property> <property name="hibernate.connection.password">123456 </property>
<property name="hibernate.connection.pool.size">20 </property> <property name="hibernate.connection.pool.size">100 </property>
<property name="hibernate.show_sql">false</property> <property name="hibernate.show_sql">false</property>
<property name="format_sql">true</property> <property name="format_sql">true</property>
<property name="Connection.useUnicode">true </property> <property name="Connection.useUnicode">true </property>