Exemple #1
0
  public static void main(String[] args) throws IOException {
    DB db = new DB();

    if (!db.open(file, DB.OWRITER | DB.OCREATE)) {
      System.err.println("open error: " + db.error());
      return;
    }

    long tm = System.currentTimeMillis();

    for (int i = 0; i < RECORDS; i++) {

      if (i % 10000 == 0) {
        // myDatabase.sync();
        System.out.println(
            "Rec " + i + " (" + (i * 1000.0 / (System.currentTimeMillis() - tm)) + "rec/s)");
      }

      byte[] theKey = ("key" + i).getBytes();

      Camera p = new Camera();

      p.setCity("city" + i);
      p.setCountry("country" + (i / 100));

      for (int j = 1; j <= nRec; j++) {
        LogRecord lr = new LogRecord();

        lr.setTime(1111);
        lr.setEventHash("hash-" + i + "-" + j);

        p.addLogRecord(lr);
      }

      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ObjectOutputStream oos = new ObjectOutputStream(baos);

      oos.writeObject(p);

      oos.close();

      db.set(theKey, baos.toByteArray());
    }

    if (!db.close()) {
      System.err.println("close error: " + db.error());
    }
  }
  /**
   * @param args
   * @throws IOException
   * @throws ParseException
   */
  public static void main(String[] args) throws IOException, ParseException {
    if (args.length > 0) {
      Config.basePath = new File(args[0]);
    }

    IndexReader reader =
        DirectoryReader.open(NIOFSDirectory.open(new File(Config.basePath, "lucene")));
    IndexSearcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);

    QueryParser parser = new QueryParser(Version.LUCENE_44, "city", analyzer);

    long tm = System.currentTimeMillis();

    for (int i = 0; i < RUNS; i++) {
      long cn = (long) (Math.random() * FillLucene.CAMERAS);

      String qs = "city" + cn;

      Query query = parser.parse(qs);

      TopDocs results = searcher.search(query, 5);
      ScoreDoc[] hits = results.scoreDocs;

      if (results.totalHits != 1) {
        System.out.println("Wrong results num: " + results.totalHits + " Query: " + qs);
        continue;
      }

      Document doc = searcher.doc(hits[0].doc);

      //   System.out.println("City: "+doc.get("city"));
      //   System.out.println("Country: "+doc.get("country"));

      BytesRef data = doc.getBinaryValue("data");

      Camera cam = Camera.load(ByteBuffer.wrap(data.bytes));

      if (!cam.getCity().equals(qs)) System.out.println("Invalid result");
    }

    tm = System.currentTimeMillis() - tm;

    System.out.println("Time: " + StringUtils.millisToString(tm) + " Rate: " + (RUNS * 1000 / tm));

    /*
    Query query = parser.parse("city800000000");

    TopDocs results = searcher.search(query, 5);
    ScoreDoc[] hits = results.scoreDocs;

    System.out.println("Results: "+results.totalHits);

    Document doc = searcher.doc(hits[0].doc);

    System.out.println("City: "+doc.get("city"));
    System.out.println("Country: "+doc.get("country"));

    BytesRef data =  doc.getBinaryValue("data");

    Camera cam = Camera.load( ByteBuffer.wrap(data.bytes) );

    System.out.println("Camera: "+cam.getId()+" City: "+cam.getCity()+" Country: "+cam.getCountry());
    */
  }