0

The problem with the code below is that the VFS does not seem to be able to persist the byte array. As Xodus VFS writes zero byte:

  @Override
  public FileModel put(
      String appId, String namespace, String name, InputStream is) {
    final FileModel[] createdFile = {null};
    final Environment env = manager.getEnvironment(xodusRoot, appId);
    final VirtualFileSystem vfs  = new VirtualFileSystem(env);
    env.executeInTransaction(
        new TransactionalExecutable() {
          @Override
          public void execute(@NotNull final Transaction txn) {
            final File file = vfs.openFile(txn, name, true);;
            try {
              byte[] ba = ByteStreams.toByteArray(is);
              LOG.info("Byte array size: " + ba.length); // Size: 3466
              vfs.writeFile(txn, file).write(ba, 0, ba.length);
            } catch (IOException e) {
              e.printStackTrace();
            }
            long fileSize = vfs.getFileLength(txn, file);
            LOG.info("File Size: " + fileSize); // Size: 0 <----
            createdFile[0] = new FileModel();
            createdFile[0].setDescriptor(file.getDescriptor());
            createdFile[0].setName(name);
            createdFile[0].setCreated(file.getCreated());
            createdFile[0].setModified(file.getLastModified());
          }
        });
    vfs.shutdown();
    return createdFile[0];
  }

And here's the log:

[qtp1007568224-16] WARN jetbrains.exodus.io.FileDataWriter - Can't open directory channel. Log directory fsync won't be performed.
[qtp1007568224-16] WARN jetbrains.exodus.io.FileDataWriter - Can't open directory channel. Log directory fsync won't be performed.
[qtp1007568224-16] INFO jetbrains.exodus.env.EnvironmentImpl - Exodus environment created: \tmp\xodus\master
[qtp1007568224-16] WARN jetbrains.exodus.io.FileDataWriter - Can't open directory channel. Log directory fsync won't be performed.
[qtp1007568224-16] INFO jetbrains.exodus.env.EnvironmentImpl - Exodus environment created: \tmp\xodus\ab5b92099ad443259b4deaf8df6facc4
[qtp1007568224-16] INFO com.backend.repository.jee.JeeXodusVFSRepository - Byte array size: 3466
[qtp1007568224-16] INFO com.backend.repository.jee.JeeXodusVFSRepository - File Size: 0
[qtp1007568224-16] INFO com.backend.resource.jee.JeeFileServerResource - File size=3466; File.created=1575274836678; File.name="index.html"; File.modified=1575274836678; File.etag=<null>; File.descriptor=261; File.url=<null>
quarks
  • 33,478
  • 73
  • 290
  • 513

1 Answers1

1

The method vfs.writeFile(txn, file) returns an OutputStream instance that should be closed to save written data.

Vyacheslav Lukianov
  • 1,913
  • 8
  • 12
  • The problem with the VFS also is that when you try to override a file, like `File file = vfs.openFile(txn, name, true);` it appends the stream and corrupts the file. – quarks Dec 03 '19 at 16:18
  • It seems if the new bytes to write into the filed is shorter the other bytes that are part of the file still exists. – quarks Dec 03 '19 at 16:20
  • And in order to work around this ` `txn -> { vfs.deleteFile(txn, name); File file = vfs.openFile(txn, name, true);}` but I feel that this is quite not the correct way to do it. – quarks Dec 03 '19 at 16:42
  • 1
    It shouldn't append. It doesn't clear the content of the file before writing, it writes above existing content from the beginning – Vyacheslav Lukianov Dec 03 '19 at 16:53
  • Ah yes, I mean it writes but does not remove existing bytes, what is the proper way to achieve normal override? – quarks Dec 03 '19 at 17:08