-
Notifications
You must be signed in to change notification settings - Fork 641
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
zhunan
committed
Apr 9, 2017
1 parent
7b5c997
commit 3274dca
Showing
3 changed files
with
194 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package week4; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.FileInputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by zndbl on 2017/4/9. | ||
*/ | ||
public class ClassFileLoader { | ||
|
||
private static final int BUFFER_SIZE = 1024; | ||
|
||
private List<String> clzPaths = new ArrayList<String>(); | ||
|
||
public byte[] readBinaryCode(String className) { | ||
for (String classPath : clzPaths) { | ||
String fileName = classPath + className.replace(".", "\\") + ".class"; | ||
BufferedInputStream bis = null; | ||
ByteArrayOutputStream baos = null; | ||
try { | ||
bis = new BufferedInputStream(new FileInputStream(fileName), BUFFER_SIZE); | ||
baos = new ByteArrayOutputStream(); | ||
byte[] buffer = new byte[BUFFER_SIZE]; | ||
int len = 0; | ||
while ((len = bis.read(buffer)) != -1) { | ||
baos.write(buffer, 0, len); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return baos.toByteArray(); | ||
} | ||
return null; | ||
} | ||
|
||
public void addClassPath(String classPath) { | ||
if (classPath == null) { | ||
return; | ||
} | ||
clzPaths.add(classPath); | ||
} | ||
|
||
public String getClassPath() { | ||
StringBuilder sb = new StringBuilder(); | ||
int size = clzPaths.size(); | ||
for (int i = 0; i < size; i++) { | ||
sb.append(clzPaths.get(i)); | ||
if (i + 1 < size) { | ||
sb.append(";"); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
} |
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,136 @@ | ||
package week4; | ||
|
||
/** | ||
* 用双向链表实现LRU算法 | ||
* | ||
* @author liuxin | ||
*/ | ||
class LruPageFrame { | ||
|
||
private static class Node { | ||
Node prev; | ||
Node next; | ||
int pageNum; | ||
|
||
Node() { | ||
} | ||
|
||
Node(int pageNum) { | ||
this.pageNum = pageNum; | ||
} | ||
} | ||
|
||
private int capacity; | ||
|
||
|
||
private Node first;// 链表头 | ||
private Node last;// 链表尾 | ||
|
||
|
||
public LruPageFrame(int capacity) { | ||
|
||
this.capacity = capacity; | ||
|
||
} | ||
|
||
/** | ||
* 获取缓存中对象 | ||
* | ||
* @param pageNum | ||
* @return | ||
*/ | ||
public void access(int pageNum) { | ||
if (first == null) { | ||
first = new Node(pageNum); | ||
first.next = first.prev = null; | ||
last = first; | ||
} else if (size() < capacity) { | ||
Node node = new Node(pageNum); | ||
if (last.prev == null) { | ||
last.prev = node; | ||
node.next = last; | ||
} else { | ||
node.next = first; | ||
first.prev = node; | ||
node.prev = null; | ||
} | ||
first = node; | ||
} else if (size() >= capacity) { | ||
Node node = getNode(pageNum); | ||
LRU(node, pageNum); | ||
} | ||
} | ||
|
||
/** | ||
* lru算法 | ||
* | ||
* @param node | ||
* @param pageNum | ||
*/ | ||
private void LRU(Node node, int pageNum) { | ||
if (node != null) { | ||
if (last.pageNum == node.pageNum) { | ||
last = node.prev; | ||
last.next = null; | ||
node.next = first; | ||
first.prev = node; | ||
node.prev = null; | ||
first = node; | ||
} else if (last.pageNum != node.pageNum && first.pageNum != node.pageNum) { | ||
node.prev.next = node.next; | ||
node.next.prev = node.prev; | ||
node.prev = null; | ||
node.next = first; | ||
first = node; | ||
} | ||
} else { | ||
last = last.prev; | ||
last.next = null; | ||
node = new Node(pageNum); | ||
node.next = first; | ||
first.prev = node; | ||
first = node; | ||
} | ||
} | ||
|
||
/** | ||
* 根据数据在缓存中获取节点 | ||
* | ||
* @param pageNum | ||
* @return | ||
*/ | ||
private Node getNode(int pageNum) { | ||
Node node = first; | ||
while (node != null) { | ||
if (node.pageNum == pageNum) { | ||
return node; | ||
} | ||
node = node.next; | ||
} | ||
return null; | ||
} | ||
|
||
public int size() { | ||
int num = 0; | ||
Node node = first; | ||
while (node != null) { | ||
num++; | ||
node = node.next; | ||
} | ||
return num; | ||
} | ||
|
||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
Node node = first; | ||
while (node != null) { | ||
sb.append(node.pageNum); | ||
node = node.next; | ||
if (node != null) { | ||
sb.append(","); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
http://note.youdao.com/noteshare?id=63cfdc5e194deb6458f9733681088516 ���� | ||
http://note.youdao.com/noteshare?id=3078d5c9a9f86e590973ee40ba3fdb25 ��������������� | ||
http://note.youdao.com/noteshare?id=8fb7ea6223b152c647f09dc98882751b lucene���� | ||
http://note.youdao.com/noteshare?id=930aef92deb715a6bbb265bc1799959e mongodb��mysql�ļȽ� |