@Override public long getDiskSize() throws JasDBStorageException { try { return channel.size(); } catch(IOException e) { throw new DatastoreException("Unable to determine disksize"); } }
@Override public void closeWriter() throws JasDBStorageException { try { if(channel != null) { if(this.fileLock != null) { this.fileLock.release(); } this.channel.close(); this.randomAccess.close(); this.channel = null; } } catch(IOException e) { throw new DatastoreException("Unable to cleanly close record store", e); } }
private void loadHeader() throws DatastoreException { lock.lock(); try { headerBuffer.clear(); int bytesRead = channel.read(headerBuffer, 0); if(bytesRead == HEADER_SIZE) { long version = headerBuffer.getLong(0); recordCount.set(headerBuffer.getLong(LONGSIZE)); recordPosition = headerBuffer.getLong(LONGSIZE + LONGSIZE); if(version != RECORD_VERSIONS) { throw new DatastoreException("Record version not supported"); } } else { throw new DatastoreException("Invalid record header information"); } } catch(IOException e) { throw new DatastoreException("Unable to read header information", e); } finally { lock.unlock(); } }
private void writeHeader() throws DatastoreException { lock.lock(); try { headerBuffer.clear(); headerBuffer.putLong(0, RECORD_VERSIONS); headerBuffer.putLong(LONGSIZE, recordCount.get()); headerBuffer.putLong(LONGSIZE + LONGSIZE, recordPosition); channel.write(headerBuffer, 0); } catch(IOException e) { throw new DatastoreException("Unable to write header information", e); } finally { lock.unlock(); } }
throw new DatastoreException("Unable to store record to storage", e);
throw new DatastoreException("Unable to read/write to storage", e); } finally { lock.unlock();
@Override public void openWriter() throws DatastoreException { try { File parentDir = recordLocation.getParentFile(); checkState(parentDir.exists() || parentDir.mkdirs()); this.randomAccess = new RandomAccessFile(recordLocation, "rw"); this.channel = randomAccess.getChannel(); LOG.debug("Acquiring exclusive file lock on: {}", recordLocation); this.fileLock = this.channel.tryLock(); if(this.fileLock != null) { LOG.debug("Got an exclusive lock on: {}", recordLocation); if(channel.size() > 0) { loadHeader(); } else { recordCount = new AtomicLong(0); recordPosition = HEADER_SIZE; writeHeader(); } } else { throw new RecordStoreInUseException("Record datastore: " + recordLocation + " is already in use, cannot be opened"); } } catch(IOException e) { throw new DatastoreException("Unable to open record store", e); } catch(OverlappingFileLockException e) { throw new RecordStoreInUseException("Record datastore: " + recordLocation + " is already in use, cannot be opened"); } }
outputBuffer.append(new String(buffer, 0, bytesUncompressed, DATA_ENCODING)); } catch(DataFormatException e) { throw new DatastoreException("Unable to deflate data store", e); throw new DatastoreException("Unable to read record at position: " + recordPosition, e);