| 1 | /******************************************************************** |
| 2 | * Copyright (c) 2013 - 2014, Pivotal Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Author: Zhanwei Wang |
| 6 | ********************************************************************/ |
| 7 | /******************************************************************** |
| 8 | * 2014 - |
| 9 | * open source under Apache License Version 2.0 |
| 10 | ********************************************************************/ |
| 11 | /** |
| 12 | * Licensed to the Apache Software Foundation (ASF) under one |
| 13 | * or more contributor license agreements. See the NOTICE file |
| 14 | * distributed with this work for additional information |
| 15 | * regarding copyright ownership. The ASF licenses this file |
| 16 | * to you under the Apache License, Version 2.0 (the |
| 17 | * "License"); you may not use this file except in compliance |
| 18 | * with the License. You may obtain a copy of the License at |
| 19 | * |
| 20 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | * |
| 22 | * Unless required by applicable law or agreed to in writing, software |
| 23 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | * See the License for the specific language governing permissions and |
| 26 | * limitations under the License. |
| 27 | */ |
| 28 | #include "DirectoryIterator.h" |
| 29 | #include "FileStatus.h" |
| 30 | #include "Exception.h" |
| 31 | #include "ExceptionInternal.h" |
| 32 | #include "FileSystemImpl.h" |
| 33 | |
| 34 | namespace Hdfs { |
| 35 | |
| 36 | DirectoryIterator::DirectoryIterator() : |
| 37 | needLocations(false), filesystem(NULL), next(0) { |
| 38 | } |
| 39 | |
| 40 | DirectoryIterator::DirectoryIterator(Hdfs::Internal::FileSystemImpl * const fs, |
| 41 | std::string path, bool needLocations) : |
| 42 | needLocations(needLocations), filesystem(fs), next(0), path(path) { |
| 43 | } |
| 44 | |
| 45 | DirectoryIterator::DirectoryIterator(const DirectoryIterator & it) : |
| 46 | needLocations(it.needLocations), filesystem(it.filesystem), next(it.next), path(it.path), startAfter( |
| 47 | it.startAfter), lists(it.lists) { |
| 48 | } |
| 49 | |
| 50 | DirectoryIterator & DirectoryIterator::operator =(const DirectoryIterator & it) { |
| 51 | if (this == &it) { |
| 52 | return *this; |
| 53 | } |
| 54 | |
| 55 | needLocations = it.needLocations; |
| 56 | filesystem = it.filesystem; |
| 57 | next = it.next; |
| 58 | path = it.path; |
| 59 | startAfter = it.startAfter; |
| 60 | lists = it.lists; |
| 61 | return *this; |
| 62 | } |
| 63 | |
| 64 | bool DirectoryIterator::getListing() { |
| 65 | bool more; |
| 66 | |
| 67 | if (NULL == filesystem) { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | next = 0; |
| 72 | lists.clear(); |
| 73 | more = filesystem->getListing(path, startAfter, needLocations, lists); |
| 74 | |
| 75 | if (!lists.empty()) { |
| 76 | startAfter = lists.back().getPath(); |
| 77 | } |
| 78 | |
| 79 | return more || !lists.empty(); |
| 80 | } |
| 81 | |
| 82 | bool DirectoryIterator::hasNext() { |
| 83 | if (next >= lists.size()) { |
| 84 | return getListing(); |
| 85 | } |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | Hdfs::FileStatus DirectoryIterator::getNext() { |
| 91 | if (next >= lists.size()) { |
| 92 | if (!getListing()) { |
| 93 | THROW(HdfsIOException, "End of the dir flow" ); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return lists[next++]; |
| 98 | } |
| 99 | |
| 100 | } |
| 101 | |