Exemplo n.º 1
0
  private void scanFile(File source, Indexer indexer) throws FileNotFoundException, IOException {
    if (source.isDirectory()) {
      File[] children = source.listFiles();
      if (children == null)
        throw new FileNotFoundException("Source directory disappeared: " + source);

      for (File child : children) scanFile(child, indexer);

      return;
    }

    if (!source.getName().endsWith(".class")) return;

    FileInputStream input = new FileInputStream(source);

    try {
      ClassInfo info = indexer.index(input);
      if (verbose && info != null) printIndexEntryInfo(info);
    } catch (Exception e) {
      String message = e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage();
      System.err.println("ERROR: Could not index " + source.getName() + ": " + message);
      if (verbose) e.printStackTrace(System.err);
    } finally {
      safeClose(input);
    }
  }
Exemplo n.º 2
0
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   Bundle extras = this.getIntent().getExtras();
   Indexer indexer = new Indexer(getApplicationContext().getFilesDir());
   String url = extras.getString("url");
   Source source = null;
   try {
     source = new Source(new URL(url));
     indexer.index(source.toString());
   } catch (MalformedURLException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   }
   mWebView = new WebView(this);
   mWebView.getSettings().setJavaScriptEnabled(true);
   mWebView.loadUrl(url);
   setContentView(mWebView);
 }
  public boolean checkPDF(File pdfFile, String text) {
    int result = 0;
    try {
      IndexItem pdfIndexItem = index(pdfFile);
      // creating an instance of the indexer class and indexing the items
      Indexer indexer = new Indexer(INDEX_DIR);
      indexer.index(pdfIndexItem);
      indexer.close();

      // creating an instance of the Searcher class to the query the index
      Searcher searcher = new Searcher(INDEX_DIR);
      result = searcher.findByContent(text, DEFAULT_RESULT_SIZE);
      searcher.close();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return containsWord(result);
  }
  public void insert(
      String keySpace,
      String columnFamily,
      String key,
      Map<String, Object> values,
      boolean probablyNew)
      throws StorageClientException {
    checkClosed();

    Map<String, PreparedStatement> statementCache = Maps.newHashMap();
    boolean autoCommit = true;
    try {
      autoCommit = startBlock();
      String rid = rowHash(keySpace, columnFamily, key);
      for (Entry<String, Object> e : values.entrySet()) {
        String k = e.getKey();
        Object o = e.getValue();
        if (o instanceof byte[]) {
          throw new RuntimeException(
              "Invalid content in " + k + ", storing byte[] rather than streaming it");
        }
      }

      Map<String, Object> m = get(keySpace, columnFamily, key);
      if (storageClientListener != null) {
        storageClientListener.before(keySpace, columnFamily, key, m);
      }
      if (TRUE.equals(m.get(DELETED_FIELD))) {
        // if the map was previously deleted, delete all content since we don't want the old map
        // becoming part of the new map.
        m.clear();
      }
      for (Entry<String, Object> e : values.entrySet()) {
        String k = e.getKey();
        Object o = e.getValue();

        if (o instanceof RemoveProperty || o == null) {
          m.remove(k);
        } else {
          m.put(k, o);
        }
      }
      if (storageClientListener != null) {
        storageClientListener.after(keySpace, columnFamily, key, m);
      }
      LOGGER.debug("Saving {} {} {} ", new Object[] {key, rid, m});
      if (probablyNew && !UPDATE_FIRST_SEQUENCE.equals(getSql(SQL_STATEMENT_SEQUENCE))) {
        PreparedStatement insertBlockRow =
            getStatement(keySpace, columnFamily, SQL_BLOCK_INSERT_ROW, rid, statementCache);
        insertBlockRow.clearWarnings();
        insertBlockRow.clearParameters();
        insertBlockRow.setString(1, rid);
        InputStream insertStream = null;
        try {
          insertStream = Types.storeMapToStream(rid, m, columnFamily);
        } catch (UTFDataFormatException e) {
          throw new DataFormatException(INVALID_DATA_ERROR, e);
        }
        if ("1.5".equals(getSql(JDBC_SUPPORT_LEVEL))) {
          insertBlockRow.setBinaryStream(2, insertStream, insertStream.available());
        } else {
          insertBlockRow.setBinaryStream(2, insertStream);
        }
        int rowsInserted = 0;
        try {
          long t1 = System.currentTimeMillis();
          rowsInserted = insertBlockRow.executeUpdate();
          checkSlow(t1, getSql(keySpace, columnFamily, SQL_BLOCK_INSERT_ROW));
        } catch (SQLException e) {
          LOGGER.debug(e.getMessage(), e);
        }
        if (rowsInserted == 0) {
          PreparedStatement updateBlockRow =
              getStatement(keySpace, columnFamily, SQL_BLOCK_UPDATE_ROW, rid, statementCache);
          updateBlockRow.clearWarnings();
          updateBlockRow.clearParameters();
          updateBlockRow.setString(2, rid);
          try {
            insertStream = Types.storeMapToStream(rid, m, columnFamily);
          } catch (UTFDataFormatException e) {
            throw new DataFormatException(INVALID_DATA_ERROR, e);
          }
          if ("1.5".equals(getSql(JDBC_SUPPORT_LEVEL))) {
            updateBlockRow.setBinaryStream(1, insertStream, insertStream.available());
          } else {
            updateBlockRow.setBinaryStream(1, insertStream);
          }
          long t = System.currentTimeMillis();
          int u = updateBlockRow.executeUpdate();
          checkSlow(t, getSql(keySpace, columnFamily, SQL_BLOCK_UPDATE_ROW));
          if (u == 0) {
            throw new StorageClientException("Failed to save " + rid);
          } else {
            LOGGER.debug("Updated {} ", rid);
          }
        } else {
          LOGGER.debug("Inserted {} ", rid);
        }
      } else {
        PreparedStatement updateBlockRow =
            getStatement(keySpace, columnFamily, SQL_BLOCK_UPDATE_ROW, rid, statementCache);
        updateBlockRow.clearWarnings();
        updateBlockRow.clearParameters();
        updateBlockRow.setString(2, rid);
        InputStream updateStream = null;
        try {
          updateStream = Types.storeMapToStream(rid, m, columnFamily);
        } catch (UTFDataFormatException e) {
          throw new DataFormatException(INVALID_DATA_ERROR, e);
        }
        if ("1.5".equals(getSql(JDBC_SUPPORT_LEVEL))) {
          updateBlockRow.setBinaryStream(1, updateStream, updateStream.available());
        } else {
          updateBlockRow.setBinaryStream(1, updateStream);
        }
        long t = System.currentTimeMillis();
        int u = updateBlockRow.executeUpdate();
        checkSlow(t, getSql(keySpace, columnFamily, SQL_BLOCK_UPDATE_ROW));
        if (u == 0) {
          PreparedStatement insertBlockRow =
              getStatement(keySpace, columnFamily, SQL_BLOCK_INSERT_ROW, rid, statementCache);
          insertBlockRow.clearWarnings();
          insertBlockRow.clearParameters();
          insertBlockRow.setString(1, rid);
          try {
            updateStream = Types.storeMapToStream(rid, m, columnFamily);
          } catch (UTFDataFormatException e) {
            throw new DataFormatException(INVALID_DATA_ERROR, e);
          }
          if ("1.5".equals(getSql(JDBC_SUPPORT_LEVEL))) {
            insertBlockRow.setBinaryStream(2, updateStream, updateStream.available());
          } else {
            insertBlockRow.setBinaryStream(2, updateStream);
          }
          t = System.currentTimeMillis();
          u = insertBlockRow.executeUpdate();
          checkSlow(t, getSql(keySpace, columnFamily, SQL_BLOCK_INSERT_ROW));

          if (u == 0) {
            throw new StorageClientException("Failed to save " + rid);
          } else {
            LOGGER.debug("Inserted {} ", rid);
          }
        } else {
          LOGGER.debug("Updated {} ", rid);
        }
      }

      // Indexing ---------------------------------------------------------------------------
      indexer.index(statementCache, keySpace, columnFamily, key, rid, values);

      endBlock(autoCommit);
    } catch (SQLException e) {
      abandonBlock(autoCommit);
      LOGGER.warn(
          "Failed to perform insert/update operation on {}:{}:{} ",
          new Object[] {keySpace, columnFamily, key},
          e);
      throw new StorageClientException(e.getMessage(), e);
    } catch (IOException e) {
      abandonBlock(autoCommit);
      LOGGER.warn(
          "Failed to perform insert/update operation on {}:{}:{} ",
          new Object[] {keySpace, columnFamily, key},
          e);
      throw new StorageClientException(e.getMessage(), e);
    } finally {
      closeStatementCache(statementCache);
    }
  }