DataContextImpl(OptiqConnectionImpl connection, List<Object> parameterValues) {
      this.queryProvider = connection;
      this.typeFactory = connection.getTypeFactory();
      this.rootSchema = connection.rootSchema;

      // Store the time at which the query started executing. The SQL
      // standard says that functions such as CURRENT_TIMESTAMP return the
      // same value throughout the query.
      final long time = System.currentTimeMillis();
      final TimeZone timeZone = connection.getTimeZone();
      final long localOffset = timeZone.getOffset(time);
      final long currentOffset = localOffset;

      ImmutableMap.Builder<Object, Object> builder = ImmutableMap.builder();
      builder
          .put("utcTimestamp", time)
          .put("currentTimestamp", time + currentOffset)
          .put("localTimestamp", time + localOffset)
          .put("timeZone", timeZone);
      for (Ord<Object> value : Ord.zip(parameterValues)) {
        Object e = value.e;
        if (e == null) {
          e = AvaticaParameter.DUMMY_VALUE;
        }
        builder.put("?" + value.i, e);
      }
      map = builder.build();
    }
  /**
   * collect saved Reflections resources from all urls that contains the given packagePrefix and
   * matches the given resourceNameFilter and de-serializes them using the default serializer {@link
   * XmlSerializer} or using the optionally supplied optionalSerializer
   *
   * <p>it is preferred to use a designated resource prefix (for example META-INF/reflections but
   * not just META-INF), so that relevant urls could be found much faster
   *
   * @param optionalSerializer - optionally supply one serializer instance. if not specified or
   *     null, {@link XmlSerializer} will be used
   */
  public static Reflections collect(
      final String packagePrefix,
      final Predicate<String> resourceNameFilter,
      @Nullable Serializer... optionalSerializer) {
    Serializer serializer =
        optionalSerializer != null && optionalSerializer.length == 1
            ? optionalSerializer[0]
            : new XmlSerializer();

    Collection<URL> urls = ClasspathHelper.forPackage(packagePrefix);
    if (urls.isEmpty()) return null;
    long start = System.currentTimeMillis();
    final Reflections reflections = new Reflections();
    Iterable<Vfs.File> files = Vfs.findFiles(urls, packagePrefix, resourceNameFilter);
    for (final Vfs.File file : files) {
      InputStream inputStream = null;
      try {
        inputStream = file.openInputStream();
        reflections.merge(serializer.read(inputStream));
      } catch (IOException e) {
        throw new ReflectionsException("could not merge " + file, e);
      } finally {
        close(inputStream);
      }
    }

    if (log != null) {
      Store store = reflections.getStore();
      int keys = 0;
      int values = 0;
      for (String index : store.keySet()) {
        keys += store.get(index).keySet().size();
        values += store.get(index).size();
      }

      log.info(
          format(
              "Reflections took %d ms to collect %d url%s, producing %d keys and %d values [%s]",
              System.currentTimeMillis() - start,
              urls.size(),
              urls.size() > 1 ? "s" : "",
              keys,
              values,
              Joiner.on(", ").join(urls)));
    }
    return reflections;
  }
 /**
  * @param statsQuery StatisticsQueryCondition
  * @param statisticsStorage
  * @param scoringExps Set of experiments that have at least one non-zero score for
  *     statisticsQuery. This is used retrieving efos to be displayed in heatmap when no query efvs
  *     exist (c.f. atlasStatisticsQueryService.getScoringAttributesForGenes())
  * @return experiment counts corresponding for statsQuery
  */
 public static Multiset<Integer> getExperimentCounts(
     StatisticsQueryCondition statsQuery,
     StatisticsStorage statisticsStorage,
     Set<ExperimentInfo> scoringExps) {
   long start = System.currentTimeMillis();
   Multiset<Integer> counts =
       StatisticsQueryUtils.scoreQuery(statsQuery, statisticsStorage, scoringExps);
   long dur = System.currentTimeMillis() - start;
   int numOfGenesWithCounts = counts.elementSet().size();
   if (numOfGenesWithCounts > 0) {
     log.debug(
         "StatisticsQuery: "
             + statsQuery.prettyPrint()
             + " ==> result set size: "
             + numOfGenesWithCounts
             + " (duration: "
             + dur
             + " ms)");
   }
   return counts;
 }
 @GET
 @Path("/files")
 public List<ToDo> files() {
   List<ToDo> response = Lists.newArrayList();
   File[] files = new File("/lib").listFiles();
   if (files != null) {
     for (File file : files) {
       if (file.isFile())
         response.add(
             new ToDo(
                 ++id,
                 file.getName(),
                 file.isFile() ? file.length() : -1,
                 System.currentTimeMillis()));
     }
   }
   return response;
 }
  protected void scan() {
    if (configuration.getUrls() == null || configuration.getUrls().isEmpty()) {
      if (log != null) log.warn("given scan urls are empty. set urls in the configuration");
      return;
    }

    if (log != null && log.isDebugEnabled()) {
      log.debug("going to scan these urls:\n" + Joiner.on("\n").join(configuration.getUrls()));
    }

    long time = System.currentTimeMillis();
    int scannedUrls = 0;
    ExecutorService executorService = configuration.getExecutorService();
    List<Future<?>> futures = Lists.newArrayList();

    for (final URL url : configuration.getUrls()) {
      try {
        if (executorService != null) {
          futures.add(
              executorService.submit(
                  new Runnable() {
                    public void run() {
                      if (log != null && log.isDebugEnabled())
                        log.debug("[" + Thread.currentThread().toString() + "] scanning " + url);
                      scan(url);
                    }
                  }));
        } else {
          scan(url);
        }
        scannedUrls++;
      } catch (ReflectionsException e) {
        if (log != null && log.isWarnEnabled())
          log.warn("could not create Vfs.Dir from url. ignoring the exception and continuing", e);
      }
    }

    // todo use CompletionService
    if (executorService != null) {
      for (Future future : futures) {
        try {
          future.get();
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
      }
    }

    time = System.currentTimeMillis() - time;

    // gracefully shutdown the parallel scanner executor service.
    if (executorService != null) {
      executorService.shutdown();
    }

    if (log != null) {
      int keys = 0;
      int values = 0;
      for (String index : store.keySet()) {
        keys += store.get(index).keySet().size();
        values += store.get(index).size();
      }

      log.info(
          format(
              "Reflections took %d ms to scan %d urls, producing %d keys and %d values %s",
              time,
              scannedUrls,
              keys,
              values,
              executorService != null && executorService instanceof ThreadPoolExecutor
                  ? format(
                      "[using %d cores]",
                      ((ThreadPoolExecutor) executorService).getMaximumPoolSize())
                  : ""));
    }
  }
  private void loadConfig() throws IOException, JedisConnectionException {
    if (!getDataFolder().exists()) {
      getDataFolder().mkdir();
    }

    File file = new File(getDataFolder(), "config.yml");

    if (!file.exists()) {
      file.createNewFile();
      try (InputStream in = getResourceAsStream("example_config.yml");
          OutputStream out = new FileOutputStream(file)) {
        ByteStreams.copy(in, out);
      }
    }

    final Configuration configuration =
        ConfigurationProvider.getProvider(YamlConfiguration.class).load(file);

    final String redisServer = configuration.getString("redis-server", "localhost");
    final int redisPort = configuration.getInt("redis-port", 6379);
    String redisPassword = configuration.getString("redis-password");
    String serverId = configuration.getString("server-id");

    if (redisPassword != null && (redisPassword.isEmpty() || redisPassword.equals("none"))) {
      redisPassword = null;
    }

    // Configuration sanity checks.
    if (serverId == null || serverId.isEmpty()) {
      throw new RuntimeException("server-id is not specified in the configuration or is empty");
    }

    if (redisServer != null && !redisServer.isEmpty()) {
      final String finalRedisPassword = redisPassword;
      FutureTask<JedisPool> task =
          new FutureTask<>(
              new Callable<JedisPool>() {
                @Override
                public JedisPool call() throws Exception {
                  // With recent versions of Jedis, we must set the classloader to the one
                  // BungeeCord used
                  // to load RedisBungee with.
                  ClassLoader previous = Thread.currentThread().getContextClassLoader();
                  Thread.currentThread().setContextClassLoader(RedisBungee.class.getClassLoader());

                  // Create the pool...
                  JedisPoolConfig config = new JedisPoolConfig();
                  config.setMaxTotal(configuration.getInt("max-redis-connections", 8));
                  JedisPool pool =
                      new JedisPool(config, redisServer, redisPort, 0, finalRedisPassword);

                  // Reset classloader and return the pool
                  Thread.currentThread().setContextClassLoader(previous);
                  return pool;
                }
              });

      getProxy().getScheduler().runAsync(this, task);

      try {
        pool = task.get();
      } catch (InterruptedException | ExecutionException e) {
        throw new RuntimeException("Unable to create Redis pool", e);
      }

      // Test the connection
      try (Jedis rsc = pool.getResource()) {
        rsc.ping();
        // If that worked, now we can check for an existing, alive Bungee:
        File crashFile = new File(getDataFolder(), "restarted_from_crash.txt");
        if (crashFile.exists()) {
          crashFile.delete();
        } else if (rsc.hexists("heartbeats", serverId)) {
          try {
            long value = Long.parseLong(rsc.hget("heartbeats", serverId));
            if (System.currentTimeMillis() < value + 20000) {
              getLogger()
                  .severe(
                      "You have launched a possible impostor BungeeCord instance. Another instance is already running.");
              getLogger()
                  .severe("For data consistency reasons, RedisBungee will now disable itself.");
              getLogger()
                  .severe(
                      "If this instance is coming up from a crash, create a file in your RedisBungee plugins directory with the name 'restarted_from_crash.txt' and RedisBungee will not perform this check.");
              throw new RuntimeException("Possible impostor instance!");
            }
          } catch (NumberFormatException ignored) {
          }
        }

        FutureTask<Void> task2 =
            new FutureTask<>(
                new Callable<Void>() {
                  @Override
                  public Void call() throws Exception {
                    httpClient = new OkHttpClient();
                    Dispatcher dispatcher = new Dispatcher(getExecutorService());
                    httpClient.setDispatcher(dispatcher);
                    NameFetcher.setHttpClient(httpClient);
                    UUIDFetcher.setHttpClient(httpClient);
                    RedisBungee.configuration =
                        new RedisBungeeConfiguration(RedisBungee.this.getPool(), configuration);
                    return null;
                  }
                });

        getProxy().getScheduler().runAsync(this, task2);

        try {
          task2.get();
        } catch (InterruptedException | ExecutionException e) {
          throw new RuntimeException("Unable to create HTTP client", e);
        }

        getLogger().log(Level.INFO, "Successfully connected to Redis.");
      } catch (JedisConnectionException e) {
        pool.destroy();
        pool = null;
        throw e;
      }
    } else {
      throw new RuntimeException("No redis server specified!");
    }
  }
  @Override
  public void onEnable() {
    try {
      loadConfig();
    } catch (IOException e) {
      throw new RuntimeException("Unable to load/save config", e);
    } catch (JedisConnectionException e) {
      throw new RuntimeException("Unable to connect to your Redis server!", e);
    }
    if (pool != null) {
      try (Jedis tmpRsc = pool.getResource()) {
        // This is more portable than INFO <section>
        String info = tmpRsc.info();
        for (String s : info.split("\r\n")) {
          if (s.startsWith("redis_version:")) {
            String version = s.split(":")[1];
            if (!(usingLua = RedisUtil.canUseLua(version))) {
              getLogger()
                  .warning(
                      "Your version of Redis ("
                          + version
                          + ") is not at least version 2.6. RedisBungee requires a newer version of Redis.");
              throw new RuntimeException("Unsupported Redis version detected");
            } else {
              LuaManager manager = new LuaManager(this);
              serverToPlayersScript =
                  manager.createScript(
                      IOUtil.readInputStreamAsString(
                          getResourceAsStream("lua/server_to_players.lua")));
              getPlayerCountScript =
                  manager.createScript(
                      IOUtil.readInputStreamAsString(
                          getResourceAsStream("lua/get_player_count.lua")));
              getServerPlayersScript =
                  manager.createScript(
                      IOUtil.readInputStreamAsString(
                          getResourceAsStream("lua/get_server_players.lua")));
            }
            break;
          }
        }

        tmpRsc.hset(
            "heartbeats", configuration.getServerId(), String.valueOf(System.currentTimeMillis()));

        long uuidCacheSize = tmpRsc.hlen("uuid-cache");
        if (uuidCacheSize > 750000) {
          getLogger()
              .info(
                  "Looks like you have a really big UUID cache! Run https://www.spigotmc.org/resources/redisbungeecleaner.8505/ as soon as possible.");
        }
      }
      serverIds = getCurrentServerIds(true, false);
      uuidTranslator = new UUIDTranslator(this);
      heartbeatTask =
          getProxy()
              .getScheduler()
              .schedule(
                  this,
                  new Runnable() {
                    @Override
                    public void run() {
                      try (Jedis rsc = pool.getResource()) {
                        long redisTime = getRedisTime(rsc.time());
                        rsc.hset(
                            "heartbeats", configuration.getServerId(), String.valueOf(redisTime));
                      } catch (JedisConnectionException e) {
                        // Redis server has disappeared!
                        getLogger()
                            .log(
                                Level.SEVERE,
                                "Unable to update heartbeat - did your Redis server go away?",
                                e);
                      }
                      serverIds = getCurrentServerIds(true, false);
                      globalPlayerCount.set(getCurrentCount());
                    }
                  },
                  0,
                  3,
                  TimeUnit.SECONDS);
      dataManager = new DataManager(this);
      if (configuration.isRegisterBungeeCommands()) {
        getProxy()
            .getPluginManager()
            .registerCommand(this, new RedisBungeeCommands.GlistCommand(this));
        getProxy()
            .getPluginManager()
            .registerCommand(this, new RedisBungeeCommands.FindCommand(this));
        getProxy()
            .getPluginManager()
            .registerCommand(this, new RedisBungeeCommands.LastSeenCommand(this));
        getProxy()
            .getPluginManager()
            .registerCommand(this, new RedisBungeeCommands.IpCommand(this));
      }
      getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.SendToAll(this));
      getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.ServerId(this));
      getProxy().getPluginManager().registerCommand(this, new RedisBungeeCommands.ServerIds());
      getProxy()
          .getPluginManager()
          .registerCommand(this, new RedisBungeeCommands.PlayerProxyCommand(this));
      getProxy()
          .getPluginManager()
          .registerCommand(this, new RedisBungeeCommands.PlistCommand(this));
      getProxy()
          .getPluginManager()
          .registerCommand(this, new RedisBungeeCommands.DebugCommand(this));
      api = new RedisBungeeAPI(this);
      getProxy()
          .getPluginManager()
          .registerListener(
              this, new RedisBungeeListener(this, configuration.getExemptAddresses()));
      getProxy().getPluginManager().registerListener(this, dataManager);
      psl = new PubSubListener();
      getProxy().getScheduler().runAsync(this, psl);
      integrityCheck =
          getProxy()
              .getScheduler()
              .schedule(
                  this,
                  new Runnable() {
                    @Override
                    public void run() {
                      try (Jedis tmpRsc = pool.getResource()) {
                        Set<String> players = getLocalPlayersAsUuidStrings();
                        Set<String> playersInRedis =
                            tmpRsc.smembers(
                                "proxy:" + configuration.getServerId() + ":usersOnline");
                        List<String> lagged = getCurrentServerIds(false, true);

                        // Clean up lagged players.
                        for (String s : lagged) {
                          Set<String> laggedPlayers =
                              tmpRsc.smembers("proxy:" + s + ":usersOnline");
                          tmpRsc.del("proxy:" + s + ":usersOnline");
                          if (!laggedPlayers.isEmpty()) {
                            getLogger()
                                .info(
                                    "Cleaning up lagged proxy "
                                        + s
                                        + " ("
                                        + laggedPlayers.size()
                                        + " players)...");
                            for (String laggedPlayer : laggedPlayers) {
                              RedisUtil.cleanUpPlayer(laggedPlayer, tmpRsc);
                            }
                          }
                        }

                        for (Iterator<String> it = playersInRedis.iterator(); it.hasNext(); ) {
                          String member = it.next();
                          if (!players.contains(member)) {
                            // Are they simply on a different proxy?
                            String found = null;
                            for (String proxyId : getServerIds()) {
                              if (proxyId.equals(configuration.getServerId())) continue;
                              if (tmpRsc.sismember("proxy:" + proxyId + ":usersOnline", member)) {
                                // Just clean up the set.
                                found = proxyId;
                                break;
                              }
                            }
                            if (found == null) {
                              RedisUtil.cleanUpPlayer(member, tmpRsc);
                              getLogger()
                                  .warning(
                                      "Player found in set that was not found locally and globally: "
                                          + member);
                            } else {
                              tmpRsc.srem(
                                  "proxy:" + configuration.getServerId() + ":usersOnline", member);
                              getLogger()
                                  .warning(
                                      "Player found in set that was not found locally, but is on another proxy: "
                                          + member);
                            }

                            it.remove();
                          }
                        }

                        Pipeline pipeline = tmpRsc.pipelined();

                        for (String player : players) {
                          if (playersInRedis.contains(player)) continue;

                          // Player not online according to Redis but not BungeeCord.
                          getLogger()
                              .warning("Player " + player + " is on the proxy but not in Redis.");

                          ProxiedPlayer proxiedPlayer =
                              ProxyServer.getInstance().getPlayer(UUID.fromString(player));
                          if (proxiedPlayer == null) continue; // We'll deal with it later.

                          RedisUtil.createPlayer(proxiedPlayer, pipeline, true);
                        }

                        pipeline.sync();
                      }
                    }
                  },
                  0,
                  1,
                  TimeUnit.MINUTES);
    }
    getProxy().registerChannel("RedisBungee");
  }