Permalink
Browse files

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...
1 parent c41e67e commit 53ed98708711786594ee29661b9c8871c60dec56 Mikael Norrman committed Apr 5, 2012
View
9 datastorageproject/log.txt
@@ -2,8 +2,8 @@
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
- <date>2012-04-02T21:43:49</date>
- <millis>1333395829157</millis>
+ <date>2012-04-02T23:44:39</date>
+ <millis>1333403079952</millis>
<sequence>0</sequence>
<logger>b-log</logger>
<level>INFO</level>
@@ -12,4 +12,7 @@
<thread>1</thread>
<message>BackStorage initialized</message>
</record>
-Mon Apr 02 21:43:49 CEST 2012: INFO: Console Input Manager started
+Mon Apr 02 23:44:39 CEST 2012: INFO: Console Input Manager started
+Mon Apr 02 23:44:42 CEST 2012: INFO: BackStorage: Begin reindex of data
+Mon Apr 02 23:44:42 CEST 2012: INFO: BackStorage: Reindexing complete
+Mon Apr 02 23:54:30 CEST 2012: INFO: BackStorage: Starting integrity check
View
4 datastorageproject/src/com/mnorrman/datastorageproject/ConsoleInputManager.java
@@ -61,7 +61,7 @@ public void run() {
UnindexedDataObject udo = new UnindexedDataObject(new FileInputStream(file), colname, rowname, owner, file.length());
DataProcessor dp = m.getNewDataProcessor();
- Main.localIndex.insertIndex(dp.storeData(udo));
+ Main.localIndex.insert(dp.storeData(udo));
print("Done storing \"" + file.getName() + "\"");
} catch (IOException e) {
Main.logger.log(e, LogTool.CRITICAL);
@@ -94,7 +94,7 @@ public void run() {
}else if(command.toLowerCase().equals("printindex")){
- for(ArrayList<IndexedDataObject> al : Main.localIndex.values()){
+ for(ArrayList<IndexedDataObject> al : Main.localIndex.getData()){
for(IndexedDataObject ido : al){
System.out.println(ido);
}
View
11 datastorageproject/src/com/mnorrman/datastorageproject/DataProcessor.java
@@ -114,6 +114,17 @@ public IndexedDataObject storeData(UnindexedDataObject udo){
dataChannel.position(newOffset);
dataChannel.write(buffer);
+ //-This part makes sure that the full amount of bytes are pre-
+ //allocated, thus making it easier to rollback the changes.
+ //(Since we still know how much data to remove)
+ long tempPos = dataChannel.position();
+ ByteBuffer voidbuf = ByteBuffer.allocate(1);
+ voidbuf.put((byte)0);
+ voidbuf.flip();
+ dataChannel.position(tempPos+udo.getLength());
+ dataChannel.write(voidbuf);
+ dataChannel.position(tempPos);
+
FileInputStream fis = new FileInputStream(file);
FileChannel fc = fis.getChannel();
fc.transferTo(0, file.length(), dataChannel);
View
5 datastorageproject/src/com/mnorrman/datastorageproject/Main.java
@@ -8,10 +8,9 @@
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
+import java.util.Timer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
-import java.util.concurrent.ThreadFactory;
-import java.util.concurrent.ThreadPoolExecutor;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
@@ -24,6 +23,7 @@
public static LogTool logger;
public static LocalIndex localIndex;
public static ExecutorService pool;
+ public static Timer timer;
public BackStorage storage;
public Main(){
@@ -57,6 +57,7 @@ public static void main(String[] args) {
logger = new LogTool(LogTool.INFO);
localIndex = new LocalIndex();
pool = Executors.newFixedThreadPool(5, Executors.defaultThreadFactory());
+ timer = new Timer("TimerThread");
Main m = new Main();
ConsoleInputManager console = new ConsoleInputManager(m);
console.start();
View
46 datastorageproject/src/com/mnorrman/datastorageproject/index/Index.java
@@ -0,0 +1,46 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package com.mnorrman.datastorageproject.index;
+
+import com.mnorrman.datastorageproject.Main;
+import com.mnorrman.datastorageproject.objects.DataObject;
+import com.mnorrman.datastorageproject.objects.IndexedDataObject;
+import com.mnorrman.datastorageproject.tools.Hash;
+import java.util.*;
+
+/**
+ *
+ * @author Mikael
+ */
+public abstract class Index<T extends DataObject> {
+
+ public Index(){
+ super();
+ }
+
+ public abstract Collection<ArrayList<T>> getData();
+
+ public abstract boolean contains(String hash);
+
+ public abstract boolean contains(String a, String b);
+
+ public abstract void remove(String a, String b);
+
+ public abstract void remove(String hash);
+
+ public abstract T get(String a, String b);
+
+ public abstract T get(String a, String b, int version);
+
+ public abstract T get(String hash);
+
+ public abstract T get(String hash, int version);
+
+ public abstract void insert(T dataObject);
+
+ public abstract void insertAll(List<T> list);
+
+ public abstract int versionCount(T dataObject);
+}
View
27 datastorageproject/src/com/mnorrman/datastorageproject/index/IndexPercistanceTimerTask.java
@@ -0,0 +1,27 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package com.mnorrman.datastorageproject.index;
+
+import com.mnorrman.datastorageproject.Main;
+import java.util.TimerTask;
+
+/**
+ *
+ * @author Mikael
+ */
+public class IndexPercistanceTimerTask extends TimerTask{
+
+ private Index master;
+
+ public IndexPercistanceTimerTask(Index master){
+ this.master = master;
+ }
+
+ @Override
+ public void run() {
+ Main.pool.submit(new IndexPercistance(master.getClass().getSimpleName(), master.getData()));
+ }
+
+}
View
123 datastorageproject/src/com/mnorrman/datastorageproject/index/LocalIndex.java
@@ -4,30 +4,69 @@
*/
package com.mnorrman.datastorageproject.index;
-import com.mnorrman.datastorageproject.Main;
import com.mnorrman.datastorageproject.objects.IndexedDataObject;
import com.mnorrman.datastorageproject.tools.Hash;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
+import java.util.*;
/**
*
* @author Mikael
*/
-public class LocalIndex extends HashMap<String, ArrayList<IndexedDataObject>> {
-
+public class LocalIndex extends Index<IndexedDataObject> {
+
+ private HashMap<String, ArrayList<IndexedDataObject>> table;
+
public LocalIndex(){
- super();
+ table = new HashMap<>();
+ }
+
+ public void clear(){
+ table.clear();
}
- public void insertIndex(IndexedDataObject ido){
- String checksum = ido.getHash();
- if(containsKey(checksum)){
- ArrayList<IndexedDataObject> temp = get(checksum);
+ @Override
+ public boolean contains(String hash) {
+ return table.containsKey(hash);
+ }
+
+ @Override
+ public boolean contains(String a, String b) {
+ return table.containsKey(Hash.get(a, b));
+ }
+
+ @Override
+ public IndexedDataObject get(String a, String b) {
+ return table.get(Hash.get(a, b)).get(0);
+ }
+
+ @Override
+ public IndexedDataObject get(String a, String b, int version) {
+ if(table.get(Hash.get(a, b)).size() > version && version >= 0)
+ return table.get(Hash.get(a, b)).get(version);
+ else
+ return null;
+ }
+
+ @Override
+ public IndexedDataObject get(String hash) {
+ return table.get(hash).get(0);
+ }
+
+ @Override
+ public IndexedDataObject get(String hash, int version) {
+ if(table.get(hash).size() > version && version >= 0)
+ return table.get(hash).get(version);
+ else
+ return null;
+ }
+
+ @Override
+ public void insert(IndexedDataObject dataObject) {
+ String checksum = dataObject.getHash();
+ if(table.containsKey(checksum)){
+ ArrayList<IndexedDataObject> temp = table.get(checksum);
IndexedDataObject idoTemp = null;
- temp.add(ido);
+ temp.add(dataObject);
for(int a = temp.size() - 1; a > 0; a--){
if(temp.get(a).getVersion() > temp.get(a-1).getVersion()){
idoTemp = temp.get(a-1);
@@ -38,45 +77,37 @@ public void insertIndex(IndexedDataObject ido){
}
}
}else{
- put(checksum, new ArrayList<IndexedDataObject>());
- get(checksum).add(ido);
+ table.put(checksum, new ArrayList<IndexedDataObject>());
+ table.get(checksum).add(dataObject);
}
-
- }
-
- public IndexedDataObject get(String a, String b){
- return get(Hash.get(a, b)).get(0); //Zero is head
}
-
- public IndexedDataObject get(String a, String b, int version){
- if(get(Hash.get(a, b)).size() > version)
- return get(Hash.get(a, b)).get(version);
- else
- return null;
- }
-
- public IndexedDataObject getWithHash(String hash){
- return get(hash).get(0); //Zero is head
+
+ @Override
+ public void insertAll(List<IndexedDataObject> list) {
+ Iterator<IndexedDataObject> iterator = list.iterator();
+ while(iterator.hasNext()){
+ IndexedDataObject temp = iterator.next();
+ insert(temp);
+ }
}
-
- public int getNumberOfVersions(IndexedDataObject ido){
- return get(Hash.get(ido.getColname(), ido.getRowname())).size();
+
+ @Override
+ public void remove(String a, String b) {
+ throw new UnsupportedOperationException("Not supported yet.");
}
-
- public boolean contains(String hash){
- return containsKey(hash);
+
+ @Override
+ public void remove(String hash) {
+ throw new UnsupportedOperationException("Not supported yet.");
}
-
- public boolean contains(String a, String b){
- return containsKey(Hash.get(a, b));
+
+ @Override
+ public int versionCount(IndexedDataObject dataObject) {
+ return table.get(Hash.get(dataObject.getColname(), dataObject.getRowname())).size();
}
- public void insertAll(List<IndexedDataObject> list){
- Iterator<IndexedDataObject> iterator = list.iterator();
- while(iterator.hasNext()){
- IndexedDataObject temp = iterator.next();
- insertIndex(temp);
- }
- Main.pool.submit(new IndexPercistance(getClass().getSimpleName(), values()));
+ @Override
+ public Collection<ArrayList<IndexedDataObject>> getData(){
+ return table.values();
}
}

0 comments on commit 53ed987

Please sign in to comment.