All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gerrit.server.account.AccountByEmailCacheImpl Maven / Gradle / Ivy

There is a newer version: 3.10.0-rc4
Show newest version
// Copyright (C) 2009 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.account;

import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableSet;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.cache.CacheModule;
import com.google.gerrit.server.query.account.InternalAccountQuery;
import com.google.gwtorm.server.SchemaFactory;
import com.google.inject.Inject;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Named;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Translates an email address to a set of matching accounts. */
@Singleton
public class AccountByEmailCacheImpl implements AccountByEmailCache {
  private static final Logger log = LoggerFactory.getLogger(AccountByEmailCacheImpl.class);
  private static final String CACHE_NAME = "accounts_byemail";

  public static Module module() {
    return new CacheModule() {
      @Override
      protected void configure() {
        cache(CACHE_NAME, String.class, new TypeLiteral>() {}).loader(Loader.class);
        bind(AccountByEmailCacheImpl.class);
        bind(AccountByEmailCache.class).to(AccountByEmailCacheImpl.class);
      }
    };
  }

  private final LoadingCache> cache;

  @Inject
  AccountByEmailCacheImpl(@Named(CACHE_NAME) LoadingCache> cache) {
    this.cache = cache;
  }

  @Override
  public Set get(final String email) {
    try {
      return cache.get(email);
    } catch (ExecutionException e) {
      log.warn("Cannot resolve accounts by email", e);
      return Collections.emptySet();
    }
  }

  @Override
  public void evict(final String email) {
    if (email != null) {
      cache.invalidate(email);
    }
  }

  static class Loader extends CacheLoader> {
    private final SchemaFactory schema;
    private final Provider accountQueryProvider;

    @Inject
    Loader(SchemaFactory schema, Provider accountQueryProvider) {
      this.schema = schema;
      this.accountQueryProvider = accountQueryProvider;
    }

    @Override
    public Set load(String email) throws Exception {
      try (ReviewDb db = schema.open()) {
        Set r = new HashSet<>();
        for (Account a : db.accounts().byPreferredEmail(email)) {
          r.add(a.getId());
        }
        for (AccountState accountState : accountQueryProvider.get().byEmailPrefix(email)) {
          if (accountState
              .getExternalIds()
              .stream()
              .filter(e -> email.equals(e.email()))
              .findAny()
              .isPresent()) {
            r.add(accountState.getAccount().getId());
          }
        }
        return ImmutableSet.copyOf(r);
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy