1 package com.daffodilwoods.daffodildb.server.datasystem.persistentsystem; 2 3 import com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs; 4 import java.io.*; 5 import java.util.*; 6 import com.daffodilwoods.database.utility.P; 7 import com.daffodilwoods.database.resource.DException; 8 import com.daffodilwoods.database.general.QualifiedIdentifier; 9 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*; 10 import com.daffodilwoods.daffodildb.server.sql99.utils._Reference; 11 12 17 public class DRandomAccessFileUpto3_2 { 18 19 24 25 protected HashMap filesMap; 26 27 30 private boolean multiFileSupport; 31 32 35 protected FileGenerator fileGenerator; 36 37 40 protected long fileSize; 41 42 45 protected RandomAccessFile raf; 46 47 50 protected String path ; 51 52 57 protected long newFileSize; 58 59 62 protected String databaseName; 63 64 private boolean fileClosed = false; 65 66 69 private long length; 70 71 75 76 77 78 public DRandomAccessFileUpto3_2(RandomAccessFile raf0,long fileSize0,String databaseURL0,int incrementFactor0,boolean multiFileSupport1,FileGenerator fileGenerator0,int clusterSize ) throws DException{ 79 path = databaseURL0.substring(0,databaseURL0.lastIndexOf(File.separator)); 80 databaseName = databaseURL0.substring(databaseURL0.lastIndexOf(File.separator)+ 1,databaseURL0.lastIndexOf('.')); 81 fileSize = fileSize0; 82 83 newFileSize = fileSize*incrementFactor0/100; 84 long remainder = newFileSize%clusterSize; 85 newFileSize = newFileSize - remainder; 86 multiFileSupport = multiFileSupport1; 87 fileGenerator = fileGenerator0; 88 raf = raf0; 89 try { 90 length = raf.length(); 91 } 92 catch (IOException ex) { 93 } 94 if(multiFileSupport){ 95 filesMap = new HashMap(); 96 init(); 97 filesMap.put(databaseName,raf); 98 } 99 } 100 101 106 107 protected void init() throws DException { 108 int no_OfFiles = fileGenerator.getIndex(databaseName); 109 if(no_OfFiles == -1) 110 fileGenerator.insert(databaseName); 111 for( int i = 1 ; i < no_OfFiles; i++ ){ 112 filesMap.put(databaseName+i,null); 113 } 114 } 115 116 125 126 protected final void seek(long pos) throws DException{ 127 try { 128 129 if(multiFileSupport){ 130 long diff = pos - fileSize; 131 int x = (int)((diff < 0 ) ? 0 : (diff/newFileSize)+1); 132 if(x >= filesMap.size()) 133 makeNewFile(x); 134 else 135 loadNewFile(x); 136 long relativePointer = diff < 0 ? pos : diff % newFileSize; 137 raf.seek(relativePointer); 138 } 139 else{ 140 while(pos >= length){ 141 length = length + newFileSize; 142 raf.setLength(length); 143 } 144 if(pos!= raf.getFilePointer()) 145 raf.seek(pos); 146 } 147 } 148 catch (IOException ex) { 149 if(!fileClosed){ 150 Exception xyzabc = new Exception (path+File.separator+databaseName+".ddb"); 151 xyzabc.printStackTrace(); 152 throw new DException("DSE2025",new Object [] {ex.getMessage()}); 153 } 154 } 155 } 156 157 163 164 protected synchronized void write(long startAddress,byte [] bytes) throws DException{ 165 try { 166 seek(startAddress); 167 raf.write(bytes); 168 } 169 catch (IOException ex) { 170 if(!fileClosed ) 171 throw new DException("DSE2025",new Object [] {ex.getMessage()}); 172 } 173 } 174 178 185 synchronized final long readLong(long startAddr) throws DException { 186 try { 187 seek(startAddr); 188 long val = raf.readLong(); 189 return val; 190 } 191 catch (IOException ex) { 192 if(fileClosed ) 193 return 0; 194 throw new DException("DSE2025",new Object [] {ex.getMessage()}); 195 } 196 } 197 198 205 206 protected synchronized byte[] read(byte b[],long addr) throws DException { 207 try { 208 seek(addr); 209 raf.read(b); 210 return b; 211 } 212 catch (IOException ex) { 213 if(fileClosed ) 214 return b; 215 throw new DException("DSE2025",new Object [] {ex.getMessage()}); 216 } 217 } 218 222 final void close() throws IOException{ 223 if(filesMap != null){ 224 String [] str = (String [])filesMap.keySet().toArray(new String [0]); 225 for(int i = 0,size = str.length; i < size ; i++){ 226 RandomAccessFile raf1 = (RandomAccessFile) filesMap.get(str[i]); 227 if(raf1 != null) 228 raf1.close(); 229 } 230 } 231 raf.close(); 232 fileClosed = true; 233 } 234 243 244 protected void makeNewFile(int x) throws DException,IOException{ 245 try{ 246 String newFileName = fileGenerator.addNewFile(x,databaseName); 247 raf = new RandomAccessFile(path+File.separator+newFileName+".dat","rw"); 248 raf.setLength(newFileSize); 249 filesMap.put(newFileName, raf); 250 } 251 catch(NullPointerException de) { 252 throw new DException("DSE0",new Object [] {de.getMessage()}); 253 } 254 } 255 264 265 protected void loadNewFile(int x)throws DException,IOException{ 266 String name = x == 0 ? databaseName : databaseName+x; 267 raf = (RandomAccessFile)filesMap.get(name); 268 if(raf == null){ 269 raf = new RandomAccessFile(path+File.separator+name+".dat","rw"); 270 raf.setLength(newFileSize); 271 filesMap.put(name,raf); 272 } 273 } 274 275 287 synchronized byte[] readFirst(byte b[],long addr) throws DException { 288 try { 289 seek(addr); 290 raf.read(b); 291 return b; 292 } 293 catch (IOException ex) { 294 if(fileClosed ) 295 return b; 296 throw new DException("DSE2025",new Object [] {ex.getMessage()}); 297 } 298 } 299 310 311 synchronized void writeFirst(long startAddress,byte [] bytes) throws DException{ 312 try { 313 seek(startAddress); 314 raf.write(bytes); 315 } 316 catch (IOException ex) { 317 if(!fileClosed ) 318 throw new DException("DSE2025",new Object [] {ex.getMessage()}); 319 } 320 } 321 322 323 324 328 String getURL(){ 329 return path+File.separator+databaseName+".ddb"; 330 } 331 335 public RandomAccessFile getRandomAccessFile(){ 336 return raf; 337 } 338 342 public boolean isMultiFileSupport(){ 343 return multiFileSupport; 344 } 345 349 public HashMap getMultiFileMap(){ 350 return filesMap; 351 352 } 353 358 public int getIndexMultipleFiles() throws DException{ 359 return fileGenerator!= null ? fileGenerator.getIndex(databaseName) : -1; 360 } 361 362 363 } 364 | Popular Tags |