Permalink
Browse files

The basic function of the storage facility is now done.

  • Loading branch information...
Mikael Norrman
Mikael Norrman committed Mar 29, 2012
1 parent 8dfad5f commit e8d212b166aeaf25fc529ce2be7e00eb933ca3f6
@@ -0,0 +1,2 @@
+Tjohojava.lang.ArrayIndexOutOfBoundsException: 4
+ at com.mnorrman.datastorageproject.LoggerWriter.main(LoggerWriter.java:42)
@@ -4,10 +4,16 @@
*/
package com.mnorrman.datastorageproject;
+import com.mnorrman.datastorageproject.objects.IndexedDataObject;
+import com.mnorrman.datastorageproject.tools.Checksum;
+import com.mnorrman.datastorageproject.tools.HexConverter;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
+import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
+import java.util.Iterator;
+import java.util.LinkedList;
/**
*
@@ -34,9 +40,95 @@ public BackStorage initialize() throws IOException{
return this;
}
+ public LinkedList<IndexedDataObject> reindexData(){
+ LinkedList<IndexedDataObject> list = new LinkedList<IndexedDataObject>();
+ long position = 0;
+ ByteBuffer data = ByteBuffer.allocateDirect(512);
+ try{
+ FileChannel channel = fileConnection.getChannel();
+ channel.position(position);
+ do{
+ data.clear(); //Clear it first
+ channel.read(data);
+ data.rewind();
+
+ byte[] temp;
+ byte[] checksum = new byte[16];
+ String colname, rowname, owner;
+ long version, len;
+
+ temp = new byte[128];
+ data.get(temp);
+ colname = new String(temp).trim();
+
+ temp = new byte[128];
+ data.get(temp);
+ rowname = new String(temp).trim();
+
+ version = data.getLong();
+
+ len = data.getLong();
+
+ data.get(checksum);
+
+ temp = new byte[128];
+ data.get(temp);
+ owner = new String(temp).trim();
+
+ list.add(new IndexedDataObject(colname, rowname, owner, 0L, len, version, checksum));
+ position += (512+len);
+ channel.position(position);
+ }while(channel.size() - channel.position() > 0);
+ }catch(IOException e){
+ e.printStackTrace();
+ }
+ return list;
+ }
+
public boolean performIntegrityCheck(){
-
- return false;
+ long position = 0;
+ ByteBuffer length = ByteBuffer.allocate(8);
+ ByteBuffer checksum = ByteBuffer.allocate(16);
+ ByteBuffer data;
+ try{
+ FileChannel channel = fileConnection.getChannel();
+ channel.position(position);
+ do{
+ length.clear();
+ checksum.clear(); //Clear it first
+ channel.position(position + 264); //Jump to length and checksum
+ channel.read(length);
+ channel.read(checksum);
+ length.rewind();
+ checksum.rewind();
+
+ //Set variables
+ long len;
+ byte[] checksumDataFromIndex = new byte[16];
+
+ //Read from bytebuffers
+ len = length.getLong();
+ checksum.get(checksumDataFromIndex);
+
+ data = ByteBuffer.allocate((int)len);
+ channel.position(position + 512);
+ channel.read(data);
+
+ data.flip();
+
+ byte[] checksumDataFromData = Checksum.getFor(data);
+
+ if(!HexConverter.toHex(checksumDataFromIndex).equals(HexConverter.toHex(checksumDataFromData))){
+ return false;
+ }
+
+ position += (512+len);
+ channel.position(position);
+ }while(channel.size() - channel.position() > 0);
+ }catch(IOException e){
+ e.printStackTrace();
+ }
+ return true;
}
public FileChannel getChannel(){
@@ -46,7 +138,14 @@ public FileChannel getChannel(){
public static void main(String[] args) {
BackStorage t = new BackStorage();
try{
- t.initialize();
+ LinkedList<IndexedDataObject> list = t.initialize().reindexData();
+ System.out.println("List = " + list.size());
+ Iterator<IndexedDataObject> it = list.iterator();
+ while(it.hasNext()){
+ System.out.println(it.next().toString());
+ }
+
+ System.out.println("Integrity check is: " + t.performIntegrityCheck());
}catch(IOException e){
e.printStackTrace();
}
@@ -5,6 +5,7 @@
package com.mnorrman.datastorageproject;
import com.mnorrman.datastorageproject.objects.IndexedDataObject;
+import com.mnorrman.datastorageproject.objects.UnindexedDataObject;
import com.mnorrman.datastorageproject.tools.Checksum;
import java.io.IOException;
import java.nio.ByteBuffer;
@@ -36,128 +37,59 @@ public ByteBuffer retrieveData(IndexedDataObject ido){
}
return null;
}
-
- public IndexedDataObject retrieveMetaData(long offset, long length){
- try{
- ByteBuffer data = ByteBuffer.allocateDirect(512 + (int)length);
- dataChannel.position(offset);
- dataChannel.read(data);
- data.rewind();
-
- byte[] temp;
- byte[] checksum = new byte[16];
- String colname, rowname, owner;
- long version, len;
-
- temp = new byte[128];
- data.get(temp);
- colname = new String(temp).trim();
-
- temp = new byte[128];
- data.get(temp);
- rowname = new String(temp).trim();
-
- version = data.getLong();
-
- len = data.getLong();
-
- data.get(checksum);
-
- temp = new byte[128];
- data.get(temp);
- owner = new String(temp).trim();
-
- //return new IndexedDataObject(colname, rowname, owner, offset, length, version, checksum)
- }catch(IOException e){
- Logger.getGlobal().log(Level.SEVERE, e.getMessage());
- }
- return null;
- }
-
- public void testReadData(){
- try{
- ByteBuffer buff = ByteBuffer.allocateDirect(512);
- byte[] temp;
- dataChannel.position(0);
- dataChannel.read(buff);
- buff.flip();
- temp = new byte[128];
- buff.get(temp);
- System.out.println("Value 1: " + new String(temp).trim());
-
- temp = new byte[128];
- buff.get(temp);
- System.out.println("Value 2: " + new String(temp).trim());
-
- System.out.println("Value 3: " + buff.getLong());
- System.out.println("Value 4: " + buff.getLong());
-
- temp = new byte[16];
- buff.get(temp);
- System.out.println("Value 5: " + new String(temp));
-
- temp = new byte[128];
- buff.get(temp);
- System.out.println("Value 6: " + new String(temp).trim());
-
- buff.clear();
- }catch(IOException e){
- e.printStackTrace();
- }
- }
-
- public boolean storeData(String colname, String rowname, long version, long length, String owner, ByteBuffer data){
+ public IndexedDataObject storeData(UnindexedDataObject udo){
try{
ByteBuffer buffer = ByteBuffer.allocateDirect(512); //416 + 96 (Void)
byte[] colnameBytes = new byte[128];
byte[] rownameBytes = new byte[128];
byte[] ownerBytes = new byte[128];
- System.arraycopy(colname.getBytes(), 0, colnameBytes, 0, colname.getBytes().length);
- System.arraycopy(rowname.getBytes(), 0, rownameBytes, 0, rowname.getBytes().length);
- System.arraycopy(owner.getBytes(), 0, ownerBytes, 0, owner.getBytes().length);
+ System.arraycopy(udo.getColname().getBytes(), 0, colnameBytes, 0, udo.getColname().getBytes().length);
+ System.arraycopy(udo.getRowname().getBytes(), 0, rownameBytes, 0, udo.getRowname().getBytes().length);
+ System.arraycopy(udo.getOwner().getBytes(), 0, ownerBytes, 0, udo.getOwner().getBytes().length);
+
+ long newVersion = System.currentTimeMillis();
buffer.put(colnameBytes);
buffer.put(rownameBytes);
- buffer.putLong(version);
- buffer.putLong(length);
- buffer.put(Checksum.getFor(data));
+ buffer.putLong(newVersion);
+ buffer.putLong(udo.getLength());
+ buffer.put(Checksum.getFor(udo.getData()));
buffer.put(ownerBytes);
byte[] voidData = new byte[96];
buffer.put(voidData);
buffer.flip();
- data.flip();
+ udo.getData().flip();
+
+ long newOffset = dataChannel.size();
+
dataChannel.position(dataChannel.size());
dataChannel.write(buffer);
- dataChannel.write(data);
- return true;
+ dataChannel.write(udo.getData());
+ return new IndexedDataObject(udo, newOffset, newVersion);
}catch(IOException e){
Logger.getGlobal().log(Level.SEVERE, e.getMessage());
e.printStackTrace();
}
- return false;
+ return null;
}
public static void main(String[] args) {
BackStorage b = new BackStorage();
try{
b.initialize();
}catch(IOException e){ e.printStackTrace(); }
+
DataProcessor p = new DataProcessor(b.getChannel());
ByteBuffer buf = ByteBuffer.allocate(10);
buf.put(new byte[]{ 110, 111, 112, 113, 114, 115, 116, 117, 118, 119 });
- p.storeData("Testvalue a", "Testvalue b", System.currentTimeMillis(), 10L, "Ownervalue", buf);
-
- p.testReadData();
- System.out.println("Testing to retrieve teh data!!!!");
- IndexedDataObject o = new IndexedDataObject("Testvalue a", "Testvalue b", "ownervalue", 0, 10, 1L, Checksum.getFor(buf));
+ UnindexedDataObject udo = new UnindexedDataObject(buf, "This is the column", "This is the row", "Mikael Norrman");
+ IndexedDataObject ido = p.storeData(udo);
- byte[] stringData = new byte[(int)o.getLength()];
- p.retrieveData(o).get(stringData);
- System.out.println(new String(stringData).trim());
+ System.out.println(ido);
}
}
@@ -6,27 +6,59 @@
import com.mnorrman.datastorageproject.objects.IndexedDataObject;
import com.mnorrman.datastorageproject.tools.Checksum;
+import java.util.ArrayList;
import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
/**
*
* @author Mikael
*/
-public class Index extends HashMap<String, IndexedDataObject> {
+public class Index extends HashMap<String, ArrayList<IndexedDataObject>> {
public Index(){
super();
}
- public void insertNew(IndexedDataObject ido){
- put(Checksum.getFor(ido.getColname(), ido.getRowname()), ido);
+ public void insertIndex(IndexedDataObject ido){
+ String checksum = Checksum.getFor(ido.getColname(), ido.getRowname());
+ if(containsKey(checksum)){
+ ArrayList<IndexedDataObject> temp = get(checksum);
+ IndexedDataObject idoTemp = null;
+ temp.add(ido);
+ for(int a = temp.size() - 1; a > 0; a--){
+ if(temp.get(a).getVersion() > temp.get(a-1).getVersion()){
+ idoTemp = temp.get(a-1);
+ temp.set(a-1, temp.get(a));
+ temp.set(a, idoTemp);
+ }else{
+ break;
+ }
+ }
+ }else{
+ put(checksum, new ArrayList<IndexedDataObject>());
+ get(checksum).add(ido);
+ }
+ }
+
+ public IndexedDataObject get(String a, String b){
+ return get(Checksum.getFor(a, b)).get(0); //Zero is head
}
- public IndexedDataObject getIndexedObject(String hash){
- return get(hash);
+ public int getNumberOfVersions(IndexedDataObject ido){
+ return get(Checksum.getFor(ido.getColname(), ido.getRowname())).size();
}
public boolean doIndexedObjectExist(String hash){
return containsKey(hash);
}
+
+ public void insertAll(List<IndexedDataObject> list){
+ Iterator<IndexedDataObject> iterator = list.iterator();
+ while(iterator.hasNext()){
+ IndexedDataObject temp = iterator.next();
+ insertIndex(temp);
+ }
+ }
}
Oops, something went wrong.

0 comments on commit e8d212b

Please sign in to comment.