Ejemplo n.º 1
0
  public void testRun() throws Exception {
    String wsName = "test";
    DefaultCacheManager dcm = new DefaultCacheManager();
    Cache<String, Metadata> metaCache = dcm.getCache("meta");
    Cache<String, byte[]> dataCache = dcm.getCache("data");

    GridFilesystem gfs = new GridFilesystem(dataCache, metaCache);

    BuildContext bcontext =
        DirectoryBuilder.newDirectoryInstance(metaCache, dataCache, metaCache, wsName);
    bcontext.chunkSize(1024 * 1024);
    Directory directory = bcontext.create();
    Central central = CentralConfig.oldFromDir(directory).build();

    central.newIndexer().index(IndexJobs.create("/bleujin", 10));

    central.newSearcher().createRequest("").find().debugPrint();

    OutputStream output = gfs.getOutput("/test.data");
    IOUtil.copyNClose(new StringInputStream("hello bleujin"), output);

    Debug.line(IOUtil.toStringWithClose(gfs.getInput("/test.data")));

    central.newSearcher().createRequest("").find().debugPrint();

    File root = gfs.getFile("/");
    viewFile(root);

    dcm.stop();
  }
Ejemplo n.º 2
0
  public void testGetParentFile() throws IOException {
    File file = fs.getFile("file.txt");
    assertNull(file.getParentFile());

    file = fs.getFile("/parentdir/file.txt");
    File parentDir = file.getParentFile();
    assertTrue(parentDir instanceof GridFile);
    assertEquals(parentDir.getPath(), "/parentdir");
  }
Ejemplo n.º 3
0
 public void testEquals() throws Exception {
   assertFalse(fs.getFile("").equals(null));
   assertTrue(fs.getFile("").equals(fs.getFile("")));
   assertTrue(fs.getFile("").equals(fs.getFile("/")));
   assertTrue(fs.getFile("foo.txt").equals(fs.getFile("foo.txt")));
   assertTrue(fs.getFile("foo.txt").equals(fs.getFile("/foo.txt")));
   assertFalse(fs.getFile("foo.txt").equals(fs.getFile("FOO.TXT")));
   assertFalse(fs.getFile("/foo.txt").equals(new File("/foo.txt")));
 }
Ejemplo n.º 4
0
  public void testGetParent() throws IOException {
    File file = fs.getFile("file.txt");
    assertEquals(file.getParent(), null);

    file = fs.getFile("/parentdir/file.txt");
    assertEquals(file.getParent(), "/parentdir");

    file = fs.getFile("/parentdir/subdir/file.txt");
    assertEquals(file.getParent(), "/parentdir/subdir");
  }
Ejemplo n.º 5
0
 public void testMultiClose() throws Exception {
   String filePath = "test_close.dat";
   OutputStream out = fs.getOutput(filePath);
   try {
     out.write(1);
   } finally {
     out.close();
     out.close();
   }
   File f = fs.getFile(filePath);
   assertEquals(f.length(), 1);
 }
Ejemplo n.º 6
0
 private void writeToFile(String filePath, String text, boolean append, Integer chunkSize)
     throws IOException {
   OutputStream out =
       chunkSize == null
           ? fs.getOutput(filePath, append)
           : fs.getOutput(filePath, append, chunkSize);
   try {
     out.write(text.getBytes());
   } finally {
     out.close();
   }
 }
Ejemplo n.º 7
0
 // ISPN-2157
 public void testWriteAndReadNegativeByte() throws Exception {
   String filePath = "negative.dat";
   OutputStream out = fs.getOutput(filePath);
   try {
     out.write(-1);
   } finally {
     out.close();
   }
   InputStream in = fs.getInput(filePath);
   try {
     assertEquals(in.read(), 255);
   } finally {
     in.close();
   }
 }
Ejemplo n.º 8
0
  public void testGetFile() throws Exception {
    assertEquals(fs.getFile("file.txt").getPath(), "file.txt");
    assertEquals(fs.getFile("/file.txt").getPath(), "/file.txt");
    assertEquals(fs.getFile("myDir/file.txt").getPath(), "myDir/file.txt");
    assertEquals(fs.getFile("/myDir/file.txt").getPath(), "/myDir/file.txt");

    assertEquals(fs.getFile("myDir", "file.txt").getPath(), "myDir/file.txt");
    assertEquals(fs.getFile("/myDir", "file.txt").getPath(), "/myDir/file.txt");

    File dir = fs.getFile("/myDir");
    assertEquals(fs.getFile(dir, "file.txt").getPath(), "/myDir/file.txt");

    dir = fs.getFile("myDir");
    assertEquals(fs.getFile(dir, "file.txt").getPath(), "myDir/file.txt");
  }
Ejemplo n.º 9
0
  public void testSkip() throws Exception {
    String filePath = "skip.txt";
    writeToFile(filePath, "abcde" + "fghij" + "klmno" + "pqrst" + "uvwxy" + "z", 5);

    InputStream in = fs.getInput(filePath);
    try {
      long skipped = in.skip(2); // skip inside current chunk
      assertEquals(skipped, 2);
      assertEquals((char) in.read(), 'c');

      skipped = in.skip(2); // skip to end of chunk
      assertEquals(skipped, 2);
      assertEquals((char) in.read(), 'f');

      skipped = in.skip(6); // skip into next chunk
      assertEquals(skipped, 6);
      assertEquals((char) in.read(), 'm');

      skipped = in.skip(9); // skip _over_ next chunk
      assertEquals(skipped, 9);
      assertEquals((char) in.read(), 'w');

      skipped = in.skip(-1); // negative skip
      assertEquals(skipped, 0);
      assertEquals((char) in.read(), 'x');

      skipped = in.skip(10); // skip beyond EOF
      assertEquals(skipped, 2);
      assertEquals(in.read(), -1);
    } finally {
      in.close();
    }
  }
Ejemplo n.º 10
0
 public void testReadLoop() throws Exception {
   WritableGridFileChannel wgfc = fs.getWritableChannel("/readTest.txt", false, 100);
   try {
     assertTrue(wgfc.isOpen());
     wgfc.write(ByteBuffer.wrap("This tests read loop.".getBytes()));
   } finally {
     wgfc.close();
   }
   ReadableGridFileChannel rgfc = fs.getReadableChannel("/readTest.txt");
   try {
     assertTrue(
         "This tests read loop.".equals(new String(toBytes(Channels.newInputStream(rgfc)))));
   } finally {
     rgfc.close();
   }
 }
Ejemplo n.º 11
0
 @Override
 public long getResourceLength(ITransaction transaction, String uri) throws WebdavException {
   uri = normalizeURI(uri);
   log.tracef("GridStore.getResourceLength(%s)", uri);
   File file = fs.getFile(root, uri);
   return file.length();
 }
Ejemplo n.º 12
0
 @Override
 public String[] getChildrenNames(ITransaction transaction, String uri) throws WebdavException {
   uri = normalizeURI(uri);
   log.tracef("GridStore.getChildrenNames(%s)", uri);
   File file = fs.getFile(root, uri);
   try {
     String[] childrenNames = null;
     if (file.isDirectory()) {
       File[] children = file.listFiles();
       if (children == null) throw new WebdavException("IO error while listing files for " + file);
       List<String> childList = new ArrayList<String>();
       for (int i = 0; i < children.length; i++) {
         String name = children[i].getName();
         childList.add(name);
         log.trace("Child " + i + ": " + name);
       }
       childrenNames = new String[childList.size()];
       childrenNames = childList.toArray(childrenNames);
     }
     return childrenNames;
   } catch (Exception e) {
     log.error("GridStore.getChildrenNames(" + uri + ") failed", e);
     throw new WebdavException(e);
   }
 }
Ejemplo n.º 13
0
  @Override
  public InputStream getResourceContent(ITransaction transaction, String uri)
      throws WebdavException {
    uri = normalizeURI(uri);
    log.tracef("GridStore.getResourceContent(%s)", uri);
    File file = fs.getFile(root, uri);

    InputStream in;
    try {
      // in=new BufferedInputStream(fs.getInput(file));
      in = fs.getInput(file);
    } catch (IOException e) {
      log.error("GridStore.getResourceContent(" + uri + ") failed");
      throw new WebdavException(e);
    }
    return in;
  }
Ejemplo n.º 14
0
 @Override
 public void removeObject(ITransaction transaction, String uri) throws WebdavException {
   uri = normalizeURI(uri);
   File file = fs.getFile(root, uri);
   boolean success = file.delete();
   log.tracef("GridStore.removeObject(%s)=%s", uri, success);
   if (!success) throw new WebdavException("cannot delete object: " + uri);
 }
Ejemplo n.º 15
0
 @Override
 public void createFolder(ITransaction transaction, String uri) throws WebdavException {
   uri = normalizeURI(uri);
   log.tracef("GridStore.createFolder(%s)", uri);
   File file = fs.getFile(root, uri);
   if (!file.mkdir()) {
     throw new WebdavException("cannot create folder: " + uri);
   }
 }
Ejemplo n.º 16
0
  public void testWriteAfterClose() throws Exception {
    String filePath = "test_write_to_closed.dat";
    OutputStream out = fs.getOutput(filePath);

    try {
      out.write(1);
    } finally {
      out.close();
    }
    IOException e = null;
    try {
      out.write(2);
    } catch (IOException ex) {
      e = ex;
    }
    assertNotNull(e);
    File f = fs.getFile(filePath);
    assertEquals(f.length(), 1);
  }
Ejemplo n.º 17
0
  public void testList() throws Exception {
    assertNull(fs.getFile("nonExistentDir").list());
    assertEquals(createDir("/emptyDir").list().length, 0);

    File dir = createDirWithFiles();
    String[] filenames = dir.list();
    assertEquals(
        asSet(filenames),
        asSet("foo1.txt", "foo2.txt", "bar1.txt", "bar2.txt", "fooDir", "barDir"));
  }
Ejemplo n.º 18
0
 public void testGetName() throws IOException {
   assertEquals(fs.getFile("").getName(), "");
   assertEquals(fs.getFile("/").getName(), "");
   assertEquals(fs.getFile("file.txt").getName(), "file.txt");
   assertEquals(fs.getFile("/file.txt").getName(), "file.txt");
   assertEquals(fs.getFile("/dir/file.txt").getName(), "file.txt");
   assertEquals(fs.getFile("/dir/subdir/file.txt").getName(), "file.txt");
   assertEquals(fs.getFile("dir/subdir/file.txt").getName(), "file.txt");
 }
Ejemplo n.º 19
0
  public void testWritableChannelAppend() throws Exception {
    writeToFile("/append.txt", "Initial text.", 3);

    WritableGridFileChannel channel = fs.getWritableChannel("/append.txt", true);
    try {
      channel.write(ByteBuffer.wrap("Appended text.".getBytes()));
    } finally {
      channel.close();
    }
    assertEquals(getContents("/append.txt"), "Initial text.Appended text.");
  }
Ejemplo n.º 20
0
 public void testWritableChannel() throws Exception {
   WritableGridFileChannel channel = fs.getWritableChannel("/channelTest.txt", false, 10);
   try {
     assertTrue(channel.isOpen());
     channel.write(ByteBuffer.wrap("This file spans multiple chunks.".getBytes()));
   } finally {
     channel.close();
   }
   assertFalse(channel.isOpen());
   assertEquals(getContents("/channelTest.txt"), "This file spans multiple chunks.");
 }
Ejemplo n.º 21
0
  public void testRootDir() throws Exception {
    File rootDir = fs.getFile("/");
    assertTrue(rootDir.exists());
    assertTrue(rootDir.isDirectory());

    createFile("/foo.txt");
    String[] filenames = rootDir.list();
    assertNotNull(filenames);
    assertEquals(filenames.length, 1);
    assertEquals(filenames[0], "foo.txt");
  }
Ejemplo n.º 22
0
 public void testCanReadClosed() throws Exception {
   String filePath = "file_read_closed.txt";
   OutputStream out = fs.getOutput(filePath);
   try {
     out.write(1);
     out.write(2);
     out.write(3);
   } finally {
     out.close();
   }
   InputStream in = fs.getInput(filePath);
   in.read();
   in.close();
   IOException e = null;
   try {
     in.read();
   } catch (IOException ex) {
     e = ex;
   }
   assertNotNull(e);
 }
Ejemplo n.º 23
0
  public void testDeleteRemovesAllChunks() throws Exception {
    assertEquals(numberOfChunksInCache(), 0);
    assertEquals(numberOfMetadataEntries(), 0);

    writeToFile("delete.txt", "delete me", 100);

    GridFile file = (GridFile) fs.getFile("delete.txt");
    boolean deleted = file.delete();
    assertTrue(deleted);
    assertFalse(file.exists());
    assertEquals(numberOfChunksInCache(), 0);
    assertEquals(numberOfMetadataEntries(), 0);
  }
Ejemplo n.º 24
0
  @Override
  public long setResourceContent(
      ITransaction transaction,
      String uri,
      InputStream is,
      String contentType,
      String characterEncoding)
      throws WebdavException {
    uri = normalizeURI(uri);
    log.tracef("GridStore.setResourceContent(%s)", uri);
    File file = fs.getFile(root, uri);
    try {
      OutputStream os = fs.getOutput((GridFile) file);
      // OutputStream os=new BufferedOutputStream(fs.getOutput((GridFile)file), BUF_SIZE);
      try {
        int read;
        byte[] copyBuffer = new byte[BUF_SIZE];

        while ((read = is.read(copyBuffer, 0, copyBuffer.length)) != -1) {
          os.write(copyBuffer, 0, read);
        }
      } finally {
        Util.close(is);
        Util.close(os);
      }
    } catch (IOException e) {
      log.error("GridStore.setResourceContent(" + uri + ") failed", e);
      throw new WebdavException(e);
    }
    long length = -1;

    try {
      length = file.length();
    } catch (SecurityException e) {
      log.error("GridStore.setResourceContent(" + uri + ") failed" + "\nCan't get file.length");
    }
    return length;
  }
Ejemplo n.º 25
0
 @Override
 public void createResource(ITransaction transaction, String uri) throws WebdavException {
   uri = normalizeURI(uri);
   log.tracef("GridStore.createResource(%s)", uri);
   File file = fs.getFile(root, uri);
   try {
     if (!file.createNewFile()) {
       throw new WebdavException("cannot create file: " + uri);
     }
   } catch (IOException e) {
     log.error("GridStore.createResource(" + uri + ") failed", e);
     throw new WebdavException(e);
   }
 }
Ejemplo n.º 26
0
  public void testWriteAcrossMultipleChunksWithNonDefaultChunkSizeAfterFileIsExplicitlyCreated()
      throws Exception {
    GridFile file = (GridFile) fs.getFile("multipleChunks.txt", 20); // chunkSize = 20
    file.createNewFile();

    writeToFile(
        "multipleChunks.txt",
        "This text spans multiple chunks, because each chunk is only 20 bytes long.",
        10); // chunkSize = 10 (but it is ignored, because the file was already created with
    // chunkSize = 20

    String text = getContents("multipleChunks.txt");
    assertEquals(
        text, "This text spans multiple chunks, because each chunk is only 20 bytes long.");
  }
Ejemplo n.º 27
0
  public void testReadableChannel() throws Exception {
    String content = "This is the content of channelTest.txt";
    writeToFile("/channelTest.txt", content, 10);

    ReadableGridFileChannel channel = fs.getReadableChannel("/channelTest.txt");
    try {
      assertTrue(channel.isOpen());
      ByteBuffer buffer = ByteBuffer.allocate(1000);
      channel.read(buffer);
      assertEquals(getStringFrom(buffer), content);
    } finally {
      channel.close();
    }

    assertFalse(channel.isOpen());
  }
Ejemplo n.º 28
0
  public void testListFiles() throws Exception {
    assertNull(fs.getFile("nonExistentDir").listFiles());
    assertEquals(createDir("/emptyDir").listFiles().length, 0);

    File dir = createDirWithFiles();
    File[] files = dir.listFiles();
    assertEquals(
        asSet(getPaths(files)),
        asSet(
            "/myDir/foo1.txt",
            "/myDir/foo2.txt",
            "/myDir/fooDir",
            "/myDir/bar1.txt",
            "/myDir/bar2.txt",
            "/myDir/barDir"));
  }
Ejemplo n.º 29
0
  @Override
  public StoredObject getStoredObject(ITransaction transaction, String uri) {
    uri = normalizeURI(uri);
    log.tracef("GridStore.getStoredObject(%s)", uri);
    StoredObject so = null;

    File file = fs.getFile(root, uri);
    if (file.exists()) {
      so = new StoredObject();
      so.setFolder(file.isDirectory());
      so.setLastModified(new Date(file.lastModified()));
      so.setCreationDate(new Date(file.lastModified()));
      so.setResourceLength(getResourceLength(transaction, uri));
    }

    return so;
  }
Ejemplo n.º 30
0
  public GridStore(File root) {
    data = CacheManagerHolder.cacheContainer.getCache(CacheManagerHolder.dataCacheName);
    metadata = CacheManagerHolder.cacheContainer.getCache(CacheManagerHolder.metadataCacheName);

    try {
      data.start();
      metadata.start();
    } catch (Exception e) {
      throw new RuntimeException("creation of cluster failed", e);
    }

    fs = new GridFilesystem(data, metadata);

    this.root = fs.getFile(root.getPath());
    if (!this.root.mkdirs())
      throw new WebdavException(
          "root path: " + root.getAbsolutePath() + " does not exist and could not be created");
  }