From dbcb2e60df3db7daf2f329ba2411173ff3abc6f0 Mon Sep 17 00:00:00 2001 From: lzb Date: Thu, 11 May 2017 03:06:59 +0800 Subject: [PATCH 1/2] =?UTF-8?q?jvm=E7=AC=AC=E4=B8=83=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1148285693/{ => learning2017}/.gitignore | 0 .../src/main/java/me/lzb/basic/FileList.java | 54 ++++++ .../me/lzb/basic/{ => list}/ArrayList.java | 2 +- .../me/lzb/basic/{ => list}/ArrayUtil.java | 2 +- .../me/lzb/basic/{ => list}/Iterator.java | 2 +- .../me/lzb/basic/{ => list}/LinkedList.java | 2 +- .../java/me/lzb/basic/{ => list}/List.java | 2 +- .../main/java/me/lzb/basic/queue/Queue.java | 2 +- .../main/java/me/lzb/basic/stack/Stack.java | 2 +- .../{BinaryTreeNode.java => tree/BTNode.java} | 14 +- .../me/lzb/basic/tree/BinaryTreeNode.java | 43 +++++ .../me/lzb/basic/tree/BinaryTreeUtil.java | 177 ++++++++++++++++++ .../test/java/me/lzb/basic/FileListTest.java | 23 +++ .../lzb/basic/{ => list}/ArrayListTest.java | 4 +- .../lzb/basic/{ => list}/LinkedListTest.java | 6 +- .../me/lzb/basic/tree/BinaryTreeUtilTest.java | 90 +++++++++ .../consistenthash/ConsistentHash.java | 119 ++++++++++++ .../consistenthash/HashFunction.java | 48 +++++ .../consistenthash/PhysicalNode.java | 46 +++++ .../algorithm/consistenthash/VirtualNode.java | 43 +++++ .../me/lzb/other/proxy/UserServiceImpl.java | 2 +- .../other/algorithm/ConsistentHashTest.java | 94 ++++++++++ group24/1148285693/learning2017/pom.xml | 5 + 23 files changed, 761 insertions(+), 21 deletions(-) rename group24/1148285693/{ => learning2017}/.gitignore (100%) create mode 100644 group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/FileList.java rename group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/{ => list}/ArrayList.java (98%) rename group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/{ => list}/ArrayUtil.java (99%) rename group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/{ => list}/Iterator.java (81%) rename group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/{ => list}/LinkedList.java (99%) rename group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/{ => list}/List.java (88%) rename group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/{BinaryTreeNode.java => tree/BTNode.java} (76%) create mode 100644 group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/tree/BinaryTreeNode.java create mode 100644 group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/tree/BinaryTreeUtil.java create mode 100644 group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/basic/FileListTest.java rename group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/basic/{ => list}/ArrayListTest.java (97%) rename group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/basic/{ => list}/LinkedListTest.java (98%) create mode 100644 group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/basic/tree/BinaryTreeUtilTest.java create mode 100644 group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/ConsistentHash.java create mode 100644 group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/HashFunction.java create mode 100644 group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/PhysicalNode.java create mode 100644 group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/VirtualNode.java create mode 100644 group24/1148285693/learning2017/other/src/test/java/me/lzb/other/algorithm/ConsistentHashTest.java diff --git a/group24/1148285693/.gitignore b/group24/1148285693/learning2017/.gitignore similarity index 100% rename from group24/1148285693/.gitignore rename to group24/1148285693/learning2017/.gitignore diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/FileList.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/FileList.java new file mode 100644 index 0000000000..ce0d19e330 --- /dev/null +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/FileList.java @@ -0,0 +1,54 @@ +package me.lzb.basic; + +import java.io.File; + +/** + * 给定一个目录,递归的列出下面所有的子目录和文件 + * + * @author LZB + */ +public class FileList { + + public void list(File f) { + System.out.println(f.getPath()); + + list(f, 0); + } + + + private void list(File file, int level) { + if (file.isDirectory()) { + print(file, level); + } + level++; + File[] files = file.listFiles(); + for (int i = 0; i < files.length; i++) { + if (files[i].isDirectory()) { + list(files[i], level); + } else { + print(files[i], level); + } + } + } + + private void print(File f, int level) { + System.out.println(getFileFormat(level) + f.getName()); + + } + + + private String getFileFormat(int level) { + StringBuffer sb = new StringBuffer(); + if (level > 1) { + sb.append("|"); + } + + for (int i = 0; i < level - 1; i++) { + sb.append(" "); + } + if (level > 0) { + sb.append("|--"); + } + return sb.toString(); + } +} diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/ArrayList.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/list/ArrayList.java similarity index 98% rename from group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/ArrayList.java rename to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/list/ArrayList.java index af897cd58f..a999b662f1 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/ArrayList.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/list/ArrayList.java @@ -1,4 +1,4 @@ -package me.lzb.basic; +package me.lzb.basic.list; /** * 简易ArrayList diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/ArrayUtil.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/list/ArrayUtil.java similarity index 99% rename from group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/ArrayUtil.java rename to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/list/ArrayUtil.java index eab32c80cc..dcfa9c39f6 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/ArrayUtil.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/list/ArrayUtil.java @@ -1,4 +1,4 @@ -package me.lzb.basic; +package me.lzb.basic.list; public class ArrayUtil { diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/Iterator.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/list/Iterator.java similarity index 81% rename from group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/Iterator.java rename to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/list/Iterator.java index 86e8cae942..d6122132cc 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/Iterator.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/list/Iterator.java @@ -1,4 +1,4 @@ -package me.lzb.basic; +package me.lzb.basic.list; /** * Created by LZB on 2017/3/11. diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/LinkedList.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/list/LinkedList.java similarity index 99% rename from group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/LinkedList.java rename to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/list/LinkedList.java index 268b69bf50..f55c92cdc6 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/LinkedList.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/list/LinkedList.java @@ -1,4 +1,4 @@ -package me.lzb.basic; +package me.lzb.basic.list; /** diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/List.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/list/List.java similarity index 88% rename from group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/List.java rename to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/list/List.java index df7f30812b..3e34154092 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/List.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/list/List.java @@ -1,4 +1,4 @@ -package me.lzb.basic; +package me.lzb.basic.list; /** * list接口 diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/queue/Queue.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/queue/Queue.java index a70c6065c4..b1ef522855 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/queue/Queue.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/queue/Queue.java @@ -1,6 +1,6 @@ package me.lzb.basic.queue; -import me.lzb.basic.LinkedList; +import me.lzb.basic.list.LinkedList; /** * 先进先出 diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/stack/Stack.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/stack/Stack.java index ff5f8156a0..a5469ff8ce 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/stack/Stack.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/stack/Stack.java @@ -1,6 +1,6 @@ package me.lzb.basic.stack; -import me.lzb.basic.ArrayList; +import me.lzb.basic.list.ArrayList; /** * 先进后出 diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/BinaryTreeNode.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/tree/BTNode.java similarity index 76% rename from group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/BinaryTreeNode.java rename to group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/tree/BTNode.java index 88395e3010..ee0b9d6e33 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/BinaryTreeNode.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/tree/BTNode.java @@ -1,16 +1,16 @@ -package me.lzb.basic; +package me.lzb.basic.tree; /** * 左边比父节点小,右边比父节点大 * Created by LZB on 2017/3/11. */ -public class BinaryTreeNode { +public class BTNode { private int data; - private BinaryTreeNode left; - private BinaryTreeNode right; + private BTNode left; + private BTNode right; - public BinaryTreeNode(int data){ + public BTNode(int data){ this.data = data; } @@ -21,7 +21,7 @@ public int getData() { //这层满了就下一层继续add,直到找到空位 public void add(int d){ - BinaryTreeNode b = new BinaryTreeNode(d); + BTNode b = new BTNode(d); if(compareTo(b)){ //比父节点小,左边 if(this.left == null){ @@ -42,7 +42,7 @@ public void add(int d){ } - public boolean compareTo(BinaryTreeNode node){ + public boolean compareTo(BTNode node){ if(this.data > node.getData()){ return true; } diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/tree/BinaryTreeNode.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/tree/BinaryTreeNode.java new file mode 100644 index 0000000000..fc56ca100f --- /dev/null +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/tree/BinaryTreeNode.java @@ -0,0 +1,43 @@ +package me.lzb.basic.tree; + +/** + * @author LZB + */ +public class BinaryTreeNode { + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(T data) { + this.data = data; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o) { + return null; + } + +} diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/tree/BinaryTreeUtil.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/tree/BinaryTreeUtil.java new file mode 100644 index 0000000000..c5543271ad --- /dev/null +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/basic/tree/BinaryTreeUtil.java @@ -0,0 +1,177 @@ +package me.lzb.basic.tree; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +/** + * 前序遍历:根节点->左子树->右子树 + * 中序遍历:左子树->根节点->右子树 + * 后序遍历:左子树->右子树->根节点 + * + * @author LZB + */ +public class BinaryTreeUtil { + /** + * 用递归的方式实现对二叉树的前序遍历 + * + * @param root + * @return + */ + public static List preOrderVisit(BinaryTreeNode root) { + List result = new ArrayList<>(); + preOrderVisit(root, result); + return result; + } + + + private static void preOrderVisit(BinaryTreeNode root, List result) { + result.add(root.getData()); + + if (root.getLeft() != null) { + preOrderVisit(root.getLeft(), result); + } + if (root.getRight() != null) { + preOrderVisit(root.getRight(), result); + } + } + + + /** + * 用递归的方式实现对二叉树的中遍历 + * + * @param root + * @return + */ + public static List inOrderVisit(BinaryTreeNode root) { + List result = new ArrayList<>(); + inOrderVisit(root, result); + return result; + } + + private static void inOrderVisit(BinaryTreeNode root, List result) { + if (root.getLeft() != null) { + inOrderVisit(root.getLeft(), result); + } + + result.add(root.getData()); + + if (root.getRight() != null) { + inOrderVisit(root.getRight(), result); + } + } + + /** + * 用递归的方式实现对二叉树的后遍历 + * + * @param root + * @return + */ + public static List postOrderVisit(BinaryTreeNode root) { + List result = new ArrayList<>(); + postOrderVisit(root, result); + return result; + } + + private static void postOrderVisit(BinaryTreeNode root, List result) { + if (root.getLeft() != null) { + postOrderVisit(root.getLeft(), result); + } + if (root.getRight() != null) { + postOrderVisit(root.getRight(), result); + } + result.add(root.getData()); + } + + + /** + * 用非递归的方式实现对二叉树的前序遍历 + * + * @param root + * @return + */ + public static List preOrderWithoutRecursion(BinaryTreeNode root) { + + List result = new ArrayList<>(); + Stack> stack = new Stack<>(); + BinaryTreeNode node = root; + //先把左侧节点全部入栈,然后一个个pop出来检查右侧节点 + while (node != null || stack.size() > 0) { + while (node != null) { + result.add(node.getData()); + stack.push(node); + node = node.getLeft(); + } + if (stack.size() > 0) { + node = stack.pop(); + node = node.getRight(); + } + } + + + return result; + } + + /** + * 用非递归的方式实现对二叉树的中序遍历 + * + * @param root + * @return + */ + public static List inOrderWithoutRecursion(BinaryTreeNode root) { + + List result = new ArrayList<>(); + Stack> stack = new Stack<>(); + BinaryTreeNode node = root; + while (node != null || stack.size() > 0) { + while (node != null) { + stack.push(node); + node = node.getLeft(); + } + if (stack.size() > 0) { + node = stack.pop(); + result.add(node.getData()); + node = node.getRight(); + } + } + return result; + } + + /** + * 用非递归的方式实现对二叉树的后序遍历 + * + * @param root + * @return + */ + public static List postOrderWithoutRecursion(BinaryTreeNode root) { + + List result = new ArrayList<>(); + Stack> stack = new Stack<>(); + + Stack> s2 = new Stack<>(); + + BinaryTreeNode node = root; + + BinaryTreeNode r = root; + while (node != null || stack.size() > 0) { + //s2先入根节点,在入右边节点 + while (node != null) { + stack.push(node); + s2.push(node); + node = node.getRight(); + } + + //最底层节点出栈,左节点继续循环 + if (stack.size() > 0) { + node = stack.pop(); + node = node.getLeft(); + } + } + while (s2.size() > 0) { + result.add(s2.pop().getData()); + } + return result; + } + + +} diff --git a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/basic/FileListTest.java b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/basic/FileListTest.java new file mode 100644 index 0000000000..c9f6cd2429 --- /dev/null +++ b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/basic/FileListTest.java @@ -0,0 +1,23 @@ +package me.lzb.basic; + +import org.junit.Test; + +import java.io.File; + +/** + * @author LZB + * @date 2017/5/10 + */ +public class FileListTest { + + @Test + public void listTest(){ + File file = new File("D:\\code\\learning\\tmp"); + + + FileList list = new FileList(); + list.list(file); + + } + +} diff --git a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/basic/ArrayListTest.java b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/basic/list/ArrayListTest.java similarity index 97% rename from group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/basic/ArrayListTest.java rename to group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/basic/list/ArrayListTest.java index efcfdbae1d..2240264590 100644 --- a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/basic/ArrayListTest.java +++ b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/basic/list/ArrayListTest.java @@ -1,7 +1,5 @@ -package me.lzb.basic; +package me.lzb.basic.list; -import me.lzb.basic.ArrayList; -import me.lzb.basic.Iterator; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; diff --git a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/basic/LinkedListTest.java b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/basic/list/LinkedListTest.java similarity index 98% rename from group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/basic/LinkedListTest.java rename to group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/basic/list/LinkedListTest.java index 6f02328060..486957915f 100644 --- a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/basic/LinkedListTest.java +++ b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/basic/list/LinkedListTest.java @@ -1,7 +1,7 @@ -package me.lzb.basic; +package me.lzb.basic.list; -import me.lzb.basic.Iterator; -import me.lzb.basic.LinkedList; +import me.lzb.basic.list.Iterator; +import me.lzb.basic.list.LinkedList; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; diff --git a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/basic/tree/BinaryTreeUtilTest.java b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/basic/tree/BinaryTreeUtilTest.java new file mode 100644 index 0000000000..6656fdfc4a --- /dev/null +++ b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/basic/tree/BinaryTreeUtilTest.java @@ -0,0 +1,90 @@ +package me.lzb.basic.tree; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +/** + * @author LZB + */ +public class BinaryTreeUtilTest { + BinaryTreeNode root = null; + + @Before + public void setUp() throws Exception { + root = new BinaryTreeNode<>(1); + root.setLeft(new BinaryTreeNode<>(2)); + root.setRight(new BinaryTreeNode<>(5)); + root.getLeft().setLeft(new BinaryTreeNode<>(3)); + root.getLeft().setRight(new BinaryTreeNode<>(4)); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testPreOrderVisit() { + + List result = BinaryTreeUtil.preOrderVisit(root); + Assert.assertEquals("[1, 2, 3, 4, 5]", result.toString()); + + + } + + @Test + public void testInOrderVisit() { + + + List result = BinaryTreeUtil.inOrderVisit(root); + Assert.assertEquals("[3, 2, 4, 1, 5]", result.toString()); + + } + + @Test + public void testPostOrderVisit() { + + + List result = BinaryTreeUtil.postOrderVisit(root); + Assert.assertEquals("[3, 4, 2, 5, 1]", result.toString()); + + } + + + + @Test + public void testPreOrderVisitWithoutRecursion() { + BinaryTreeNode node = root.getLeft().getRight(); + node.setLeft(new BinaryTreeNode<>(6)); + node.setRight(new BinaryTreeNode<>(7)); + + List result = BinaryTreeUtil.preOrderWithoutRecursion(root); + Assert.assertEquals("[1, 2, 3, 4, 6, 7, 5]", result.toString()); + + } + + @Test + public void testInOrderVisitWithoutRecursion() { + BinaryTreeNode node = root.getLeft().getRight(); + node.setLeft(new BinaryTreeNode<>(6)); + node.setRight(new BinaryTreeNode<>(7)); + + List result = BinaryTreeUtil.inOrderWithoutRecursion(root); + Assert.assertEquals("[3, 2, 6, 4, 7, 1, 5]", result.toString()); + + } + + @Test + public void testPostOrderWithoutRecursion() { + BinaryTreeNode node = root.getLeft().getRight(); + node.setLeft(new BinaryTreeNode<>(6)); + node.setRight(new BinaryTreeNode<>(7)); + + List result = BinaryTreeUtil.postOrderWithoutRecursion(root); + Assert.assertEquals("[3, 6, 7, 4, 2, 5, 1]", result.toString()); + + } +} diff --git a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/ConsistentHash.java b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/ConsistentHash.java new file mode 100644 index 0000000000..01e7c8f8b2 --- /dev/null +++ b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/ConsistentHash.java @@ -0,0 +1,119 @@ +package me.lzb.other.algorithm.consistenthash; + +import java.util.*; + +/** + * @author LZB + * @date 2017/5/9 + */ +public class ConsistentHash { + + private int vnNumber; + + private HashFunction hashFunction; + + private final SortedMap nodes = new TreeMap<>(); + + private int size; + + private final Map pns = new HashMap<>(); + + public ConsistentHash(int pnNum, int vnNum) { + this.hashFunction = new HashFunction(); + this.vnNumber = vnNum; + + //创建好节点 + int a = 100; + + for (int i = 0; i < pnNum; i++) { + PhysicalNode pn = new PhysicalNode("192.168.1." + a, "8888"); + pns.put(pn.getAddr(), pn); + addNode(pn); + a++; + } + } + + + public void addNode(PhysicalNode pn) { + //添加一个物理节点,增加vnNumber个虚拟节点 + for (int i = 0; i < this.vnNumber; i++) { + VirtualNode vn = new VirtualNode(pn, String.valueOf(i)); + nodes.put(hashFunction.hash(vn.getAddr()), vn); + } + + } + + public void removeNode(PhysicalNode node) { + for (int i = 0; i < this.vnNumber; i++) + nodes.remove(hashFunction.hash(node.getAddr() + "-" + i)); + } + + /** + * 获取顺时针方向最近的节点 + * + * @param key + * @return + */ + public VirtualNode getNode(String key) { + if (nodes.isEmpty()) { + return null; + } + + long hash = hashFunction.hash(key); + if (!nodes.containsKey(hash)) { + SortedMap tailMap = nodes.tailMap(hash); + hash = tailMap.isEmpty() ? nodes.firstKey() : tailMap.firstKey(); + } + return nodes.get(hash); + } + + public long getSize() { + return nodes.size(); + } + + + @Override + public String toString() { + StringBuffer sb = new StringBuffer(); + for (Long key : nodes.keySet()) { + VirtualNode n = nodes.get(key); + sb.append(n.getAddr()); + sb.append(" "); + sb.append(n.size()); + sb.append("\r\n"); + } + return sb.toString(); + } + + + public SortedMap getNodes() { + return this.nodes; + } + + public Map getPNodes() { + return this.pns; + } + + + public void put(String key, String value) { + VirtualNode node = getNode(key); + node.put(key, value); + size++; + } + + public void remove(String key) { + VirtualNode node = getNode(key); + node.remove(key); + size--; + } + + public String get(String key) { + VirtualNode node = getNode(key); + return node.get(key); + } + + public int size() { + return size; + } + +} diff --git a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/HashFunction.java b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/HashFunction.java new file mode 100644 index 0000000000..f17fd7957a --- /dev/null +++ b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/HashFunction.java @@ -0,0 +1,48 @@ +package me.lzb.other.algorithm.consistenthash; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * @author LZB + * @date 2017/5/11 + */ +public class HashFunction { + + /** + * ketama + * @param key + * @return + */ + public long hash(String key) { + byte[] bytes = md5(key); + return ketama(bytes, 0); + + } + + private long ketama(byte[] digest, int nTime) { + //0xff = 255 + long a = ((long) (digest[3 + nTime * 4] & 0xFF) << 24) + | ((long) (digest[2 + nTime * 4] & 0xFF) << 16) + | ((long) (digest[1 + nTime * 4] & 0xFF) << 8) + | (digest[0 + nTime * 4] & 0xFF); + + return a & 0xffffffffL; + } + + //输入任意长的信息,输出128位的信息,不可逆,唯一 + private byte[] md5(String key) { + MessageDigest md5 = null; + if (md5 == null) { + try { + md5 = MessageDigest.getInstance("MD5"); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + } + + md5.reset(); + md5.update(key.getBytes()); + return md5.digest(); + } +} diff --git a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/PhysicalNode.java b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/PhysicalNode.java new file mode 100644 index 0000000000..0c3a286494 --- /dev/null +++ b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/PhysicalNode.java @@ -0,0 +1,46 @@ +package me.lzb.other.algorithm.consistenthash; + +import java.util.HashMap; + +/** + * @author LZB + * @date 2017/5/10 + */ +public class PhysicalNode { + private String ip; + + private String port; + + private HashMap data = new HashMap<>(); + + public PhysicalNode(String ip, String port) { + this.ip = ip; + this.port = port; + } + + + public void put(String key, String value) { + data.put(key, value); + } + + public void remove(String key) { + data.remove(key); + } + + public String get(String key) { + return data.get(key); + } + + public String getAddr() { + return this.ip + ":" + this.port; + } + + public int size() { + return data.size(); + } + + public HashMap getDate(){ + return data; + } + +} diff --git a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/VirtualNode.java b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/VirtualNode.java new file mode 100644 index 0000000000..e409786389 --- /dev/null +++ b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/VirtualNode.java @@ -0,0 +1,43 @@ +package me.lzb.other.algorithm.consistenthash; + +/** + * @author LZB + * @date 2017/5/10 + */ +public class VirtualNode { + private PhysicalNode physicalNode; + + private String id; + + private int size; + + public VirtualNode(PhysicalNode node, String id) { + this.physicalNode = node; + this.id = id; + } + + + public void put(String key, String value) { + physicalNode.put(key, value); + size++; + } + + public void remove(String key) { + physicalNode.remove(key); + size--; + } + + public String get(String key) { + return physicalNode.get(key); + } + + public String getAddr() { + return physicalNode.getAddr() + "-" + id; + } + + public int size() { + return size; + } + + +} diff --git a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/UserServiceImpl.java b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/UserServiceImpl.java index 614b60d9c9..147abda59b 100644 --- a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/UserServiceImpl.java +++ b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/UserServiceImpl.java @@ -6,6 +6,6 @@ public class UserServiceImpl implements UserService { public void add() { - System.out.println("--------------------add---------------"); + System.out.println("--------------------addNode---------------"); } } \ No newline at end of file diff --git a/group24/1148285693/learning2017/other/src/test/java/me/lzb/other/algorithm/ConsistentHashTest.java b/group24/1148285693/learning2017/other/src/test/java/me/lzb/other/algorithm/ConsistentHashTest.java new file mode 100644 index 0000000000..08902c756a --- /dev/null +++ b/group24/1148285693/learning2017/other/src/test/java/me/lzb/other/algorithm/ConsistentHashTest.java @@ -0,0 +1,94 @@ +package me.lzb.other.algorithm; + +import me.lzb.other.algorithm.consistenthash.ConsistentHash; +import me.lzb.other.algorithm.consistenthash.HashFunction; +import me.lzb.other.algorithm.consistenthash.PhysicalNode; +import org.junit.Test; + +import java.text.NumberFormat; +import java.util.HashMap; +import java.util.Map; + +/** + * @author LZB + * @date 2017/5/10 + */ +public class ConsistentHashTest { + + + @Test + public void hashFunctionTest() { + + HashFunction hashFunction = new HashFunction(); + + System.out.println(hashFunction.hash("192.168.1.100")); + System.out.println(hashFunction.hash("192.168.1.101")); + System.out.println(hashFunction.hash("192.168.1.102")); + System.out.println(hashFunction.hash("192.168.1.103")); + System.out.println(hashFunction.hash("192.168.1.104")); + } + + @Test + public void consistentHashTest() { + int all = 100000; + + ConsistentHash ch10 = new ConsistentHash(10, 5); + put(ch10, all); + + + ConsistentHash ch12 = new ConsistentHash(12, 5); + put(ch12, all); + + System.out.print("增加两台 "); + hitRate(ch10.getPNodes(), ch12.getPNodes(), all); + + System.out.print("减少两台 "); + ConsistentHash ch8 = new ConsistentHash(8, 5); + put(ch8, all); + + hitRate(ch10.getPNodes(), ch8.getPNodes(), all); + } + + + public static void put(ConsistentHash ch, int all) { + for (int i = 0; i < all; i++) { + ch.put(String.valueOf(i), "aaaaa" + i); + } + } + + public static void hitRate(Map m1, Map m2, int all) { + + Map s; + Map l; + + if (m1.size() < m2.size()) { + s = m1; + l = m2; + } else { + s = m2; + l = m1; + } + + int count = 0; + + for (String key : s.keySet()) { + + PhysicalNode sn = s.get(key); + PhysicalNode ln = l.get(key); + + HashMap sData = sn.getDate(); + + HashMap lData = ln.getDate(); + for (String k : sData.keySet()) { + if (lData.containsKey(k)) { + count++; + } + } + } + NumberFormat nf = NumberFormat.getPercentInstance(); + nf.setMinimumFractionDigits(2); + System.out.println("hit rate : " + (nf.format((double) count / (double) all))); + } + + +} diff --git a/group24/1148285693/learning2017/pom.xml b/group24/1148285693/learning2017/pom.xml index 8dc41781ba..d1b38a50b4 100644 --- a/group24/1148285693/learning2017/pom.xml +++ b/group24/1148285693/learning2017/pom.xml @@ -116,6 +116,11 @@ httpclient 4.5.3 + + org.apache.zookeeper + zookeeper + 3.4.10 + From 877b31e463b327417ba7960371c52d0ab285ec91 Mon Sep 17 00:00:00 2001 From: lzb Date: Fri, 12 May 2017 01:23:04 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=95=B4=E7=90=86=E5=88=86=E6=94=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group24/1148285693/learning2017/.gitignore | 79 ++-- group24/1148285693/learning2017/other/pom.xml | 15 - .../other/src/main/java/ThreadTest.java | 25 -- .../java/me/lzb/other/algorithm/Paxos.java | 7 - .../consistenthash/ConsistentHash.java | 119 ------ .../consistenthash/HashFunction.java | 48 --- .../consistenthash/PhysicalNode.java | 46 --- .../algorithm/consistenthash/VirtualNode.java | 43 -- .../main/java/me/lzb/other/graph/Graph.java | 366 ------------------ .../me/lzb/other/lock/ReentrantTest1.java | 27 -- .../me/lzb/other/lock/ReentrantTest2.java | 35 -- .../lzb/other/proxy/MyInvocationHandler.java | 52 --- .../java/me/lzb/other/proxy/UserService.java | 11 - .../me/lzb/other/proxy/UserServiceImpl.java | 11 - .../other/algorithm/ConsistentHashTest.java | 94 ----- .../java/me/lzb/other/proxy/ProxyTest.java | 25 -- group24/1148285693/learning2017/pom.xml | 319 +++++++-------- 17 files changed, 199 insertions(+), 1123 deletions(-) delete mode 100644 group24/1148285693/learning2017/other/pom.xml delete mode 100644 group24/1148285693/learning2017/other/src/main/java/ThreadTest.java delete mode 100644 group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/Paxos.java delete mode 100644 group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/ConsistentHash.java delete mode 100644 group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/HashFunction.java delete mode 100644 group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/PhysicalNode.java delete mode 100644 group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/VirtualNode.java delete mode 100644 group24/1148285693/learning2017/other/src/main/java/me/lzb/other/graph/Graph.java delete mode 100644 group24/1148285693/learning2017/other/src/main/java/me/lzb/other/lock/ReentrantTest1.java delete mode 100644 group24/1148285693/learning2017/other/src/main/java/me/lzb/other/lock/ReentrantTest2.java delete mode 100644 group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/MyInvocationHandler.java delete mode 100644 group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/UserService.java delete mode 100644 group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/UserServiceImpl.java delete mode 100644 group24/1148285693/learning2017/other/src/test/java/me/lzb/other/algorithm/ConsistentHashTest.java delete mode 100644 group24/1148285693/learning2017/other/src/test/java/me/lzb/other/proxy/ProxyTest.java diff --git a/group24/1148285693/learning2017/.gitignore b/group24/1148285693/learning2017/.gitignore index f41f37aecb..556caf3bbd 100644 --- a/group24/1148285693/learning2017/.gitignore +++ b/group24/1148285693/learning2017/.gitignore @@ -1,40 +1,39 @@ -# Mobile Tools for Java (J2ME) -.mtj.tmp/ - -# Package Files -*.war -*.ear -*.bk -.gradle -target -*.class -*.real - -# virtual machine crash logs -# see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* - -# Eclipse Files # -.project -.classpath -.settings - -# Idea -*.iml -*.ipr -*.iws -.idea - -# log -*_IS_UNDEFINED -logs -*.log - -# other -*.bak -.directory -.DS_Store - - -Test.java -example \ No newline at end of file +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files +*.war +*.ear +*.bk +.gradle +target +*.class +*.real + +# virtual machine crash logs +# see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +# Eclipse Files # +.project +.classpath +.settings + +# Idea +*.iml +*.ipr +*.iws +.idea + +# log +*_IS_UNDEFINED +logs +*.log + +# other +*.bak +.directory +.DS_Store + + +Test.java diff --git a/group24/1148285693/learning2017/other/pom.xml b/group24/1148285693/learning2017/other/pom.xml deleted file mode 100644 index 1f0b7eb9c0..0000000000 --- a/group24/1148285693/learning2017/other/pom.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - learning2017 - me.lzb - 1.0 - - 4.0.0 - - other - - - \ No newline at end of file diff --git a/group24/1148285693/learning2017/other/src/main/java/ThreadTest.java b/group24/1148285693/learning2017/other/src/main/java/ThreadTest.java deleted file mode 100644 index fb460c5bf9..0000000000 --- a/group24/1148285693/learning2017/other/src/main/java/ThreadTest.java +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Created by LZB on 2017/5/3. - */ -public class ThreadTest extends Thread { - boolean stop = false; - int value = 0; - public void run() { - while (!stop) { - value++; - } - } - public static void main(String[] args) - throws Exception { - - ThreadTest t = new ThreadTest(); - t.start(); - Thread.sleep(2000); - t.stop = true; - System.out.println("value =" + t.value); - Thread.sleep(2000); - System.out.println("value =" + t.value); - - } - -} diff --git a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/Paxos.java b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/Paxos.java deleted file mode 100644 index 20ca1177dd..0000000000 --- a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/Paxos.java +++ /dev/null @@ -1,7 +0,0 @@ -package me.lzb.other.algorithm; - -/** - * Created by LZB on 2017/5/4. - */ -public class Paxos { -} diff --git a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/ConsistentHash.java b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/ConsistentHash.java deleted file mode 100644 index 01e7c8f8b2..0000000000 --- a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/ConsistentHash.java +++ /dev/null @@ -1,119 +0,0 @@ -package me.lzb.other.algorithm.consistenthash; - -import java.util.*; - -/** - * @author LZB - * @date 2017/5/9 - */ -public class ConsistentHash { - - private int vnNumber; - - private HashFunction hashFunction; - - private final SortedMap nodes = new TreeMap<>(); - - private int size; - - private final Map pns = new HashMap<>(); - - public ConsistentHash(int pnNum, int vnNum) { - this.hashFunction = new HashFunction(); - this.vnNumber = vnNum; - - //创建好节点 - int a = 100; - - for (int i = 0; i < pnNum; i++) { - PhysicalNode pn = new PhysicalNode("192.168.1." + a, "8888"); - pns.put(pn.getAddr(), pn); - addNode(pn); - a++; - } - } - - - public void addNode(PhysicalNode pn) { - //添加一个物理节点,增加vnNumber个虚拟节点 - for (int i = 0; i < this.vnNumber; i++) { - VirtualNode vn = new VirtualNode(pn, String.valueOf(i)); - nodes.put(hashFunction.hash(vn.getAddr()), vn); - } - - } - - public void removeNode(PhysicalNode node) { - for (int i = 0; i < this.vnNumber; i++) - nodes.remove(hashFunction.hash(node.getAddr() + "-" + i)); - } - - /** - * 获取顺时针方向最近的节点 - * - * @param key - * @return - */ - public VirtualNode getNode(String key) { - if (nodes.isEmpty()) { - return null; - } - - long hash = hashFunction.hash(key); - if (!nodes.containsKey(hash)) { - SortedMap tailMap = nodes.tailMap(hash); - hash = tailMap.isEmpty() ? nodes.firstKey() : tailMap.firstKey(); - } - return nodes.get(hash); - } - - public long getSize() { - return nodes.size(); - } - - - @Override - public String toString() { - StringBuffer sb = new StringBuffer(); - for (Long key : nodes.keySet()) { - VirtualNode n = nodes.get(key); - sb.append(n.getAddr()); - sb.append(" "); - sb.append(n.size()); - sb.append("\r\n"); - } - return sb.toString(); - } - - - public SortedMap getNodes() { - return this.nodes; - } - - public Map getPNodes() { - return this.pns; - } - - - public void put(String key, String value) { - VirtualNode node = getNode(key); - node.put(key, value); - size++; - } - - public void remove(String key) { - VirtualNode node = getNode(key); - node.remove(key); - size--; - } - - public String get(String key) { - VirtualNode node = getNode(key); - return node.get(key); - } - - public int size() { - return size; - } - -} diff --git a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/HashFunction.java b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/HashFunction.java deleted file mode 100644 index f17fd7957a..0000000000 --- a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/HashFunction.java +++ /dev/null @@ -1,48 +0,0 @@ -package me.lzb.other.algorithm.consistenthash; - -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - -/** - * @author LZB - * @date 2017/5/11 - */ -public class HashFunction { - - /** - * ketama - * @param key - * @return - */ - public long hash(String key) { - byte[] bytes = md5(key); - return ketama(bytes, 0); - - } - - private long ketama(byte[] digest, int nTime) { - //0xff = 255 - long a = ((long) (digest[3 + nTime * 4] & 0xFF) << 24) - | ((long) (digest[2 + nTime * 4] & 0xFF) << 16) - | ((long) (digest[1 + nTime * 4] & 0xFF) << 8) - | (digest[0 + nTime * 4] & 0xFF); - - return a & 0xffffffffL; - } - - //输入任意长的信息,输出128位的信息,不可逆,唯一 - private byte[] md5(String key) { - MessageDigest md5 = null; - if (md5 == null) { - try { - md5 = MessageDigest.getInstance("MD5"); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } - } - - md5.reset(); - md5.update(key.getBytes()); - return md5.digest(); - } -} diff --git a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/PhysicalNode.java b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/PhysicalNode.java deleted file mode 100644 index 0c3a286494..0000000000 --- a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/PhysicalNode.java +++ /dev/null @@ -1,46 +0,0 @@ -package me.lzb.other.algorithm.consistenthash; - -import java.util.HashMap; - -/** - * @author LZB - * @date 2017/5/10 - */ -public class PhysicalNode { - private String ip; - - private String port; - - private HashMap data = new HashMap<>(); - - public PhysicalNode(String ip, String port) { - this.ip = ip; - this.port = port; - } - - - public void put(String key, String value) { - data.put(key, value); - } - - public void remove(String key) { - data.remove(key); - } - - public String get(String key) { - return data.get(key); - } - - public String getAddr() { - return this.ip + ":" + this.port; - } - - public int size() { - return data.size(); - } - - public HashMap getDate(){ - return data; - } - -} diff --git a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/VirtualNode.java b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/VirtualNode.java deleted file mode 100644 index e409786389..0000000000 --- a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/algorithm/consistenthash/VirtualNode.java +++ /dev/null @@ -1,43 +0,0 @@ -package me.lzb.other.algorithm.consistenthash; - -/** - * @author LZB - * @date 2017/5/10 - */ -public class VirtualNode { - private PhysicalNode physicalNode; - - private String id; - - private int size; - - public VirtualNode(PhysicalNode node, String id) { - this.physicalNode = node; - this.id = id; - } - - - public void put(String key, String value) { - physicalNode.put(key, value); - size++; - } - - public void remove(String key) { - physicalNode.remove(key); - size--; - } - - public String get(String key) { - return physicalNode.get(key); - } - - public String getAddr() { - return physicalNode.getAddr() + "-" + id; - } - - public int size() { - return size; - } - - -} diff --git a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/graph/Graph.java b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/graph/Graph.java deleted file mode 100644 index 5ef6aba81a..0000000000 --- a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/graph/Graph.java +++ /dev/null @@ -1,366 +0,0 @@ -package me.lzb.other.graph; - -import java.util.*; - -/** - * 遍历无向图的所有最长一笔画 - * 深度优先,达到最深时,后退,继续搜索另一条路径 - * Created by LZB on 2017/4/8. - */ -public class Graph { - /** - * 换行符 - */ - private static final String NEWLINE = System.getProperty("line.separator"); - /** - * 路径分割符号 - */ - private static final String PATH_SEPARATOR = "->"; - - /** - * 顶点数目 - */ - private int vertexCount; - - /** - * 边的数目 - */ - private int edgeCount; - - /** - * 出现过路径的最长变数 - * 如果等于总边数,说明存在欧拉路径 - */ - int maxEdge = 0; - - /** - * 顶点数组,每个list是与顶点关联的所有边 - */ - private LinkedList[] edgeList; - - /** - * 边 - */ - private class Edge { - /** - * 边的id - */ - int id; - - /** - * 是否被正向搜索 - */ - boolean isSearched; - - /** - * 顶点v - */ - int v; - - /** - * 顶点b - */ - int w; - - /** - * 保存回滚操作中,被回滚的的路径方向,以及,前提路径 - * 因为在不同级别的回滚中,可能会有多条临时路径,所以用list存放 - * 顶点->顶点:路径id->路径id->路径id - * 1->2:0->1->2 - */ - ArrayList to = new ArrayList<>(); - - /** - * 构造函数 - * @param v 顶点v - * @param w 顶点w - */ - public Edge(int v, int w) { - this.v = v; - this.w = w; - isSearched = false; - id = edgeCount; - } - - - /** - * 在当前前提路径下,是否有 - * @param v0 出发顶点 - * @param P 前提路径 - * @return true false - */ - public boolean isFrom(int v0, String P) { - return isTheSameTo(v0, getAnotherV(v0), P); - } - - /** - * 临时路径是否相同 - * @param v0 出发顶点 - * @param v1 到达顶点 - * @param p 前提路径 - * @return true false - */ - public boolean isTheSameTo(int v0, int v1, String p) { - if (to.size() == 0) { - return false; - } - String ss = v0 + PATH_SEPARATOR + v1 + ":" + p; - for (String s : to) { - if (ss.equals(s)) { - return true; - } - } - return false; - } - - /** - * 删除临时路径 - * @param v0 出发顶点 - * @param v1 到达顶点 - * @param p 前提路径 - */ - public void removeTo(int v0, int v1, String p) { - if (to.size() == 0) { - return; - } - String ss = v0 + PATH_SEPARATOR + v1 + ":" + p; - for (Iterator iterator = to.iterator(); iterator.hasNext(); ) { - String s = iterator.next(); - if (ss.equals(s)) { - iterator.remove(); - return; - } - } - } - - /** - * 增加临时路径 - * @param v0 出发顶点 - * @param v1 到达顶点 - * @param p 前提路径 - */ - public void addTo(int v0, int v1, String p) { - String ss = v0 + PATH_SEPARATOR + v1 + ":" + p; - for (String s : to) { - if (ss.equals(s)) { - return; - } - } - to.add(ss); - - } - - /** - * 获取边的另外一条顶点 - * @param vertex - * @return - */ - public int getAnotherV(int vertex) { - if (vertex == v) { - return w; - } else { - return v; - } - } - - @Override - public boolean equals(Object obj) { - if (this == obj) return true; - if (obj == null || getClass() != obj.getClass()) return false; - Edge c = (Edge) obj; - return this.id == c.id; - } - - @Override - public int hashCode() { - return id; - } - } - - /** - * 构造函数 - * @param vertexNum 顶点总数 - * @param edgeCount 边的总数 - */ - public Graph(int vertexNum, int edgeCount) { - this.vertexCount = vertexNum; - this.edgeCount = 0; - edgeList = new LinkedList[edgeCount]; - for (int i = 0; i < edgeCount; i++) { - edgeList[i] = new LinkedList<>(); - } - } - - public void addEdge(int v1, int v2) { - Edge c = new Edge(v2, v1); - edgeList[v1].add(c); - edgeList[v2].add(c); - edgeCount++; - } - - - public void addEdge(int[][] edgeArray) { - for (int i = 0; i < edgeArray.length; i++) { - addEdge(edgeArray[i][0], edgeArray[i][1]); - } - } - - public String toString() { - StringBuilder s = new StringBuilder(); - s.append(vertexCount + " vertices, " + edgeCount + " edges " + NEWLINE); - for (int v = 0; v < vertexCount; v++) { - s.append(v + ": "); - for (Edge w : edgeList[v]) { - s.append(w.getAnotherV(v) + " "); - } - s.append(NEWLINE); - } - return s.toString(); - } - - - /** - * 更新出现过路径的最长边数 - * @param a - */ - private void updateMax(int a) { - if (a > maxEdge) { - maxEdge = a; - } - } - - - public boolean isEuler() { - int start = 0; - Stack stack = new Stack<>(); - stack.push(start); - - //TODO 退出递归的条件 -// try { - search(start, start, stack, new Stack<>()); -// }catch (EmptyStackException e){ - -// } - - System.out.println("最长边数:" + maxEdge); - return maxEdge == edgeCount; - } - - - - - /** - * 正向搜索 - * 传进去一个节点,顺着一条没有搜索过的边找到下一个节点。当搜索到死路时,回滚 - * @param v 当前提点 - * @param stack 当前路径的节点顺序 - * @param sp 当前路径的路径顺序 - */ - public void search(int start, int v, Stack stack, Stack sp) { - - LinkedList list = edgeList[v]; - - boolean anotherWay = false; - for (Edge w : list) { - if (!w.isSearched && !w.isTheSameTo(v, w.getAnotherV(v), getPath(sp))) { - anotherWay = true; - w.isSearched = true; - stack.push(w.getAnotherV(v)); - updateMax(sp.size()); - sp.push(w); - search(start, w.getAnotherV(v), stack, sp); - } - } - - if (!anotherWay) { - System.out.println("最长:==============================="); - rollback(start, stack, sp); - } - - } - - - - /** - * 回滚,回滚当上一个节点,如果当前节点有可以使用的边,调用搜索,如果没有,递归继续回滚 - * 如果需要递归回滚,回滚到第二级之前,清空所有,当前路径下,从该点出发的方向 - * 被回滚的路径,需要保存路径方向,以及提前路径 - * @param stack 当前路径的节点顺序 - * @param sp 当前路径的路径顺序 - */ - public void rollback(int start, Stack stack, Stack sp) { - - String ss = getPath(sp); - String output = "顶点:" + stack.toString() - + NEWLINE + "路径:" + ss - + NEWLINE; - System.out.println(output); - -// if(stack.size() == 1){ -// return; -// } - - - Edge e = sp.pop(); //需要回滚的路径 - String pp = getPath(sp); //前提路径 - - int vz = stack.pop(); - int vy = stack.peek(); - - boolean rollbakc2 = true; - - LinkedList l = edgeList[vy]; - - //判断当前节点是否存在空闲路径,是否要回滚两级 - //空闲路径:没有被正向搜索,也没有被缓存当前前提路径下,从改节点出发的方向 - for (Edge w : l) { - if (!w.isSearched && !w.isTheSameTo(vy, w.getAnotherV(vy), pp)) { - rollbakc2 = false; - break; - } - } - - - //回滚当前路径,回滚一级 - int r = vy; - for (Edge w : l) { - if (w.equals(e)) { - w.addTo(vy, vz, pp); - w.isSearched = false; - break; - } - } - - if (rollbakc2) { - //回滚两级, 清空所有,当前路径下,从该点出发的方向 - - for (Edge w : l) { - if (!w.isSearched && w.isFrom(vy, pp)) { - w.removeTo(vy, w.getAnotherV(vy), pp); - } - } - rollback(start, stack, sp); - } - - search(start, r, stack, sp); - - } - - public String getPath(Stack stack) { - String s = ""; - for (Edge x : stack) { - s = s + x.id + PATH_SEPARATOR; - } - s = s.replaceAll(PATH_SEPARATOR + "$", ""); - return s; - } - - - public static void main(String[] args) { - int[][] aa = new int[][]{{0, 1}, {0, 1}, {0, 3}, {1, 3}, {1, 2}, {1, 2}, {2, 3}}; - Graph g = new Graph(4, aa.length); - g.addEdge(aa); - System.out.println(g.toString()); - System.out.println(g.isEuler()); - } -} diff --git a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/lock/ReentrantTest1.java b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/lock/ReentrantTest1.java deleted file mode 100644 index 4b8b3d4ce8..0000000000 --- a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/lock/ReentrantTest1.java +++ /dev/null @@ -1,27 +0,0 @@ -package me.lzb.other.lock; - -/** - * Created by LZB on 2017/3/30. - */ -public class ReentrantTest1 implements Runnable{ - - public synchronized void get(){ - System.out.println(Thread.currentThread().getId()); - set(); - } - - public synchronized void set(){ - System.out.println(Thread.currentThread().getId()); - } - - @Override - public void run() { - get(); - } - public static void main(String[] args) { - ReentrantTest1 ss=new ReentrantTest1(); - new Thread(ss).start(); - new Thread(ss).start(); - new Thread(ss).start(); - } -} \ No newline at end of file diff --git a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/lock/ReentrantTest2.java b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/lock/ReentrantTest2.java deleted file mode 100644 index c630ea9e33..0000000000 --- a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/lock/ReentrantTest2.java +++ /dev/null @@ -1,35 +0,0 @@ -package me.lzb.other.lock; - -import java.util.concurrent.locks.ReentrantLock; - -/** - * Created by LZB on 2017/3/30. - */ -public class ReentrantTest2 implements Runnable { - ReentrantLock lock = new ReentrantLock(); - - public void get() { - lock.lock(); - System.out.println(Thread.currentThread().getId()); - set(); - lock.unlock(); - } - - public void set() { - lock.lock(); - System.out.println(Thread.currentThread().getId()); - lock.unlock(); - } - - @Override - public void run() { - get(); - } - - public static void main(String[] args) { - ReentrantTest2 ss = new ReentrantTest2(); - new Thread(ss).start(); - new Thread(ss).start(); - new Thread(ss).start(); - } -} \ No newline at end of file diff --git a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/MyInvocationHandler.java b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/MyInvocationHandler.java deleted file mode 100644 index d89298c786..0000000000 --- a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/MyInvocationHandler.java +++ /dev/null @@ -1,52 +0,0 @@ -package me.lzb.other.proxy; - -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Method; -import java.lang.reflect.Proxy; - -/** - * Created by LZB on 2017/3/29. - */ -public class MyInvocationHandler implements InvocationHandler { - - // 目标对象 - private Object target; - - /** - * 构造方法 - * - * @param target 目标对象 - */ - public MyInvocationHandler(Object target) { - super(); - this.target = target; - } - - - /** - * 执行目标对象的方法 - */ - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - - // 在目标对象的方法执行之前简单的打印一下 - System.out.println("------------------before------------------"); - - // 执行目标对象的方法 - Object result = method.invoke(target, args); - - // 在目标对象的方法执行之后简单的打印一下 - System.out.println("-------------------after------------------"); - - return result; - } - - /** - * 获取目标对象的代理对象 - * - * @return 代理对象 - */ - public Object getProxy() { - return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), - target.getClass().getInterfaces(), this); - } -} diff --git a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/UserService.java b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/UserService.java deleted file mode 100644 index d57431acab..0000000000 --- a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/UserService.java +++ /dev/null @@ -1,11 +0,0 @@ -package me.lzb.other.proxy; - -/** - * Created by LZB on 2017/3/29. - */ -public interface UserService { - /** - * 目标方法 - */ - void add(); -} diff --git a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/UserServiceImpl.java b/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/UserServiceImpl.java deleted file mode 100644 index 147abda59b..0000000000 --- a/group24/1148285693/learning2017/other/src/main/java/me/lzb/other/proxy/UserServiceImpl.java +++ /dev/null @@ -1,11 +0,0 @@ -package me.lzb.other.proxy; - -/** - * Created by LZB on 2017/3/29. - */ -public class UserServiceImpl implements UserService { - - public void add() { - System.out.println("--------------------addNode---------------"); - } -} \ No newline at end of file diff --git a/group24/1148285693/learning2017/other/src/test/java/me/lzb/other/algorithm/ConsistentHashTest.java b/group24/1148285693/learning2017/other/src/test/java/me/lzb/other/algorithm/ConsistentHashTest.java deleted file mode 100644 index 08902c756a..0000000000 --- a/group24/1148285693/learning2017/other/src/test/java/me/lzb/other/algorithm/ConsistentHashTest.java +++ /dev/null @@ -1,94 +0,0 @@ -package me.lzb.other.algorithm; - -import me.lzb.other.algorithm.consistenthash.ConsistentHash; -import me.lzb.other.algorithm.consistenthash.HashFunction; -import me.lzb.other.algorithm.consistenthash.PhysicalNode; -import org.junit.Test; - -import java.text.NumberFormat; -import java.util.HashMap; -import java.util.Map; - -/** - * @author LZB - * @date 2017/5/10 - */ -public class ConsistentHashTest { - - - @Test - public void hashFunctionTest() { - - HashFunction hashFunction = new HashFunction(); - - System.out.println(hashFunction.hash("192.168.1.100")); - System.out.println(hashFunction.hash("192.168.1.101")); - System.out.println(hashFunction.hash("192.168.1.102")); - System.out.println(hashFunction.hash("192.168.1.103")); - System.out.println(hashFunction.hash("192.168.1.104")); - } - - @Test - public void consistentHashTest() { - int all = 100000; - - ConsistentHash ch10 = new ConsistentHash(10, 5); - put(ch10, all); - - - ConsistentHash ch12 = new ConsistentHash(12, 5); - put(ch12, all); - - System.out.print("增加两台 "); - hitRate(ch10.getPNodes(), ch12.getPNodes(), all); - - System.out.print("减少两台 "); - ConsistentHash ch8 = new ConsistentHash(8, 5); - put(ch8, all); - - hitRate(ch10.getPNodes(), ch8.getPNodes(), all); - } - - - public static void put(ConsistentHash ch, int all) { - for (int i = 0; i < all; i++) { - ch.put(String.valueOf(i), "aaaaa" + i); - } - } - - public static void hitRate(Map m1, Map m2, int all) { - - Map s; - Map l; - - if (m1.size() < m2.size()) { - s = m1; - l = m2; - } else { - s = m2; - l = m1; - } - - int count = 0; - - for (String key : s.keySet()) { - - PhysicalNode sn = s.get(key); - PhysicalNode ln = l.get(key); - - HashMap sData = sn.getDate(); - - HashMap lData = ln.getDate(); - for (String k : sData.keySet()) { - if (lData.containsKey(k)) { - count++; - } - } - } - NumberFormat nf = NumberFormat.getPercentInstance(); - nf.setMinimumFractionDigits(2); - System.out.println("hit rate : " + (nf.format((double) count / (double) all))); - } - - -} diff --git a/group24/1148285693/learning2017/other/src/test/java/me/lzb/other/proxy/ProxyTest.java b/group24/1148285693/learning2017/other/src/test/java/me/lzb/other/proxy/ProxyTest.java deleted file mode 100644 index 0a01679ad3..0000000000 --- a/group24/1148285693/learning2017/other/src/test/java/me/lzb/other/proxy/ProxyTest.java +++ /dev/null @@ -1,25 +0,0 @@ -package me.lzb.other.proxy; - -import org.junit.Test; - -/** - * Created by LZB on 2017/3/29. - */ -public class ProxyTest { - - @Test - public void testProxy() throws Throwable { - // 实例化目标对象 - UserService userService = new UserServiceImpl(); - - // 实例化InvocationHandler - MyInvocationHandler invocationHandler = new MyInvocationHandler(userService); - - // 根据目标对象生成代理对象 - UserService proxy = (UserService) invocationHandler.getProxy(); - - // 调用代理对象的方法 - proxy.add(); - - } -} diff --git a/group24/1148285693/learning2017/pom.xml b/group24/1148285693/learning2017/pom.xml index d1b38a50b4..5fd8108696 100644 --- a/group24/1148285693/learning2017/pom.xml +++ b/group24/1148285693/learning2017/pom.xml @@ -1,160 +1,161 @@ - - - 4.0.0 - - me.lzb - learning2017 - 1.0 - learning2017 - pom - - https://github.com/lzbferrari/coding2017 - 2017编程提高 - - - - lzb - https://github.com/lzbferrari - lzbferrari@gmail.com - - - - - common - learning-basic - mini-jvm - other - - - - 1.8 - 1.8 - UTF-8 - UTF-8 - UTF-8 - - - - - - aliyun - aliyun - http://maven.aliyun.com/nexus/content/groups/public - - true - never - - - false - - - - - - - aliyun - aliyun - http://maven.aliyun.com/nexus/content/groups/public - - true - - - false - - - - - - - - - junit - junit - 4.12 - - - - - dom4j - dom4j - 1.6.1 - - - - jaxen - jaxen - 1.1.6 - - - - - commons-io - commons-io - 2.5 - - - org.apache.commons - commons-lang3 - 3.5 - - - commons-codec - commons-codec - 1.10 - - - org.apache.commons - commons-collections4 - 4.1 - - - - - org.apache.httpcomponents - httpclient - 4.5.3 - - - org.apache.zookeeper - zookeeper - 3.4.10 - - - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.6.1 - - ${java.version} - ${java.version} - UTF-8 - - - - - - org.apache.maven.plugins - maven-surefire-plugin - 2.19.1 - - - org.apache.maven.surefire - surefire-junit47 - 2.19.1 - - - - false - - - - - + + + 4.0.0 + + me.lzb + learning2017 + 1.0 + learning2017 + pom + + https://github.com/lzbferrari/coding2017 + 2017编程提高 + + + + lzb + https://github.com/lzbferrari + lzbferrari@gmail.com + + + + + common + learning-basic + mini-jvm + + + + 1.8 + 1.8 + UTF-8 + UTF-8 + UTF-8 + + + + + + aliyun + aliyun + http://maven.aliyun.com/nexus/content/groups/public + + true + never + + + false + + + + + + + aliyun + aliyun + http://maven.aliyun.com/nexus/content/groups/public + + true + + + false + + + + + + + + + junit + junit + 4.12 + + + + + dom4j + dom4j + 1.6.1 + + + + jaxen + jaxen + 1.1.6 + + + + + commons-io + commons-io + 2.5 + + + org.apache.commons + commons-lang3 + 3.5 + + + commons-codec + commons-codec + 1.10 + + + org.apache.commons + commons-collections4 + 4.1 + + + + + org.apache.httpcomponents + httpclient + 4.5.3 + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + ${java.version} + ${java.version} + UTF-8 + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.19.1 + + + + org.apache.maven.surefire + surefire-junit47 + 2.19.1 + + + + false + true + 1 + false + + ${java.io.tmpdir} + + + + + + \ No newline at end of file