public Slot allocateSlot(int length) {
   if (length <= 0) {
     throw new IllegalArgumentException();
   }
   if (_freespaceManager != null && _freespaceManager.isStarted()) {
     Slot slot = _freespaceManager.allocateSlot(length);
     if (slot != null) {
       if (DTrace.enabled) {
         DTrace.GET_SLOT.logLength(slot.address(), slot.length());
       }
       return slot;
     }
     while (growDatabaseByConfiguredSize()) {
       slot = _freespaceManager.allocateSlot(length);
       if (slot != null) {
         if (DTrace.enabled) {
           DTrace.GET_SLOT.logLength(slot.address(), slot.length());
         }
         return slot;
       }
     }
   }
   Slot appendedSlot = appendBytes(length);
   if (DTrace.enabled) {
     DTrace.GET_SLOT.logLength(appendedSlot.address(), appendedSlot.length());
   }
   return appendedSlot;
 }
  void readThis() throws OldFormatException {
    newSystemData(AbstractFreespaceManager.FM_LEGACY_RAM, StandardIdSystemFactory.LEGACY);
    blockSizeReadFromFile(1);

    _fileHeader = FileHeader.read(this);

    if (config().generateCommitTimestamps().isUnspecified()) {
      config().generateCommitTimestamps(_systemData.idToTimestampIndexId() != 0);
    }

    createStringIO(_systemData.stringEncoding());

    createIdSystem();

    initializeClassMetadataRepository();
    initalizeWeakReferenceSupport();

    setNextTimeStampId(systemData().lastTimeStampID());

    classCollection().setID(_systemData.classCollectionID());
    classCollection().read(systemTransaction());

    Converter.convert(new ConversionStage.ClassCollectionAvailableStage(this));

    _fileHeader.readIdentity(this);

    if (_config.isReadOnly()) {
      return;
    }

    if (!configImpl().commitRecoveryDisabled()) {
      _fileHeader.completeInterruptedTransaction(this);
    }

    FreespaceManager blockedFreespaceManager =
        AbstractFreespaceManager.createNew(this, _systemData.freespaceSystem());

    installFreespaceManager(blockedFreespaceManager);

    blockedFreespaceManager.read(this, _systemData.inMemoryFreespaceSlot());
    blockedFreespaceManager.start(_systemData.bTreeFreespaceId());

    _fileHeader = _fileHeader.convert(this);

    if (freespaceMigrationRequired(blockedFreespaceManager)) {
      migrateFreespace(blockedFreespaceManager);
    }

    writeHeader(true, false);

    if (Converter.convert(new ConversionStage.SystemUpStage(this))) {
      _systemData.converterVersion(Converter.VERSION);
      _fileHeader.writeVariablePart(this);
      transaction().commit();
    }
  }
  private void migrateFreespace(FreespaceManager oldFreespaceManager) {

    FreespaceManager newFreespaceManager =
        AbstractFreespaceManager.createNew(this, configImpl().freespaceSystem());
    newFreespaceManager.start(0);

    systemData().freespaceSystem(configImpl().freespaceSystem());

    installFreespaceManager(newFreespaceManager);

    AbstractFreespaceManager.migrate(oldFreespaceManager, newFreespaceManager);
    _fileHeader.writeVariablePart(this);
  }
 private boolean freespaceMigrationRequired(FreespaceManager freespaceManager) {
   if (freespaceManager == null) {
     return false;
   }
   byte readSystem = _systemData.freespaceSystem();
   byte configuredSystem = configImpl().freespaceSystem();
   if (freespaceManager.systemType() == configuredSystem) {
     return false;
   }
   if (configuredSystem != 0) {
     return true;
   }
   return AbstractFreespaceManager.migrationRequired(readSystem);
 }
  public void writeHeader(boolean startFileLockingThread, boolean shuttingDown) {
    if (shuttingDown) {
      _freespaceManager.write(this);
      _freespaceManager = null;
    }

    StatefulBuffer writer = createStatefulBuffer(systemTransaction(), 0, _fileHeader.length());

    _fileHeader.writeFixedPart(this, startFileLockingThread, shuttingDown, writer, blockSize());

    if (shuttingDown) {
      ensureLastSlotWritten();
    }
    syncFiles();
  }
 private boolean growDatabaseByConfiguredSize() {
   int reservedStorageSpace = configImpl().databaseGrowthSize();
   if (reservedStorageSpace <= 0) {
     return false;
   }
   int reservedBlocks = _blockConverter.bytesToBlocks(reservedStorageSpace);
   int reservedBytes = _blockConverter.blocksToBytes(reservedBlocks);
   Slot slot = new Slot(_blockEndAddress, reservedBlocks);
   if (Debug4.xbytes && Deploy.overwrite) {
     overwriteDeletedBlockedSlot(slot);
   } else {
     writeBytes(new ByteArrayBuffer(reservedBytes), _blockEndAddress, 0);
   }
   _freespaceManager.free(_blockConverter.toNonBlockedLength(slot));
   _blockEndAddress += reservedBlocks;
   return true;
 }
  public void free(Slot slot) {
    if (slot.isNull()) {
      return;

      // TODO: This should really be an IllegalArgumentException but old database files
      //       with index-based FreespaceManagers appear to deliver zeroed slots.
      // throw new IllegalArgumentException();
    }
    if (_freespaceManager == null) {
      // Can happen on early free before freespacemanager
      // is up, during conversion.
      return;
    }

    if (DTrace.enabled) {
      DTrace.FILE_FREE.logLength(slot.address(), slot.length());
    }

    _freespaceManager.free(slot);
  }