Skip to content

Commit

Permalink
improve: use AutoCloseableIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
Radeity committed May 28, 2023
1 parent 53d5565 commit 025f714
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.hugegraph.computer.core.common;

import java.util.Iterator;

public interface AutoCloseableIterator<E> extends AutoCloseable, Iterator<E> {

@Override
default void close() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
package org.apache.hugegraph.computer.core.input;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.function.Consumer;
import java.util.function.Supplier;

import org.apache.hugegraph.computer.core.common.AutoCloseableIterator;
import org.apache.hugegraph.computer.core.common.ComputerContext;
import org.apache.hugegraph.computer.core.common.exception.ComputerException;
import org.apache.hugegraph.computer.core.config.ComputerOptions;
Expand Down Expand Up @@ -131,17 +131,13 @@ public void loadGraph() {
}

private CompletableFuture<?> send(Consumer<Vertex> sendConsumer,
Supplier<Iterator<Vertex>> iteratorSupplier) {
Supplier<AutoCloseableIterator<Vertex>> iteratorSupplier) {
return CompletableFuture.runAsync(() -> {
Iterator<Vertex> iterator = iteratorSupplier.get();
while (iterator.hasNext()) {
Vertex vertex = iterator.next();
sendConsumer.accept(vertex);
}
try {
((AutoCloseable) iterator).close();
} catch (Exception e) {
throw new ComputerException("Failed to close iterator", e);
try (AutoCloseableIterator<Vertex> iterator = iteratorSupplier.get()) {
while (iterator.hasNext()) {
Vertex vertex = iterator.next();
sendConsumer.accept(vertex);
}
}
}, this.sendExecutor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

package org.apache.hugegraph.computer.core.worker.load;

import java.io.IOException;
import java.util.Iterator;
import java.util.NoSuchElementException;

import org.apache.hugegraph.computer.core.common.AutoCloseableIterator;
import org.apache.hugegraph.computer.core.common.ComputerContext;
import org.apache.hugegraph.computer.core.config.ComputerOptions;
import org.apache.hugegraph.computer.core.config.Config;
Expand Down Expand Up @@ -66,17 +64,17 @@ public void rpcService(InputSplitRpcService rpcService) {
this.rpcService = rpcService;
}

public Iterator<Vertex> createIteratorFromVertex() {
public AutoCloseableIterator<Vertex> createIteratorFromVertex() {
GraphFetcher fetcher = InputSourceFactory.createGraphFetcher(this.config, this.rpcService);
return new IteratorFromVertex(fetcher);
}

public Iterator<Vertex> createIteratorFromEdge() {
public AutoCloseableIterator<Vertex> createIteratorFromEdge() {
GraphFetcher fetcher = InputSourceFactory.createGraphFetcher(this.config, this.rpcService);
return new IteratorFromEdge(fetcher);
}

private class IteratorFromVertex implements Iterator<Vertex>, AutoCloseable {
private class IteratorFromVertex implements AutoCloseableIterator<Vertex> {

private InputSplit currentSplit;

Expand Down Expand Up @@ -128,12 +126,12 @@ private Vertex convert(org.apache.hugegraph.structure.graph.Vertex
}

@Override
public void close() throws IOException {
public void close() {
this.fetcher.close();
}
}

private class IteratorFromEdge implements Iterator<Vertex>, AutoCloseable {
private class IteratorFromEdge implements AutoCloseableIterator<Vertex> {

/*
* TODO: If it is an in edge, we should get the data from the in shard;
Expand Down Expand Up @@ -233,7 +231,7 @@ private Edge convert(org.apache.hugegraph.structure.graph.Edge edge) {
}

@Override
public void close() throws Exception {
public void close() {
this.fetcher.close();
}
}
Expand Down

0 comments on commit 025f714

Please sign in to comment.