<commit_before>package org.apache.solr.update;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TermRangeFilter;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.join.ScoreMode;
import org.apache.lucene.search.join.ToParentBlockJoinQuery;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.client.solrj.request.RequestWriter;
import org.apache.solr.client.solrj.request.UpdateRequest;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.util.JavaBinCodec;
import org.apache.solr.handler.loader.XMLLoader;
import org.apache.solr.search.SolrIndexSearcher;
import org.apache.solr.util.DefaultSolrThreadFactory;
import org.apache.solr.util.RefCounted;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.xml.sax.SAXException;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;




import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

/**
 * 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.
 */

public class AddBlockUpdateTest extends SolrTestCaseJ4 {
  
  private static final String child = "child_s";
  private static final String parent = "parent_s";
  private static final String type = "type_s";
  
  private static ExecutorService exe;
  private static AtomicInteger counter = new AtomicInteger();
  private static boolean cachedMode;
  
  private static XMLInputFactory inputFactory;
  
  private RefCounted<SolrIndexSearcher> searcherRef;
  private SolrIndexSearcher _searcher;
  
  @BeforeClass
  public static void beforeClass() throws Exception {
    String oldCacheNamePropValue = System
        .getProperty("blockJoinParentFilterCache");
    System.setProperty("blockJoinParentFilterCache", (cachedMode = random()
        .nextBoolean()) ? "blockJoinParentFilterCache" : "don't cache");
    if (oldCacheNamePropValue != null) {
      System.setProperty("blockJoinParentFilterCache", oldCacheNamePropValue);
    }
    inputFactory = XMLInputFactory.newInstance();
    
    exe = // Executors.newSingleThreadExecutor();
    rarely() ? Executors.newFixedThreadPool(atLeast(2), new DefaultSolrThreadFactory("AddBlockUpdateTest")) : Executors
        .newCachedThreadPool(new DefaultSolrThreadFactory("AddBlockUpdateTest"));


    initCore("solrconfig.xml", "schema15.xml");
  }
  
  @Before
  public void prepare() {
    // assertU("<rollback/>");
    assertU(delQ("*:*"));
    assertU(commit("expungeDeletes", "true"));
    
  }
  
  private SolrIndexSearcher getSearcher() {
    if (_searcher == null) {
      searcherRef = h.getCore().getSearcher();
      _searcher = searcherRef.get();
    }
    return _searcher;
  }
  
  @After
  public void cleanup() {
    if (searcherRef != null || _searcher != null) {
      searcherRef.decref();
      searcherRef = null;
      _searcher = null;
    }
  }
  
  @AfterClass
  public static void afterClass() throws Exception {
    exe.shutdownNow();
    
    exe = null;
    inputFactory = null;
    counter = null;<commit_msg>SOLR-5177: tests - add overwrite test for block join<commit_after>package org.apache.solr.update;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TermRangeFilter;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.join.ScoreMode;
import org.apache.lucene.search.join.ToParentBlockJoinQuery;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.client.solrj.request.RequestWriter;
import org.apache.solr.client.solrj.request.UpdateRequest;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.util.JavaBinCodec;
import org.apache.solr.handler.loader.XMLLoader;
import org.apache.solr.search.SolrIndexSearcher;
import org.apache.solr.util.DefaultSolrThreadFactory;
import org.apache.solr.util.RefCounted;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.xml.sax.SAXException;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;





import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

/**
 * 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.
 */

public class AddBlockUpdateTest extends SolrTestCaseJ4 {
  
  private static final String child = "child_s";
  private static final String parent = "parent_s";
  private static final String type = "type_s";
  
  private static ExecutorService exe;
  private static AtomicInteger counter = new AtomicInteger();
  private static boolean cachedMode;
  
  private static XMLInputFactory inputFactory;
  
  private RefCounted<SolrIndexSearcher> searcherRef;
  private SolrIndexSearcher _searcher;
  
  @BeforeClass
  public static void beforeClass() throws Exception {
    String oldCacheNamePropValue = System
        .getProperty("blockJoinParentFilterCache");
    System.setProperty("blockJoinParentFilterCache", (cachedMode = random()
        .nextBoolean()) ? "blockJoinParentFilterCache" : "don't cache");
    if (oldCacheNamePropValue != null) {
      System.setProperty("blockJoinParentFilterCache", oldCacheNamePropValue);
    }
    inputFactory = XMLInputFactory.newInstance();
    
    exe = // Executors.newSingleThreadExecutor();
    rarely() ? Executors.newFixedThreadPool(atLeast(2), new DefaultSolrThreadFactory("AddBlockUpdateTest")) : Executors
        .newCachedThreadPool(new DefaultSolrThreadFactory("AddBlockUpdateTest"));


    initCore("solrconfig.xml", "schema15.xml");
  }
  
  @Before
  public void prepare() {
    // assertU("<rollback/>");
    assertU(delQ("*:*"));
    assertU(commit("expungeDeletes", "true"));
    
  }
  
  private SolrIndexSearcher getSearcher() {
    if (_searcher == null) {
      searcherRef = h.getCore().getSearcher();
      _searcher = searcherRef.get();
    }
    return _searcher;
  }
  
  @After
  public void cleanup() {
    if (searcherRef != null || _searcher != null) {
      searcherRef.decref();
      searcherRef = null;
      _searcher = null;
    }
  }
  
  @AfterClass
  public static void afterClass() throws Exception {
    exe.shutdownNow();
    
    exe = null;
    inputFactory = null;
    counter = null;
  }
  
  @Test
  public void testOverwrite() throws IOException{
    assertU(add(
      nest(doc("id","X", parent, "X"), 
             doc(child,"a", "id", "66"), 
             doc(child,"b", "id", "66"))));
    assertU(add(
      nest(doc("id","Y", parent, "Y"), 
             doc(child,"a", "id", "66"), 
             doc(child,"b", "id", "66"))));
    String overwritten = random().nextBoolean() ? "X": "Y";
    String dubbed = overwritten=="X" ? "Y":"X";
    
    assertU(add(
        nest(doc("id",overwritten, parent, overwritten), 
               doc(child,"c","id", "66"), 
               doc(child,"d","id", "66")), "overwrite", "true"));
    assertU(add(
        nest(doc("id",dubbed, parent, dubbed), 
               doc(child,"c","id", "66"), 
               doc(child,"d","id", "66")), "overwrite", "false"));
    
    assertU(commit());
    
    assertQ(req(parent+":"+overwritten, "//*[@numFound='1']"));
    assertQ(req(parent+":"+dubbed, "//*[@numFound='2']"));
    
    final SolrIndexSearcher searcher = getSearcher();
    assertSingleParentOf(searcher, one("ab"), dubbed);
    
    final TopDocs docs = searcher.search(join(one("cd")), 10);
    assertEquals(2, docs.totalHits);
    final String pAct = searcher.doc(docs.scoreDocs[0].doc).get(parent)+
                        searcher.doc(docs.scoreDocs[1].doc).get(parent);
    assertTrue(pAct.contains(dubbed) && pAct.contains(overwritten) && pAct.length()==2);
    
    assertQ(req("id:66", "//*[@numFound='6']"));
    assertQ(req(child+":(a b)", "//*[@numFound='2']"));
    assertQ(req(child+":(c d)", "//*[@numFound='4']"));
  }
  
  private static XmlDoc nest(XmlDoc parent, XmlDoc ... children){
    XmlDoc xmlDoc = new XmlDoc();
    xmlDoc.xml = parent.xml.replace("</doc>",
        Arrays.toString(children).replaceAll("[\\[\\]]", "")+"</doc>");
    return xmlDoc;
{'fraction_non_alphanumeric': 0.12186509732910819, 'fraction_numerical': 0.005160706201901313, 'mean_word_length': 4.173770491803278, 'pattern_counts': {'":': 6, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 21}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1088124', 'n_tokens_mistral': 3377, 'n_tokens_neox': 3339, 'n_words': 783}