Example usage for java.util TreeSet isEmpty

List of usage examples for java.util TreeSet isEmpty

Introduction

In this page you can find the example usage for java.util TreeSet isEmpty.

Prototype

public boolean isEmpty() 

Source Link

Document

Returns true if this set contains no elements.

Usage

From source file:com.eucalyptus.objectstorage.WalrusManager.java

public ListBucketResponseType listBucket(ListBucketType request) throws EucalyptusCloudException {
    ListBucketResponseType reply = (ListBucketResponseType) request.getReply();

    EntityWrapper<BucketInfo> db = EntityWrapper.get(BucketInfo.class);

    try {/*from   w  w w .  j a v a  2  s  .co  m*/
        String bucketName = request.getBucket();
        BucketInfo bucketInfo = new BucketInfo(bucketName);
        bucketInfo.setHidden(false);
        List<BucketInfo> bucketList = db.queryEscape(bucketInfo);

        Context ctx = Contexts.lookup();
        Account account = ctx.getAccount();

        int maxKeys = -1;
        String maxKeysString = request.getMaxKeys();
        if (maxKeysString != null) {
            maxKeys = Integer.parseInt(maxKeysString);
            if (maxKeys < 0) {
                throw new InvalidArgumentException("max-keys",
                        "Argument max-keys must be an integer between 0 and " + Integer.MAX_VALUE);
            }
        } else {
            maxKeys = WalrusProperties.MAX_KEYS;
        }

        if (bucketList.size() > 0) {
            BucketInfo bucket = bucketList.get(0);
            BucketLogData logData = bucket.getLoggingEnabled() ? request.getLogData() : null;
            if (ctx.hasAdministrativePrivileges() || (bucket.canRead(account.getAccountNumber())
                    && (bucket.isGlobalRead() || Lookups.checkPrivilege(PolicySpec.S3_LISTBUCKET,
                            PolicySpec.VENDOR_S3, PolicySpec.S3_RESOURCE_BUCKET, bucketName, null)))) {
                if (logData != null) {
                    updateLogData(bucket, logData);
                    reply.setLogData(logData);
                }

                if (Contexts.lookup().hasAdministrativePrivileges()) {
                    try {
                        if (bucketHasSnapshots(bucketName)) {
                            db.rollback();
                            throw new NoSuchBucketException(bucketName);
                        }
                    } catch (Exception e) {
                        db.rollback();
                        throw new EucalyptusCloudException(e);
                    }
                }

                String prefix = request.getPrefix();
                String delimiter = request.getDelimiter();
                String marker = request.getMarker();

                reply.setName(bucketName);
                reply.setIsTruncated(false);
                reply.setPrefix(prefix);
                reply.setMarker(marker);
                reply.setDelimiter(delimiter);
                reply.setMaxKeys(maxKeys);

                if (maxKeys == 0) {
                    // No keys requested, so just return
                    reply.setContents(new ArrayList<ListEntry>());
                    reply.setCommonPrefixesList(new ArrayList<CommonPrefixesEntry>());
                    db.commit();
                    return reply;
                }

                final int queryStrideSize = maxKeys + 1;
                EntityWrapper<ObjectInfo> dbObject = db.recast(ObjectInfo.class);

                ObjectInfo searchObj = new ObjectInfo();
                searchObj.setBucketName(bucketName);
                searchObj.setLast(true);
                searchObj.setDeleted(false);

                Criteria objCriteria = dbObject.createCriteria(ObjectInfo.class);
                objCriteria.add(Example.create(searchObj));
                objCriteria.addOrder(Order.asc("objectKey"));
                objCriteria.setMaxResults(queryStrideSize); // add one to, hopefully, indicate truncation in one call

                if (!Strings.isNullOrEmpty(marker)) {
                    // The result set should be exclusive of the marker. marker could be a common prefix from a previous response. Look for keys that
                    // lexicographically follow the marker and don't contain the marker as the prefix.
                    objCriteria.add(Restrictions.gt("objectKey", marker));
                } else {
                    marker = "";
                }

                if (!Strings.isNullOrEmpty(prefix)) {
                    objCriteria.add(Restrictions.like("objectKey", prefix, MatchMode.START));
                } else {
                    prefix = "";
                }

                // Ensure not null.
                if (Strings.isNullOrEmpty(delimiter)) {
                    delimiter = "";
                }

                List<ObjectInfo> objectInfos = null;
                int resultKeyCount = 0;
                ArrayList<ListEntry> contents = new ArrayList<ListEntry>(); // contents for reply
                String nextMarker = null;
                TreeSet<String> commonPrefixes = new TreeSet<String>();
                int firstResult = -1;

                // Iterate over result sets of size maxkeys + 1
                do {
                    // Start listing from the 0th element and increment the first element to be listed by the query size
                    objCriteria.setFirstResult(queryStrideSize * (++firstResult));
                    objectInfos = (List<ObjectInfo>) objCriteria.list();

                    if (objectInfos.size() > 0) {
                        for (ObjectInfo objectInfo : objectInfos) {
                            String objectKey = objectInfo.getObjectKey();

                            // Check if it will get aggregated as a commonprefix
                            if (!Strings.isNullOrEmpty(delimiter)) {
                                String[] parts = objectKey.substring(prefix.length()).split(delimiter);
                                if (parts.length > 1) {
                                    String prefixString = prefix + parts[0] + delimiter;
                                    if (!StringUtils.equals(prefixString, marker)
                                            && !commonPrefixes.contains(prefixString)) {
                                        if (resultKeyCount == maxKeys) {
                                            // This is a new record, so we know we're truncating if this is true
                                            reply.setNextMarker(nextMarker);
                                            reply.setIsTruncated(true);
                                            resultKeyCount++;
                                            break;
                                        }

                                        commonPrefixes.add(prefixString);
                                        resultKeyCount++; // count the unique commonprefix as a single return entry

                                        // If max keys have been collected, set the next-marker. It might be needed for the response if the list is
                                        // truncated
                                        // If the common prefixes hit the limit set by max-keys, next-marker is the last common prefix
                                        if (resultKeyCount == maxKeys) {
                                            nextMarker = prefixString;
                                        }
                                    }
                                    continue;
                                }
                            }

                            if (resultKeyCount == maxKeys) {
                                // This is a new (non-commonprefix) record, so we know we're truncating
                                reply.setNextMarker(nextMarker);
                                reply.setIsTruncated(true);
                                resultKeyCount++;
                                break;
                            }

                            // Process the entry as a full key listing
                            ListEntry listEntry = new ListEntry();
                            listEntry.setKey(objectKey);
                            listEntry.setEtag(objectInfo.getEtag());
                            listEntry.setLastModified(DateUtils.format(objectInfo.getLastModified().getTime(),
                                    DateUtils.ALT_ISO8601_DATE_PATTERN));
                            listEntry.setStorageClass(objectInfo.getStorageClass());
                            listEntry.setSize(objectInfo.getSize());
                            listEntry.setStorageClass(objectInfo.getStorageClass());
                            try {
                                Account ownerAccount = Accounts.lookupAccountById(objectInfo.getOwnerId());
                                listEntry.setOwner(new CanonicalUserType(ownerAccount.getCanonicalId(),
                                        ownerAccount.getName()));
                            } catch (AuthException e) {
                                db.rollback();
                                throw new AccessDeniedException("Bucket", bucketName, logData);
                            }
                            contents.add(listEntry);

                            resultKeyCount++;

                            // If max keys have been collected, set the next-marker. It might be needed for the response if the list is truncated
                            if (resultKeyCount == maxKeys) {
                                nextMarker = objectKey;
                            }
                        }
                    }

                    if (resultKeyCount <= maxKeys && objectInfos.size() <= maxKeys) {
                        break;
                    }
                } while (resultKeyCount <= maxKeys);

                reply.setContents(contents);

                // Prefixes are already sorted, add them to the proper data structures and populate the reply
                if (!commonPrefixes.isEmpty()) {
                    ArrayList<CommonPrefixesEntry> commonPrefixesList = new ArrayList<CommonPrefixesEntry>();
                    for (String prefixEntry : commonPrefixes) {
                        commonPrefixesList.add(new CommonPrefixesEntry().add(new PrefixEntry(prefixEntry)));
                    }
                    reply.setCommonPrefixesList(commonPrefixesList);
                }
            } else {
                db.rollback();
                throw new AccessDeniedException("Bucket", bucketName, logData);
            }
        } else {
            db.rollback();
            throw new NoSuchBucketException(bucketName);
        }
        db.commit();
        return reply;
    } finally {
        if (db.isActive()) {
            db.rollback();
        }
    }
}

From source file:com.eucalyptus.walrus.WalrusFSManager.java

@Override
public ListVersionsResponseType listVersions(ListVersionsType request) throws WalrusException {
    ListVersionsResponseType reply = (ListVersionsResponseType) request.getReply();
    EntityWrapper<BucketInfo> db = EntityWrapper.get(BucketInfo.class);

    try {/*from  w  w w. j  a  v a  2  s .c  om*/
        String bucketName = request.getBucket();
        BucketInfo bucketInfo = new BucketInfo(bucketName);
        bucketInfo.setHidden(false);
        List<BucketInfo> bucketList = db.queryEscape(bucketInfo);

        Context ctx = Contexts.lookup();
        Account account = ctx.getAccount();

        int maxKeys = -1;
        String maxKeysString = request.getMaxKeys();
        if (maxKeysString != null) {
            maxKeys = Integer.parseInt(maxKeysString);
            if (maxKeys < 0) {
                throw new InvalidArgumentException("max-keys",
                        "Argument max-keys must be an integer between 0 and " + Integer.MAX_VALUE);
            }
        } else {
            maxKeys = WalrusProperties.MAX_KEYS;
        }

        if (bucketList.size() > 0) {
            BucketInfo bucket = bucketList.get(0);
            BucketLogData logData = bucket.getLoggingEnabled() ? request.getLogData() : null;

            if (logData != null) {
                updateLogData(bucket, logData);
                reply.setLogData(logData);
            }

            String prefix = request.getPrefix();
            String keyMarker = request.getKeyMarker();
            String versionMarker = request.getVersionIdMarker();
            String delimiter = request.getDelimiter();

            reply.setName(bucketName);
            reply.setIsTruncated(false);
            reply.setPrefix(prefix);
            reply.setMaxKeys(maxKeys);
            reply.setDelimiter(delimiter);
            reply.setKeyMarker(keyMarker);
            reply.setVersionIdMarker(versionMarker);

            if (bucket.isVersioningDisabled()) {
                db.commit();
                return reply;
            }

            if (maxKeys == 0) {
                // No keys requested, so just return
                reply.setKeyEntries(new ArrayList<KeyEntry>());
                reply.setCommonPrefixesList(new ArrayList<CommonPrefixesEntry>());
                db.commit();
                return reply;
            }

            final int queryStrideSize = maxKeys + 1;
            EntityWrapper<ObjectInfo> dbObject = db.recast(ObjectInfo.class);

            ObjectInfo searchObj = new ObjectInfo();
            searchObj.setBucketName(bucketName);

            Criteria objCriteria = dbObject.createCriteria(ObjectInfo.class);
            objCriteria.add(Example.create(searchObj));
            objCriteria.addOrder(Order.asc("objectKey"));
            objCriteria.addOrder(Order.desc("lastModified"));
            objCriteria.setMaxResults(queryStrideSize); // add one to, hopefully, indicate truncation in one call

            // Ensure these aren't null
            keyMarker = (Strings.isNullOrEmpty(keyMarker) ? "" : keyMarker);
            prefix = (Strings.isNullOrEmpty(prefix) ? "" : prefix);
            versionMarker = (Strings.isNullOrEmpty(versionMarker) ? "" : versionMarker);

            if (!Strings.isNullOrEmpty(keyMarker)) {
                if (!Strings.isNullOrEmpty(versionMarker)) {
                    Date resumeDate = null;
                    try {
                        ObjectInfo markerObj = new ObjectInfo();
                        markerObj.setBucketName(bucketName);
                        markerObj.setVersionId(versionMarker);
                        markerObj.setObjectKey(keyMarker);
                        ObjectInfo lastFromPrevObj = dbObject.uniqueResultEscape(markerObj);
                        if (lastFromPrevObj != null && lastFromPrevObj.getLastModified() != null) {
                            resumeDate = lastFromPrevObj.getLastModified();
                        } else {
                            dbObject.rollback();
                            throw new NoSuchEntityException("VersionIDMarker " + versionMarker
                                    + " does not match an existing object version");
                        }
                    } catch (TransactionException e) {
                        LOG.error(e);
                        dbObject.rollback();
                        throw new InternalErrorException("Next-Key-Marker or Next-Version-Id marker invalid");
                    }
                    // The result set should be exclusive of the key with the key-marker version-id-marker pair. Look for keys that lexicographically
                    // follow the version-id-marker for a given key-marker and also the keys that follow the key-marker.
                    objCriteria.add(Restrictions.or(
                            Restrictions.and(Restrictions.eq("objectKey", keyMarker),
                                    Restrictions.lt("lastModified", resumeDate)),
                            Restrictions.gt("objectKey", keyMarker)));
                } else {
                    // The result set should be exclusive of the key-marker. key-marker could be a common prefix from a previous response. Look for keys
                    // that lexicographically follow the key-marker and don't contain the key-marker as the prefix.
                    objCriteria.add(Restrictions.gt("objectKey", keyMarker));
                }
            }

            if (!Strings.isNullOrEmpty(prefix)) {
                objCriteria.add(Restrictions.like("objectKey", prefix, MatchMode.START));
            } else {
                prefix = ""; // ensure not null has already been set in the reply, so this is safe
            }

            List<ObjectInfo> objectInfos = null;
            int resultKeyCount = 0;
            ArrayList<KeyEntry> keyEntries = new ArrayList<KeyEntry>();
            String nextKeyMarker = null;
            String nextVersionIdMarker = null;
            TreeSet<String> commonPrefixes = new TreeSet<String>();
            int firstResult = -1;

            // Iterate over result sets of size maxkeys + 1
            do {
                // Start listing from the 0th element and increment the first element to be listed by the query size
                objCriteria.setFirstResult(queryStrideSize * (++firstResult));
                objectInfos = (List<ObjectInfo>) objCriteria.list();

                if (objectInfos.size() > 0) {

                    for (ObjectInfo objectInfo : objectInfos) {
                        String objectKey = objectInfo.getObjectKey();

                        // Check if it will get aggregated as a commonprefix
                        if (!Strings.isNullOrEmpty(delimiter)) {
                            String[] parts = objectKey.substring(prefix.length()).split(delimiter);
                            if (parts.length > 1) {
                                String prefixString = prefix + parts[0] + delimiter;
                                if (!StringUtils.equals(prefixString, keyMarker)
                                        && !commonPrefixes.contains(prefixString)) {
                                    if (resultKeyCount == maxKeys) {
                                        // This is a new record, so we know we're truncating if this is true
                                        reply.setNextKeyMarker(nextKeyMarker);
                                        reply.setNextVersionIdMarker(nextVersionIdMarker);
                                        reply.setIsTruncated(true);
                                        resultKeyCount++;
                                        break;
                                    }

                                    commonPrefixes.add(prefixString);
                                    resultKeyCount++; // count the unique commonprefix as a single return entry

                                    // If max keys have been collected, set the next-key-marker. It might be needed for the response if the list is
                                    // truncated
                                    // If the common prefixes hit the limit set by max-keys, next-key-marker is the last common prefix and there is no
                                    // version-id-marker
                                    if (resultKeyCount == maxKeys) {
                                        nextKeyMarker = prefixString;
                                        nextVersionIdMarker = null;
                                    }
                                }
                                continue;
                            }
                        }

                        if (resultKeyCount == maxKeys) {
                            // This is a new (non-commonprefix) record, so we know we're truncating
                            reply.setNextKeyMarker(nextKeyMarker);
                            reply.setNextVersionIdMarker(nextVersionIdMarker);
                            reply.setIsTruncated(true);
                            resultKeyCount++;
                            break;
                        }

                        // This is either a version entry or a delete marker
                        KeyEntry keyEntry = null;
                        if (!objectInfo.getDeleted()) {
                            keyEntry = new VersionEntry();
                            ((VersionEntry) keyEntry).setEtag(objectInfo.getEtag());
                            ((VersionEntry) keyEntry).setSize(objectInfo.getSize());
                            ((VersionEntry) keyEntry).setStorageClass(objectInfo.getStorageClass());
                        } else {
                            keyEntry = new DeleteMarkerEntry();
                        }
                        keyEntry.setKey(objectKey);
                        keyEntry.setVersionId(objectInfo.getVersionId());
                        keyEntry.setIsLatest(objectInfo.getLast());
                        keyEntry.setLastModified(
                                DateFormatter.dateToListingFormattedString(objectInfo.getLastModified()));
                        try {
                            Account ownerAccount = Accounts.lookupAccountById(objectInfo.getOwnerId());
                            keyEntry.setOwner(
                                    new CanonicalUser(ownerAccount.getCanonicalId(), ownerAccount.getName()));
                        } catch (AuthException e) {
                            db.rollback();
                            throw new AccessDeniedException("Bucket", bucketName, logData);
                        }
                        keyEntries.add(keyEntry);

                        resultKeyCount++;

                        // If max keys have been collected, set the next- markers. They might be needed for the response if the list is truncated
                        if (resultKeyCount == maxKeys) {
                            nextKeyMarker = objectKey;
                            nextVersionIdMarker = objectInfo.getVersionId();
                        }
                    }
                }
                if (resultKeyCount <= maxKeys && objectInfos.size() <= maxKeys) {
                    break;
                }
            } while (resultKeyCount <= maxKeys);

            reply.setKeyEntries(keyEntries);

            // Prefixes are already sorted, add them to the proper data structures and populate the reply
            if (!commonPrefixes.isEmpty()) {
                ArrayList<CommonPrefixesEntry> commonPrefixesList = new ArrayList<CommonPrefixesEntry>();
                for (String prefixEntry : commonPrefixes) {
                    commonPrefixesList.add(new CommonPrefixesEntry(prefixEntry));
                }
                reply.setCommonPrefixesList(commonPrefixesList);
            }
        } else {
            db.rollback();
            throw new NoSuchBucketException(bucketName);
        }
        db.commit();
        return reply;
    } finally {
        if (db.isActive()) {
            db.rollback();
        }
    }
}

From source file:com.eucalyptus.objectstorage.WalrusManager.java

public ListVersionsResponseType listVersions(ListVersionsType request) throws EucalyptusCloudException {
    ListVersionsResponseType reply = (ListVersionsResponseType) request.getReply();

    EntityWrapper<BucketInfo> db = EntityWrapper.get(BucketInfo.class);

    try {/*from   w  w  w  .j  a va 2s.c om*/
        String bucketName = request.getBucket();
        BucketInfo bucketInfo = new BucketInfo(bucketName);
        bucketInfo.setHidden(false);
        List<BucketInfo> bucketList = db.queryEscape(bucketInfo);

        Context ctx = Contexts.lookup();
        Account account = ctx.getAccount();

        int maxKeys = -1;
        String maxKeysString = request.getMaxKeys();
        if (maxKeysString != null) {
            maxKeys = Integer.parseInt(maxKeysString);
            if (maxKeys < 0) {
                throw new InvalidArgumentException("max-keys",
                        "Argument max-keys must be an integer between 0 and " + Integer.MAX_VALUE);
            }
        } else {
            maxKeys = WalrusProperties.MAX_KEYS;
        }

        if (bucketList.size() > 0) {
            BucketInfo bucket = bucketList.get(0);
            BucketLogData logData = bucket.getLoggingEnabled() ? request.getLogData() : null;

            if (ctx.hasAdministrativePrivileges() || (bucket.canRead(account.getAccountNumber())
                    && (bucket.isGlobalRead() || Lookups.checkPrivilege(PolicySpec.S3_LISTBUCKETVERSIONS,
                            PolicySpec.VENDOR_S3, PolicySpec.S3_RESOURCE_BUCKET, bucketName, null)))) {

                if (logData != null) {
                    updateLogData(bucket, logData);
                    reply.setLogData(logData);
                }

                if (Contexts.lookup().hasAdministrativePrivileges()) {
                    try {
                        if (bucketHasSnapshots(bucketName)) {
                            db.rollback();
                            throw new NoSuchBucketException(bucketName);
                        }
                    } catch (Exception e) {
                        db.rollback();
                        throw new EucalyptusCloudException(e);
                    }
                }

                String prefix = request.getPrefix();
                String keyMarker = request.getKeyMarker();
                String versionMarker = request.getVersionIdMarker();
                String delimiter = request.getDelimiter();

                reply.setName(bucketName);
                reply.setIsTruncated(false);
                reply.setPrefix(prefix);
                reply.setMaxKeys(maxKeys);
                reply.setDelimiter(delimiter);
                reply.setKeyMarker(keyMarker);
                reply.setVersionIdMarker(versionMarker);

                if (bucket.isVersioningDisabled()) {
                    db.commit();
                    return reply;
                }

                if (maxKeys == 0) {
                    // No keys requested, so just return
                    reply.setKeyEntries(new ArrayList<KeyEntry>());
                    reply.setCommonPrefixesList(new ArrayList<CommonPrefixesEntry>());
                    db.commit();
                    return reply;
                }

                final int queryStrideSize = maxKeys + 1;
                EntityWrapper<ObjectInfo> dbObject = db.recast(ObjectInfo.class);

                ObjectInfo searchObj = new ObjectInfo();
                searchObj.setBucketName(bucketName);

                Criteria objCriteria = dbObject.createCriteria(ObjectInfo.class);
                objCriteria.add(Example.create(searchObj));
                objCriteria.addOrder(Order.asc("objectKey"));
                objCriteria.addOrder(Order.desc("lastModified"));
                objCriteria.setMaxResults(queryStrideSize); // add one to, hopefully, indicate truncation in one call

                // Ensure these aren't null
                keyMarker = (Strings.isNullOrEmpty(keyMarker) ? "" : keyMarker);
                prefix = (Strings.isNullOrEmpty(prefix) ? "" : prefix);
                versionMarker = (Strings.isNullOrEmpty(versionMarker) ? "" : versionMarker);

                if (!Strings.isNullOrEmpty(keyMarker)) {
                    if (!Strings.isNullOrEmpty(versionMarker)) {
                        Date resumeDate = null;
                        try {
                            ObjectInfo markerObj = new ObjectInfo();
                            markerObj.setBucketName(bucketName);
                            markerObj.setVersionId(versionMarker);
                            markerObj.setObjectKey(keyMarker);
                            ObjectInfo lastFromPrevObj = dbObject.uniqueResultEscape(markerObj);
                            if (lastFromPrevObj != null && lastFromPrevObj.getLastModified() != null) {
                                resumeDate = lastFromPrevObj.getLastModified();
                            } else {
                                dbObject.rollback();
                                throw new NoSuchEntityException("VersionIDMarker " + versionMarker
                                        + " does not match an existing object version");
                            }
                        } catch (TransactionException e) {
                            LOG.error(e);
                            dbObject.rollback();
                            throw new EucalyptusCloudException(
                                    "Next-Key-Marker or Next-Version-Id marker invalid");
                        }
                        // The result set should be exclusive of the key with the key-marker version-id-marker pair. Look for keys that lexicographically
                        // follow the version-id-marker for a given key-marker and also the keys that follow the key-marker.
                        objCriteria.add(Restrictions.or(
                                Restrictions.and(Restrictions.eq("objectKey", keyMarker),
                                        Restrictions.lt("lastModified", resumeDate)),
                                Restrictions.gt("objectKey", keyMarker)));
                    } else {
                        // The result set should be exclusive of the key-marker. key-marker could be a common prefix from a previous response. Look for keys
                        // that lexicographically follow the key-marker and don't contain the key-marker as the prefix.
                        objCriteria.add(Restrictions.gt("objectKey", keyMarker));
                    }
                }

                if (!Strings.isNullOrEmpty(prefix)) {
                    objCriteria.add(Restrictions.like("objectKey", prefix, MatchMode.START));
                } else {
                    prefix = ""; // ensure not null has already been set in the reply, so this is safe
                }

                List<ObjectInfo> objectInfos = null;
                int resultKeyCount = 0;
                ArrayList<KeyEntry> keyEntries = new ArrayList<KeyEntry>();
                String nextKeyMarker = null;
                String nextVersionIdMarker = null;
                TreeSet<String> commonPrefixes = new TreeSet<String>();
                int firstResult = -1;

                // Iterate over result sets of size maxkeys + 1
                do {
                    // Start listing from the 0th element and increment the first element to be listed by the query size
                    objCriteria.setFirstResult(queryStrideSize * (++firstResult));
                    objectInfos = (List<ObjectInfo>) objCriteria.list();

                    if (objectInfos.size() > 0) {

                        for (ObjectInfo objectInfo : objectInfos) {
                            String objectKey = objectInfo.getObjectKey();

                            // Check if it will get aggregated as a commonprefix
                            if (!Strings.isNullOrEmpty(delimiter)) {
                                String[] parts = objectKey.substring(prefix.length()).split(delimiter);
                                if (parts.length > 1) {
                                    String prefixString = prefix + parts[0] + delimiter;
                                    if (!StringUtils.equals(prefixString, keyMarker)
                                            && !commonPrefixes.contains(prefixString)) {
                                        if (resultKeyCount == maxKeys) {
                                            // This is a new record, so we know we're truncating if this is true
                                            reply.setNextKeyMarker(nextKeyMarker);
                                            reply.setNextVersionIdMarker(nextVersionIdMarker);
                                            reply.setIsTruncated(true);
                                            resultKeyCount++;
                                            break;
                                        }

                                        commonPrefixes.add(prefixString);
                                        resultKeyCount++; // count the unique commonprefix as a single return entry

                                        // If max keys have been collected, set the next-key-marker. It might be needed for the response if the list is
                                        // truncated
                                        // If the common prefixes hit the limit set by max-keys, next-key-marker is the last common prefix and there is no
                                        // version-id-marker
                                        if (resultKeyCount == maxKeys) {
                                            nextKeyMarker = prefixString;
                                            nextVersionIdMarker = null;
                                        }
                                    }
                                    continue;
                                }
                            }

                            if (resultKeyCount == maxKeys) {
                                // This is a new (non-commonprefix) record, so we know we're truncating
                                reply.setNextKeyMarker(nextKeyMarker);
                                reply.setNextVersionIdMarker(nextVersionIdMarker);
                                reply.setIsTruncated(true);
                                resultKeyCount++;
                                break;
                            }

                            // This is either a version entry or a delete marker
                            KeyEntry keyEntry = null;
                            if (!objectInfo.getDeleted()) {
                                keyEntry = new VersionEntry();
                                ((VersionEntry) keyEntry).setEtag(objectInfo.getEtag());
                                ((VersionEntry) keyEntry).setSize(objectInfo.getSize());
                                ((VersionEntry) keyEntry).setStorageClass(objectInfo.getStorageClass());
                            } else {
                                keyEntry = new DeleteMarkerEntry();
                            }
                            keyEntry.setKey(objectKey);
                            keyEntry.setVersionId(objectInfo.getVersionId());
                            keyEntry.setIsLatest(objectInfo.getLast());
                            keyEntry.setLastModified(DateUtils.format(objectInfo.getLastModified().getTime(),
                                    DateUtils.ALT_ISO8601_DATE_PATTERN));
                            try {
                                Account ownerAccount = Accounts.lookupAccountById(objectInfo.getOwnerId());
                                keyEntry.setOwner(new CanonicalUserType(ownerAccount.getCanonicalId(),
                                        ownerAccount.getName()));
                            } catch (AuthException e) {
                                db.rollback();
                                throw new AccessDeniedException("Bucket", bucketName, logData);
                            }
                            keyEntries.add(keyEntry);

                            resultKeyCount++;

                            // If max keys have been collected, set the next- markers. They might be needed for the response if the list is truncated
                            if (resultKeyCount == maxKeys) {
                                nextKeyMarker = objectKey;
                                nextVersionIdMarker = objectInfo.getVersionId();
                            }
                        }
                    }
                    if (resultKeyCount <= maxKeys && objectInfos.size() <= maxKeys) {
                        break;
                    }
                } while (resultKeyCount <= maxKeys);

                reply.setKeyEntries(keyEntries);

                // Prefixes are already sorted, add them to the proper data structures and populate the reply
                if (!commonPrefixes.isEmpty()) {
                    ArrayList<CommonPrefixesEntry> commonPrefixesList = new ArrayList<CommonPrefixesEntry>();
                    for (String prefixEntry : commonPrefixes) {
                        commonPrefixesList.add(new CommonPrefixesEntry().add(new PrefixEntry(prefixEntry)));
                    }
                    reply.setCommonPrefixesList(commonPrefixesList);
                }
            } else {
                db.rollback();
                throw new AccessDeniedException("Bucket", bucketName, logData);
            }
        } else {
            db.rollback();
            throw new NoSuchBucketException(bucketName);
        }
        db.commit();
        return reply;
    } finally {
        if (db.isActive()) {
            db.rollback();
        }
    }
}

From source file:com.joliciel.talismane.parser.TransitionBasedGlobalLearningParser.java

public List<ParseConfiguration> parseSentence(List<PosTagSequence> posTagSequences,
        FeatureWeightVector weightVector, RankingSolution correctSolution) {
    MONITOR.startTask("parseSentence");
    try {//  w  w  w.  j av a 2  s.  c  om
        long startTime = (new Date()).getTime();
        int maxAnalysisTimeMilliseconds = maxAnalysisTimePerSentence * 1000;
        int minFreeMemoryBytes = minFreeMemory * KILOBYTE;

        TokenSequence tokenSequence = posTagSequences.get(0).getTokenSequence();

        TreeMap<Integer, TreeSet<ParseConfiguration>> heaps = new TreeMap<Integer, TreeSet<ParseConfiguration>>();

        TreeSet<ParseConfiguration> heap0 = new TreeSet<ParseConfiguration>();
        for (PosTagSequence posTagSequence : posTagSequences) {
            // add an initial ParseConfiguration for each postag sequence
            ParseConfiguration initialConfiguration = this.getParserServiceInternal()
                    .getInitialConfiguration(posTagSequence);
            initialConfiguration.setScoringStrategy(new SimpleRankingScoringStrategy());
            initialConfiguration.setRankingScore(0.0);
            heap0.add(initialConfiguration);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Adding initial posTagSequence: " + posTagSequence);
            }
        }
        heaps.put(0, heap0);
        TreeSet<ParseConfiguration> backupHeap = null;

        TreeSet<ParseConfiguration> finalHeap = null;
        while (heaps.size() > 0) {
            Entry<Integer, TreeSet<ParseConfiguration>> heapEntry = heaps.firstEntry();
            TreeSet<ParseConfiguration> currentHeap = heapEntry.getValue();
            int currentHeapIndex = heapEntry.getKey();
            if (LOG.isTraceEnabled()) {
                LOG.trace("##### Polling next heap: " + heapEntry.getKey() + ", size: "
                        + heapEntry.getValue().size());
            }

            boolean finished = false;
            // systematically set the final heap here, just in case we exit "naturally" with no more heaps
            finalHeap = heapEntry.getValue();
            backupHeap = new TreeSet<ParseConfiguration>();

            // we jump out when either (a) all tokens have been attached or (b) we go over the max alloted time
            ParseConfiguration topConf = currentHeap.first();
            if (topConf.isTerminal()) {
                LOG.trace("Exiting with terminal heap: " + heapEntry.getKey() + ", size: "
                        + heapEntry.getValue().size());
                finished = true;
            }

            // check if we've gone over alloted time for this sentence
            long analysisTime = (new Date()).getTime() - startTime;
            if (maxAnalysisTimePerSentence > 0 && analysisTime > maxAnalysisTimeMilliseconds) {
                LOG.info("Parse tree analysis took too long for sentence: " + tokenSequence.getText());
                LOG.info("Breaking out after " + maxAnalysisTimePerSentence + " seconds.");
                finished = true;
            }

            // check if we've enough memory to process this sentence
            if (minFreeMemory > 0) {
                long freeMemory = Runtime.getRuntime().freeMemory();
                if (freeMemory < minFreeMemoryBytes) {
                    LOG.info("Not enough memory left to parse sentence: " + tokenSequence.getText());
                    LOG.info("Min free memory (bytes):" + minFreeMemoryBytes);
                    LOG.info("Current free memory (bytes): " + freeMemory);
                    finished = true;
                }
            }

            // check if any of the remaining top-N solutions on any heap can lead to the correct solution
            if (correctSolution != null) {
                boolean canReachCorrectSolution = false;
                for (TreeSet<ParseConfiguration> heap : heaps.values()) {
                    int j = 1;
                    for (ParseConfiguration solution : heap) {
                        if (j > beamWidth)
                            break;
                        if (solution.canReach(correctSolution)) {
                            canReachCorrectSolution = true;
                            break;
                        }
                        j++;
                    }
                    if (canReachCorrectSolution)
                        break;
                }
                if (!canReachCorrectSolution) {
                    LOG.debug("None of the solutions on the heap can reach the gold solution. Exiting.");
                    finished = true;
                }
            }

            if (finished) {
                // combine any remaining heaps
                for (TreeSet<ParseConfiguration> heap : heaps.values()) {
                    if (finalHeap != heap) {
                        finalHeap.addAll(heap);
                    }
                }
                break;
            }

            // remove heap from set of heaps
            heapEntry = heaps.pollFirstEntry();

            // limit the breadth to K
            int maxSolutions = currentHeap.size() > this.beamWidth ? this.beamWidth : currentHeap.size();

            int j = 0;
            while (currentHeap.size() > 0) {
                ParseConfiguration history = currentHeap.pollFirst();
                backupHeap.add(history);
                if (LOG.isTraceEnabled()) {
                    LOG.trace("### Next configuration on heap " + heapEntry.getKey() + ":");
                    LOG.trace(history.toString());
                    LOG.trace("Score: " + df.format(history.getScore()));
                    LOG.trace(history.getPosTagSequence());
                }

                Set<Transition> transitions = new HashSet<Transition>();

                // test the positive rules on the current configuration
                boolean ruleApplied = false;
                if (parserPositiveRules != null) {
                    MONITOR.startTask("check rules");
                    try {
                        for (ParserRule rule : parserPositiveRules) {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Checking rule: " + rule.getCondition().getName());
                            }
                            RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                            FeatureResult<Boolean> ruleResult = rule.getCondition().check(history, env);
                            if (ruleResult != null && ruleResult.getOutcome()) {
                                transitions.add(rule.getTransition());
                                ruleApplied = true;
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("Rule applies. Setting transition to: "
                                            + rule.getTransition().getCode());
                                }

                                if (!rule.getTransition().checkPreconditions(history)) {
                                    LOG.error("Cannot apply rule, preconditions not met.");
                                    ruleApplied = false;
                                }
                                break;
                            }
                        }
                    } finally {
                        MONITOR.endTask("check rules");
                    }
                }

                if (!ruleApplied) {
                    transitions = parsingConstrainer.getPossibleTransitions(history);

                    Set<Transition> eliminatedTransitions = new HashSet<Transition>();
                    for (Transition transition : transitions) {
                        if (!transition.checkPreconditions(history)) {
                            eliminatedTransitions.add(transition);
                        }
                    }
                    transitions.removeAll(eliminatedTransitions);

                    // apply the negative rules
                    eliminatedTransitions = new HashSet<Transition>();
                    if (parserNegativeRules != null) {
                        MONITOR.startTask("check negative rules");
                        try {
                            for (ParserRule rule : parserNegativeRules) {
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("Checking negative rule: " + rule.getCondition().getName());
                                }
                                RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                                FeatureResult<Boolean> ruleResult = rule.getCondition().check(history, env);
                                if (ruleResult != null && ruleResult.getOutcome()) {
                                    eliminatedTransitions.add(rule.getTransition());
                                    if (LOG.isTraceEnabled()) {
                                        LOG.debug("Rule applies. Eliminating transition: "
                                                + rule.getTransition().getCode());
                                    }
                                }
                            }

                            if (eliminatedTransitions.size() == transitions.size()) {
                                LOG.debug("All transitions eliminated! Restoring original transitions.");
                            } else {
                                transitions.removeAll(eliminatedTransitions);
                            }
                        } finally {
                            MONITOR.endTask("check negative rules");
                        }
                    }
                } // has a positive rule been applied?

                if (transitions.size() == 0) {
                    // just in case the we run out of both heaps and analyses, we build this backup heap
                    backupHeap.add(history);
                    if (LOG.isTraceEnabled())
                        LOG.trace(
                                "No transitions could be applied: not counting this solution as part of the beam");
                } else {
                    // up the counter, since we will count this solution towards the heap
                    j++;
                    // add solutions to the heap, one per valid transition
                    MONITOR.startTask("heap sort");
                    try {
                        Map<Transition, Double> deltaScorePerTransition = new HashMap<Transition, Double>();
                        double absoluteMax = 1;

                        for (Transition transition : transitions) {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Applying transition: " + transition.getCode());
                            }
                            ParseConfiguration configuration = this.parserServiceInternal
                                    .getConfiguration(history);
                            transition.apply(configuration);
                            configuration.setRankingScore(history.getRankingScore());
                            configuration.getIncrementalFeatureResults()
                                    .addAll(history.getIncrementalFeatureResults());

                            // test the features on the new configuration
                            double scoreDelta = 0.0;
                            MONITOR.startTask("feature analyse");
                            List<FeatureResult<?>> featureResults = new ArrayList<FeatureResult<?>>();
                            try {
                                for (ParseConfigurationFeature<?> feature : this.parseFeatures) {
                                    MONITOR.startTask(feature.getName());
                                    try {
                                        RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                                        FeatureResult<?> featureResult = feature.check(configuration, env);
                                        if (featureResult != null) {
                                            featureResults.add(featureResult);
                                            double weight = weightVector.getWeight(featureResult);
                                            scoreDelta += weight;
                                            if (LOG.isTraceEnabled()) {
                                                LOG.trace(featureResult.toString() + " = " + weight);
                                            }
                                        }
                                    } finally {
                                        MONITOR.endTask(feature.getName());
                                    }
                                }
                                configuration.getIncrementalFeatureResults().add(featureResults);
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("Score = " + configuration.getRankingScore() + " + " + scoreDelta
                                            + " = " + (configuration.getRankingScore() + scoreDelta));
                                }
                                configuration.setRankingScore(configuration.getRankingScore() + scoreDelta);
                                deltaScorePerTransition.put(transition, scoreDelta);
                                if (Math.abs(scoreDelta) > absoluteMax)
                                    absoluteMax = Math.abs(scoreDelta);

                            } finally {
                                MONITOR.endTask("feature analyse");
                            }

                            int nextHeapIndex = parseComparisonStrategy.getComparisonIndex(configuration)
                                    * 1000;
                            while (nextHeapIndex <= currentHeapIndex)
                                nextHeapIndex++;

                            TreeSet<ParseConfiguration> nextHeap = heaps.get(nextHeapIndex);
                            if (nextHeap == null) {
                                nextHeap = new TreeSet<ParseConfiguration>();
                                heaps.put(nextHeapIndex, nextHeap);
                                if (LOG.isTraceEnabled())
                                    LOG.trace("Created heap with index: " + nextHeapIndex);
                            }
                            nextHeap.add(configuration);
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Added configuration with score " + configuration.getScore()
                                        + " to heap: " + nextHeapIndex + ", total size: " + nextHeap.size());
                            }

                            configuration.clearMemory();
                        } // next transition

                        // Create a probability distribution of transitions
                        // normalise probabilities for each transition via normalised exponential
                        // e^(x/absmax)/sum(e^(x/absmax))
                        // where x/absmax is in [-1,1]
                        // e^(x/absmax) is in [1/e,e]

                        double total = 0.0;
                        for (Transition transition : deltaScorePerTransition.keySet()) {
                            double deltaScore = deltaScorePerTransition.get(transition);
                            deltaScore = Math.exp(deltaScore / absoluteMax);
                            deltaScorePerTransition.put(transition, deltaScore);
                            total += deltaScore;
                        }

                        for (Transition transition : deltaScorePerTransition.keySet()) {
                            double probability = deltaScorePerTransition.get(transition);
                            probability /= total;
                            Decision<Transition> decision = machineLearningService.createDecision(transition,
                                    probability);
                            transition.setDecision(decision);
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Transition: " + transition.getCode() + ", Prob: " + probability);
                            }
                        }

                    } finally {
                        MONITOR.endTask("heap sort");
                    }
                } // have we any transitions?

                // beam width test
                if (j == maxSolutions)
                    break;
            } // next history   
        } // next atomic index

        // return the best sequences on the heap
        List<ParseConfiguration> bestConfigurations = new ArrayList<ParseConfiguration>();
        int i = 0;

        if (finalHeap.isEmpty())
            finalHeap = backupHeap;

        while (!finalHeap.isEmpty()) {
            bestConfigurations.add(finalHeap.pollFirst());
            i++;
            if (i >= this.getBeamWidth())
                break;
        }
        if (LOG.isDebugEnabled()) {
            if (correctSolution != null) {
                LOG.debug("Gold transitions: " + correctSolution.getIncrementalOutcomes());
            }
            for (ParseConfiguration finalConfiguration : bestConfigurations) {
                LOG.debug(df.format(finalConfiguration.getScore()) + ": " + finalConfiguration.toString());
                LOG.debug("Pos tag sequence: " + finalConfiguration.getPosTagSequence());
                LOG.debug("Transitions: " + finalConfiguration.getTransitions());
                if (LOG.isTraceEnabled()) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(" * PosTag sequence score ");
                    sb.append(df.format(finalConfiguration.getPosTagSequence().getScore()));
                    sb.append(" = ");
                    for (PosTaggedToken posTaggedToken : finalConfiguration.getPosTagSequence()) {
                        sb.append(" * ");
                        sb.append(df.format(posTaggedToken.getDecision().getProbability()));
                    }
                    sb.append(" root ");
                    sb.append(finalConfiguration.getPosTagSequence().size());
                    LOG.trace(sb.toString());

                    sb = new StringBuilder();
                    sb.append(" * Token sequence score = ");
                    sb.append(df.format(finalConfiguration.getPosTagSequence().getTokenSequence().getScore()));
                    LOG.trace(sb.toString());

                }
            }
        }
        return bestConfigurations;
    } finally {
        MONITOR.endTask("parseSentence");
    }
}