Package org.apache.derby.io

Examples of org.apache.derby.io.StorageFile


    @exception IOException Any exception during accessing the file for read/write
   */
  private StorageRandomAccessFile privAccessFile(StorageFactory storageFactory,String fileName,String filePerms)
    throws java.io.IOException
  {
    StorageFile verifyKeyFile = storageFactory.newStorageFile("",fileName);
    activeFile  = verifyKeyFile;
    this.action = 2;
    activePerms = filePerms;
      try
        {
View Full Code Here


   @exception IOException Any exception during accessing the file for read
   */
  private InputStream privAccessGetInputStream(StorageFactory storageFactory,String fileName)
  throws StandardException
  {
      StorageFile verifyKeyFile = storageFactory.newStorageFile("",fileName);
      activeFile  = verifyKeyFile;
      this.action = 3;
      try
      {
          return (InputStream)java.security.AccessController.doPrivileged(this);
View Full Code Here

    long generationId = factory.getNextId();

    try
    {
      StorageFile file = getAsFile(name, generationId);
            if (file.exists())
            {
        throw StandardException.newException(
                        SQLState.FILE_EXISTS, file);
            }

            ContextManager cm =
                ContextService.getFactory().getCurrentContextManager();

            RawTransaction tran =
                factory.getRawStoreFactory().getXactFactory().findUserTransaction(
                        factory.getRawStoreFactory(),
                        cm,
                        AccessFactoryGlobals.USER_TRANS_NAME);
           
            // Block the backup, If backup is already in progress wait
            // for the backup to finish. Jar files are unlogged but the
            // changes to the  references to the jar file in the catalogs
            // is logged. A consistent backup can not be made when jar file
            // is being added.

            tran.blockBackup(true);

      StorageFile directory = file.getParentDir();
            if (!directory.exists())
      {
                if (!directory.mkdirs())
                {
          throw StandardException.newException(
                            SQLState.FILE_CANNOT_CREATE_SEGMENT, directory);
                }
      }
View Full Code Here

        {
      if (SanityManager.DEBUG)
        SanityManager.THROWASSERT(
                    "cannot create stream container in non-temp segments yet.");

      StorageFile container = dataFactory.getContainerPath( identity, false);

      if (!privExists(container))
            {

        if (!forCreate)
          return null;

        StorageFile directory = container.getParentDir();

        if (!privExists(directory))
                {
          // make sure only 1 thread can create a segment at one time
          synchronized(dataFactory)
View Full Code Here

    protected StorageFile privGetFileName(ContainerKey identity, boolean stub,
                                    boolean errorOK, boolean tryAlternatePath)
        throws StandardException
    {
        StorageFile container = dataFactory.getContainerPath( identity, stub);

        // retry with small case 'c' and 'd'
        // bug fix for track 3444
        if (!container.exists() && tryAlternatePath)
        {
            container = dataFactory.getAlternateContainerPath( identity, stub);
        }

        if (!container.exists()) {

            StorageFile directory = container.getParentDir();

            if (!directory.exists())
            {
                // make sure only 1 thread can create a segment at one time
                synchronized(dataFactory)
                {
                    if (!directory.exists())
                    {
                        if (!directory.mkdirs())
                        {
                            if (errorOK)
                            {
                                return null;
                            }
View Full Code Here

                }
     
                // create container at the backup location.
                if (isStub) {
                    // get the stub ( it is a committted drop table container )
                    StorageFile file = privGetFileName((ContainerKey)getIdentity(),
                                                       true, false, true);
                    backupFile = new File(backupLocation, file.getName());

          // directly copy the stub to the backup
          if(!FileUtil.copyFile(dataFactory.getStorageFactory(),
                                          file, backupFile))
                    {
                        throw StandardException.newException(
                                              SQLState.RAWSTORE_ERROR_COPYING_FILE,
                                              file, backupFile);
                    }
                }else {
                    // regular container file
                    long lastPageNumber= getLastPageNumber(handle);
                    if (lastPageNumber == ContainerHandle.INVALID_PAGE_NUMBER) {
                        // last page number is invalid if there are no pages in
                        // the container yet. No need to backup this container,
                        // this container creation is yet to complete.The reason
                        // backup is getting called on such a container is
                        // because container handle appears in the cache after
                        // the file is created on the disk but before it's
                        // first page is allocated.
                        return;
                    }

                    StorageFile file =
                        privGetFileName(
                            (ContainerKey)getIdentity(), false, false, true);

                    backupFile = new File(backupLocation , file.getName());
                    backupRaf  = new RandomAccessFile(backupFile,  "rw");

                    byte[] encryptionBuf = null;
                    if (dataFactory.databaseEncrypted()) {
                        // Backup uses seperate encryption buffer to encrypt the
View Full Code Here

    protected void encryptContainer(BaseContainerHandle handle,
                                    String newFilePath
        throws StandardException
    {
        BasePage page = null;
        StorageFile newFile =
            dataFactory.getStorageFactory().newStorageFile(newFilePath);
        StorageRandomAccessFile newRaf = null;
        try {
            long lastPageNumber= getLastPageNumber(handle);
View Full Code Here

         case GET_FILE_NAME_ACTION:
             return privGetFileName( actionIdentity, actionStub, actionErrorOK, actionTryAlternatePath);

         case CREATE_CONTAINER_ACTION:
         {
             StorageFile file = privGetFileName( actionIdentity, false, false, false);

             try {
                 if (file.exists()) {
                     // note I'm left in the no-identity state as fillInIdentity()
                     // hasn't been called.
                     throw StandardException.newException( SQLState.FILE_EXISTS, file);
                 }
             } catch (SecurityException se) {
                 throw StandardException.newException( SQLState.FILE_CREATE, se, file);
             }

             try {

                 // OK not to force WAL here, in fact, this operation preceeds the
                 // creation of the log record to ensure sufficient space.

                 dataFactory.writeInProgress();
                 try
                 {
                     fileData = file.getRandomAccessFile( "rw");
                 }
                 finally
                 {
                     dataFactory.writeFinished();
                 }

                 // This container format specifies that the first page is an
                 // allocation page and the container information is stored within
                 // it.  The allocation page needs to be somewhat formatted
                 // because if the system crashed after the create container log
                 // operation is written, it needs to be well formed enough to get
                 // the container information back out of it.
                 //
                 // Don't try to go thru the page cache here because the container
                 // object cannot be found in the container cache at this point
                 // yet.  However, if we use the page cache to store the first
                 // allocation page, then in order to write itself out, it needs to
                 // ask the container to do so, which is going to create a
                 // deadlock.  The allocation page cannot write itself out without
                 // going thru the container because it doesn't know where its
                 // offset is.  Here we effectively hardwired page 0 at offset 0 of
                 // the container file to be the first allocation page.

                 // create an embryonic page - if this is not a temporary container,
                 // synchronously write out the file header.
                 writeRAFHeader(fileData, true,
                                (actionIdentity.getSegmentId() != ContainerHandle.TEMPORARY_SEGMENT));

             } catch (SecurityException se) {

                 // only thrown by the RandomeAccessFile constructor,
                 // so the file won't exist
                 throw StandardException.newException( SQLState.FILE_CREATE, se, file);

             } catch (IOException ioe) {

                 boolean fileDeleted;
                 try {
                     fileDeleted = privRemoveFile(file);
                 } catch (SecurityException se) {
                     throw StandardException.newException( SQLState.FILE_CREATE_NO_CLEANUP, ioe, file, se.toString());
                 }

                 if (!fileDeleted) {
                     throw StandardException.newException( SQLState.FILE_CREATE_NO_CLEANUP, ioe, file, ioe.toString());
                 }

                 throw StandardException.newException( SQLState.FILE_CREATE, ioe, file);
             }

             canUpdate = true;
             return null;
         } // end of case CREATE_CONTAINER_ACTION

         case REMOVE_FILE_ACTION:
             return privRemoveFile( actionFile) ? this : null;

         case OPEN_CONTAINER_ACTION:
         {
             boolean isStub = false// is this a stub?

             StorageFile file = privGetFileName( actionIdentity, false, true, true);
             if (file == null)
                 return null;

             try {
                 if (!file.exists()) {

                     // file does not exist, may be it has been stubbified
                     file = privGetFileName( actionIdentity, true, true, true);
                     if (!file.exists())
                         return null;
                     isStub = true;
                 }
             } catch (SecurityException se) {
                 throw StandardException.newException(
                     SQLState.DATA_UNEXPECTED_EXCEPTION, se);
             }

             canUpdate = false;
             try {
                 if (!dataFactory.isReadOnly() && file.canWrite())
                     canUpdate = true;
             } catch (SecurityException se) {
                 // just means we can't write to it.
             }

             try {

                 fileData = file.getRandomAccessFile(canUpdate ? "rw" : "r");
                 fileData.seek(FIRST_ALLOC_PAGE_OFFSET);
                 readHeader(fileData);

                 if (SanityManager.DEBUG)
                 {
                     if (isStub)
                         SanityManager.ASSERT(getDroppedState() && getCommittedDropState(),
                                              "a stub failed to set drop state");
                 }

             } catch (IOException ioe) {

                 if (isStub)
                 {
                     throw dataFactory.
                         markCorrupt(StandardException.
                                     newException(SQLState.
                                                  FILE_CONTAINER_EXCEPTION,
                                                  ioe, this));
                 }

                 // maybe it is being stubbified... try that
                 StorageFile stub =
                     privGetFileName(actionIdentity, true, true, true);

                 if (stub.exists())
                 {
                     try
                     {
                         boolean delete_status = privRemoveFile(file);
                         if (SanityManager.DEBUG)
                         {
                             if (!delete_status)
                             {
                                 SanityManager.THROWASSERT(
                                     "delete of file (" + file + ") failed.");
                             }
                         }

                         fileData =
                             stub.getRandomAccessFile(canUpdate ? "rw" : "r");

                         readHeader(fileData);
                     }
                     catch (IOException ioe2)
                     {
                         throw dataFactory.
                             markCorrupt(StandardException.
                                         newException(SQLState.
                                                      FILE_CONTAINER_EXCEPTION,
                                                      ioe2, this));
                     }

                     // RESOLVE: this is a temporary hack

                 }
                 else
                     throw dataFactory.
                         markCorrupt(StandardException.
                                     newException(SQLState.
                                                  FILE_CONTAINER_EXCEPTION,
                                                  ioe, this));
             }

             return this;
         } // end of case OPEN_CONTAINER_ACTION

         case STUBBIFY_ACTION:
         {
             StorageFile file = privGetFileName( actionIdentity, false, false, true);
             StorageFile stub = privGetFileName( actionIdentity, true, false, false);

             StorageRandomAccessFile stubData = null;

             try
             {
                 // !!!!!
                 // bumpContainerVersion();
                 //
                 // do NOT bump the container version.  We WANT the stubbify
                 // operation to get redone every time.  This is because this
                 // operation first writes out the stub and then remove the
                 // container file.  If we bump the version, then the stub will
                 // contain the new version.  And if the system crashes right then,
                 // then we will skip the whole operation during redo even though
                 // the container file may not have been removed.  Since we don't
                 // want to have the remove happen before the stub is written, we
                 // cannot sync it and therefore cannot be sure the remove
                 // happened before the system crashed.

                 if (!stub.exists())
                 {
                     // write the header to the stub
                     stubData = stub.getRandomAccessFile( "rw");

                     writeRAFHeader(stubData,
                                    true, /* create */
                                    true); /* sync */

                     stubData.close();
                     stubData = null;
                 }


                 // Force WAL and check for database corruption before removing file.
                 // This is one operation where the container is changed on disk
                 // directly without going thru the container cache, which otherwise
                 // would have force WAL.  Take care of it here.
                 dataFactory.flush(actionInstant);

                 // try to remove the container file
                 // fileDate is not null only if we are redoing a removeContainer
                 // (stubbify) operation.  Then fileData acutally is opened against
                 // the stub and the original container file does not exist.
                 // Then we need to close it here because this method is called by
                 // cache.remove and nobody will be able to see fileData after this.
                 privRemoveFile(file);

             }
             catch (SecurityException se)
             {
                 throw StandardException.
                     newException(SQLState.FILE_CANNOT_REMOVE_FILE, se, file,
                                  se.toString());
             }
             catch (IOException ioe)
             {
                 // exception thrown while in creating the stub.  Remove the
                 // (half-baked) stub
                 try
                 {
                     if (stubData != null)
                     {
                         stubData.close();
                         stub.delete();
                         stubData = null;
                     }

                     if (fileData != null)
                     {
View Full Code Here

                            home = relativeRoot.getPath();
                            canonicalHome = relativeRoot.getCanonicalPath();
                            rootStorageFactory = getStorageFactoryInstance( true, null, null, null);
                            if( home != null)
                            {
                                StorageFile rootDir = rootStorageFactory.newStorageFile( null);
                                rootDir.mkdirs();
                            }
                            return null;
                        }
                    }
                    );
View Full Code Here

                            return new FileInputStream(propFile);
                        }
                        else
                        {
                            StorageFactory storageFactory = privGetStorageFactoryInstance( true, serviceName, null, null);
                            StorageFile file = storageFactory.newStorageFile( PersistentService.PROPERTIES_NAME);
                            InputStream is1 = file.getInputStream();
                            storageFactory.shutdown();
                            return is1;
                        }
                    }
                }
View Full Code Here

TOP

Related Classes of org.apache.derby.io.StorageFile

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.