Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.61 KB | None | 0 0
  1. import java.io.ByteArrayInputStream;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.io.ObjectInputStream;
  5. import java.io.ObjectOutputStream;
  6. import java.io.RandomAccessFile;
  7. import java.util.ArrayList;
  8.  
  9. public class Test {
  10.  
  11. public static void main(String[] args) throws IOException, ClassNotFoundException {
  12. //JsoupTestStringManipulating();
  13. //testAdd();
  14. //testFindPredecessor();
  15. //testDelete();
  16. //testPrefexFind();
  17. //testSave();
  18. testSave2();
  19.  
  20. }
  21.  
  22. public static void JsoupTestStringManipulating(){
  23. JsoupParser pars = new JsoupParser();
  24. String word = "Spike Spegal";
  25. String URL = "http://en.wikipedia.org/wiki/Zombie";
  26.  
  27. for(String s: pars.urlTrimming(URL)){
  28. System.out.println("words: "+s);
  29. }
  30.  
  31. System.out.println(pars.getPadding(word.length()));
  32. }
  33.  
  34. public static void testAdd() throws IOException, ClassNotFoundException{
  35. BTree tree = new BTree();
  36. String padding,fixedString;
  37. ArrayList<String> testWords = new ArrayList<String>();
  38. testWords.add("apple");
  39. testWords.add("sand");
  40. testWords.add("math");
  41. testWords.add("tree");
  42. testWords.add("north");
  43. testWords.add("onion");
  44. testWords.add("pan");
  45. testWords.add("pink");
  46. testWords.add("pool");
  47. testWords.add("net");
  48. testWords.add("never");
  49.  
  50. for(String k : testWords){
  51. padding = getPadding(k.length());
  52. fixedString = k + padding;
  53. tree.insert(fixedString);
  54. }
  55.  
  56. Node temp1,temp2,temp3,temp4,temp5,temp6,temp7,temp8,root;
  57. root = tree.per.read(0);
  58. temp1 = tree.per.read(root.links.get(0));
  59. temp2 = tree.per.read(root.links.get(1));
  60. temp3 = tree.per.read(temp1.links.get(0));
  61. temp4 = tree.per.read(temp1.links.get(1));
  62. temp5 = tree.per.read(temp1.links.get(2));
  63. temp6 = tree.per.read(temp2.links.get(0));
  64. temp7 = tree.per.read(temp2.links.get(1));
  65. temp8 = tree.per.read(temp2.links.get(2));
  66.  
  67. /*
  68. System.out.println("root: "+root.keys);
  69. System.out.println("left: "+temp1.keys);
  70. System.out.println("right: "+temp2.keys);
  71. System.out.println("left 0: "+temp3.keys);
  72. System.out.println(" left 1: "+temp4.keys);
  73. System.out.println(" left 2: "+temp5.keys);
  74. System.out.println(" right 0: "+temp6.keys);
  75. System.out.println(" right 1: "+temp7.keys);
  76. System.out.println(" right 2: "+temp8.keys);
  77. System.out.println("node count: "+tree.getNodeCount());
  78. */
  79.  
  80. System.out.println("root: "+root.getStartIndex());
  81. System.out.println("left: "+temp1.getStartIndex());
  82. System.out.println("right: "+temp2.getStartIndex());
  83. System.out.println("left 0: "+temp3.getStartIndex());
  84. System.out.println(" left 1: "+temp4.getStartIndex());
  85. System.out.println(" left 2: "+temp5.getStartIndex());
  86. System.out.println(" right 0: "+temp6.getStartIndex());
  87. System.out.println(" right 1: "+temp7.getStartIndex());
  88. System.out.println(" right 2: "+temp8.getStartIndex());
  89. System.out.println("node count: "+tree.getNodeCount());
  90.  
  91. }
  92. public static void testFindPredecessor() throws IOException, ClassNotFoundException{
  93. BTree tree = new BTree();
  94. tree.insert("apple");
  95. tree.insert("sand");
  96. tree.insert("math");
  97. tree.insert("tree");
  98. tree.insert("north");
  99. tree.insert("onion");
  100. tree.insert("pan");
  101. tree.insert("pink");
  102. tree.insert("pool");
  103. System.out.println("get predicessor: "+tree.getRoot().predacessor(0));
  104. }
  105. public static void testDelete() throws IOException, ClassNotFoundException{
  106. BTree tree = new BTree();
  107. tree.insert("apple");
  108. tree.insert("sand");
  109. tree.insert("math");
  110. tree.insert("tree");
  111. tree.insert("north");
  112. tree.insert("onion");
  113. tree.insert("pan");
  114. tree.insert("pink");
  115. tree.insert("pool");
  116. tree.insert("net");
  117. tree.insert("never");
  118. tree.delete("apple");
  119. /*
  120. System.out.println("root: "+tree.getRoot().keys);
  121. System.out.println("left: "+tree.getRoot().links.get(0).keys);
  122. System.out.println("right: "+tree.getRoot().links.get(1).keys);
  123. System.out.println(" left 0: "+tree.getRoot().links.get(0).links.get(0).keys);
  124. System.out.println(" left 1: "+tree.getRoot().links.get(0).links.get(1).keys);
  125. //System.out.println(" left 2: "+tree.getRoot().links.get(0).links.get(2).keys);
  126. System.out.println(" right 0: "+tree.getRoot().links.get(1).links.get(0).keys);
  127. System.out.println(" right 1: "+tree.getRoot().links.get(1).links.get(1).keys);
  128. System.out.println(" right 2: "+tree.getRoot().links.get(1).links.get(2).keys);
  129. */
  130. }
  131.  
  132. public static void testPrefexFind() throws IOException, ClassNotFoundException{
  133. BTree tree = new BTree();
  134. tree.insert("apple");
  135. tree.insert("sand");
  136. tree.insert("math");
  137. tree.insert("tree");
  138. tree.insert("north");
  139. tree.insert("onion");
  140. tree.insert("pan");
  141. tree.insert("pink");
  142. tree.insert("pool");
  143. tree.insert("net");
  144. tree.insert("never");
  145.  
  146. System.out.println(tree.findPrefix("s"));
  147. }//end prefex test
  148. private static String getPadding(int wordLength){
  149. int diffrence;
  150. String pad =" ";
  151. String padding = "";
  152. diffrence = 34 - wordLength;
  153. for(int i =0; i < diffrence;i++){
  154. padding = padding + pad;
  155. }// end for
  156. return padding;
  157. }
  158.  
  159. public static void testSave() throws IOException,ClassNotFoundException{
  160. Node node = new Node();
  161. node.keys.add("apple");
  162. node.keys.add("math");
  163. node.keys.add("sand");
  164.  
  165. ByteArrayOutputStream b2 = new ByteArrayOutputStream();
  166. ObjectOutputStream OOS = new ObjectOutputStream(b2);
  167. OOS.writeObject(node);
  168. byte[] array = b2.toByteArray();
  169. ByteArrayInputStream in2 = new ByteArrayInputStream(array);
  170. ObjectInputStream OIS = new ObjectInputStream(in2);
  171. Node temp = (Node) OIS.readObject();
  172. OIS.close();
  173.  
  174. for(String s : temp.keys){
  175. System.out.println(s);
  176. }
  177. }
  178.  
  179. public static void testSave2() throws IOException,ClassNotFoundException{
  180. ArrayList<Node>nodeList = new ArrayList<Node>();
  181. RandomAccessFile raf = new RandomAccessFile("Test.dat","rw");
  182. byte[] recivingAry;
  183. Node node = new Node();
  184. Node node2 = new Node();
  185. node.setStartIndex(0);
  186. node2.setStartIndex(560);
  187. node.keys.add("apple");
  188. node.keys.add("math");
  189. node.keys.add("sand");
  190. node2.keys.add("candle");
  191. node2.keys.add("final");
  192. node2.keys.add("better");
  193. node.links.add((long) 560);
  194. nodeList.add(node);
  195. nodeList.add(node2);
  196.  
  197.  
  198. ByteArrayOutputStream b2 = new ByteArrayOutputStream();
  199. ObjectOutputStream OOS = new ObjectOutputStream(b2);
  200. for(Node n : nodeList){
  201. OOS.writeObject(n);
  202. byte[] array = b2.toByteArray();
  203. raf.write(array);
  204. }
  205. // byte[] array = b2.toByteArray();
  206. // raf.write(array);
  207. recivingAry = new byte[560];
  208. raf.seek(0);
  209. raf.read(recivingAry);
  210. ByteArrayInputStream in2 = new ByteArrayInputStream(recivingAry);
  211. ObjectInputStream OIS = new ObjectInputStream(in2);
  212. Node temp = (Node) OIS.readObject();
  213. if(temp.links.size()!= 0){
  214. recivingAry = new byte[560];
  215. //System.out.raf.length();
  216. raf.seek(temp.links.get(0));
  217. raf.read(recivingAry);
  218. ByteArrayInputStream in1 = new ByteArrayInputStream(recivingAry);
  219. ObjectInputStream OIS1 = new ObjectInputStream(in2);
  220. Node temp1 = (Node) OIS.readObject();
  221.  
  222. for(String s1:temp1.keys){
  223. System.out.println(s1);
  224.  
  225. }
  226. }
  227. OIS.close();
  228.  
  229. for(String s : temp.keys){
  230. System.out.println(s);
  231. }
  232. }
  233. }//end class
  234.  
  235. import java.io.IOException;
  236. import java.io.Serializable;
  237. import java.util.ArrayList;
  238. // this needs to use the trees persist instead of its own.
  239. public class Node implements Serializable{
  240. private static final long serialVersionUID = 1L;
  241. ArrayList<Node> temp = new ArrayList<Node>();
  242. final int MAXKEYS = 3; //31
  243. final int middle = MAXKEYS/2;
  244. ArrayList<String> keys = new ArrayList<String>();
  245. ArrayList<Long> links = new ArrayList<Long>();
  246. int incrementSize =2048;//2364 - for 31 keys 32 links/ 394 - for 3 keys 4 links
  247. long startIndex;
  248. BTree tree;
  249. public Node(){
  250.  
  251. }
  252. public boolean isLeaf(){
  253. if(links.size() == 0)
  254. {
  255. return true;
  256. }
  257. else
  258. return false;
  259. }// end is leaf
  260.  
  261. public boolean isFull(){
  262. if(keys.size() == MAXKEYS)
  263. {
  264. return true;
  265. }
  266. else{
  267. return false;
  268. }
  269. }//end isFull
  270.  
  271. public void split(Node link, int nodeCount) throws IOException{
  272. Node right = new Node();
  273. right.setStartIndex(nodeCount * incrementSize);
  274. String middleVal;
  275. int count = 0;
  276. //get right
  277. while(link.keys.size()> middle+1){
  278. right.keys.add(link.keys.remove(middle+1));
  279. }
  280. //get the middle value
  281. middleVal= link.keys.remove(link.keys.size()-1);
  282.  
  283. //compare the keys to the middle guy
  284. for(String value: keys){
  285. if(middleVal.compareTo(value)>0){
  286. count++;
  287. }
  288. }
  289. //add keys and links in proper spot
  290. keys.add(count,middleVal);
  291. links.add(count+1,(long) (nodeCount * incrementSize));
  292. // i will need to send per the incrament size
  293. tree.per.write(right);
  294. }//end split
  295.  
  296. //look over how i write to splits
  297. public void rootSplit(int nodeCount) throws IOException{
  298. int leftCount = nodeCount -1;
  299. long offLeft = (leftCount * incrementSize);
  300. long offRight = (nodeCount * incrementSize);
  301. Node myLeft = makeNewLeft();
  302. Node myRight = makeNewRight();
  303. if(!links.isEmpty()){
  304. addLeftLinks(myLeft);
  305. addRightLinks(myRight);
  306. }
  307. hookLinks(offLeft,offRight);
  308.  
  309. //send in the new incrament size
  310. temp.add(myLeft);
  311. temp.add(myRight);
  312. //tree.per.write(myLeft);
  313. //tree.per.write(myRight);
  314. }//end split root
  315.  
  316. private Node makeNewLeft(){
  317. int mid = middle;
  318. Node left = new Node();
  319. //get all the left keys
  320. for(int i =0; i < mid;i++){
  321. left.keys.add(keys.remove(i));
  322. }//end for
  323. return left;
  324. }// end makeNewLeft
  325.  
  326. private void addLeftLinks(Node left){
  327. int mid = middle+1;
  328. for(int i =0; i<mid;i++){
  329. left.links.add(links.remove(0));
  330. }//end for
  331. }//end add left links
  332.  
  333. private Node makeNewRight(){
  334. Node right = new Node();
  335. //get all the keys from the right
  336. while(keys.size() >1){
  337. right.keys.add(keys.remove(1));
  338. }
  339. return right;
  340. }// end makeNewRight
  341.  
  342. private void addRightLinks(Node right){
  343. right.links.addAll(links);
  344. links.clear();
  345. }//end add right links
  346.  
  347. // this needs to take in the incrament size
  348. private void hookLinks(long offleft,long offright){
  349. links.add(offleft);
  350. links.add(offright);
  351. }
  352.  
  353. public void internalRepair(int count) throws ClassNotFoundException, IOException{
  354. Node temp,temp2;
  355. long nodeLocA = links.get(count);
  356. temp = tree.per.read(nodeLocA);
  357. goRightRepair(temp,count+1);
  358. for(int i = 0; i < links.size();i++){
  359. long nodeLocB = links.get(i);
  360. temp2 = tree.per.read(nodeLocB);
  361. if(temp2.minSize()){
  362. repair(i);
  363. i = 0;
  364. tree.per.write(temp2);
  365. }
  366. }
  367. }// end internalRepair
  368.  
  369. //will repair upto the internal node
  370. public void goRightRepair(Node myNode,int count) throws ClassNotFoundException, IOException{
  371. if(myNode.isLeaf()){
  372. return;
  373. }//end if
  374. else{
  375. goRightRepair(tree.per.read(myNode.links.get(count)),count);
  376. Node temp;
  377. for(int i = 0; i < links.size();i++){
  378. long nodeLocA = links.get(i);
  379. temp = tree.per.read(nodeLocA);
  380. if(temp.minSize()){
  381. repair(i);
  382. i = 0;
  383. tree.per.write(temp);
  384.  
  385. }
  386. }
  387. }
  388. }
  389.  
  390. public void repair(int count) throws ClassNotFoundException, IOException{
  391. //write node back to file
  392. Node temp = new Node();
  393. Node temp2 = new Node();
  394. long nodeLocA = links.get(count -1);
  395. long nodeLocB = links.get(count +1);
  396. temp = tree.per.read(nodeLocA);
  397. temp2 = tree.per.read(nodeLocB);
  398. if(count != 0 && temp.keys.size() > middle ){
  399. rotateLeft(count);
  400. tree.per.write(temp);
  401. }
  402. else if(temp2.keys.size() > middle){
  403. rotateRight(count);
  404. tree.per.write(temp2);
  405. }
  406. else{
  407. merge(count);
  408. }
  409. }// end steal
  410. // need to get the link before i remove the key---error
  411. private void rotateLeft(int count) throws ClassNotFoundException, IOException{
  412. String parentKey;
  413. String replaceKey;
  414. Node temp;
  415. Node temp2;
  416. long nodeLocA = links.get(count -1);
  417. long nodeLocB = links.get(count);
  418. int apple =tree.per.read(nodeLocA).keys.size()-1;
  419.  
  420. //get the link to the deficient right node
  421. temp2 = tree.per.read(nodeLocA);
  422. temp = tree.per.read(nodeLocB);
  423.  
  424. // the parent key
  425. parentKey = keys.remove(count -1 );
  426. // parent key is placed in deficient right node brining it to minimum
  427. temp.keys.add(0,parentKey);
  428. // get the key from the over full left node
  429. replaceKey = temp2.keys.remove(apple);
  430.  
  431. //put the new key in the proper spot.
  432. keys.add(count -1,replaceKey);
  433.  
  434. //write node back to file
  435. tree.per.write(temp);
  436. tree.per.write(temp2);
  437. }
  438.  
  439. private void rotateRight(int count) throws ClassNotFoundException, IOException{
  440. String parentKey;
  441. String replaceKey;
  442. long nodeLocA = links.get(count);
  443. long nodeLocB = links.get(count+1);
  444. Node temp = tree.per.read(nodeLocA);
  445. Node temp2 = tree.per.read(nodeLocB);
  446. //the parent key
  447. parentKey = keys.remove(count);
  448. //parent key is placed in deficient left node brining it to minimum
  449. temp.keys.add(parentKey);
  450. //get the key from the over full right node
  451. replaceKey = temp2.keys.remove(0);
  452. //put the new key in the proper spot
  453. keys.add(count,replaceKey);
  454.  
  455. //write node back to file
  456. tree.per.write(temp);
  457. tree.per.write(temp2);
  458. }
  459.  
  460. private void merge(int count) throws ClassNotFoundException, IOException{
  461. String parentKey;
  462. Node temp,temp2;
  463. long nodeLocA = links.get(count+1);
  464. long nodeLocB = links.get(count);
  465.  
  466. temp = tree.per.read(nodeLocA);
  467. temp2 = tree.per.read(nodeLocB);
  468. //get the parentKey
  469. parentKey = keys.remove(count);
  470.  
  471. //put parentKey into right link
  472. temp.keys.add(0,parentKey);
  473. // put left values into right values
  474. for(String s: temp2.keys) {
  475. temp.keys.add(0,s);
  476. }// end for
  477. //left links go into rights links
  478. for(long Link: temp2.links){
  479. temp.links.add(0,Link);
  480. }// end for
  481.  
  482. links.remove(count);
  483.  
  484. tree.per.write(temp);
  485. }// end merge
  486.  
  487. public String predacessor(int count) throws ClassNotFoundException, IOException{
  488. long nodeLocA = links.get(count);
  489. Node temp = tree.per.read(nodeLocA);
  490. return goRight(temp,count+1);
  491. }
  492.  
  493. public String goRight(Node myNode,int count) throws ClassNotFoundException, IOException{
  494. String toReturn;
  495. if(myNode.isLeaf()){
  496. toReturn = myNode.keys.remove(myNode.keys.size()-1);
  497. }//end if
  498. else{
  499. toReturn = goRight(tree.per.read(myNode.links.get(count)),count);
  500. }
  501. return toReturn;
  502. }
  503. public boolean minSize(){
  504. boolean result;
  505. if(keys.size() < MAXKEYS/2){
  506. result = true;
  507. }
  508. else{
  509. result = false;
  510. }
  511. return result;
  512. }// end minSize
  513. public void setStartIndex(long startIndex){
  514. this.startIndex = startIndex;
  515. }
  516. public long getStartIndex(){
  517. return startIndex;
  518. }
  519. public ArrayList<Node> getNode(){
  520. return temp;
  521. }
  522. }//end node
  523.  
  524. public class Persistance implements Serializable {
  525. .....
  526. public void test(Node node) throws IOException{
  527. ByteArrayOutputStream b2= new ByteArrayOutputStream();
  528. ObjectOutput out2 = new ObjectOutputStream(b2);
  529. out2.writeObject(node);
  530. out2.close();
  531. byte[] array2 = b.toByteArray();
  532. ByteArrayInputStream in2 = new ByteArrayInputStream(array2);
  533. ObjectInputStream OIS2 = new ObjectInputStream(in2);
  534. Node temp = (Node) OIS2.readObject();
  535. OIS2.close();
  536. }
  537. .....
  538. }
  539.  
  540. ByteArrayOutputStream b2= new ByteArrayOutputStream();
  541. ObjectOutput out2 = new ObjectOutputStream(b2);
  542. out2.writeObject(node);
  543. out2.close();
  544. byte[] array2 = b2.toByteArray();
  545. int length = array2.length;
  546.  
  547. raf.writeInt(length);
  548. raf.write(array2);
  549.  
  550. int length = raf.readInt();
  551. byte inArray[] = new byte[length];
  552. raf.read(inArray,0,length);
  553.  
  554. ByteArrayInputStream in2 = new ByteArrayInputStream(inArray);
  555. ObjectInputStream OIS2 = new ObjectInputStream(in2);
  556. Node temp = (Node) OIS2.readObject();
  557. OIS2.close();
  558.  
  559. import java.io.RandomAccessFile;
  560. import java.io.IOException;
  561. import java.io.Serializable;
  562.  
  563. /**
  564. * John
  565. * Fall 2012
  566. */
  567.  
  568. public class FLRAF extends RandomAccessFile implements Serializable {
  569. private int blockSize;
  570.  
  571. public FLRAF(int blockSize, String file) throws IOException {
  572. super(file, "rw");
  573. this.blockSize = blockSize;
  574. }
  575.  
  576. public byte[] read(int blockNumber) throws IOException {
  577. seek(blockNumber*blockSize);
  578. byte[] b = new byte[blockSize];
  579. read(b);
  580. return b;
  581. }
  582.  
  583. public void write(byte[] b, int blockNumber) throws IOException {
  584. seek(blockNumber*blockSize);
  585. write(b);
  586. return;
  587. }
  588. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement