*/
package org.sonar.server.search;
-import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableList;
-import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Lists;
-import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.sonar.core.cluster.WorkQueue;
import org.sonar.server.search.action.EmbeddedIndexAction;
import org.sonar.server.search.action.IndexAction;
-import org.sonar.server.search.action.KeyIndexAction;
-import java.util.HashMap;
import java.util.HashSet;
-import java.util.LinkedList;
import java.util.List;
-import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
} else if (actions.size() > 1) {
/* Purge actions that would be overridden */
- Map<String, Integer> itemOffset = new HashMap<String, Integer>();
- ArrayListMultimap<String, IndexAction> itemActions = ArrayListMultimap.create();
- List<IndexAction> embeddedActions = new LinkedList<IndexAction>();
+ Long purgeStart = System.currentTimeMillis();
+ List<IndexAction> itemActions = Lists.newArrayList();
+ List<IndexAction> embeddedActions = Lists.newArrayList();
+
for (IndexAction action : actions) {
if(action.getClass().isAssignableFrom(EmbeddedIndexAction.class)){
embeddedActions.add(action);
} else {
- String actionKey = action.getKey();
- Integer offset = 0;
- if(!itemOffset.containsKey(actionKey)){
- itemOffset.put(actionKey, offset);
- itemActions.put(actionKey, action);
- } else {
- offset = itemOffset.get(actionKey);
- if(action.getClass().isAssignableFrom(KeyIndexAction.class)){
- itemOffset.put(actionKey, 0);
- itemActions.get(actionKey).set(0, action);
- } else {
- itemActions.get(actionKey).set(offset, action);
- }
- }
+ itemActions.add(action);
}
}
+ LOGGER.debug("INDEX - compressed {} items into {} in {}ms,",
+ actions.size(), itemActions.size() + embeddedActions.size(), System.currentTimeMillis() - purgeStart);
+
try {
/* execute all item actions */
- Multimap<String, IndexAction> itemBulks = makeBulkByType(itemActions);
- CountDownLatch itemLatch = new CountDownLatch(itemBulks.size());
+ CountDownLatch itemLatch = new CountDownLatch(itemActions.size());
indexTime = System.currentTimeMillis();
- for (IndexAction action : itemBulks.values()) {
+ for (IndexAction action : itemActions) {
action.setLatch(itemLatch);
this.offer(action, 1000, TimeUnit.SECONDS);
types.add(action.getPayloadClass().getSimpleName());
bcount, ecount, StringUtils.join(refreshes, ","));
}
}
-
- private Multimap<String, IndexAction> makeBulkByType(ArrayListMultimap<String, IndexAction> actions) {
- Multimap<String, IndexAction> bulks = LinkedListMultimap.create();
- for (IndexAction action : actions.values()) {
- bulks.put(action.getIndexType(), action);
- }
- return bulks;
- }
-
}
import org.sonar.server.db.DbClient;
import org.sonar.server.log.db.LogDao;
import org.sonar.server.log.index.LogIndex;
+import org.sonar.server.log.index.LogQuery;
+import org.sonar.server.search.QueryOptions;
import org.sonar.server.tester.ServerTester;
+import java.util.Iterator;
import java.util.Map;
import static org.fest.assertions.Assertions.assertThat;
@Test
public void massive_insert() {
+
+ // 0 Assert no logs in DB
assertThat(dao.findAll(dbSession)).hasSize(0);
- int max = 20;
+ int max = 200;
final String testValue = "hello world";
for (int i = 0; i < max; i++) {
service.write(dbSession, Log.Type.ACTIVE_RULE, testValue + "_" + i);
}
dbSession.commit();
+
+ // 1. assert both backends have all logs
assertThat(dao.findAll(dbSession)).hasSize(max);
+ assertThat(index.findAll().getHits()).hasSize(max);
+
+ // 2. assert scrollable
+ int count = 0;
+ Iterator<Log> logs = index.search(new LogQuery(), new QueryOptions().setScroll(true)).scroll();
+ while (logs.hasNext()) {
+ logs.next();
+ count++;
+ }
+ assertThat(count).isEqualTo(max);
+
}
}