guiyanakuang commented on a change in pull request #941:
URL: https://github.com/apache/orc/pull/941#discussion_r736179410



##########
File path: java/tools/src/test/org/apache/orc/tools/TestFileDump.java
##########
@@ -709,6 +714,79 @@ public void testIndexOf() {
     byte[] bytes = ("OO" + OrcFile.MAGIC).getBytes(StandardCharsets.UTF_8);
     byte[] pattern = OrcFile.MAGIC.getBytes(StandardCharsets.UTF_8);
 
-    assertEquals(FileDump.indexOf(bytes, pattern, 1), 2);
+    assertEquals(2, FileDump.indexOf(bytes, pattern, 1));
+  }
+
+  @Test
+  public void testRecover() throws Exception {
+    TypeDescription schema = getMyRecordType();
+    Writer writer = OrcFile.createWriter(testFilePath,
+        OrcFile.writerOptions(conf)
+            .fileSystem(fs)
+            .setSchema(schema));
+    Random r1 = new Random(1);
+    String[] words = new String[]{"It", "was", "the", "best", "of", "times,",
+        "it", "was", "the", "worst", "of", "times,", "it", "was", "the", "age",
+        "of", "wisdom,", "it", "was", "the", "age", "of", "foolishness,", "it",
+        "was", "the", "epoch", "of", "belief,", "it", "was", "the", "epoch",
+        "of", "incredulity,", "it", "was", "the", "season", "of", "Light,",
+        "it", "was", "the", "season", "of", "Darkness,", "it", "was", "the",
+        "spring", "of", "hope,", "it", "was", "the", "winter", "of", 
"despair,",
+        "we", "had", "everything", "before", "us,", "we", "had", "nothing",
+        "before", "us,", "we", "were", "all", "going", "direct", "to",
+        "Heaven,", "we", "were", "all", "going", "direct", "the", "other",
+        "way"};
+    VectorizedRowBatch batch = schema.createRowBatch(1000);
+    for(int i=0; i < 21000; ++i) {
+      appendMyRecord(batch, r1.nextInt(), r1.nextLong(),
+          words[r1.nextInt(words.length)]);
+      if (batch.size == batch.getMaxSize()) {
+        writer.addRowBatch(batch);
+        batch.reset();
+      }
+    }
+    if (batch.size > 0) {
+      writer.addRowBatch(batch);
+    }
+    writer.close();
+
+    long fileSize = fs.getFileStatus(testFilePath).getLen();
+    byte[] bytes = new byte[1024];
+    Path corruptedFilePath = new Path("corruptedFile.orc");
+
+    try {
+      FSDataInputStream fdis = fs.open(testFilePath);
+      FileStatus fileStatus = fs.getFileStatus(testFilePath);
+      FSDataOutputStream fdos = fs.create(corruptedFilePath, true,
+          conf.getInt("io.file.buffer.size", 4096),
+          fileStatus.getReplication(),
+          fileStatus.getBlockSize());
+      long remaining = fileSize;
+
+      while (remaining > 0) {
+        int toRead = (int) Math.min(DEFAULT_BLOCK_SIZE, remaining);
+        byte[] data = new byte[toRead];
+        long startPos = fileSize - remaining;
+        fdis.readFully(startPos, data, 0, toRead);
+        fdos.write(data);
+        remaining = remaining - toRead;
+      }
+      fdos.write(bytes);
+      fdos.write(OrcFile.MAGIC.getBytes(StandardCharsets.UTF_8));
+      fdos.write(bytes);
+      fdis.close();
+      fdos.close();

Review comment:
       Line 774 ~ 776 added redundant writes to turn this file into a corrupt 
file




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@orc.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to