/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.lucene.index;

import org.apache.lucene.search.TermQuery;
import org.apache.lucene.util.LuceneTestCase;

import java.util.stream.IntStream;

/**
 * Unit test for {@link BufferedUpdates}
 */
public class TestBufferedUpdates extends LuceneTestCase {
  /**
   * return a term that maybe duplicated with pre
   */
  private static Term mayDuplicate(int bound) {
    boolean shouldDuplicated = bound > 3 && random().nextBoolean();
    if (shouldDuplicated) {
      return new Term("myField", String.valueOf(random().nextInt(bound)));
    }
    return new Term("myField", String.valueOf(bound));
  }

  public void testRamBytesUsed() {
    BufferedUpdates bu = new BufferedUpdates("seg1");
    assertEquals(bu.ramBytesUsed(), 0L);
    assertFalse(bu.any());
    IntStream.range(0, random().nextInt(atLeast(200))).forEach(id -> {
      int reminder = random().nextInt(3);
      if (reminder == 0) {
        bu.addDocID(id);
      } else if (reminder == 1) {
        bu.addQuery(new TermQuery(mayDuplicate(id)), id);
      } else if (reminder == 2) {
        bu.addTerm((mayDuplicate(id)), id);
      }
    });
    assertTrue("we have added tons of docIds, terms and queries", bu.any());

    long totalUsed = bu.ramBytesUsed();
    assertTrue(totalUsed > 0);

    bu.clearDeletedDocIds();
    assertTrue("only docIds are cleaned, buffer shouldn't be empty", bu.any());
    assertTrue("docIds are cleaned, ram in used should decrease", totalUsed > bu.ramBytesUsed());
    totalUsed = bu.ramBytesUsed();

    bu.clearDeleteTerms();
    assertTrue("only terms and docIds are cleaned, the queries are still in memory", bu.any());
    assertTrue("terms are cleaned, ram in used should decrease", totalUsed > bu.ramBytesUsed());

    bu.clear();
    assertFalse(bu.any());
    assertEquals(bu.ramBytesUsed(), 0L);
  }
}

@@ -923,44 +923,8 @@
 e;%0A%0A
-import java.util.stream.IntStream;%0A%0A
 /**%0A
@@ -1029,354 +1029,8 @@
 se %7B
-%0A  /**%0A   * return a term that maybe duplicated with pre%0A   */%0A  private static Term mayDuplicate(int bound) %7B%0A    boolean shouldDuplicated = bound %3E 3 && random().nextBoolean();%0A    if (shouldDuplicated) %7B%0A      return new Term(%22myField%22, String.valueOf(random().nextInt(bound)));%0A    %7D%0A    return new Term(%22myField%22, String.valueOf(bound));%0A  %7D
 %0A%0A  
@@ -1192,95 +1192,263 @@
     
-IntStream.range(0, random().nextInt(atLeast(200))).forEach(id -%3E %7B%0A      int reminder =
+int docIds = atLeast(1);%0A    for (int i = 0; i %3C docIds; i++) %7B%0A      bu.addDocID(random().nextInt(100));%0A    %7D%0A%0A    int queries = atLeast(1);%0A    for (int i = 0; i %3C queries; i++) %7B%0A      final int docIDUpto = random().nextBoolean() ? Integer.MAX_VALUE :
  ran
@@ -1465,9 +1465,8 @@
 Int(
-3
 );%0A 
@@ -1474,239 +1474,421 @@
     
-if (remind
+final Term t
 er
+m
  =
-= 0) %7B%0A        bu.addDocID(id);%0A      %7D else if (reminder == 1) %7B%0A        bu.addQuery(new TermQuery(mayDuplicate(id)), id);%0A      %7D else if (reminder == 2) %7B%0A        bu.addTerm((mayDuplicate(id)), id);%0A      %7D
+ new Term(%22id%22, Integer.toString(random().nextInt(100)));%0A      bu.addQuery(new TermQuery(term), docIDUpto);%0A    %7D%0A%0A    int terms = atLeast(1);%0A    for (int i = 0; i %3C terms; i++) %7B%0A      final int docIDUpto = random().nextBoolean() ? Integer.MAX_VALUE : random().nextInt();%0A      final Term term = new Term(%22id%22, Integer.toString(random().nextInt(100)));%0A      bu.addTerm(term, docIDUpto);
 %0A    %7D
-);
 %0A   

Fix TestBufferedUpdates