Exemplo n.º 1
0
 @Override
 public void configure(Map<String, String> parameters)
     throws IllegalArgumentException, IllegalStateException {
   if (this.parameters != null) {
     throw new IllegalStateException(
         "cannot change configuration, may be already in use somewhere");
   }
   directoryName = parameters.get(PARAM_DIRECTORY);
   if (directoryName != null) {
     directoryName = directoryName.trim();
   }
   if (directoryName == null || directoryName.isEmpty()) {
     throw new IllegalArgumentException(
         "missing directory parameter. A directory name is necessary");
   }
   fetchDirectory();
   idField = directory.getIdField();
   schema = directory.getSchema();
   if (schema.endsWith("xvocabulary")) {
     hierarchical = true;
     parentField = "parent";
     separator = "/";
   }
   String parentFieldParam = StringUtils.trim(parameters.get(PARAM_PARENT_FIELD));
   String separatorParam = StringUtils.trim(parameters.get(PARAM_SEPARATOR));
   if (!StringUtils.isBlank(parentFieldParam) && !StringUtils.isBlank(separatorParam)) {
     hierarchical = true;
     parentField = parentFieldParam;
     separator = separatorParam;
   }
   this.parameters = new HashMap<String, Serializable>();
   this.parameters.put(PARAM_DIRECTORY, directory.getName());
 }
  @Ignore // git based vfs does not yet support move
  @Test
  public void testMoveEmptyDirectory() throws NoSuchFileException {
    Repository repository = new VFSRepository(producer.getIoService());
    ((VFSRepository) repository).setDescriptor(descriptor);
    Directory sourceDir = repository.createDirectory("/source");

    boolean directoryExists =
        repository.directoryExists(sourceDir.getLocation() + sourceDir.getName());
    assertTrue(directoryExists);
    Collection<Asset> foundAsset = repository.listAssets("/source", new FilterByExtension("bpmn2"));

    assertNotNull(foundAsset);
    assertEquals(0, foundAsset.size());

    boolean copied = repository.moveDirectory("/source", "/", "target");
    assertTrue(copied);

    boolean movedDirectoryExists = repository.directoryExists("/source");
    assertFalse(movedDirectoryExists);
    movedDirectoryExists = repository.directoryExists("/target");
    assertTrue(movedDirectoryExists);

    foundAsset = repository.listAssets("/target", new FilterByExtension("bpmn2"));

    assertNotNull(foundAsset);
    assertEquals(0, foundAsset.size());
  }
Exemplo n.º 3
0
 /**
  * This helper method determines how a list of elements should be presented to the user, based on
  * options given in the instruction.
  *
  * @param elements the file system elements to be listed
  * @param instruction the instruction instance created based on the user input
  */
 private void showResult(ArrayList<FileSystemElement> elements, Instruction instruction) {
   ArrayList<String> options = instruction.getOptions();
   if (!elements.isEmpty()) {
     if (options.contains("l")) {
       int totalSize = 0;
       for (FileSystemElement element : elements) {
         totalSize += element.getSize();
       }
       System.out.printf("total %d%n", totalSize);
       for (FileSystemElement element : elements) {
         System.out.printf("%s%n", element);
       }
     } else {
       for (FileSystemElement element : elements) {
         System.out.printf("%s%n", element.getName());
       }
     }
     if (options.contains("R")) {
       for (FileSystemElement element : elements) {
         if (element instanceof Directory) {
           Directory directory = (Directory) element;
           showResult(directory.getChildren(), instruction);
         }
       }
     }
   }
 }
  @Test
  public void testDirectoryExists() {
    Repository repository = new VFSRepository(producer.getIoService());
    ((VFSRepository) repository).setDescriptor(descriptor);
    boolean rootFolderExists = repository.directoryExists("/test");
    assertFalse(rootFolderExists);

    Directory directoryId = repository.createDirectory("/test");
    assertNotNull(directoryId);
    assertEquals("test", directoryId.getName());
    assertEquals("/", directoryId.getLocation());
    assertNotNull(directoryId.getUniqueId());

    rootFolderExists = repository.directoryExists("/test");
    assertTrue(rootFolderExists);

    AssetBuilder builder = AssetBuilderFactory.getAssetBuilder(Asset.AssetType.Byte);
    builder.content("simple content".getBytes()).type("png").name("test").location("/test");

    String id = repository.createAsset(builder.getAsset());

    assertNotNull(id);

    boolean assetPathShouldNotExists = repository.directoryExists("/test/test.png");
    assertFalse(assetPathShouldNotExists);
  }
Exemplo n.º 5
0
 /**
  * @param sName
  * @param dParentDirectory
  * @return ItemManager ID
  */
 protected static String createID(String sName, Directory dParentDirectory) {
   return MD5Processor.hash(
       new StringBuilder(dParentDirectory.getDevice().getName())
           .append(dParentDirectory.getRelativePath())
           .append(sName)
           .toString());
 }
Exemplo n.º 6
0
  public void testRAMDirectory() throws IOException {

    Directory dir = newFSDirectory(indexDir);
    MockDirectoryWrapper ramDir = new MockDirectoryWrapper(random, new RAMDirectory(dir));

    // close the underlaying directory
    dir.close();

    // Check size
    assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes());

    // open reader to test document count
    IndexReader reader = IndexReader.open(ramDir, true);
    assertEquals(docsToAdd, reader.numDocs());

    // open search zo check if all doc's are there
    IndexSearcher searcher = newSearcher(reader);

    // search for all documents
    for (int i = 0; i < docsToAdd; i++) {
      Document doc = searcher.doc(i);
      assertTrue(doc.getField("content") != null);
    }

    // cleanup
    reader.close();
    searcher.close();
  }
Exemplo n.º 7
0
 public IndexOutput createOutput(String name, IOContext context, boolean raw)
     throws IOException {
   Directory directory;
   if (isChecksum(name)) {
     directory = distributor.primary();
   } else {
     directory = distributor.any();
   }
   IndexOutput out = directory.createOutput(name, context);
   synchronized (mutex) {
     StoreFileMetaData metaData = new StoreFileMetaData(name, -1, null, directory);
     filesMetadata = MapBuilder.newMapBuilder(filesMetadata).put(name, metaData).immutableMap();
     files = filesMetadata.keySet().toArray(new String[filesMetadata.size()]);
     boolean computeChecksum = !raw;
     if (computeChecksum) {
       // don't compute checksum for segment based files
       if ("segments.gen".equals(name) || name.startsWith("segments")) {
         computeChecksum = false;
       }
     }
     if (computeChecksum) {
       out = new BufferedChecksumIndexOutput(out, new Adler32());
     }
     return new StoreIndexOutput(metaData, out, name);
   }
 }
Exemplo n.º 8
0
  public void updateEntry(String name, EntryConfig entryConfig) throws Exception {

    log.debug(TextUtil.repeat("-", 70));

    Directory directory = getDirectory();
    List<Entry> children = null;

    if (directory != null) {
      try {
        Entry oldEntry = directory.removeEntry(entryConfig.getName());
        if (oldEntry != null) {
          children = oldEntry.getChildren();
          oldEntry.destroy();
        }
      } catch (Exception e) {
        log.error(e.getMessage(), e);
      }
    }

    removeEntryService(entryConfig.getName());

    DirectoryConfig directoryConfig = getDirectoryConfig();
    directoryConfig.updateEntryConfig(name, entryConfig);

    if (directory != null) {
      try {
        Entry newEntry = directory.createEntry(entryConfig);
        if (children != null) newEntry.addChildren(children);
      } catch (Exception e) {
        log.error(e.getMessage(), e);
      }
    }

    createEntryService(entryConfig.getName());
  }
Exemplo n.º 9
0
  private static void print(Metadata metadata) {
    System.out.println("-------------------------------------");

    // Iterate over the data and print to System.out

    //
    // A Metadata object contains multiple Directory objects
    //
    for (Directory directory : metadata.getDirectories()) {

      //
      // Each Directory stores values in Tag objects
      //
      for (Tag tag : directory.getTags()) {
        System.out.println(tag);
      }

      //
      // Each Directory may also contain error messages
      //
      if (directory.hasErrors()) {
        for (String error : directory.getErrors()) {
          System.err.println("ERROR: " + error);
        }
      }
    }
  }
Exemplo n.º 10
0
    public void valueChanged(ListSelectionEvent e) {
      ListSelectionModel lsm = (ListSelectionModel) e.getSource();

      int index = list.getSelectedIndex();
      Directory dir = directoryList.getDirectory(index);

      waitField.setValue(dir.getWaitInterval());
      checkField.setValue(dir.getInterval());
      if (dir.copy) {
        copyButton.setSelected(true);
      } else {
        moveButton.setSelected(true);
      }
      if (dir.destination != null) {

        destination.setText("Destination Directory: " + dir.destination.toString());
        startButton.setEnabled(true);
        if (dir.backupNumber == 0) {
          startButton.setEnabled(true);
          stopButton.setEnabled(false);
        } else {
          startButton.setEnabled(false);
          stopButton.setEnabled(true);
        }
      } else {
        destination.setText("Destination Directory: ");
        startButton.setEnabled(false);
      }
    }
Exemplo n.º 11
0
  // setup the index
  @Override
  public void setUp() throws Exception {
    super.setUp();
    indexDir = _TestUtil.getTempDir("RAMDirIndex");

    Directory dir = newFSDirectory(indexDir);
    IndexWriter writer =
        new IndexWriter(
            dir,
            new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))
                .setOpenMode(OpenMode.CREATE));
    // add some documents
    Document doc = null;
    for (int i = 0; i < docsToAdd; i++) {
      doc = new Document();
      doc.add(
          newField(
              "content",
              English.intToEnglish(i).trim(),
              Field.Store.YES,
              Field.Index.NOT_ANALYZED));
      writer.addDocument(doc);
    }
    assertEquals(docsToAdd, writer.maxDoc());
    writer.close();
    dir.close();
  }
Exemplo n.º 12
0
  // Verify: do stress test, by opening IndexReaders and
  // IndexWriters over & over in 2 threads and making sure
  // no unexpected exceptions are raised:
  public void testStressLocks() throws Exception {
    Path tempPath = createTempDir();
    assumeFalse("cannot handle buggy Files.delete", TestUtil.hasWindowsFS(tempPath));

    Directory dir = getDirectory(tempPath);

    // First create a 1 doc index:
    IndexWriter w =
        new IndexWriter(
            dir, new IndexWriterConfig(new MockAnalyzer(random())).setOpenMode(OpenMode.CREATE));
    addDoc(w);
    w.close();

    WriterThread writer = new WriterThread(100, dir);
    SearcherThread searcher = new SearcherThread(100, dir);
    writer.start();
    searcher.start();

    while (writer.isAlive() || searcher.isAlive()) {
      Thread.sleep(1000);
    }

    assertTrue("IndexWriter hit unexpected exceptions", !writer.hitException);
    assertTrue("IndexSearcher hit unexpected exceptions", !searcher.hitException);

    dir.close();
  }
Exemplo n.º 13
0
 static Map<String, String> readChecksums(Directory[] dirs, Map<String, String> defaultValue)
     throws IOException {
   long lastFound = -1;
   Directory lastDir = null;
   for (Directory dir : dirs) {
     for (String name : dir.listAll()) {
       if (!isChecksum(name)) {
         continue;
       }
       long current = Long.parseLong(name.substring(CHECKSUMS_PREFIX.length()));
       if (current > lastFound) {
         lastFound = current;
         lastDir = dir;
       }
     }
   }
   if (lastFound == -1) {
     return defaultValue;
   }
   IndexInput indexInput = lastDir.openInput(CHECKSUMS_PREFIX + lastFound, IOContext.READONCE);
   try {
     indexInput.readInt(); // version
     return indexInput.readStringStringMap();
   } catch (Exception e) {
     // failed to load checksums, ignore and return an empty map
     return defaultValue;
   } finally {
     indexInput.close();
   }
 }
Exemplo n.º 14
0
  public void testUpdateSameDoc() throws Exception {
    final Directory dir = newDirectory();

    final LineFileDocs docs = new LineFileDocs(random());
    for (int r = 0; r < 3; r++) {
      final IndexWriter w =
          new IndexWriter(
              dir, newIndexWriterConfig(new MockAnalyzer(random())).setMaxBufferedDocs(2));
      final int numUpdates = atLeast(20);
      int numThreads = TestUtil.nextInt(random(), 2, 6);
      IndexingThread[] threads = new IndexingThread[numThreads];
      for (int i = 0; i < numThreads; i++) {
        threads[i] = new IndexingThread(docs, w, numUpdates);
        threads[i].start();
      }

      for (int i = 0; i < numThreads; i++) {
        threads[i].join();
      }

      w.close();
    }

    IndexReader open = DirectoryReader.open(dir);
    assertEquals(1, open.numDocs());
    open.close();
    docs.close();
    dir.close();
  }
Exemplo n.º 15
0
  // TODO: Make ln fit the 30 line
  public void ln(String firstPath, String secondPath) throws Exception {
    // Initialize the variables.
    JShellItem targetItem;
    JShellItem aliasItem;
    String newName;
    Directory destinationDirectory;

    // Find and reference the target item. Throw Exception if the path
    // doesn't exist.
    targetItem = getItemAtPath(firstPath, 0);
    if (targetItem == null) {
      throw new Exception(firstPath + ": doesn't exist.");
    }

    // Find and reference the destination Directory.
    if (secondPath.endsWith("/")) {
      destinationDirectory = (Directory) getItemAtPath(secondPath, 0);
      newName = targetItem.getName();
    } else {
      destinationDirectory = (Directory) getItemAtPath(secondPath, 1);
      newName = secondPath.substring(secondPath.lastIndexOf("/") + 1);
    }

    // If the Directory destination is not found.
    if (destinationDirectory == null) {
      throw new Exception(secondPath + " is an invalid destination path.");
    }

    // Throw Exception when there is a JShellItem that already exist within
    // the destination Directory.
    if (destinationDirectory.contains(newName)) {
      throw new Exception(secondPath + ": already exists.");
    }

    // If the target item is a Directory object.
    if (targetItem instanceof Directory)
      // Create a new Directory object that will references it's contents
      // from the target item.
      aliasItem =
          new DirectoryAlias(
              newName,
              destinationDirectory.getPath() + newName + "/",
              destinationDirectory,
              (Directory) targetItem);

    // If the target item is a File object.
    else
      // Create a new FileAlias object that will reference it's contents
      // from the target item.
      aliasItem =
          new FileAlias(
              newName,
              destinationDirectory.getPath() + newName,
              destinationDirectory,
              (File) targetItem);

    // Add the alias object to the destination Directory.
    destinationDirectory.addItem(aliasItem);
  }
Exemplo n.º 16
0
 /** Test ensureValid returns true after acquire */
 public void testValidAfterAcquire() throws IOException {
   Path tempPath = createTempDir();
   Directory dir = getDirectory(tempPath);
   Lock l = dir.obtainLock("commit");
   l.ensureValid(); // no exception
   l.close();
   dir.close();
 }
 public String getDirectoryFilesName(String path, User currentUser, Directory currentDir)
     throws FileUnknownException, AccessDeniedException {
   if (path.equals(".")) return currentDir.getDirectoryFilesName();
   else if (path.equals("..")) return currentDir.getFather().getDirectoryFilesName();
   File target = absolutePath(path, currentUser, currentDir).getFileByName(getLastPathToken(path));
   target.checkAccessRead(currentUser);
   return target.getDirectoryFilesName();
 }
 public String[] listAll() throws IOException {
   String[] primaryFiles = primaryDir.listAll();
   String[] secondaryFiles = secondaryDir.listAll();
   String[] files = new String[primaryFiles.length + secondaryFiles.length];
   System.arraycopy(primaryFiles, 0, files, 0, primaryFiles.length);
   System.arraycopy(secondaryFiles, 0, files, primaryFiles.length, secondaryFiles.length);
   return files;
 }
Exemplo n.º 19
0
  /** @param args */
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    PPrint.pprint(Directory.walk("src/thinking").dirs);

    for (File file : Directory.local("src/thinking/java/space18", "D.*")) {
      System.out.println(file);
    }
  }
  String readFile(String path, Directory currentDirectory, User currentUser)
      throws FileUnknownException, IsNotPlainFileException, AccessDeniedException {

    Directory directory = absolutePath(path, currentUser, currentDirectory);
    String filename = getLastPathToken(path);
    File f = directory.getFileByName(filename);
    f.checkAccessRead(currentUser);
    return f.printContent(currentUser);
  }
 /**
  * return ordered child files recursively.
  *
  * @return child files recursively
  */
 public List<org.jajuk.base.File> getFilesRecursively() {
   // looks for the root directory for this device
   Directory dirRoot = getRootDirectory();
   if (dirRoot != null) {
     return dirRoot.getFilesRecursively();
   }
   // nothing found, return empty list
   return new ArrayList<org.jajuk.base.File>();
 }
Exemplo n.º 22
0
  @Override
  public boolean loadExisting() {
    baseGraph.checkInit();
    if (properties.loadExisting()) {
      properties.checkVersions(false);
      // check encoding for compatiblity
      String acceptStr = properties.get("graph.flagEncoders");

      if (encodingManager == null) {
        if (acceptStr.isEmpty())
          throw new IllegalStateException(
              "No EncodingManager was configured. And no one was found in the graph: "
                  + dir.getLocation());

        int bytesForFlags = 4;
        if ("8".equals(properties.get("graph.bytesForFlags"))) bytesForFlags = 8;
        encodingManager = new EncodingManager(acceptStr, bytesForFlags);
      } else if (!acceptStr.isEmpty()
          && !encodingManager.toDetailsString().equalsIgnoreCase(acceptStr)) {
        throw new IllegalStateException(
            "Encoding does not match:\nGraphhopper config: "
                + encodingManager.toDetailsString()
                + "\nGraph: "
                + acceptStr
                + ", dir:"
                + dir.getLocation());
      }

      String byteOrder = properties.get("graph.byteOrder");
      if (!byteOrder.equalsIgnoreCase("" + dir.getByteOrder()))
        throw new IllegalStateException(
            "Configured graph.byteOrder ("
                + dir.getByteOrder()
                + ") is not equal to loaded "
                + byteOrder
                + "");

      String bytesForFlags = properties.get("graph.bytesForFlags");
      if (!bytesForFlags.equalsIgnoreCase("" + encodingManager.getBytesForFlags()))
        throw new IllegalStateException(
            "Configured graph.bytesForFlags ("
                + encodingManager.getBytesForFlags()
                + ") is not equal to loaded "
                + bytesForFlags);

      String dim = properties.get("graph.dimension");
      baseGraph.loadExisting(dim);

      for (CHGraphImpl cg : chGraphs) {
        if (!cg.loadExisting()) throw new IllegalStateException("Cannot load " + cg);
      }

      return true;
    }
    return false;
  }
Exemplo n.º 23
0
  @Test
  public void testEnsureSize() {
    Directory dir = new RAMDirectory();
    graph = newGHStorage(dir, false).create(defaultSize);
    int testIndex = dir.find("edges").getSegmentSize() * 3;
    graph.edge(0, testIndex, 10, true);

    // test if optimize works without error
    graph.optimize();
  }
Exemplo n.º 24
0
 @Override
 public void close() throws IOException {
   for (Directory delegate : distributor.all()) {
     delegate.close();
   }
   synchronized (mutex) {
     filesMetadata = ImmutableMap.of();
     files = Strings.EMPTY_ARRAY;
   }
 }
 public void close() throws IOException {
   if (doClose) {
     try {
       secondaryDir.close();
     } finally {
       primaryDir.close();
     }
     doClose = false;
   }
 }
Exemplo n.º 26
0
  public static void main(String[] args) {

    Directory directory = new Directory(new TextFile(), new AudioFile(), new ImageFile());

    directory.setFilesInDir(new AudioFile(), new AudioFile(), new TextFile());

    for (File f : directory.getFilesList()) {
      System.out.println(f.getCurrentString());
    }
  }
Exemplo n.º 27
0
  /** Test closing locks twice */
  public void testDoubleClose() throws IOException {
    Path tempPath = createTempDir();
    Directory dir = getDirectory(tempPath);

    Lock l = dir.obtainLock("commit");
    l.close();
    l.close(); // close again, should be no exception

    dir.close();
  }
Exemplo n.º 28
0
    public void actionPerformed(ActionEvent e) {
      int index = list.getSelectedIndex();
      stopButton.setEnabled(false);
      timers.get(index).cancel();
      timers.remove(index);
      startButton.setEnabled(true);

      Directory task = directoryList.getDirectory(index);
      task.backupNumber = 0;
      log.setCaretPosition(log.getDocument().getLength());
    }
  private void doTest(Random random, PrintWriter out, boolean useCompoundFiles, int MAX_DOCS)
      throws Exception {
    Directory directory = newDirectory();
    Analyzer analyzer = new MockAnalyzer(random);
    IndexWriterConfig conf = newIndexWriterConfig(analyzer);
    final MergePolicy mp = conf.getMergePolicy();
    mp.setNoCFSRatio(useCompoundFiles ? 1.0 : 0.0);
    IndexWriter writer = new IndexWriter(directory, conf);
    if (VERBOSE) {
      System.out.println("TEST: now build index MAX_DOCS=" + MAX_DOCS);
    }

    for (int j = 0; j < MAX_DOCS; j++) {
      Document d = new Document();
      d.add(newTextField(PRIORITY_FIELD, HIGH_PRIORITY, Field.Store.YES));
      d.add(newTextField(ID_FIELD, Integer.toString(j), Field.Store.YES));
      writer.addDocument(d);
    }
    writer.close();

    // try a search without OR
    IndexReader reader = DirectoryReader.open(directory);
    IndexSearcher searcher = newSearcher(reader);

    Query query = new TermQuery(new Term(PRIORITY_FIELD, HIGH_PRIORITY));
    out.println("Query: " + query.toString(PRIORITY_FIELD));
    if (VERBOSE) {
      System.out.println("TEST: search query=" + query);
    }

    final Sort sort = new Sort(SortField.FIELD_SCORE, new SortField(ID_FIELD, SortField.Type.INT));

    ScoreDoc[] hits = searcher.search(query, null, MAX_DOCS, sort).scoreDocs;
    printHits(out, hits, searcher);
    checkHits(hits, MAX_DOCS, searcher);

    // try a new search with OR
    searcher = newSearcher(reader);
    hits = null;

    BooleanQuery booleanQuery = new BooleanQuery();
    booleanQuery.add(
        new TermQuery(new Term(PRIORITY_FIELD, HIGH_PRIORITY)), BooleanClause.Occur.SHOULD);
    booleanQuery.add(
        new TermQuery(new Term(PRIORITY_FIELD, MED_PRIORITY)), BooleanClause.Occur.SHOULD);
    out.println("Query: " + booleanQuery.toString(PRIORITY_FIELD));

    hits = searcher.search(booleanQuery, null, MAX_DOCS, sort).scoreDocs;
    printHits(out, hits, searcher);
    checkHits(hits, MAX_DOCS, searcher);

    reader.close();
    directory.close();
  }
Exemplo n.º 30
0
 public void assertDeleteContent(Store store, DirectoryService service) throws IOException {
   store.deleteContent();
   assertThat(
       Arrays.toString(store.directory().listAll()),
       store.directory().listAll().length,
       equalTo(0));
   assertThat(store.stats().sizeInBytes(), equalTo(0l));
   for (Directory dir : service.build()) {
     assertThat(dir.listAll().length, equalTo(0));
   }
 }