From 6022c3643eb5a1bac10d0dfe9ffba3cbc8100afe Mon Sep 17 00:00:00 2001 From: navid Date: Fri, 23 Jan 2009 02:29:12 +0000 Subject: [PATCH] *** empty log message *** --- .../core/ExtendedTransaction.java | 3 +- .../TransactionalIO/core/GlobalLength.java | 1 - .../core/TransactionalFile.java | 77 ++++++++++ .../core/DualFileTableTransactional.java | 136 +++++++++++------- .../tuplesoup/core/RowTransactional.java | 4 +- .../tuplesoup/core/ValueTransactional.java | 5 +- 6 files changed, 168 insertions(+), 58 deletions(-) diff --git a/Robust/Transactions/TransactionalIO/src/TransactionalIO/core/ExtendedTransaction.java b/Robust/Transactions/TransactionalIO/src/TransactionalIO/core/ExtendedTransaction.java index 90f5b764..db536b17 100644 --- a/Robust/Transactions/TransactionalIO/src/TransactionalIO/core/ExtendedTransaction.java +++ b/Robust/Transactions/TransactionalIO/src/TransactionalIO/core/ExtendedTransaction.java @@ -195,8 +195,9 @@ public class ExtendedTransaction implements TransactionStatu { } public void addFile(TransactionalFile tf, long offsetnumber/*, TransactionLocalFileAttributes tmp*/) { - + tf.getInodestate().commitedfilesize.lengthlock.lock(); TransactionLocalFileAttributes tmp = new TransactionLocalFileAttributes(offsetnumber, tf.getInodestate().commitedfilesize.getLength()); + tf.getInodestate().commitedfilesize.lengthlock.unlock(); Vector dummy; if (AccessedFiles.containsKey(tf.getInode())) { diff --git a/Robust/Transactions/TransactionalIO/src/TransactionalIO/core/GlobalLength.java b/Robust/Transactions/TransactionalIO/src/TransactionalIO/core/GlobalLength.java index 44d19fda..de68c1c7 100644 --- a/Robust/Transactions/TransactionalIO/src/TransactionalIO/core/GlobalLength.java +++ b/Robust/Transactions/TransactionalIO/src/TransactionalIO/core/GlobalLength.java @@ -16,7 +16,6 @@ public class GlobalLength { private long length; //private Vector offsetReaders; private Vector lengthReaders = new Vector(); - private ExtendedTransaction offsetOwner; public ReentrantLock lengthlock; public GlobalLength(long length) { diff --git a/Robust/Transactions/TransactionalIO/src/TransactionalIO/core/TransactionalFile.java b/Robust/Transactions/TransactionalIO/src/TransactionalIO/core/TransactionalFile.java index dcadae6d..9f5be140 100644 --- a/Robust/Transactions/TransactionalIO/src/TransactionalIO/core/TransactionalFile.java +++ b/Robust/Transactions/TransactionalIO/src/TransactionalIO/core/TransactionalFile.java @@ -12,11 +12,13 @@ import TransactionalIO.core.ExtendedTransaction.Status; import TransactionalIO.interfaces.BlockAccessModesEnum; import TransactionalIO.interfaces.OffsetDependency; import com.sun.org.apache.bcel.internal.generic.IFEQ; +import java.io.DataOutputStream; import java.io.File; import java.io.FileDescriptor; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; +import java.io.UTFDataFormatException; import java.nio.ByteBuffer; import java.util.Collections; import java.util.Iterator; @@ -269,6 +271,10 @@ public class TransactionalFile implements Comparable { return tmp.getLocaloffset(); } + public void force(){ + + } + public void seek(long offset) { ExtendedTransaction me = Wrapper.getTransaction(); @@ -402,6 +408,10 @@ public class TransactionalFile implements Comparable { } + public final void writeFloat(Float v){ + writeInt(Float.floatToIntBits(v)); + } + public final void writeLong(long value){ try { byte[] result = new byte[4]; @@ -420,6 +430,14 @@ public class TransactionalFile implements Comparable { } + public final void writeDouble(Double v) { + writeLong(Double.doubleToLongBits(v)); + } + + public final void writeBoolean(boolean v){ + writeByte(v ? 1 : 0); + } + public final void writeChars(String s) throws IOException { int clen = s.length(); int blen = 2*clen; @@ -432,6 +450,65 @@ public class TransactionalFile implements Comparable { } write(b); } + + public final int writeUTF(String str) throws UTFDataFormatException{ + int strlen = str.length(); + int utflen = 0; + int c, count = 0; + /* use charAt instead of copying String to char array */ + for (int i = 0; i < strlen; i++) { + c = str.charAt(i); + if ((c >= 0x0001) && (c <= 0x007F)) { + utflen++; + } else if (c > 0x07FF) { + utflen += 3; + } else { + utflen += 2; + } + } + + if (utflen > 65535) + throw new UTFDataFormatException( + "encoded string too long: " + utflen + " bytes"); + + byte[] bytearr = null; + + bytearr = new byte[utflen + 2]; + + + bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF); + bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF); + + int i = 0; + for (i = 0; i < strlen; i++) { + c = str.charAt(i); + if (!((c >= 0x0001) && (c <= 0x007F))) + break; + bytearr[count++] = (byte) c; + } + + for (; i < strlen; i++) { + c = str.charAt(i); + if ((c >= 0x0001) && (c <= 0x007F)) { + bytearr[count++] = (byte) c; + + } else if (c > 0x07FF) { + bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F)); + bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F)); + bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); + } else { + bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F)); + bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); + } + } + try { + write(bytearr); + } catch (IOException ex) { + Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex); + } + //write(bytearr, 0, utflen + 2); + return utflen + 2; + } public int read(byte[] b) { diff --git a/Robust/Transactions/mytuplesoup/src/com/solidosystems/tuplesoup/core/DualFileTableTransactional.java b/Robust/Transactions/mytuplesoup/src/com/solidosystems/tuplesoup/core/DualFileTableTransactional.java index d506fa38..af4be46f 100644 --- a/Robust/Transactions/mytuplesoup/src/com/solidosystems/tuplesoup/core/DualFileTableTransactional.java +++ b/Robust/Transactions/mytuplesoup/src/com/solidosystems/tuplesoup/core/DualFileTableTransactional.java @@ -32,6 +32,7 @@ package com.solidosystems.tuplesoup.core; +import TransactionalIO.core.TransactionalFile; import java.io.*; import java.util.*; import java.nio.channels.*; @@ -51,16 +52,19 @@ public class DualFileTableTransactional implements TableTransactional{ private String filealock="filea-dummy"; private String fileblock="fileb-dummy"; - private DataOutputStream fileastream=null; - private DataOutputStream filebstream=null; - private RandomAccessFile filearandom=null; - private RandomAccessFile filebrandom=null; +// private DataOutputStream fileastream=null; +// private DataOutputStream filebstream=null; + private TransactionalFile fileastream=null; + private TransactionalFile filebstream=null; +// private RandomAccessFile filearandom=null; + private TransactionalFile filearandom=null; + private TransactionalFile filebrandom=null; FileChannel fca=null; FileChannel fcb=null; private TableIndexTransactional index=null; - private long fileaposition=0; - private long filebposition=0; +// private long fileaposition=0; + // private long filebposition=0; private boolean rowswitch=true; @@ -69,7 +73,7 @@ public class DualFileTableTransactional implements TableTransactional{ private TableIndexNodeTransactional indexcachefirst; private TableIndexNodeTransactional indexcachelast; - private int indexcacheusage; + //private int indexcacheusage; private StringKeyHashMap indexcache; //private Hashtable indexcache; @@ -88,6 +92,9 @@ public class DualFileTableTransactional implements TableTransactional{ long getstat_cache_hit(); long getstat_cache_miss(); long getstat_cache_drop(); + long getFileaposition(); + long getFilebposition(); + int getIndexcacheusage(); void setstat_add(long val); void setstat_update(long val); @@ -99,6 +106,9 @@ public class DualFileTableTransactional implements TableTransactional{ void setstat_cache_hit(long val); void setstat_cache_miss(long val); void setstat_cache_drop(long val); + void setIndexcacheusage(int val); + void setFileaposition(long val); + void setFilebposition(long val); } /*long stat_add=0; @@ -179,7 +189,9 @@ public class DualFileTableTransactional implements TableTransactional{ } indexcachefirst=null; indexcachelast=null; - indexcacheusage=0; + atomicfields.setFileaposition(0); + atomicfields.setFilebposition(0); + atomicfields.setIndexcacheusage(0); indexcache=new StringKeyHashMap(); } @@ -248,15 +260,21 @@ public class DualFileTableTransactional implements TableTransactional{ private synchronized void openFile(int type) throws IOException{ switch(type){ case FILEA : if(fileastream==null){ - fileastream=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(getFileName(FILEA),true))); - File ftest=new File(getFileName(FILEA)); - fileaposition=ftest.length(); + // fileastream=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(getFileName(FILEA),true))); + fileastream=new TransactionalFile(getFileName(FILEA),"rw"); + + //File ftest=new File(getFileName(FILEA)); + //atomicfields.setFileaposition(ftest.length()); + atomicfields.setFileaposition(fileastream.length()); + fileastream.seek(fileastream.length()); } break; case FILEB : if(filebstream==null){ - filebstream=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(getFileName(FILEB),true))); - File ftest=new File(getFileName(FILEB)); - filebposition=ftest.length(); + filebstream=new TransactionalFile(getFileName(FILEB),"rw"); + //File ftest=new File(getFileName(FILEB)); + //atomicfields.setFilebposition(ftest.length()); + atomicfields.setFilebposition(filebstream.length()); + filebstream.seek(filebstream.length()); } break; } @@ -277,11 +295,11 @@ public class DualFileTableTransactional implements TableTransactional{ private void addCacheEntry(TableIndexEntryTransactional entry){ synchronized(indexcache){ - if(indexcacheusage>INDEXCACHESIZE){ + if(atomicfields.getIndexcacheusage()>INDEXCACHESIZE){ // remove first entry TableIndexNodeTransactional node=indexcachefirst; indexcache.remove(node.getData().getId()); - indexcacheusage--; + atomicfields.setIndexcacheusage(atomicfields.getIndexcacheusage()-1); synchronized(statlock){ atomicfields.setstat_cache_drop(atomicfields.getstat_cache_drop()+1); } @@ -301,48 +319,54 @@ public class DualFileTableTransactional implements TableTransactional{ } indexcachelast=node; indexcache.put(entry.getId(),node); - indexcacheusage++; + atomicfields.setIndexcacheusage(atomicfields.getIndexcacheusage()+1); } } private void addRowA(RowTransactional row) throws IOException{ synchronized(filealock){ - openFile(FILEA); - int pre=fileastream.size(); - row.writeToStream(fileastream); - int post=fileastream.size(); - fileastream.flush(); + openFile(FILEA); + //int pre=fileastream.size(); + int pre= (int)fileastream.getFilePointer(); + //row.writeToStream(fileastream); + row.writeToFile(fileastream); + //int post= fileastream.size(); + int post= (int)fileastream.getFilePointer(); + //fileastream.flush(); synchronized(statlock){ atomicfields.setstat_add(atomicfields.getstat_add()+1); atomicfields.setstat_add_size(atomicfields.getstat_add_size()+row.getSize()); } - index.addEntry(row.getId(),row.getSize(),FILEA,fileaposition); + index.addEntry(row.getId(),row.getSize(),FILEA,atomicfields.getFilebposition()); if(INDEXCACHESIZE>0){ - TableIndexEntryTransactional entry=new TableIndexEntryTransactional(row.getId(),row.getSize(),FILEA,fileaposition); + TableIndexEntryTransactional entry=new TableIndexEntryTransactional(row.getId(),row.getSize(),FILEA,atomicfields.getFileaposition()); addCacheEntry(entry); } - fileaposition+=Row.calcSize(pre,post); + atomicfields.setFileaposition(atomicfields.getFileaposition()+Row.calcSize(pre,post)); } } private void addRowB(RowTransactional row) throws IOException{ synchronized(fileblock){ openFile(FILEB); - int pre=filebstream.size(); - row.writeToStream(filebstream); - int post=filebstream.size(); - filebstream.flush(); + //int pre=filebstream.size(); + int pre= (int)filebstream.getFilePointer(); + //row.writeToStream(filebstream); + row.writeToFile(filebstream); + int post=(int)filebstream.getFilePointer(); + //int post=filebstream.size(); + //filebstream.flush(); synchronized(statlock){ atomicfields.setstat_add(atomicfields.getstat_add()+1); atomicfields.setstat_add_size(atomicfields.getstat_add_size()+row.getSize()); } - index.addEntry(row.getId(),row.getSize(),FILEB,filebposition); + index.addEntry(row.getId(),row.getSize(),FILEB,atomicfields.getFilebposition()); if(INDEXCACHESIZE>0){ - TableIndexEntryTransactional entry=new TableIndexEntryTransactional(row.getId(),row.getSize(),FILEB,filebposition); + TableIndexEntryTransactional entry=new TableIndexEntryTransactional(row.getId(),row.getSize(),FILEB,atomicfields.getFilebposition()); addCacheEntry(entry); } - filebposition+=RowTransactional.calcSize(pre,post); + atomicfields.setFilebposition(atomicfields.getFilebposition()+Row.calcSize(pre,post)); } } @@ -373,10 +397,10 @@ public class DualFileTableTransactional implements TableTransactional{ if(indexcache.containsKey(id)){ TableIndexNodeTransactional node=indexcache.get(id); indexcache.remove(id); - if(indexcacheusage==1){ + if(atomicfields.getIndexcacheusage()==1){ indexcachefirst=null; indexcachelast=null; - indexcacheusage=0; + atomicfields.setIndexcacheusage(0); return; } if(node==indexcachefirst){ @@ -388,7 +412,7 @@ public class DualFileTableTransactional implements TableTransactional{ }else{ node.remove(); } - indexcacheusage--; + atomicfields.setIndexcacheusage(atomicfields.getIndexcacheusage()-1); synchronized(statlock){ atomicfields.setstat_cache_drop(atomicfields.getstat_cache_drop()+1); } @@ -457,8 +481,8 @@ public class DualFileTableTransactional implements TableTransactional{ switch(entry.getLocation()){ case FILEA :synchronized(filealock){ if(filearandom==null){ - filearandom=new RandomAccessFile(getFileName(FILEA),"rw"); - fca=filearandom.getChannel(); + filearandom=new TransactionalFile(getFileName(FILEA),"rw"); + // fca=filearandom.getChannel(); } filearandom.seek(entry.getPosition()); row.writeToFile(filearandom); @@ -468,8 +492,8 @@ public class DualFileTableTransactional implements TableTransactional{ break; case FILEB :synchronized(fileblock){ if(filebrandom==null){ - filebrandom=new RandomAccessFile(getFileName(FILEB),"rw"); - fcb=filebrandom.getChannel(); + filebrandom=new TransactionalFile(getFileName(FILEB),"rw"); + // fcb=filebrandom.getChannel(); } filebrandom.seek(entry.getPosition()); row.writeToFile(filebrandom); @@ -495,34 +519,40 @@ public class DualFileTableTransactional implements TableTransactional{ private void updateRowA(RowTransactional row) throws IOException{ synchronized(filealock){ openFile(FILEA); - int pre=fileastream.size(); - row.writeToStream(fileastream); - int post=fileastream.size(); - fileastream.flush(); - index.updateEntry(row.getId(),row.getSize(),FILEA,fileaposition); + //int pre=filebstream.size(); + int pre=(int)fileastream.getFilePointer(); + //row.writeToStream(filebstream); + row.writeToFile(fileastream); + //int post=filebstream.size(); + int post=(int)fileastream.getFilePointer(); + //fileastream.flush(); + index.updateEntry(row.getId(),row.getSize(),FILEA,atomicfields.getFileaposition()); // Handle index entry caching if(INDEXCACHESIZE>0){ - updateCacheEntry(new TableIndexEntryTransactional(row.getId(),row.getSize(),FILEA,fileaposition)); + updateCacheEntry(new TableIndexEntryTransactional(row.getId(),row.getSize(),FILEA,atomicfields.getFileaposition())); } - fileaposition+=Row.calcSize(pre,post); + atomicfields.setFileaposition(atomicfields.getFilebposition()+RowTransactional.calcSize(pre,post)); } } private void updateRowB(RowTransactional row) throws IOException{ synchronized(fileblock){ openFile(FILEB); - int pre=filebstream.size(); - row.writeToStream(filebstream); - int post=filebstream.size(); - filebstream.flush(); - index.updateEntry(row.getId(),row.getSize(),FILEB,filebposition); + //int pre=filebstream.size(); + int pre=(int)filebstream.getFilePointer(); + //row.writeToStream(filebstream); + row.writeToFile(filebstream); + //int post=filebstream.size(); + int post=(int)filebstream.getFilePointer(); + //filebstream.flush(); + index.updateEntry(row.getId(),row.getSize(),FILEB,atomicfields.getFilebposition()); // Handle index entry caching // Handle index entry caching if(INDEXCACHESIZE>0){ - updateCacheEntry(new TableIndexEntryTransactional(row.getId(),row.getSize(),FILEB,filebposition)); + updateCacheEntry(new TableIndexEntryTransactional(row.getId(),row.getSize(),FILEB,atomicfields.getFilebposition())); } - filebposition+=Row.calcSize(pre,post); + atomicfields.setFilebposition(atomicfields.getFilebposition()+RowTransactional.calcSize(pre,post)); } } diff --git a/Robust/Transactions/mytuplesoup/src/com/solidosystems/tuplesoup/core/RowTransactional.java b/Robust/Transactions/mytuplesoup/src/com/solidosystems/tuplesoup/core/RowTransactional.java index 1e6e005c..cbe908cd 100644 --- a/Robust/Transactions/mytuplesoup/src/com/solidosystems/tuplesoup/core/RowTransactional.java +++ b/Robust/Transactions/mytuplesoup/src/com/solidosystems/tuplesoup/core/RowTransactional.java @@ -5,6 +5,7 @@ package com.solidosystems.tuplesoup.core; +import TransactionalIO.core.TransactionalFile; import dstm2.atomic; import dstm2.util.StringKeyHashMap; import java.io.ByteArrayOutputStream; @@ -246,7 +247,7 @@ public class RowTransactional { /** * Writes the contents of this row to the given RandomAccessFile */ - public void writeToFile(RandomAccessFile out) throws IOException{ + public void writeToFile(TransactionalFile out) throws IOException{ long pre=out.getFilePointer(); out.writeUTF(id); @@ -260,6 +261,7 @@ public class RowTransactional { out.writeUTF(key); value.writeToFile(out); } + long post=out.getFilePointer(); int size=(int)(post-pre); this.atomicfields.setSize(size+4); diff --git a/Robust/Transactions/mytuplesoup/src/com/solidosystems/tuplesoup/core/ValueTransactional.java b/Robust/Transactions/mytuplesoup/src/com/solidosystems/tuplesoup/core/ValueTransactional.java index f3be9829..ab7083b9 100644 --- a/Robust/Transactions/mytuplesoup/src/com/solidosystems/tuplesoup/core/ValueTransactional.java +++ b/Robust/Transactions/mytuplesoup/src/com/solidosystems/tuplesoup/core/ValueTransactional.java @@ -5,6 +5,7 @@ package com.solidosystems.tuplesoup.core; +import TransactionalIO.core.TransactionalFile; import dstm2.AtomicByteArray; import dstm2.AtomicSuperClass; import dstm2.atomic; @@ -487,7 +488,7 @@ public class ValueTransactional implements AtomicSuperClass{ * * @param out the DataOutputStream the Value should be written to */ - public void writeToFile(DataOutput out) throws IOException{ + public void writeToFile(TransactionalFile out) throws IOException{ out.writeByte(atomicfields.getType()); switch(atomicfields.getType()){ case STRING : out.writeInt(atomicfields.getStr_value().length()); @@ -509,7 +510,7 @@ public class ValueTransactional implements AtomicSuperClass{ break; case BINARY : out.writeInt(atomicfields.getBinary().length()); for (int i=0; i