Search in sources :

Example 6 with MultiMap

use of org.apache.commons.collections.MultiMap in project tdq-studio-se by Talend.

the class EventManager method findRegisteredEvent.

/**
 * find if there are some registered event for the context, if existed, return the index position in the event list.
 *
 * @param context
 * @param event
 * @return
 */
public IEventReceiver findRegisteredEvent(Object context, EventEnum event, int index) {
    MultiMap receverQueryMap = ctxToReceiverQueueMap.get(context);
    if (receverQueryMap == null || receverQueryMap.isEmpty()) {
        return null;
    }
    List<IEventReceiver> receivers = (List<IEventReceiver>) receverQueryMap.get(event);
    if (receivers == null || receivers.size() == 0 || receivers.size() < index) {
        return null;
    }
    return receivers.get(index);
}
Also used : MultiMap(org.apache.commons.collections.MultiMap) List(java.util.List)

Example 7 with MultiMap

use of org.apache.commons.collections.MultiMap in project zm-mailbox by Zimbra.

the class BlobDeduper method deDupe.

private Pair<Integer, Long> deDupe(List<BlobReference> blobs) throws ServiceException {
    int linksCreated = 0;
    long sizeSaved = 0;
    long srcInodeNum = 0;
    String srcPath = null;
    // check if there is any processed blob
    for (BlobReference blob : blobs) {
        if (blob.isProcessed()) {
            String path = FileBlobStore.getBlobPath(blob.getMailboxId(), blob.getItemId(), blob.getRevision(), blob.getVolumeId());
            try {
                IO.FileInfo fileInfo = IO.fileInfo(path);
                if (fileInfo != null) {
                    srcInodeNum = fileInfo.getInodeNum();
                    srcPath = path;
                    break;
                }
            } catch (IOException e) {
            // ignore
            }
        }
    }
    if (srcInodeNum == 0) {
        // check the path with maximum links
        // organize the paths based on inode
        MultiMap inodeMap = new MultiValueMap();
        for (BlobReference blob : blobs) {
            String path = FileBlobStore.getBlobPath(blob.getMailboxId(), blob.getItemId(), blob.getRevision(), blob.getVolumeId());
            try {
                IO.FileInfo fileInfo = IO.fileInfo(path);
                if (fileInfo != null) {
                    inodeMap.put(fileInfo.getInodeNum(), path);
                    blob.setFileInfo(fileInfo);
                }
            } catch (IOException e) {
            // ignore
            }
        }
        // find inode which has maximum paths
        int maxPaths = 0;
        @SuppressWarnings("unchecked") Iterator<Map.Entry<Long, Collection<String>>> iter = inodeMap.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry<Long, Collection<String>> entry = iter.next();
            if (entry.getValue().size() > maxPaths) {
                maxPaths = entry.getValue().size();
                srcInodeNum = entry.getKey();
                srcPath = entry.getValue().iterator().next();
            }
        }
    }
    if (srcInodeNum == 0) {
        return new Pair<Integer, Long>(0, Long.valueOf(0));
    }
    // First create a hard link for the source path, so that the file
    // doesn't get deleted in the middle.
    String holdPath = srcPath + "_HOLD";
    File holdFile = new File(holdPath);
    try {
        IO.link(srcPath, holdPath);
        // Now link the other paths to source path
        for (BlobReference blob : blobs) {
            if (blob.isProcessed()) {
                continue;
            }
            String path = FileBlobStore.getBlobPath(blob.getMailboxId(), blob.getItemId(), blob.getRevision(), blob.getVolumeId());
            try {
                if (blob.getFileInfo() == null) {
                    blob.setFileInfo(IO.fileInfo(path));
                }
            } catch (IOException e) {
            // ignore
            }
            if (blob.getFileInfo() == null) {
                continue;
            }
            if (srcInodeNum == blob.getFileInfo().getInodeNum()) {
                markBlobAsProcessed(blob);
                continue;
            }
            // create the links for paths in two steps.
            // first create a temp link and then rename it to actual path
            // this guarantees that the file is always available.
            String tempPath = path + "_TEMP";
            File tempFile = new File(tempPath);
            try {
                IO.link(holdPath, tempPath);
                File destFile = new File(path);
                tempFile.renameTo(destFile);
                markBlobAsProcessed(blob);
                linksCreated++;
                sizeSaved += blob.getFileInfo().getSize();
            } catch (IOException e) {
                ZimbraLog.misc.warn("Ignoring the error while deduping " + path, e);
            } finally {
                if (tempFile.exists()) {
                    tempFile.delete();
                }
            }
        }
    } catch (IOException e) {
        ZimbraLog.misc.warn("Ignoring the error while creating a link for " + srcPath, e);
    } finally {
        // delete the hold file
        if (holdFile.exists()) {
            holdFile.delete();
        }
    }
    return new Pair<Integer, Long>(linksCreated, sizeSaved);
}
Also used : IO(com.zimbra.znative.IO) IOException(java.io.IOException) MultiMap(org.apache.commons.collections.MultiMap) Collection(java.util.Collection) LinkedHashMap(java.util.LinkedHashMap) MultiValueMap(org.apache.commons.collections.map.MultiValueMap) Map(java.util.Map) MultiMap(org.apache.commons.collections.MultiMap) File(java.io.File) MultiValueMap(org.apache.commons.collections.map.MultiValueMap) Pair(com.zimbra.common.util.Pair)

Aggregations

MultiMap (org.apache.commons.collections.MultiMap)7 MultiValueMap (org.apache.commons.collections.map.MultiValueMap)3 List (java.util.List)2 Map (java.util.Map)2 Pair (com.zimbra.common.util.Pair)1 IO (com.zimbra.znative.IO)1 File (java.io.File)1 IOException (java.io.IOException)1 AbstractMap (java.util.AbstractMap)1 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1