/** Sends a record to the metrics system. */
 public void emitRecord(String recordName, OutputRecord outRec) throws IOException {
   // emit each metric in turn
   for (String metricName : outRec.getMetricNames()) {
     Object metric = outRec.getMetric(metricName);
     String type = (String) typeTable_.get(metric.getClass());
     emitMetric(metricName, type, metric.toString());
   }
 }
Example #2
0
  /*
   * Sends a change cipher spec message and updates the write side
   * cipher state so that future messages use the just-negotiated spec.
   */
  void sendChangeCipherSpec(Finished mesg, boolean lastMessage) throws IOException {

    output.flush(); // i.e. handshake data

    /*
     * The write cipher state is protected by the connection write lock
     * so we must grab it while making the change. We also
     * make sure no writes occur between sending the ChangeCipherSpec
     * message, installing the new cipher state, and sending the
     * Finished message.
     *
     * We already hold SSLEngine/SSLSocket "this" by virtue
     * of this being called from the readRecord code.
     */
    OutputRecord r;
    if (conn != null) {
      r = new OutputRecord(Record.ct_change_cipher_spec);
    } else {
      r = new EngineOutputRecord(Record.ct_change_cipher_spec, engine);
    }

    r.setVersion(protocolVersion);
    r.write(1); // single byte of data

    if (conn != null) {
      conn.writeLock.lock();
      try {
        conn.writeRecord(r);
        conn.changeWriteCiphers();
        if (debug != null && Debug.isOn("handshake")) {
          mesg.print(System.out);
        }
        mesg.write(output);
        output.flush();
      } finally {
        conn.writeLock.unlock();
      }
    } else {
      synchronized (engine.writeLock) {
        engine.writeRecord((EngineOutputRecord) r);
        engine.changeWriteCiphers();
        if (debug != null && Debug.isOn("handshake")) {
          mesg.print(System.out);
        }
        mesg.write(output);

        if (lastMessage) {
          output.setFinishedMsg();
        }
        output.flush();
      }
    }
  }
  public void testSearch(String[] record, int topDocLimit, int resultLimit) {
    SynonymRecordSearcher recSearcher = new SynonymRecordSearcher(record.length);
    for (int i = 0; i < record.length; i++) {
      initIdx(
          PluginUtil.getPluginInstallPath(HandLuceneImplTest.PLUGIN_ID).concat("data/idx")
              + (i + 1));
      SynonymIndexSearcher searcher =
          new SynonymIndexSearcher(
              PluginUtil.getPluginInstallPath(HandLuceneImplTest.PLUGIN_ID).concat("data/idx")
                  + (i + 1));
      searcher.setTopDocLimit(topDocLimit);
      recSearcher.addSearcher(searcher, i);
    }

    try {
      TopDocs topDocs;
      int hits = 1;
      for (int i = 0; i < record.length; i++) {
        topDocs = recSearcher.getSearcher(i).searchDocumentBySynonym(record[i]);
        hits *= topDocs.totalHits;
      }

      List<OutputRecord> results = recSearcher.search(resultLimit, record);
      Assert.assertNotNull(results);
      Assert.assertFalse(results.isEmpty());
      for (OutputRecord outputRecord : results) {
        Assert.assertNotNull(outputRecord);
        String[] resultingRecord = outputRecord.getRecord();
        Assert.assertEquals(record.length, resultingRecord.length);
        System.out.println(StringUtils.join(resultingRecord, '|'));
        System.out.println("\t" + outputRecord.getScore());
      }
      Assert.assertEquals(Math.min(hits, resultLimit), results.size());

      for (int i = 0; i < record.length; i++) {
        recSearcher.getSearcher(i).close();
      }
    } catch (ParseException e) {
      e.printStackTrace();
      fail("should not get an exception here");
    } catch (IOException e) {
      e.printStackTrace();
      fail("should not get an exception here");
    }
    System.out.println("");
  }
  private int testRecordResultCompute(String[][] wordresults) {
    int nbDuplicateFound = 0;

    // prepare wordresults
    List<List<WordResult>> wordResults = new ArrayList<List<WordResult>>();
    for (String[] elts : wordresults) {
      List<WordResult> wrs = new ArrayList<WordResult>();
      for (String elt : elts) {
        WordResult wr = new WordResult();
        wr.input = "input " + elt;
        wr.word = "word " + elt;
        wr.score = Integer.valueOf(elt);
        wrs.add(wr);
      }
      wordResults.add(wrs);
    }

    // --- compute output results as a list
    RecordResult recRes = new RecordResult();
    recRes.record = initializeRecordToSearch(wordresults);
    recRes.wordResults.addAll(wordResults);

    List<OutputRecord> expectedOutputRows = null;
    expectedOutputRows = new ArrayList<OutputRecord>();
    SynonymRecordSearcher.RecordResult.computeOutputRows(
        wordresults.length, new ArrayList<WordResult>(), recRes.wordResults, expectedOutputRows);
    for (OutputRecord outputRecord : expectedOutputRows) {
      System.out.println(outputRecord);
    }

    // --- test that duplicates are removed when using a set instead of a list
    Set<OutputRecord> uniques = new HashSet<OutputRecord>();
    uniques.addAll(expectedOutputRows);
    Assert.assertTrue(uniques.size() <= expectedOutputRows.size());
    if (uniques.size() < expectedOutputRows.size()) {
      nbDuplicateFound++;
    }

    List<OutputRecord> outputRows = recRes.computeOutputRows();

    // --- check some assertions

    // verify number of results
    int expectedNbOutput = 1;
    for (String[] in : wordresults) {
      expectedNbOutput *= Math.max(in.length, 1);
    }

    Assert.assertEquals(expectedNbOutput, expectedOutputRows.size());
    Assert.assertTrue(expectedOutputRows.size() >= outputRows.size());

    for (OutputRecord outputRecord : outputRows) {
      boolean found = false;
      for (OutputRecord expectedRecord : expectedOutputRows) {
        if (expectedRecord.equals(outputRecord)) {
          found = true;
          break;
        }
      }
      Assert.assertTrue("Record not found: " + outputRecord, found);
    }
    return nbDuplicateFound;
  }