Skip to content

Commit

Permalink
fix (core): Implement IPFSBlobStore.store()
Browse files Browse the repository at this point in the history
  • Loading branch information
vorburger committed Feb 28, 2025
1 parent 20bb082 commit e85a8f9
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 18 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ jobs:
(github.event.pull_request.draft == false)
runs-on: ubuntu-latest
steps:
- name: Install IPFS Node daemon
uses: oduwsdl/setup-ipfs@e92fedca9f61ab9184cb74940254859f4d7af4d9
with:
ipfs_version: ^0.33
run_daemon: true

- uses: actions/checkout@v4

- name: Install bun
Expand Down
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ enola_maven.install(

# IPFS & IPLD etc.
"com.github.ipld:java-cid:1.3.8",
"com.github.ipfs:java-ipfs-http-client:d982fc0fa1",

# NB: Due to https://github.com/bazel-contrib/rules_jvm_external/issues/1324,
# this only just so happens to work because this Git SHA starts with a digit.
Expand Down
2 changes: 2 additions & 0 deletions java/dev/enola/cas/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ java_binary(
"//java/dev/enola/common",
"//java/dev/enola/common/context",
"//java/dev/enola/common/io",
"@enola_maven//:com_github_ipfs_java_ipfs_http_client",
"@enola_maven//:com_github_ipld_java_cid",
"@enola_maven//:com_github_multiformats_java_multibase",
"@enola_maven//:com_github_multiformats_java_multihash",
Expand All @@ -52,6 +53,7 @@ junit_tests(
":cas",
"//java/dev/enola/common/context/testlib",
"//java/dev/enola/common/io",
"@enola_maven//:com_github_ipfs_java_ipfs_http_client",
"@enola_maven//:com_github_ipld_java_cid",
"@enola_maven//:org_slf4j_slf4j_jdk14",
],
Expand Down
6 changes: 4 additions & 2 deletions java/dev/enola/cas/BlobStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import io.ipfs.cid.Cid;

import java.io.IOException;

/**
* BlobStore stores bytes, which can then be loaded given their CID.
*
Expand All @@ -37,9 +39,9 @@
*/
public interface BlobStore {

Cid store(ByteSource source);
Cid store(ByteSource source) throws IOException;

ByteSource load(Cid cid);
ByteSource load(Cid cid) throws IOException;

// TODO void delete(Cid cid)
}
23 changes: 14 additions & 9 deletions java/dev/enola/cas/IPFSBlobStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,32 @@
*/
package dev.enola.cas;

import static java.util.Objects.requireNonNull;

import com.google.common.io.ByteSource;

import io.ipfs.api.IPFS;
import io.ipfs.api.NamedStreamable;
import io.ipfs.cid.Cid;

import java.io.IOException;

public class IPFSBlobStore implements BlobStore { // TODO , IdStore

private final IPFSResource.Provider ipfsResourceProvider;
private final IPFS ipfs;

public IPFSBlobStore(IPFSResource.Provider ipfsResourceProvider) {
this.ipfsResourceProvider = ipfsResourceProvider;
public IPFSBlobStore(IPFS ipfs) {
this.ipfs = ipfs;
}

@Override
public Cid store(ByteSource source) {
throw new UnsupportedOperationException();
public Cid store(ByteSource source) throws IOException {
var namedStreamable = new NamedStreamable.ByteArrayWrapper(source.read());
var merkleNode = ipfs.add(namedStreamable);
// TODO Why does it not work with version == 1 ?!
return Cid.build(0, Cid.Codec.Raw, merkleNode.get(0).hash);
}

@Override
public ByteSource load(Cid cid) {
return requireNonNull(ipfsResourceProvider.get("ipfs://" + cid)).byteSource();
public ByteSource load(Cid cid) throws IOException {
return ByteSource.wrap(ipfs.cat(cid));
}
}
26 changes: 21 additions & 5 deletions java/dev/enola/cas/IPFSBlobStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,44 @@

import static com.google.common.truth.Truth.assertThat;

import com.google.common.io.ByteSource;

import dev.enola.common.context.testlib.SingletonRule;
import dev.enola.common.io.mediatype.MediaTypeProviders;
import dev.enola.common.io.resource.OkHttpResource;

import io.ipfs.api.IPFS;
import io.ipfs.cid.Cid;

import org.junit.Rule;
import org.junit.Test;

import java.io.IOException;
import java.util.Random;

public class IPFSBlobStoreTest {

public @Rule SingletonRule singleton = SingletonRule.$(MediaTypeProviders.set());

IPFSBlobStore ipfs = new IPFSBlobStore(new IPFS("/ip4/127.0.0.1/tcp/5001"));

@Test
public void hello() throws IOException {
var ipfs =
new IPFSBlobStore(
new IPFSResource.Provider(
new OkHttpResource.Provider(), "http://localhost:8080/ipfs/"));
var bytes = ipfs.load(Cid.decode("QmXV7pL1CB7A8Tzk7jP2XE9kRyk8HZd145KDptdxzmNLfu"));
assertThat(new String(bytes.read())).isEqualTo("hello, world\n");
}

@Test
public void random() throws IOException {
var bytes = generateRandomBytes(1024);
var cid = ipfs.store(ByteSource.wrap(bytes));
var loaded = ipfs.load(cid);
assertThat(loaded.read()).isEqualTo(bytes);
}

private static byte[] generateRandomBytes(int length) {
byte[] bytes = new byte[length];
Random random = new Random();
random.nextBytes(bytes);
return bytes;
}
}
40 changes: 38 additions & 2 deletions maven_install.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL",
"__INPUT_ARTIFACTS_HASH": -1258296041,
"__RESOLVED_ARTIFACTS_HASH": 2125806203,
"__INPUT_ARTIFACTS_HASH": -230791335,
"__RESOLVED_ARTIFACTS_HASH": -1108617503,
"conflict_resolution": {
"com.fasterxml.jackson.core:jackson-databind:2.13.5": "com.fasterxml.jackson.core:jackson-databind:2.17.2"
},
Expand Down Expand Up @@ -76,6 +76,13 @@
},
"version": "2.5.0"
},
"com.github.ipfs:java-ipfs-http-client": {
"shasums": {
"jar": "8f2148736867cfbd31c4d1eeca5b082b5378896c5fbeeabb06c572675b9a3b87",
"sources": "2839865d193cca9c91e7ebdfd1351a3b56ae903d957a7f9696a1c07608c6e01a"
},
"version": "d982fc0fa1"
},
"com.github.ipld:java-cid": {
"shasums": {
"jar": "8904dd42a363f383679ffea9cfe1d907370596541ca87e832c191080c8fbbfa8",
Expand Down Expand Up @@ -125,6 +132,13 @@
},
"version": "7.5.5"
},
"com.github.multiformats:java-multiaddr": {
"shasums": {
"jar": "9944cb0083c1f3a2ffca2b5c7c1c67d746f8dc5e2e9b8781083146b7d936e6c7",
"sources": "6794fad11caf27177e5dae1bd5b90fcb57e0fa880990ae331268242ed542ede3"
},
"version": "v1.4.12"
},
"com.github.multiformats:java-multibase": {
"shasums": {
"jar": "4f893f97c755413b51b42ebea9977573460879a22fb99ae3f318f4e13d88f3db",
Expand Down Expand Up @@ -1634,6 +1648,9 @@
"com.fasterxml.woodstox:woodstox-core": [
"org.codehaus.woodstox:stax2-api"
],
"com.github.ipfs:java-ipfs-http-client": [
"com.github.multiformats:java-multiaddr"
],
"com.github.ipld:java-cid": [
"com.github.multiformats:java-multihash"
],
Expand All @@ -1652,6 +1669,9 @@
"com.github.junrar:junrar": [
"org.slf4j:slf4j-api"
],
"com.github.multiformats:java-multiaddr": [
"com.github.ipld:java-cid"
],
"com.github.multiformats:java-multihash": [
"com.github.multiformats:java-multibase"
],
Expand Down Expand Up @@ -2609,6 +2629,11 @@
"org.mozilla.universalchardet.prober.sequence",
"org.mozilla.universalchardet.prober.statemachine"
],
"com.github.ipfs:java-ipfs-http-client": [
"io.ipfs.api",
"io.ipfs.api.cbor",
"io.ipfs.api.demo"
],
"com.github.ipld:java-cid": [
"io.ipfs.cid"
],
Expand Down Expand Up @@ -2688,6 +2713,9 @@
"com.github.junrar.unsigned",
"com.github.junrar.volume"
],
"com.github.multiformats:java-multiaddr": [
"io.ipfs.multiaddr"
],
"com.github.multiformats:java-multibase": [
"io.ipfs.multibase",
"io.ipfs.multibase.binary"
Expand Down Expand Up @@ -4938,6 +4966,8 @@
"com.fasterxml.woodstox:woodstox-core:jar:sources",
"com.github.albfernandez:juniversalchardet",
"com.github.albfernandez:juniversalchardet:jar:sources",
"com.github.ipfs:java-ipfs-http-client",
"com.github.ipfs:java-ipfs-http-client:jar:sources",
"com.github.ipld:java-cid",
"com.github.ipld:java-cid:jar:sources",
"com.github.jai-imageio:jai-imageio-core",
Expand All @@ -4952,6 +4982,8 @@
"com.github.jsonld-java:jsonld-java:jar:sources",
"com.github.junrar:junrar",
"com.github.junrar:junrar:jar:sources",
"com.github.multiformats:java-multiaddr",
"com.github.multiformats:java-multiaddr:jar:sources",
"com.github.multiformats:java-multibase",
"com.github.multiformats:java-multibase:jar:sources",
"com.github.multiformats:java-multihash",
Expand Down Expand Up @@ -5395,6 +5427,8 @@
"com.fasterxml.woodstox:woodstox-core:jar:sources",
"com.github.albfernandez:juniversalchardet",
"com.github.albfernandez:juniversalchardet:jar:sources",
"com.github.ipfs:java-ipfs-http-client",
"com.github.ipfs:java-ipfs-http-client:jar:sources",
"com.github.ipld:java-cid",
"com.github.ipld:java-cid:jar:sources",
"com.github.jai-imageio:jai-imageio-core",
Expand All @@ -5409,6 +5443,8 @@
"com.github.jsonld-java:jsonld-java:jar:sources",
"com.github.junrar:junrar",
"com.github.junrar:junrar:jar:sources",
"com.github.multiformats:java-multiaddr",
"com.github.multiformats:java-multiaddr:jar:sources",
"com.github.multiformats:java-multibase",
"com.github.multiformats:java-multibase:jar:sources",
"com.github.multiformats:java-multihash",
Expand Down

0 comments on commit e85a8f9

Please sign in to comment.