fakemongo / fongo

faked out in-memory mongo for java
Apache License 2.0
523 stars 156 forks source link

deleteMany() is too slow. org.bson.BasicBSONObject#hashCode very expensive call #297

Open VISTALL opened 7 years ago

VISTALL commented 7 years ago

Hi.

deleteMany() is too slow. Reproduce example eat more that one minute

Problem in HashMap.get(K) here https://github.com/fakemongo/fongo/blob/master/src/main/java/com/github/fakemongo/impl/index/IndexedList.java#L89

Reproduce example: FongoIndexTest.java

  @Test(timeout = 10000)
  public void testBigCollectionAndRemovingSomeDataBigTimeout() {
    MongoCollection<Document> collection = fongoRule.newMongoCollection("mock");
    collection.createIndex(Indexes.ascending("year"));

    String[] years = new String[] {"2017", "2016", "2015", "2014"};
    List<Document> objects = new ArrayList<Document>();
    for (int i = 0; i < 50000; i++) {
      Document object = new Document();
      object.put("year", years[random.nextInt(years.length)]);

      Document paramsDoc = new Document();
      paramsDoc.put("test", String.valueOf(i));
      object.put("params", paramsDoc);

      objects.add(object);
    }
    collection.insertMany(objects);

    long count = collection.count(Filters.eq("year", years[0]));
    DeleteResult result = collection.deleteMany(Filters.eq("year", years[0]));
    assertEquals(count, result.getDeletedCount());
    assertEquals(0, collection.count(Filters.eq("year", years[0])));
  }