Search in sources :

Example 56 with SortedMap

use of java.util.SortedMap in project lucene-solr by apache.

the class WordDelimiterGraphFilterFactory method parseTypes.

// parses a list of MappingCharFilter style rules into a custom byte[] type table
private byte[] parseTypes(List<String> rules) {
    SortedMap<Character, Byte> typeMap = new TreeMap<>();
    for (String rule : rules) {
        Matcher m = typePattern.matcher(rule);
        if (!m.find())
            throw new IllegalArgumentException("Invalid Mapping Rule : [" + rule + "]");
        String lhs = parseString(m.group(1).trim());
        Byte rhs = parseType(m.group(2).trim());
        if (lhs.length() != 1)
            throw new IllegalArgumentException("Invalid Mapping Rule : [" + rule + "]. Only a single character is allowed.");
        if (rhs == null)
            throw new IllegalArgumentException("Invalid Mapping Rule : [" + rule + "]. Illegal type.");
        typeMap.put(lhs.charAt(0), rhs);
    }
    // ensure the table is always at least as big as DEFAULT_WORD_DELIM_TABLE for performance
    byte[] types = new byte[Math.max(typeMap.lastKey() + 1, WordDelimiterIterator.DEFAULT_WORD_DELIM_TABLE.length)];
    for (int i = 0; i < types.length; i++) types[i] = WordDelimiterIterator.getType(i);
    for (Map.Entry<Character, Byte> mapping : typeMap.entrySet()) types[mapping.getKey()] = mapping.getValue();
    return types;
}
Also used : Matcher(java.util.regex.Matcher) TreeMap(java.util.TreeMap) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 57 with SortedMap

use of java.util.SortedMap in project jackrabbit-oak by apache.

the class MongoDocumentStoreHelper method repair.

public static void repair(MongoDocumentStore store, String path) {
    DBCollection col = store.getDBCollection(NODES);
    String id = Utils.getIdFromPath(path);
    NodeDocument doc = store.find(NODES, id);
    if (doc == null) {
        System.out.println("No document for path " + path);
        return;
    }
    Set<Revision> changes = Sets.newHashSet();
    for (String key : doc.keySet()) {
        if (Utils.isPropertyName(key) || NodeDocument.isDeletedEntry(key)) {
            changes.addAll(NodeDocumentHelper.getLocalMap(doc, key).keySet());
        }
    }
    SortedMap<Revision, String> commitRoot = Maps.newTreeMap(NodeDocumentHelper.getLocalCommitRoot(doc));
    if (!commitRoot.keySet().retainAll(changes)) {
        System.out.println("Nothing to repair on " + path);
        return;
    }
    Number modCount = doc.getModCount();
    if (modCount == null) {
        System.err.println("Document does not have a modCount " + path);
        return;
    }
    DBObject query = QueryBuilder.start(Document.ID).is(id).and(Document.MOD_COUNT).is(modCount).get();
    DBObject cr = new BasicDBObject();
    for (Map.Entry<Revision, String> entry : commitRoot.entrySet()) {
        cr.put(entry.getKey().toString(), entry.getValue());
    }
    DBObject update = new BasicDBObject();
    update.put("$set", new BasicDBObject(NodeDocumentHelper.commitRoot(), cr));
    update.put("$inc", new BasicDBObject(Document.MOD_COUNT, 1L));
    WriteResult result = col.update(query, update);
    if (result.getN() == 1) {
        int num = NodeDocumentHelper.getLocalCommitRoot(doc).size() - commitRoot.size();
        System.out.println("Removed " + num + " _commitRoot entries on " + path);
    } else {
        System.out.println("Unable to repair " + path + " (concurrent update).");
    }
}
Also used : NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) WriteResult(com.mongodb.WriteResult) Revision(org.apache.jackrabbit.oak.plugins.document.Revision) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 58 with SortedMap

use of java.util.SortedMap in project logging-log4j2 by apache.

the class DefaultRolloverStrategy method purgeAscending.

/**
     * Purges and renames old log files in preparation for rollover. The oldest file will have the smallest index, the
     * newest the highest.
     *
     * @param lowIndex low index. Log file associated with low index will be deleted if needed.
     * @param highIndex high index.
     * @param manager The RollingFileManager
     * @return true if purge was successful and rollover should be attempted.
     */
private int purgeAscending(final int lowIndex, final int highIndex, final RollingFileManager manager) {
    final SortedMap<Integer, Path> eligibleFiles = getEligibleFiles(manager);
    final int maxFiles = highIndex - lowIndex + 1;
    boolean renameFiles = false;
    while (eligibleFiles.size() >= maxFiles) {
        try {
            LOGGER.debug("Eligible files: {}", eligibleFiles);
            Integer key = eligibleFiles.firstKey();
            LOGGER.debug("Deleting {}", eligibleFiles.get(key).toFile().getAbsolutePath());
            Files.delete(eligibleFiles.get(key));
            eligibleFiles.remove(key);
            renameFiles = true;
        } catch (IOException ioe) {
            LOGGER.error("Unable to delete {}, {}", eligibleFiles.firstKey(), ioe.getMessage(), ioe);
            break;
        }
    }
    final StringBuilder buf = new StringBuilder();
    if (renameFiles) {
        for (Map.Entry<Integer, Path> entry : eligibleFiles.entrySet()) {
            buf.setLength(0);
            // LOG4J2-531: directory scan & rollover must use same format
            manager.getPatternProcessor().formatFileName(strSubstitutor, buf, entry.getKey() - 1);
            String currentName = entry.getValue().toFile().getName();
            String renameTo = buf.toString();
            int suffixLength = suffixLength(renameTo);
            if (suffixLength > 0 && suffixLength(currentName) == 0) {
                renameTo = renameTo.substring(0, renameTo.length() - suffixLength);
            }
            Action action = new FileRenameAction(entry.getValue().toFile(), new File(renameTo), true);
            try {
                LOGGER.debug("DefaultRolloverStrategy.purgeAscending executing {}", action);
                if (!action.execute()) {
                    return -1;
                }
            } catch (final Exception ex) {
                LOGGER.warn("Exception during purge in RollingFileAppender", ex);
                return -1;
            }
        }
    }
    return eligibleFiles.size() > 0 ? (eligibleFiles.lastKey() < highIndex ? eligibleFiles.lastKey() + 1 : highIndex) : lowIndex;
}
Also used : Path(java.nio.file.Path) FileRenameAction(org.apache.logging.log4j.core.appender.rolling.action.FileRenameAction) Action(org.apache.logging.log4j.core.appender.rolling.action.Action) IOException(java.io.IOException) IOException(java.io.IOException) FileRenameAction(org.apache.logging.log4j.core.appender.rolling.action.FileRenameAction) Map(java.util.Map) SortedMap(java.util.SortedMap) File(java.io.File)

Example 59 with SortedMap

use of java.util.SortedMap in project logging-log4j2 by apache.

the class DefaultRolloverStrategy method purgeDescending.

/**
     * Purges and renames old log files in preparation for rollover. The newest file will have the smallest index, the
     * oldest will have the highest.
     *
     * @param lowIndex low index
     * @param highIndex high index. Log file associated with high index will be deleted if needed.
     * @param manager The RollingFileManager
     * @return true if purge was successful and rollover should be attempted.
     */
private int purgeDescending(final int lowIndex, final int highIndex, final RollingFileManager manager) {
    // Retrieve the files in descending order, so the highest key will be first.
    final SortedMap<Integer, Path> eligibleFiles = getEligibleFiles(manager, false);
    final int maxFiles = highIndex - lowIndex + 1;
    while (eligibleFiles.size() >= maxFiles) {
        try {
            Integer key = eligibleFiles.firstKey();
            Files.delete(eligibleFiles.get(key));
            eligibleFiles.remove(key);
        } catch (IOException ioe) {
            LOGGER.error("Unable to delete {}, {}", eligibleFiles.firstKey(), ioe.getMessage(), ioe);
            break;
        }
    }
    final StringBuilder buf = new StringBuilder();
    for (Map.Entry<Integer, Path> entry : eligibleFiles.entrySet()) {
        buf.setLength(0);
        // LOG4J2-531: directory scan & rollover must use same format
        manager.getPatternProcessor().formatFileName(strSubstitutor, buf, entry.getKey() + 1);
        String currentName = entry.getValue().toFile().getName();
        String renameTo = buf.toString();
        int suffixLength = suffixLength(renameTo);
        if (suffixLength > 0 && suffixLength(currentName) == 0) {
            renameTo = renameTo.substring(0, renameTo.length() - suffixLength);
        }
        Action action = new FileRenameAction(entry.getValue().toFile(), new File(renameTo), true);
        try {
            LOGGER.debug("DefaultRolloverStrategy.purgeDescending executing {}", action);
            if (!action.execute()) {
                return -1;
            }
        } catch (final Exception ex) {
            LOGGER.warn("Exception during purge in RollingFileAppender", ex);
            return -1;
        }
    }
    return lowIndex;
}
Also used : Path(java.nio.file.Path) FileRenameAction(org.apache.logging.log4j.core.appender.rolling.action.FileRenameAction) Action(org.apache.logging.log4j.core.appender.rolling.action.Action) IOException(java.io.IOException) IOException(java.io.IOException) FileRenameAction(org.apache.logging.log4j.core.appender.rolling.action.FileRenameAction) Map(java.util.Map) SortedMap(java.util.SortedMap) File(java.io.File)

Example 60 with SortedMap

use of java.util.SortedMap in project geode by apache.

the class OpenTypeConverter method makeCompositeConverter.

/**
   * @return the open type converrter for a given type
   */
private static OpenTypeConverter makeCompositeConverter(Class c) throws OpenDataException {
    final List<Method> methods = Arrays.asList(c.getMethods());
    final SortedMap<String, Method> getterMap = OpenTypeUtil.newSortedMap();
    for (Method method : methods) {
        final String propertyName = propertyName(method);
        if (propertyName == null)
            continue;
        Method old = getterMap.put(OpenTypeUtil.decapitalize(propertyName), method);
        if (old != null) {
            final String msg = "Class " + c.getName() + " has method name clash: " + old.getName() + ", " + method.getName();
            throw new OpenDataException(msg);
        }
    }
    final int nitems = getterMap.size();
    if (nitems == 0) {
        throw new OpenDataException("Can't map " + c.getName() + " to an open data type");
    }
    final Method[] getters = new Method[nitems];
    final String[] itemNames = new String[nitems];
    final OpenType[] openTypes = new OpenType[nitems];
    int i = 0;
    for (Map.Entry<String, Method> entry : getterMap.entrySet()) {
        itemNames[i] = entry.getKey();
        final Method getter = entry.getValue();
        getters[i] = getter;
        final Type retType = getter.getGenericReturnType();
        openTypes[i] = toConverter(retType).getOpenType();
        i++;
    }
    CompositeType compositeType = new // field
    CompositeType(// field
    c.getName(), // field
    c.getName(), // field
    itemNames, // field descriptions
    itemNames, openTypes);
    return new CompositeConverter(c, compositeType, itemNames, getters);
}
Also used : OpenType(javax.management.openmbean.OpenType) Method(java.lang.reflect.Method) GenericArrayType(java.lang.reflect.GenericArrayType) ArrayType(javax.management.openmbean.ArrayType) CompositeType(javax.management.openmbean.CompositeType) OpenType(javax.management.openmbean.OpenType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TabularType(javax.management.openmbean.TabularType) OpenDataException(javax.management.openmbean.OpenDataException) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap) SortedMap(java.util.SortedMap) CompositeType(javax.management.openmbean.CompositeType)

Aggregations

SortedMap (java.util.SortedMap)228 Map (java.util.Map)174 TreeMap (java.util.TreeMap)115 HashMap (java.util.HashMap)66 NavigableMap (java.util.NavigableMap)33 ArrayList (java.util.ArrayList)31 Iterator (java.util.Iterator)31 Test (org.junit.Test)25 ImmutableMap (com.google.common.collect.ImmutableMap)21 IOException (java.io.IOException)20 List (java.util.List)17 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)17 JASIExpr (org.matheclipse.core.convert.JASIExpr)16 IExpr (org.matheclipse.core.interfaces.IExpr)16 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)14 File (java.io.File)14 Entry (java.util.Map.Entry)13 ConcurrentMap (java.util.concurrent.ConcurrentMap)12 ConcurrentNavigableMap (java.util.concurrent.ConcurrentNavigableMap)12 LinkedHashMap (java.util.LinkedHashMap)11