<commit_before>package foam.nanos.auth;

import foam.core.X;
import foam.nanos.util.LRULinkedHashMap;
import java.util.Map;

/**
 * Created by marcroopchand on 2017-05-30.
 */
public class CachedUserAndGroupAuthService
  extends UserAndGroupAuthService
{
  /**
   * The cached data structure will look like this
   *{
   *  permission1: {
   *    user1: true,
   *    user2: false
   *    ...
   *  },
   *  permission2: {
   *    user1: false,
   *    user2: true
   *    ...
   *  }
   *  ...
   *}
   */
  protected Map<String, Map<String, Boolean>> permissionMap = new LRULinkedHashMap<>(1000);
  protected Map<String, Boolean> userMap;

  @Override
  public void start() {
    super.start();
  }

  @Override
  public Boolean check(foam.core.X x, java.security.Permission permission) {
    if ( x == null || permission == null ) return false;

    User user = (User) x.get("user");
    if ( user == null ) return false;

    Group group = (Group) user.getGroup();
    if ( group == null ) return false;

    if ( permissionMap.containsKey(permission.getName()) ) {
      userMap = permissionMap.get(permission.getName());
    }
    else {
      userMap = new LRULinkedHashMap<>(100000);
    }

    if ( userMap.containsKey(user.getId()) ) return userMap.get(user.getId());

    boolean permissionCheck = group.implies(permission);
    userMap.put(user.getId(), permissionCheck);
    permissionMap.put(permission.getName(), userMap);

    return permissionCheck;
  }

  @Override
  public void logout(X x) {
    if ( x == null ) return;

    User user = (User) x.get("user");
    if ( user == null ) return;

    for ( String key: permissionMap.keySet() ) {
      permissionMap.get(key).remove(user.getId());
    }
  }
}<commit_msg>Reversed Map Structure for Users and Permissions<commit_after>package foam.nanos.auth;

import foam.core.X;
import foam.nanos.util.LRULinkedHashMap;
import java.util.Map;

/**
 * Created by marcroopchand on 2017-05-30.
 */
public class CachedUserAndGroupAuthService
  extends UserAndGroupAuthService
{
  /**
   * The cached data structure will look like this
   *{
   *  user1: {
   *    permission1: true,
   *    permission2: false
   *    ...
   *  },
   *  user2: {
   *    permission1: false,
   *    permission2: true
   *    ...
   *  }
   *  ...
   *}
   */
  protected Map<String, Map<String, Boolean>> userMap = new LRULinkedHashMap<>(1000000);
  protected Map<String, Boolean> permissionMap;

  @Override
  public void start() {
    super.start();
  }

  @Override
  public Boolean check(foam.core.X x, java.security.Permission permission) {
    if ( x == null || permission == null ) return false;

    User user = (User) x.get("user");
    if ( user == null ) return false;

    Group group = (Group) user.getGroup();
    if ( group == null ) return false;

    if ( userMap.containsKey(user.getId()) ) {
      permissionMap = userMap.get(user.getId());
    }
    else {
      permissionMap = new LRULinkedHashMap<>(1000);
    }

    if ( permissionMap.containsKey(permission.getName()) ) {
      return permissionMap.get(permission.getName());
    }

    boolean permissionCheck = group.implies(permission);
    permissionMap.put(permission.getName(), permissionCheck);
    userMap.put(user.getId(), permissionMap);

    return permissionCheck;
  }

  @Override
  public void logout(X x) {
    if ( x == null ) return;

    User user = (User) x.get("user");
    if ( user == null ) return;

    userMap.remove(user.getId());
  }
}
{'fraction_non_alphanumeric': 0.1382183908045977, 'fraction_numerical': 0.014080459770114942, 'mean_word_length': 2.8464088397790057, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '254838', 'n_tokens_mistral': 1156, 'n_tokens_neox': 1112, 'n_words': 256}