Exemplo n.º 1
0
  @Test
  public void testRenameFile() throws IOException {
    final ShardId shardId = new ShardId(new Index("index"), 1);
    DirectoryService directoryService = new LuceneManagedDirectoryService(random(), false);
    Store store =
        new Store(
            shardId,
            ImmutableSettings.EMPTY,
            directoryService,
            randomDistributor(directoryService),
            new DummyShardLock(shardId));
    {
      IndexOutput output = store.directory().createOutput("foo.bar", IOContext.DEFAULT);
      int iters = scaledRandomIntBetween(10, 100);
      for (int i = 0; i < iters; i++) {
        BytesRef bytesRef = new BytesRef(TestUtil.randomRealisticUnicodeString(random(), 10, 1024));
        output.writeBytes(bytesRef.bytes, bytesRef.offset, bytesRef.length);
      }
      CodecUtil.writeFooter(output);
      output.close();
    }
    store.renameFile("foo.bar", "bar.foo");
    assertThat(store.directory().listAll().length, is(1));
    final long lastChecksum;
    try (IndexInput input = store.directory().openInput("bar.foo", IOContext.DEFAULT)) {
      lastChecksum = CodecUtil.checksumEntireFile(input);
    }

    try {
      store.directory().openInput("foo.bar", IOContext.DEFAULT);
      fail("file was renamed");
    } catch (FileNotFoundException | NoSuchFileException ex) {
      // expected
    }
    {
      IndexOutput output = store.directory().createOutput("foo.bar", IOContext.DEFAULT);
      int iters = scaledRandomIntBetween(10, 100);
      for (int i = 0; i < iters; i++) {
        BytesRef bytesRef = new BytesRef(TestUtil.randomRealisticUnicodeString(random(), 10, 1024));
        output.writeBytes(bytesRef.bytes, bytesRef.offset, bytesRef.length);
      }
      CodecUtil.writeFooter(output);
      output.close();
    }
    DistributorDirectory distributorDirectory =
        DirectoryUtils.getLeaf(store.directory(), DistributorDirectory.class);
    if (distributorDirectory != null
        && distributorDirectory.getDirectory("foo.bar")
            != distributorDirectory.getDirectory("bar.foo")) {
      try {
        store.renameFile("foo.bar", "bar.foo");
        fail("target file already exists in a different directory");
      } catch (IOException ex) {
        // expected
      }

      try (IndexInput input = store.directory().openInput("bar.foo", IOContext.DEFAULT)) {
        assertThat(lastChecksum, equalTo(CodecUtil.checksumEntireFile(input)));
      }
      assertThat(store.directory().listAll().length, is(2));
      assertDeleteContent(store, directoryService);
      IOUtils.close(store);
    } else {
      store.renameFile("foo.bar", "bar.foo");
      assertThat(store.directory().listAll().length, is(1));
      assertDeleteContent(store, directoryService);
      IOUtils.close(store);
    }
  }
  @Test
  public void testGetLeave() throws IOException {
    Path file = createTempDir();
    final int iters = scaledRandomIntBetween(10, 100);
    for (int i = 0; i < iters; i++) {
      {
        BaseDirectoryWrapper dir = newFSDirectory(file);
        FSDirectory directory =
            DirectoryUtils.getLeaf(new FilterDirectory(dir) {}, FSDirectory.class, null);
        assertThat(directory, notNullValue());
        assertThat(directory, sameInstance(DirectoryUtils.getLeafDirectory(dir, null)));
        dir.close();
      }

      {
        BaseDirectoryWrapper dir = newFSDirectory(file);
        FSDirectory directory = DirectoryUtils.getLeaf(dir, FSDirectory.class, null);
        assertThat(directory, notNullValue());
        assertThat(directory, sameInstance(DirectoryUtils.getLeafDirectory(dir, null)));
        dir.close();
      }

      {
        Set<String> stringSet = Collections.emptySet();
        BaseDirectoryWrapper dir = newFSDirectory(file);
        FSDirectory directory =
            DirectoryUtils.getLeaf(
                new FileSwitchDirectory(stringSet, dir, dir, random().nextBoolean()),
                FSDirectory.class,
                null);
        assertThat(directory, notNullValue());
        assertThat(directory, sameInstance(DirectoryUtils.getLeafDirectory(dir, null)));
        dir.close();
      }

      {
        Set<String> stringSet = Collections.emptySet();
        BaseDirectoryWrapper dir = newFSDirectory(file);
        FSDirectory directory =
            DirectoryUtils.getLeaf(
                new FilterDirectory(
                    new FileSwitchDirectory(stringSet, dir, dir, random().nextBoolean())) {},
                FSDirectory.class,
                null);
        assertThat(directory, notNullValue());
        assertThat(directory, sameInstance(DirectoryUtils.getLeafDirectory(dir, null)));
        dir.close();
      }

      {
        Set<String> stringSet = Collections.emptySet();
        BaseDirectoryWrapper dir = newFSDirectory(file);
        RAMDirectory directory =
            DirectoryUtils.getLeaf(
                new FilterDirectory(
                    new FileSwitchDirectory(stringSet, dir, dir, random().nextBoolean())) {},
                RAMDirectory.class,
                null);
        assertThat(directory, nullValue());
        dir.close();
      }
    }
  }