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;
}
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).");
}
}
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;
}
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;
}
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);
}
Aggregations