Search code examples
javaxodus

Implementing "Move" function with VFS


I'm trying to implement a wrapped "move" function with Xodus, but something is not working out right:

  @Override
  public boolean move(String appId, String name, String targetName) {
    final boolean[] success = new boolean[1];
    final Environment env = manager.getEnvironment(xodusRoot, appId);
    final VirtualFileSystem vfs = manager.getVirtualFileSystem(env);
    env.executeInTransaction(
            new TransactionalExecutable() {
              @Override
              public void execute(@NotNull final Transaction txn) {
                File file = vfs.openFile(txn, name, false);
                InputStream input = vfs.readFile(txn, file);
                if(input != null) {
                  File targetFile = vfs.openFile(txn, targetName, true);
                  DataOutputStream output = new DataOutputStream(vfs.writeFile(txn, targetFile));
                  try {
                    output.write(ByteStreams.toByteArray(input));
                  } catch (IOException e) {
                    e.printStackTrace();
                  }
                  vfs.deleteFile(txn, name);
                  success[0] = true;
                }
              }
            });
    // vfs.shutdown();
    // env.close();
    return success[0];
  }

The problem is the file gets moved but the byte array is not getting copied, not sure if the problem is because of multiple VFS operation in the same transaction. Can someone give me a hint of why the bytes from the source file are not getting copied properly?


Solution

  • Looks like you are trying to implement another version of VirtualFileSystem.renameFile(..).