cgivre commented on a change in pull request #2084: URL: https://github.com/apache/drill/pull/2084#discussion_r470082144
########## File path: contrib/storage-ipfs/src/main/java/org/apache/drill/exec/store/ipfs/IPFSCompat.java ########## @@ -0,0 +1,318 @@ +/* + * 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.drill.exec.store.ipfs; + +import io.ipfs.api.IPFS; +import io.ipfs.api.JSONParser; +import io.ipfs.multihash.Multihash; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; +import java.util.function.Predicate; + +/** + * Compatibility fixes for java-ipfs-http-client library + */ +public class IPFSCompat { + public final String host; + public final int port; + private final String version; + public final String protocol; + public final int readTimeout; + public static final int DEFAULT_READ_TIMEOUT = 0; + + public final DHT dht = new DHT(); + public final Name name = new Name(); + + public IPFSCompat(IPFS ipfs) { + this(ipfs.host, ipfs.port); + } + + public IPFSCompat(String host, int port) { + this(host, port, "/api/v0/", false, DEFAULT_READ_TIMEOUT); + } + + public IPFSCompat(String host, int port, String version, boolean ssl, int readTimeout) { + this.host = host; + this.port = port; + + if(ssl) { + this.protocol = "https"; + } else { + this.protocol = "http"; + } + + this.version = version; + this.readTimeout = readTimeout; + } + + /** + * Resolve names to IPFS CIDs. + * See <a href="https://docs.ipfs.io/reference/http/api/#api-v0-resolve">resolve in IPFS doc</a>. + * @param scheme the scheme of the name to resolve, usually IPFS or IPNS + * @param path the path to the object + * @param recursive whether recursively resolve names until it is a IPFS CID + * @return a Map of JSON object, with the result as the value of key "Path" + */ + public Map resolve(String scheme, String path, boolean recursive) { + AtomicReference<Map> ret = new AtomicReference<>(); + getObjectStream( + "resolve?arg=/" + scheme+"/"+path +"&r="+recursive, + res -> { + ret.set((Map) res); + return true; + }, + err -> { + throw new RuntimeException(err); + } + ); + return ret.get(); + } + + /** + * As defined in https://github.com/libp2p/go-libp2p-core/blob/b77fd280f2bfcce22f10a000e8e1d9ec53c47049/routing/query.go#L16 + */ + public enum DHTQueryEventType { + // Sending a query to a peer. + SendingQuery, + // Got a response from a peer. + PeerResponse, + // Found a "closest" peer (not currently used). + FinalPeer, + // Got an error when querying. + QueryError, + // Found a provider. + Provider, + // Found a value. + Value, Review comment: Are `Value`, `AddingPeer` and `DialingPeer` ever used? ---------------------------------------------------------------- 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: us...@infra.apache.org