Example usage for com.google.common.collect ImmutableMap size

List of usage examples for com.google.common.collect ImmutableMap size

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableMap size.

Prototype

int size();

Source Link

Document

Returns the number of key-value mappings in this map.

Usage

From source file:org.apache.druid.metadata.SQLMetadataRuleManager.java

@Override
public void poll() {
    try {//from  w w  w.  j  a v  a2  s  .  co m

        ImmutableMap<String, List<Rule>> newRules = ImmutableMap
                .copyOf(dbi.withHandle(new HandleCallback<Map<String, List<Rule>>>() {
                    @Override
                    public Map<String, List<Rule>> withHandle(Handle handle) {
                        return handle.createQuery(
                                // Return latest version rule by dataSource
                                StringUtils.format("SELECT r.dataSource, r.payload " + "FROM %1$s r "
                                        + "INNER JOIN(SELECT dataSource, max(version) as version FROM %1$s GROUP BY dataSource) ds "
                                        + "ON r.datasource = ds.datasource and r.version = ds.version",
                                        getRulesTable()))
                                .map(new ResultSetMapper<Pair<String, List<Rule>>>() {
                                    @Override
                                    public Pair<String, List<Rule>> map(int index, ResultSet r,
                                            StatementContext ctx) throws SQLException {
                                        try {
                                            return Pair.of(r.getString("dataSource"), jsonMapper.readValue(
                                                    r.getBytes("payload"), new TypeReference<List<Rule>>() {
                                                    }));
                                        } catch (IOException e) {
                                            throw Throwables.propagate(e);
                                        }
                                    }
                                }).fold(Maps.newHashMap(),
                                        new Folder3<Map<String, List<Rule>>, Pair<String, List<Rule>>>() {
                                            @Override
                                            public Map<String, List<Rule>> fold(Map<String, List<Rule>> retVal,
                                                    Pair<String, List<Rule>> stringObjectMap,
                                                    FoldController foldController,
                                                    StatementContext statementContext) {
                                                try {
                                                    String dataSource = stringObjectMap.lhs;
                                                    retVal.put(dataSource, stringObjectMap.rhs);
                                                    return retVal;
                                                } catch (Exception e) {
                                                    throw Throwables.propagate(e);
                                                }
                                            }
                                        });
                    }
                }));

        log.info("Polled and found rules for %,d datasource(s)", newRules.size());

        rules.set(newRules);
        failStartTimeMs = 0;
    } catch (Exception e) {
        if (failStartTimeMs == 0) {
            failStartTimeMs = System.currentTimeMillis();
        }

        if (System.currentTimeMillis() - failStartTimeMs > config.getAlertThreshold().toStandardDuration()
                .getMillis()) {
            log.makeAlert(e, "Exception while polling for rules").emit();
            failStartTimeMs = 0;
        } else {
            log.error(e, "Exception while polling for rules");
        }
    }
}

From source file:io.druid.metadata.SQLMetadataRuleManager.java

public void poll() {
    try {//  w  w w  . j a va2  s. c o m
        ImmutableMap<String, List<Rule>> newRules = ImmutableMap
                .copyOf(dbi.withHandle(new HandleCallback<Map<String, List<Rule>>>() {
                    @Override
                    public Map<String, List<Rule>> withHandle(Handle handle) throws Exception {
                        return handle.createQuery(
                                // Return latest version rule by dataSource
                                String.format("SELECT r.dataSource, r.payload " + "FROM %1$s r "
                                        + "INNER JOIN(SELECT dataSource, max(version) as version FROM %1$s GROUP BY dataSource) ds "
                                        + "ON r.datasource = ds.datasource and r.version = ds.version",
                                        getRulesTable()))
                                .map(new ResultSetMapper<Pair<String, List<Rule>>>() {
                                    @Override
                                    public Pair<String, List<Rule>> map(int index, ResultSet r,
                                            StatementContext ctx) throws SQLException {
                                        try {
                                            return Pair.of(r.getString("dataSource"),
                                                    jsonMapper.<List<Rule>>readValue(r.getBytes("payload"),
                                                            new TypeReference<List<Rule>>() {
                                                            }));
                                        } catch (IOException e) {
                                            throw Throwables.propagate(e);
                                        }
                                    }
                                }).fold(Maps.<String, List<Rule>>newHashMap(),
                                        new Folder3<Map<String, List<Rule>>, Pair<String, List<Rule>>>() {
                                            @Override
                                            public Map<String, List<Rule>> fold(Map<String, List<Rule>> retVal,
                                                    Pair<String, List<Rule>> stringObjectMap,
                                                    FoldController foldController,
                                                    StatementContext statementContext) throws SQLException {
                                                try {
                                                    String dataSource = stringObjectMap.lhs;
                                                    retVal.put(dataSource, stringObjectMap.rhs);
                                                    return retVal;
                                                } catch (Exception e) {
                                                    throw Throwables.propagate(e);
                                                }
                                            }
                                        });
                    }
                }));

        log.info("Polled and found rules for %,d datasource(s)", newRules.size());

        rules.set(newRules);
        retryStartTime = 0;
    } catch (Exception e) {
        if (retryStartTime == 0) {
            retryStartTime = System.currentTimeMillis();
        }

        if (System.currentTimeMillis() - retryStartTime > config.get().getAlertThreshold().getMillis()) {
            log.makeAlert(e, "Exception while polling for rules").emit();
            retryStartTime = 0;
        } else {
            log.error(e, "Exception while polling for rules");
        }
    }
}