xiaoyuyao commented on a change in pull request #2008: URL: https://github.com/apache/ozone/pull/2008#discussion_r602020934
########## File path: hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzonePrefixPathImpl.java ########## @@ -0,0 +1,153 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.hadoop.ozone.om; + +import org.apache.commons.lang3.StringUtils; +import org.apache.hadoop.ozone.om.exceptions.OMException; +import org.apache.hadoop.ozone.om.helpers.OmKeyArgs; +import org.apache.hadoop.ozone.om.helpers.OzoneFileStatus; +import org.apache.hadoop.ozone.security.acl.OzonePrefixPath; + +import java.io.IOException; +import java.util.Iterator; +import java.util.List; +import java.util.NoSuchElementException; + +import static org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.KEY_NOT_FOUND; + +public class OzonePrefixPathImpl implements OzonePrefixPath { + + private String volumeName; + private String bucketName; + private KeyManager keyManager; + // TODO: based on need can make batchSize configurable. + private int batchSize = 1000; + private OzoneFileStatus pathStatus; + + public OzonePrefixPathImpl(String volumeName, String bucketName, + String keyPrefix, KeyManager keyManagerImpl) throws IOException { + this.volumeName = volumeName; + this.bucketName = bucketName; + this.keyManager = keyManagerImpl; + + OmKeyArgs omKeyArgs = new OmKeyArgs.Builder() + .setVolumeName(volumeName) + .setBucketName(bucketName) + .setKeyName(keyPrefix) + .setRefreshPipeline(false) + .build(); + try { + pathStatus = keyManager.getFileStatus(omKeyArgs); + } catch (OMException ome) { + // In existing code non-FSO code, ozone client delete and rename expects + // KNF error code. So converting FNF to KEY_NOT_FOUND error code. + if (ome.getResult() == OMException.ResultCodes.FILE_NOT_FOUND) { + throw new OMException(ome.getMessage(), KEY_NOT_FOUND); + } + throw ome; + } + } + + @Override + public OzoneFileStatus getOzoneFileStatus() { + return pathStatus; + } + + @Override + public Iterator<? extends OzoneFileStatus> getChildren(String keyPrefix) + throws IOException { + + return new PathIterator(keyPrefix); + } + + class PathIterator implements Iterator<OzoneFileStatus> { + private Iterator<OzoneFileStatus> currentIterator; + private String keyPrefix; + private OzoneFileStatus currentValue; + + /** + * Creates an Iterator to iterate over all sub paths of the given keyPrefix. + * + * @param keyPrefix + */ + PathIterator(String keyPrefix) throws IOException { + this.keyPrefix = keyPrefix; + this.currentValue = null; + List<OzoneFileStatus> statuses = getNextListOfKeys(""); + if (statuses.size() == 1) { + OzoneFileStatus keyStatus = statuses.get(0); + if (keyStatus.isFile() && StringUtils.equals(keyPrefix, + keyStatus.getTrimmedName())) { + throw new OMException("Invalid keyPrefix: " + keyPrefix + + ", file type is not allowed, expected directory type.", + OMException.ResultCodes.INVALID_KEY_NAME); + } + } + this.currentIterator = statuses.iterator(); + } + + @Override + public boolean hasNext() { + if (!currentIterator.hasNext() && currentValue != null) { + try { + currentIterator = + getNextListOfKeys(currentValue.getTrimmedName()).iterator(); + } catch (IOException e) { + throw new RuntimeException(e); Review comment: Can we avoid throw RuntimeException here? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
