// Copyright (C) 2016 The Android Open Source Project
//
// Licensed 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 com.google.gerrit.server.index.account;

import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.AccountState;
import com.google.gerrit.server.index.Index;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;

import java.io.IOException;

public class AccountIndexer {

  public interface Factory {
    AccountIndexer create(AccountIndexCollection indexes);
  }

  private final AccountIndexCollection indexes;
  private final AccountCache byIdCache;

  @AssistedInject
  AccountIndexer(AccountCache byIdCache,
      @Assisted AccountIndexCollection indexes) {
    this.byIdCache = byIdCache;
    this.indexes = indexes;
  }

  /**
   * Synchronously index an account.
   *
   * @param id account id to index.
   */
  public void index(Account.Id id) throws IOException {
    for (Index<?, AccountState> i : indexes.getWriteIndexes()) {
      i.replace(byIdCache.get(id));
    }
  }
}

@@ -988,16 +988,75 @@
 ception;
+%0Aimport java.util.Collection;%0Aimport java.util.Collections;
 %0A%0Apublic
@@ -1170,18 +1170,105 @@
 xes);%0A  
-%7D%0A
+  AccountIndexer create(AccountIndex index);%0A  %7D%0A%0A  private final AccountCache byIdCache;
 %0A  priva
@@ -1331,31 +1331,27 @@
  Account
-Cache byIdCache
+Index index
 ;%0A%0A  @As
@@ -1514,16 +1514,222 @@
 ndexes;%0A
+    this.index = null;%0A  %7D%0A%0A  @AssistedInject%0A  AccountIndexer(AccountCache byIdCache,%0A      @Assisted AccountIndex index) %7B%0A    this.byIdCache = byIdCache;%0A    this.indexes = null;%0A    this.index = index;%0A
   %7D%0A%0A  /
@@ -1911,16 +1911,8 @@
 i : 
-indexes.
 getW
@@ -1973,11 +1973,174 @@
    %7D%0A  %7D
+%0A%0A  private Collection%3CAccountIndex%3E getWriteIndexes() %7B%0A    return indexes != null%0A        ? indexes.getWriteIndexes()%0A        : Collections.singleton(index);%0A  %7D
 %0A%7D%0A

Add factory method which takes an AccountIndex