Skip to content
This repository
Browse code

Added further level of abstraction to index. Now has a generic-powere…

…d abstract Index class. Added timer to main, should be used for autosaving index in intervals. Good progress.
  • Loading branch information...
commit 53ed98708711786594ee29661b9c8871c60dec56 1 parent c41e67e
Mikael Norrman authored
9 datastorageproject/log.txt
@@ -2,8 +2,8 @@
2 2 <!DOCTYPE log SYSTEM "logger.dtd">
3 3 <log>
4 4 <record>
5   - <date>2012-04-02T21:43:49</date>
6   - <millis>1333395829157</millis>
  5 + <date>2012-04-02T23:44:39</date>
  6 + <millis>1333403079952</millis>
7 7 <sequence>0</sequence>
8 8 <logger>b-log</logger>
9 9 <level>INFO</level>
@@ -12,4 +12,7 @@
12 12 <thread>1</thread>
13 13 <message>BackStorage initialized</message>
14 14 </record>
15   -Mon Apr 02 21:43:49 CEST 2012: INFO: Console Input Manager started
  15 +Mon Apr 02 23:44:39 CEST 2012: INFO: Console Input Manager started
  16 +Mon Apr 02 23:44:42 CEST 2012: INFO: BackStorage: Begin reindex of data
  17 +Mon Apr 02 23:44:42 CEST 2012: INFO: BackStorage: Reindexing complete
  18 +Mon Apr 02 23:54:30 CEST 2012: INFO: BackStorage: Starting integrity check
4 datastorageproject/src/com/mnorrman/datastorageproject/ConsoleInputManager.java
@@ -61,7 +61,7 @@ public void run() {
61 61 UnindexedDataObject udo = new UnindexedDataObject(new FileInputStream(file), colname, rowname, owner, file.length());
62 62
63 63 DataProcessor dp = m.getNewDataProcessor();
64   - Main.localIndex.insertIndex(dp.storeData(udo));
  64 + Main.localIndex.insert(dp.storeData(udo));
65 65 print("Done storing \"" + file.getName() + "\"");
66 66 } catch (IOException e) {
67 67 Main.logger.log(e, LogTool.CRITICAL);
@@ -94,7 +94,7 @@ public void run() {
94 94
95 95
96 96 }else if(command.toLowerCase().equals("printindex")){
97   - for(ArrayList<IndexedDataObject> al : Main.localIndex.values()){
  97 + for(ArrayList<IndexedDataObject> al : Main.localIndex.getData()){
98 98 for(IndexedDataObject ido : al){
99 99 System.out.println(ido);
100 100 }
11 datastorageproject/src/com/mnorrman/datastorageproject/DataProcessor.java
@@ -114,6 +114,17 @@ public IndexedDataObject storeData(UnindexedDataObject udo){
114 114 dataChannel.position(newOffset);
115 115 dataChannel.write(buffer);
116 116
  117 + //-This part makes sure that the full amount of bytes are pre-
  118 + //allocated, thus making it easier to rollback the changes.
  119 + //(Since we still know how much data to remove)
  120 + long tempPos = dataChannel.position();
  121 + ByteBuffer voidbuf = ByteBuffer.allocate(1);
  122 + voidbuf.put((byte)0);
  123 + voidbuf.flip();
  124 + dataChannel.position(tempPos+udo.getLength());
  125 + dataChannel.write(voidbuf);
  126 + dataChannel.position(tempPos);
  127 +
117 128 FileInputStream fis = new FileInputStream(file);
118 129 FileChannel fc = fis.getChannel();
119 130 fc.transferTo(0, file.length(), dataChannel);
5 datastorageproject/src/com/mnorrman/datastorageproject/Main.java
@@ -8,10 +8,9 @@
8 8 import java.io.IOException;
9 9 import java.net.InetAddress;
10 10 import java.net.ServerSocket;
  11 +import java.util.Timer;
11 12 import java.util.concurrent.ExecutorService;
12 13 import java.util.concurrent.Executors;
13   -import java.util.concurrent.ThreadFactory;
14   -import java.util.concurrent.ThreadPoolExecutor;
15 14 import java.util.logging.FileHandler;
16 15 import java.util.logging.Logger;
17 16
@@ -24,6 +23,7 @@
24 23 public static LogTool logger;
25 24 public static LocalIndex localIndex;
26 25 public static ExecutorService pool;
  26 + public static Timer timer;
27 27 public BackStorage storage;
28 28
29 29 public Main(){
@@ -57,6 +57,7 @@ public static void main(String[] args) {
57 57 logger = new LogTool(LogTool.INFO);
58 58 localIndex = new LocalIndex();
59 59 pool = Executors.newFixedThreadPool(5, Executors.defaultThreadFactory());
  60 + timer = new Timer("TimerThread");
60 61 Main m = new Main();
61 62 ConsoleInputManager console = new ConsoleInputManager(m);
62 63 console.start();
46 datastorageproject/src/com/mnorrman/datastorageproject/index/Index.java
... ... @@ -0,0 +1,46 @@
  1 +/*
  2 + * To change this template, choose Tools | Templates
  3 + * and open the template in the editor.
  4 + */
  5 +package com.mnorrman.datastorageproject.index;
  6 +
  7 +import com.mnorrman.datastorageproject.Main;
  8 +import com.mnorrman.datastorageproject.objects.DataObject;
  9 +import com.mnorrman.datastorageproject.objects.IndexedDataObject;
  10 +import com.mnorrman.datastorageproject.tools.Hash;
  11 +import java.util.*;
  12 +
  13 +/**
  14 + *
  15 + * @author Mikael
  16 + */
  17 +public abstract class Index<T extends DataObject> {
  18 +
  19 + public Index(){
  20 + super();
  21 + }
  22 +
  23 + public abstract Collection<ArrayList<T>> getData();
  24 +
  25 + public abstract boolean contains(String hash);
  26 +
  27 + public abstract boolean contains(String a, String b);
  28 +
  29 + public abstract void remove(String a, String b);
  30 +
  31 + public abstract void remove(String hash);
  32 +
  33 + public abstract T get(String a, String b);
  34 +
  35 + public abstract T get(String a, String b, int version);
  36 +
  37 + public abstract T get(String hash);
  38 +
  39 + public abstract T get(String hash, int version);
  40 +
  41 + public abstract void insert(T dataObject);
  42 +
  43 + public abstract void insertAll(List<T> list);
  44 +
  45 + public abstract int versionCount(T dataObject);
  46 +}
27 datastorageproject/src/com/mnorrman/datastorageproject/index/IndexPercistanceTimerTask.java
... ... @@ -0,0 +1,27 @@
  1 +/*
  2 + * To change this template, choose Tools | Templates
  3 + * and open the template in the editor.
  4 + */
  5 +package com.mnorrman.datastorageproject.index;
  6 +
  7 +import com.mnorrman.datastorageproject.Main;
  8 +import java.util.TimerTask;
  9 +
  10 +/**
  11 + *
  12 + * @author Mikael
  13 + */
  14 +public class IndexPercistanceTimerTask extends TimerTask{
  15 +
  16 + private Index master;
  17 +
  18 + public IndexPercistanceTimerTask(Index master){
  19 + this.master = master;
  20 + }
  21 +
  22 + @Override
  23 + public void run() {
  24 + Main.pool.submit(new IndexPercistance(master.getClass().getSimpleName(), master.getData()));
  25 + }
  26 +
  27 +}
123 datastorageproject/src/com/mnorrman/datastorageproject/index/LocalIndex.java
@@ -4,30 +4,69 @@
4 4 */
5 5 package com.mnorrman.datastorageproject.index;
6 6
7   -import com.mnorrman.datastorageproject.Main;
8 7 import com.mnorrman.datastorageproject.objects.IndexedDataObject;
9 8 import com.mnorrman.datastorageproject.tools.Hash;
10   -import java.util.ArrayList;
11   -import java.util.HashMap;
12   -import java.util.Iterator;
13   -import java.util.List;
  9 +import java.util.*;
14 10
15 11 /**
16 12 *
17 13 * @author Mikael
18 14 */
19   -public class LocalIndex extends HashMap<String, ArrayList<IndexedDataObject>> {
20   -
  15 +public class LocalIndex extends Index<IndexedDataObject> {
  16 +
  17 + private HashMap<String, ArrayList<IndexedDataObject>> table;
  18 +
21 19 public LocalIndex(){
22   - super();
  20 + table = new HashMap<>();
  21 + }
  22 +
  23 + public void clear(){
  24 + table.clear();
23 25 }
24 26
25   - public void insertIndex(IndexedDataObject ido){
26   - String checksum = ido.getHash();
27   - if(containsKey(checksum)){
28   - ArrayList<IndexedDataObject> temp = get(checksum);
  27 + @Override
  28 + public boolean contains(String hash) {
  29 + return table.containsKey(hash);
  30 + }
  31 +
  32 + @Override
  33 + public boolean contains(String a, String b) {
  34 + return table.containsKey(Hash.get(a, b));
  35 + }
  36 +
  37 + @Override
  38 + public IndexedDataObject get(String a, String b) {
  39 + return table.get(Hash.get(a, b)).get(0);
  40 + }
  41 +
  42 + @Override
  43 + public IndexedDataObject get(String a, String b, int version) {
  44 + if(table.get(Hash.get(a, b)).size() > version && version >= 0)
  45 + return table.get(Hash.get(a, b)).get(version);
  46 + else
  47 + return null;
  48 + }
  49 +
  50 + @Override
  51 + public IndexedDataObject get(String hash) {
  52 + return table.get(hash).get(0);
  53 + }
  54 +
  55 + @Override
  56 + public IndexedDataObject get(String hash, int version) {
  57 + if(table.get(hash).size() > version && version >= 0)
  58 + return table.get(hash).get(version);
  59 + else
  60 + return null;
  61 + }
  62 +
  63 + @Override
  64 + public void insert(IndexedDataObject dataObject) {
  65 + String checksum = dataObject.getHash();
  66 + if(table.containsKey(checksum)){
  67 + ArrayList<IndexedDataObject> temp = table.get(checksum);
29 68 IndexedDataObject idoTemp = null;
30   - temp.add(ido);
  69 + temp.add(dataObject);
31 70 for(int a = temp.size() - 1; a > 0; a--){
32 71 if(temp.get(a).getVersion() > temp.get(a-1).getVersion()){
33 72 idoTemp = temp.get(a-1);
@@ -38,45 +77,37 @@ public void insertIndex(IndexedDataObject ido){
38 77 }
39 78 }
40 79 }else{
41   - put(checksum, new ArrayList<IndexedDataObject>());
42   - get(checksum).add(ido);
  80 + table.put(checksum, new ArrayList<IndexedDataObject>());
  81 + table.get(checksum).add(dataObject);
43 82 }
44   -
45   - }
46   -
47   - public IndexedDataObject get(String a, String b){
48   - return get(Hash.get(a, b)).get(0); //Zero is head
49 83 }
50   -
51   - public IndexedDataObject get(String a, String b, int version){
52   - if(get(Hash.get(a, b)).size() > version)
53   - return get(Hash.get(a, b)).get(version);
54   - else
55   - return null;
56   - }
57   -
58   - public IndexedDataObject getWithHash(String hash){
59   - return get(hash).get(0); //Zero is head
  84 +
  85 + @Override
  86 + public void insertAll(List<IndexedDataObject> list) {
  87 + Iterator<IndexedDataObject> iterator = list.iterator();
  88 + while(iterator.hasNext()){
  89 + IndexedDataObject temp = iterator.next();
  90 + insert(temp);
  91 + }
60 92 }
61   -
62   - public int getNumberOfVersions(IndexedDataObject ido){
63   - return get(Hash.get(ido.getColname(), ido.getRowname())).size();
  93 +
  94 + @Override
  95 + public void remove(String a, String b) {
  96 + throw new UnsupportedOperationException("Not supported yet.");
64 97 }
65   -
66   - public boolean contains(String hash){
67   - return containsKey(hash);
  98 +
  99 + @Override
  100 + public void remove(String hash) {
  101 + throw new UnsupportedOperationException("Not supported yet.");
68 102 }
69   -
70   - public boolean contains(String a, String b){
71   - return containsKey(Hash.get(a, b));
  103 +
  104 + @Override
  105 + public int versionCount(IndexedDataObject dataObject) {
  106 + return table.get(Hash.get(dataObject.getColname(), dataObject.getRowname())).size();
72 107 }
73 108
74   - public void insertAll(List<IndexedDataObject> list){
75   - Iterator<IndexedDataObject> iterator = list.iterator();
76   - while(iterator.hasNext()){
77   - IndexedDataObject temp = iterator.next();
78   - insertIndex(temp);
79   - }
80   - Main.pool.submit(new IndexPercistance(getClass().getSimpleName(), values()));
  109 + @Override
  110 + public Collection<ArrayList<IndexedDataObject>> getData(){
  111 + return table.values();
81 112 }
82 113 }

0 comments on commit 53ed987

Please sign in to comment.
Something went wrong with that request. Please try again.