- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的项目正在尝试编写 B 树。我在将树的节点持久保存到随机访问文件时遇到问题。我经常遇到 EOFExceptions
和 StreamCorruptionExceptions
。
我当前使用的一些资源是:
Converting any object to a byte array in java
https://inaved-momin.blogspot.com/2012/08/understanding-javaiostreamcorruptedexce.html
当前问题:从节点读取链接位置,然后尝试从随机访问文件中读取它会导致 StreamCorruptionExceptions
。
目标:能够访问随机访问文件中的所有节点,修改并写回到随机访问文件中。
测试.java
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//JsoupTestStringManipulating();
//testAdd();
//testFindPredecessor();
//testDelete();
//testPrefexFind();
//testSave();
testSave2();
}
public static void JsoupTestStringManipulating(){
JsoupParser pars = new JsoupParser();
String word = "Spike Spegal";
String URL = "http://en.wikipedia.org/wiki/Zombie";
for(String s: pars.urlTrimming(URL)){
System.out.println("words: "+s);
}
System.out.println(pars.getPadding(word.length()));
}
public static void testAdd() throws IOException, ClassNotFoundException{
BTree tree = new BTree();
String padding,fixedString;
ArrayList<String> testWords = new ArrayList<String>();
testWords.add("apple");
testWords.add("sand");
testWords.add("math");
testWords.add("tree");
testWords.add("north");
testWords.add("onion");
testWords.add("pan");
testWords.add("pink");
testWords.add("pool");
testWords.add("net");
testWords.add("never");
for(String k : testWords){
padding = getPadding(k.length());
fixedString = k + padding;
tree.insert(fixedString);
}
Node temp1,temp2,temp3,temp4,temp5,temp6,temp7,temp8,root;
root = tree.per.read(0);
temp1 = tree.per.read(root.links.get(0));
temp2 = tree.per.read(root.links.get(1));
temp3 = tree.per.read(temp1.links.get(0));
temp4 = tree.per.read(temp1.links.get(1));
temp5 = tree.per.read(temp1.links.get(2));
temp6 = tree.per.read(temp2.links.get(0));
temp7 = tree.per.read(temp2.links.get(1));
temp8 = tree.per.read(temp2.links.get(2));
/*
System.out.println("root: "+root.keys);
System.out.println("left: "+temp1.keys);
System.out.println("right: "+temp2.keys);
System.out.println("left 0: "+temp3.keys);
System.out.println(" left 1: "+temp4.keys);
System.out.println(" left 2: "+temp5.keys);
System.out.println(" right 0: "+temp6.keys);
System.out.println(" right 1: "+temp7.keys);
System.out.println(" right 2: "+temp8.keys);
System.out.println("node count: "+tree.getNodeCount());
*/
System.out.println("root: "+root.getStartIndex());
System.out.println("left: "+temp1.getStartIndex());
System.out.println("right: "+temp2.getStartIndex());
System.out.println("left 0: "+temp3.getStartIndex());
System.out.println(" left 1: "+temp4.getStartIndex());
System.out.println(" left 2: "+temp5.getStartIndex());
System.out.println(" right 0: "+temp6.getStartIndex());
System.out.println(" right 1: "+temp7.getStartIndex());
System.out.println(" right 2: "+temp8.getStartIndex());
System.out.println("node count: "+tree.getNodeCount());
}
public static void testFindPredecessor() throws IOException, ClassNotFoundException{
BTree tree = new BTree();
tree.insert("apple");
tree.insert("sand");
tree.insert("math");
tree.insert("tree");
tree.insert("north");
tree.insert("onion");
tree.insert("pan");
tree.insert("pink");
tree.insert("pool");
System.out.println("get predicessor: "+tree.getRoot().predacessor(0));
}
public static void testDelete() throws IOException, ClassNotFoundException{
BTree tree = new BTree();
tree.insert("apple");
tree.insert("sand");
tree.insert("math");
tree.insert("tree");
tree.insert("north");
tree.insert("onion");
tree.insert("pan");
tree.insert("pink");
tree.insert("pool");
tree.insert("net");
tree.insert("never");
tree.delete("apple");
/*
System.out.println("root: "+tree.getRoot().keys);
System.out.println("left: "+tree.getRoot().links.get(0).keys);
System.out.println("right: "+tree.getRoot().links.get(1).keys);
System.out.println(" left 0: "+tree.getRoot().links.get(0).links.get(0).keys);
System.out.println(" left 1: "+tree.getRoot().links.get(0).links.get(1).keys);
//System.out.println(" left 2: "+tree.getRoot().links.get(0).links.get(2).keys);
System.out.println(" right 0: "+tree.getRoot().links.get(1).links.get(0).keys);
System.out.println(" right 1: "+tree.getRoot().links.get(1).links.get(1).keys);
System.out.println(" right 2: "+tree.getRoot().links.get(1).links.get(2).keys);
*/
}
public static void testPrefexFind() throws IOException, ClassNotFoundException{
BTree tree = new BTree();
tree.insert("apple");
tree.insert("sand");
tree.insert("math");
tree.insert("tree");
tree.insert("north");
tree.insert("onion");
tree.insert("pan");
tree.insert("pink");
tree.insert("pool");
tree.insert("net");
tree.insert("never");
System.out.println(tree.findPrefix("s"));
}//end prefex test
private static String getPadding(int wordLength){
int diffrence;
String pad =" ";
String padding = "";
diffrence = 34 - wordLength;
for(int i =0; i < diffrence;i++){
padding = padding + pad;
}// end for
return padding;
}
public static void testSave() throws IOException,ClassNotFoundException{
Node node = new Node();
node.keys.add("apple");
node.keys.add("math");
node.keys.add("sand");
ByteArrayOutputStream b2 = new ByteArrayOutputStream();
ObjectOutputStream OOS = new ObjectOutputStream(b2);
OOS.writeObject(node);
byte[] array = b2.toByteArray();
ByteArrayInputStream in2 = new ByteArrayInputStream(array);
ObjectInputStream OIS = new ObjectInputStream(in2);
Node temp = (Node) OIS.readObject();
OIS.close();
for(String s : temp.keys){
System.out.println(s);
}
}
public static void testSave2() throws IOException,ClassNotFoundException{
ArrayList<Node>nodeList = new ArrayList<Node>();
RandomAccessFile raf = new RandomAccessFile("Test.dat","rw");
byte[] recivingAry;
Node node = new Node();
Node node2 = new Node();
node.setStartIndex(0);
node2.setStartIndex(560);
node.keys.add("apple");
node.keys.add("math");
node.keys.add("sand");
node2.keys.add("candle");
node2.keys.add("final");
node2.keys.add("better");
node.links.add((long) 560);
nodeList.add(node);
nodeList.add(node2);
ByteArrayOutputStream b2 = new ByteArrayOutputStream();
ObjectOutputStream OOS = new ObjectOutputStream(b2);
for(Node n : nodeList){
OOS.writeObject(n);
byte[] array = b2.toByteArray();
raf.write(array);
}
// byte[] array = b2.toByteArray();
// raf.write(array);
recivingAry = new byte[560];
raf.seek(0);
raf.read(recivingAry);
ByteArrayInputStream in2 = new ByteArrayInputStream(recivingAry);
ObjectInputStream OIS = new ObjectInputStream(in2);
Node temp = (Node) OIS.readObject();
if(temp.links.size()!= 0){
recivingAry = new byte[560];
//System.out.raf.length();
raf.seek(temp.links.get(0));
raf.read(recivingAry);
ByteArrayInputStream in1 = new ByteArrayInputStream(recivingAry);
ObjectInputStream OIS1 = new ObjectInputStream(in2);
Node temp1 = (Node) OIS.readObject();
for(String s1:temp1.keys){
System.out.println(s1);
}
}
OIS.close();
for(String s : temp.keys){
System.out.println(s);
}
}
}//end class
Node.java
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
// this needs to use the trees persist instead of its own.
public class Node implements Serializable{
private static final long serialVersionUID = 1L;
ArrayList<Node> temp = new ArrayList<Node>();
final int MAXKEYS = 3; //31
final int middle = MAXKEYS/2;
ArrayList<String> keys = new ArrayList<String>();
ArrayList<Long> links = new ArrayList<Long>();
int incrementSize =2048;//2364 - for 31 keys 32 links/ 394 - for 3 keys 4 links
long startIndex;
BTree tree;
public Node(){
}
public boolean isLeaf(){
if(links.size() == 0)
{
return true;
}
else
return false;
}// end is leaf
public boolean isFull(){
if(keys.size() == MAXKEYS)
{
return true;
}
else{
return false;
}
}//end isFull
public void split(Node link, int nodeCount) throws IOException{
Node right = new Node();
right.setStartIndex(nodeCount * incrementSize);
String middleVal;
int count = 0;
//get right
while(link.keys.size()> middle+1){
right.keys.add(link.keys.remove(middle+1));
}
//get the middle value
middleVal= link.keys.remove(link.keys.size()-1);
//compare the keys to the middle guy
for(String value: keys){
if(middleVal.compareTo(value)>0){
count++;
}
}
//add keys and links in proper spot
keys.add(count,middleVal);
links.add(count+1,(long) (nodeCount * incrementSize));
// i will need to send per the incrament size
tree.per.write(right);
}//end split
//look over how i write to splits
public void rootSplit(int nodeCount) throws IOException{
int leftCount = nodeCount -1;
long offLeft = (leftCount * incrementSize);
long offRight = (nodeCount * incrementSize);
Node myLeft = makeNewLeft();
Node myRight = makeNewRight();
if(!links.isEmpty()){
addLeftLinks(myLeft);
addRightLinks(myRight);
}
hookLinks(offLeft,offRight);
//send in the new incrament size
temp.add(myLeft);
temp.add(myRight);
//tree.per.write(myLeft);
//tree.per.write(myRight);
}//end split root
private Node makeNewLeft(){
int mid = middle;
Node left = new Node();
//get all the left keys
for(int i =0; i < mid;i++){
left.keys.add(keys.remove(i));
}//end for
return left;
}// end makeNewLeft
private void addLeftLinks(Node left){
int mid = middle+1;
for(int i =0; i<mid;i++){
left.links.add(links.remove(0));
}//end for
}//end add left links
private Node makeNewRight(){
Node right = new Node();
//get all the keys from the right
while(keys.size() >1){
right.keys.add(keys.remove(1));
}
return right;
}// end makeNewRight
private void addRightLinks(Node right){
right.links.addAll(links);
links.clear();
}//end add right links
// this needs to take in the incrament size
private void hookLinks(long offleft,long offright){
links.add(offleft);
links.add(offright);
}
public void internalRepair(int count) throws ClassNotFoundException, IOException{
Node temp,temp2;
long nodeLocA = links.get(count);
temp = tree.per.read(nodeLocA);
goRightRepair(temp,count+1);
for(int i = 0; i < links.size();i++){
long nodeLocB = links.get(i);
temp2 = tree.per.read(nodeLocB);
if(temp2.minSize()){
repair(i);
i = 0;
tree.per.write(temp2);
}
}
}// end internalRepair
//will repair upto the internal node
public void goRightRepair(Node myNode,int count) throws ClassNotFoundException, IOException{
if(myNode.isLeaf()){
return;
}//end if
else{
goRightRepair(tree.per.read(myNode.links.get(count)),count);
Node temp;
for(int i = 0; i < links.size();i++){
long nodeLocA = links.get(i);
temp = tree.per.read(nodeLocA);
if(temp.minSize()){
repair(i);
i = 0;
tree.per.write(temp);
}
}
}
}
public void repair(int count) throws ClassNotFoundException, IOException{
//write node back to file
Node temp = new Node();
Node temp2 = new Node();
long nodeLocA = links.get(count -1);
long nodeLocB = links.get(count +1);
temp = tree.per.read(nodeLocA);
temp2 = tree.per.read(nodeLocB);
if(count != 0 && temp.keys.size() > middle ){
rotateLeft(count);
tree.per.write(temp);
}
else if(temp2.keys.size() > middle){
rotateRight(count);
tree.per.write(temp2);
}
else{
merge(count);
}
}// end steal
// need to get the link before i remove the key---error
private void rotateLeft(int count) throws ClassNotFoundException, IOException{
String parentKey;
String replaceKey;
Node temp;
Node temp2;
long nodeLocA = links.get(count -1);
long nodeLocB = links.get(count);
int apple =tree.per.read(nodeLocA).keys.size()-1;
//get the link to the deficient right node
temp2 = tree.per.read(nodeLocA);
temp = tree.per.read(nodeLocB);
// the parent key
parentKey = keys.remove(count -1 );
// parent key is placed in deficient right node brining it to minimum
temp.keys.add(0,parentKey);
// get the key from the over full left node
replaceKey = temp2.keys.remove(apple);
//put the new key in the proper spot.
keys.add(count -1,replaceKey);
//write node back to file
tree.per.write(temp);
tree.per.write(temp2);
}
private void rotateRight(int count) throws ClassNotFoundException, IOException{
String parentKey;
String replaceKey;
long nodeLocA = links.get(count);
long nodeLocB = links.get(count+1);
Node temp = tree.per.read(nodeLocA);
Node temp2 = tree.per.read(nodeLocB);
//the parent key
parentKey = keys.remove(count);
//parent key is placed in deficient left node brining it to minimum
temp.keys.add(parentKey);
//get the key from the over full right node
replaceKey = temp2.keys.remove(0);
//put the new key in the proper spot
keys.add(count,replaceKey);
//write node back to file
tree.per.write(temp);
tree.per.write(temp2);
}
private void merge(int count) throws ClassNotFoundException, IOException{
String parentKey;
Node temp,temp2;
long nodeLocA = links.get(count+1);
long nodeLocB = links.get(count);
temp = tree.per.read(nodeLocA);
temp2 = tree.per.read(nodeLocB);
//get the parentKey
parentKey = keys.remove(count);
//put parentKey into right link
temp.keys.add(0,parentKey);
// put left values into right values
for(String s: temp2.keys) {
temp.keys.add(0,s);
}// end for
//left links go into rights links
for(long Link: temp2.links){
temp.links.add(0,Link);
}// end for
links.remove(count);
tree.per.write(temp);
}// end merge
public String predacessor(int count) throws ClassNotFoundException, IOException{
long nodeLocA = links.get(count);
Node temp = tree.per.read(nodeLocA);
return goRight(temp,count+1);
}
public String goRight(Node myNode,int count) throws ClassNotFoundException, IOException{
String toReturn;
if(myNode.isLeaf()){
toReturn = myNode.keys.remove(myNode.keys.size()-1);
}//end if
else{
toReturn = goRight(tree.per.read(myNode.links.get(count)),count);
}
return toReturn;
}
public boolean minSize(){
boolean result;
if(keys.size() < MAXKEYS/2){
result = true;
}
else{
result = false;
}
return result;
}// end minSize
public void setStartIndex(long startIndex){
this.startIndex = startIndex;
}
public long getStartIndex(){
return startIndex;
}
public ArrayList<Node> getNode(){
return temp;
}
}//end node
最佳答案
我首先要尝试的是缩小问题的范围。是在序列化还是在写文件。我的第一个测试是写入字节数组,然后从同一数组中读取。
public class Persistance implements Serializable {
.....
public void test(Node node) throws IOException{
ByteArrayOutputStream b2= new ByteArrayOutputStream();
ObjectOutput out2 = new ObjectOutputStream(b2);
out2.writeObject(node);
out2.close();
byte[] array2 = b.toByteArray();
ByteArrayInputStream in2 = new ByteArrayInputStream(array2);
ObjectInputStream OIS2 = new ObjectInputStream(in2);
Node temp = (Node) OIS2.readObject();
OIS2.close();
}
.....
}
如果有效,那么它是您的随机访问文件的问题。有些事情需要检查,写入然后读取字节序列是否会给出完全相同的字节序列。如果您写入文件的中间,则在您刚刚写入的内容之后会有字节。写入和读取的偏移量是否相同。
节点大小应该不重要。无论大小如何,各种 ArrayList 的序列化和反序列化都应该有效。您可以做的是写入字节数组并使用 count
字段找出长度。然后,当您写入文件时,首先写入字节数,然后写入字节数组。要读取,首先读取字节数,然后读取那么多字节。
ByteArrayOutputStream b2= new ByteArrayOutputStream();
ObjectOutput out2 = new ObjectOutputStream(b2);
out2.writeObject(node);
out2.close();
byte[] array2 = b2.toByteArray();
int length = array2.length;
raf.writeInt(length);
raf.write(array2);
阅读
int length = raf.readInt();
byte inArray[] = new byte[length];
raf.read(inArray,0,length);
ByteArrayInputStream in2 = new ByteArrayInputStream(inArray);
ObjectInputStream OIS2 = new ObjectInputStream(in2);
Node temp = (Node) OIS2.readObject();
OIS2.close();
关于java - 将 B 树节点持久保存到 RandomAccessFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22903218/
我正在使用 JavaFX 8 创建一个应用程序。我使用拖/放动态更改网格 Pane 的内容。我希望每行或每行/列迭代 GridPane 内容。JavaFX 允许通过指定行和列在 GridPane 中添
我正在尝试将图像拖放到div上。图像没有被拖到div上并给出以下错误 Uncaught TypeError: Failed to execute 'appendChild' on 'Node': pa
我正在 android studio 中创建内部构建 AR 导航。我正在寻找一种方法将 anchor 与其他 anchor 或 anchor 节点/节点“连接”起来。我不确定使用哪一个。基于我将强制用
我在 Hive 上运行一些作业:首先是 4 节点,然后是 2 节点。令我惊讶的是,我的 2 节点性能比我的 4 节点更好。 首先,我在一个 4 节点(4 个事件节点)上运行查询,然后关闭 2 个节点(
我有 Node* current ,我在其中存储指向列表“顶部”当前节点的指针。当我将一个新节点设置为当前节点时,出现错误: '=' : cannot convert from 'CircularDo
我是 dcos Mesos 的新手,在本地 Ubuntu 机器上安装了 dc os。 我可以查看 dcos 仪表板。 但我无法使用 dcos node ssh --master-proxy --lea
在 JavaFX 中,是否有类似 setLayout(); 的东西?或 setBounds(); ? 例如,我想将按钮定位到我想要的位置。 最佳答案 JavaFX 场景图上的所有内容都是 Node .
我正在开发一个 JavaFX 应用程序,其中我开发的类(从 javafx.scene.Parent 扩展)是根据用户在 ListView 控件中单击的条目动态创建的。 只是要清楚这个节点,它不是使用像
我正在尝试为节点-边缘关系创建一个类图,因为它可以在有向图中找到。我想传达的是,Nodes 引用了 Edges,Edges 也引用了 Nodes。每个 Edge 都恰好需要两个 Node(源和目标)。
在mapreduce作业期间,单个任务将在随机节点上运行,是否有任何方法限制应在其中运行任务的节点? 最佳答案 Hadoop不会选择节点来随机运行任务。考虑到数据局部性,否则将有很多网络开销。 任务与
有什么区别: a) nodetool 重建 b) nodetool 修复 [-pr] 换句话来说,各个命令到底是做什么的? 最佳答案 nodetool重建:类似于引导过程(当您向集群添加新节点时),但
我已将第一个 OneToMany 关系添加到我的 hibernate 3.6.10 项目中。这是一个类: /** * */ package com.heavyweightsoftware.leal
是否有可能找到正在监听触发当前函数的事件的元素? 在下面的代码中,event.target 返回 #xScrollPane 和 event.currentTarget 和 event 的最低子节点.f
我正在尝试覆盖我数据库中的一些数据。结构很简单,就是: recipes { user_1{ recipe_1{data} recipe_2{data} } user_2{
我使用 setInterval 来运行该函数,但它会多次执行函数 2... 如何在输入中插入一个值后执行函数 第一个输入与其余输入的距离不同 如何在插入 val(tab 选项)后将插入从 1 个输入移
我不知道代码有什么问题,但在 visual studio 中不断收到这些错误消息。 Error 18 error C1903: unable to recover from previous e
我正在尝试从其类中获取 SharePoint 搜索导航节点的对象。 var nodes = $("div.ms-qSuggest-listItem"); 我正在获取节点对象,现在想要获取“_promp
D:\nodeP>node main.js module.js:327 抛出错误; ^ 错误:在 Function.Module 的 Function.Module._resolveFilename
struct node{ int key, prior, cnt, val; node *l, *r; node(){} node(int nkey) : key(nkey),
我有以下代码使用迭代器将项目插入双链表。这就是我们被要求这样做的方式。代码有效,但问题是我有 24 字节的绝对内存泄漏。 NodeIterator insert(NodeIterator & itrP
我是一名优秀的程序员,十分优秀!