@Override
  public synchronized SequentialFileFactory newFileFactory(final SimpleString address)
      throws Exception {

    String guid = UUIDGenerator.getInstance().generateStringUUID();

    SequentialFileFactory factory = newFileFactory(guid);

    factory.createDirs();

    File fileWithID =
        new File(directory, guid + File.separatorChar + PagingStoreFactoryNIO.ADDRESS_FILE);

    BufferedWriter writer =
        new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileWithID)));

    try {
      writer.write(address.toString());
      writer.newLine();
    } finally {
      writer.close();
    }

    return factory;
  }
 @Override
 public File getFolder() {
   SequentialFileFactory factoryUsed = this.fileFactory;
   if (factoryUsed != null) {
     return factoryUsed.getDirectory();
   } else {
     return null;
   }
 }
 @Override
 public Collection<Integer> getCurrentIds() throws Exception {
   List<Integer> ids = new ArrayList<>();
   if (fileFactory != null) {
     for (String fileName : fileFactory.listFiles("page")) {
       ids.add(getPageIdFromFileName(fileName));
     }
   }
   return ids;
 }
 @Override
 public void sendPages(ReplicationManager replicator, Collection<Integer> pageIds)
     throws Exception {
   lock.writeLock().lock();
   try {
     for (Integer id : pageIds) {
       SequentialFile sFile = fileFactory.createSequentialFile(createFileName(id));
       if (!sFile.exists()) {
         continue;
       }
       replicator.syncPages(sFile, id, getAddress());
     }
   } finally {
     lock.writeLock().unlock();
   }
 }
  @Override
  public Page createPage(final int pageNumber) throws Exception {
    String fileName = createFileName(pageNumber);

    if (fileFactory == null) {
      fileFactory = storeFactory.newFileFactory(getStoreName());
    }

    SequentialFile file = fileFactory.createSequentialFile(fileName);

    Page page = new Page(storeName, storageManager, fileFactory, file, pageNumber);

    // To create the file
    file.open();

    file.position(0);

    file.close();

    return page;
  }
 @Override
 public boolean checkPageFileExists(final int pageNumber) {
   String fileName = createFileName(pageNumber);
   SequentialFile file = fileFactory.createSequentialFile(fileName);
   return file.exists();
 }
  @Override
  public void start() throws Exception {
    lock.writeLock().lock();

    try {

      if (running) {
        // don't throw an exception.
        // You could have two threads adding PagingStore to a
        // ConcurrentHashMap,
        // and having both threads calling init. One of the calls should just
        // need to be ignored
        return;
      } else {
        running = true;
        firstPageId = Integer.MAX_VALUE;

        // There are no files yet on this Storage. We will just return it empty
        if (fileFactory != null) {

          currentPageId = 0;
          if (currentPage != null) {
            currentPage.close();
          }
          currentPage = null;

          List<String> files = fileFactory.listFiles("page");

          numberOfPages = files.size();

          for (String fileName : files) {
            final int fileId = PagingStoreImpl.getPageIdFromFileName(fileName);

            if (fileId > currentPageId) {
              currentPageId = fileId;
            }

            if (fileId < firstPageId) {
              firstPageId = fileId;
            }
          }

          if (currentPageId != 0) {
            currentPage = createPage(currentPageId);
            currentPage.open();

            List<PagedMessage> messages = currentPage.read(storageManager);

            LivePageCache pageCache = new LivePageCacheImpl(currentPage);

            for (PagedMessage msg : messages) {
              pageCache.addLiveMessage(msg);
              if (msg.getMessage().isLargeMessage()) {
                // We have to do this since addLIveMessage will increment an extra one
                ((LargeServerMessage) msg.getMessage()).decrementDelayDeletionCount();
              }
            }

            currentPage.setLiveCache(pageCache);

            currentPageSize.set(currentPage.getSize());

            cursorProvider.addPageCache(pageCache);
          }

          // We will not mark it for paging if there's only a single empty file
          if (currentPage != null && !(numberOfPages == 1 && currentPage.getSize() == 0)) {
            startPaging();
          }
        }
      }

    } finally {
      lock.writeLock().unlock();
    }
  }