Skip to content

Commit 94156cb

Browse files
committed
More usage of MultiHash where appropriate. More tests. Updated readme.
1 parent f0ffe2a commit 94156cb

File tree

3 files changed

+38
-34
lines changed

3 files changed

+38
-34
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ MerkleNode addResult = ipfs.add(file);
2929

3030
To get a file use:
3131
```Java
32-
MerkleNode addResult = new MerkleNode("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB");
33-
byte[] fileContents = ipfs.cat(merkleObject);
32+
MultiHash filePointer = MultiHash.fromBase58("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB");
33+
byte[] fileContents = ipfs.cat(filePointer);
3434
```
3535

3636
##Building

src/org/ipfs/IPFS.java

+16-16
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,24 @@ public List<MerkleNode> add(List<NamedStreamable> files) throws IOException {
4949
return JSONParser.parseStream(res).stream().map(x -> MerkleNode.fromJSON((Map<String, Object>) x)).collect(Collectors.toList());
5050
}
5151

52-
public List<MerkleNode> ls(MerkleNode merkleObject) throws IOException {
53-
Map res = retrieveMap("ls/" + merkleObject.hash);
52+
public List<MerkleNode> ls(Multihash hash) throws IOException {
53+
Map res = retrieveMap("ls/" + hash);
5454
return ((List<Object>) res.get("Objects")).stream().map(x -> MerkleNode.fromJSON((Map) x)).collect(Collectors.toList());
5555
}
5656

57-
public byte[] cat(MerkleNode merkleObject) throws IOException {
58-
return retrieve("cat/" + merkleObject.hash);
57+
public byte[] cat(Multihash hash) throws IOException {
58+
return retrieve("cat/" + hash);
5959
}
6060

61-
public byte[] get(MerkleNode merkleObject) throws IOException {
62-
return retrieve("get/" + merkleObject.hash);
61+
public byte[] get(Multihash hash) throws IOException {
62+
return retrieve("get/" + hash);
6363
}
6464

65-
public Map refs(String hash, boolean recursive) throws IOException {
65+
public Map refs(Multihash hash, boolean recursive) throws IOException {
6666
return retrieveMap("refs?arg=" + hash +"&r="+recursive);
6767
}
6868

69-
public Map resolve(String scheme, String hash, boolean recursive) throws IOException {
69+
public Map resolve(String scheme, Multihash hash, boolean recursive) throws IOException {
7070
return retrieveMap("resolve?arg=/" + scheme+"/"+hash +"&r="+recursive);
7171
}
7272

@@ -87,8 +87,8 @@ public Map mount(java.io.File ipfsRoot, java.io.File ipnsRoot) throws IOExceptio
8787

8888
// level 2 commands
8989
class Refs {
90-
public List<String> local() throws IOException {
91-
return Arrays.asList(new String(retrieve("refs/local")).split("\n"));
90+
public List<Multihash> local() throws IOException {
91+
return Arrays.asList(new String(retrieve("refs/local")).split("\n")).stream().map(Multihash::fromBase58).collect(Collectors.toList());
9292
}
9393
}
9494

@@ -186,15 +186,15 @@ public Map publish(Optional<String> id, MerkleNode node) throws IOException {
186186
return retrieveMap("name/publish?arg=" + (id.isPresent() ? id+"&arg=" : "") + "/ipfs/"+node.hash);
187187
}
188188

189-
public String resolve(String addr) throws IOException {
189+
public String resolve(Multihash addr) throws IOException {
190190
Map res = (Map) retrieveAndParse("name/resolve?arg=" + addr);
191191
return (String)res.get("Path");
192192
}
193193
}
194194

195195
class DHT {
196-
public Map findprovs(MerkleNode node) throws IOException {
197-
return retrieveMap("dht/findprovs?arg=" + node.hash);
196+
public Map findprovs(Multihash hash) throws IOException {
197+
return retrieveMap("dht/findprovs?arg=" + hash);
198198
}
199199

200200
public Map query(MultiAddress addr) throws IOException {
@@ -205,8 +205,8 @@ public Map findpeer(MultiAddress addr) throws IOException {
205205
return retrieveMap("dht/findpeer?arg=" + addr.toString());
206206
}
207207

208-
public Map get(MerkleNode node) throws IOException {
209-
return retrieveMap("dht/get?arg=" + node.hash);
208+
public Map get(Multihash hash) throws IOException {
209+
return retrieveMap("dht/get?arg=" + hash);
210210
}
211211

212212
public Map put(String key, String value) throws IOException {
@@ -215,7 +215,7 @@ public Map put(String key, String value) throws IOException {
215215
}
216216

217217
class File {
218-
public Map ls(String path) throws IOException {
218+
public Map ls(Multihash path) throws IOException {
219219
return retrieveMap("file/ls?arg=" + path);
220220
}
221221
}

test/org/ipfs/Test.java

+20-16
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,26 @@ public void largeFileTest() {
3030
fileTest(largeFile);
3131
}
3232

33+
@org.junit.Test
34+
public void hostFileTest() {
35+
NamedStreamable hostFile = new NamedStreamable.FileWrapper(new File("Makefile"));
36+
fileTest(hostFile);
37+
}
38+
3339
public void fileTest(NamedStreamable file) {
3440
try {
35-
List<NamedStreamable> inputFiles = Arrays.asList(file);
36-
List<MerkleNode> addResult = ipfs.add(inputFiles);
37-
MerkleNode merkleObject = addResult.get(0);
38-
List<MerkleNode> lsResult = ipfs.ls(merkleObject);
41+
MerkleNode addResult = ipfs.add(file);
42+
List<MerkleNode> lsResult = ipfs.ls(addResult.hash);
3943
if (lsResult.size() != 1)
4044
throw new IllegalStateException("Incorrect number of objects in ls!");
41-
if (!lsResult.get(0).equals(merkleObject))
45+
if (!lsResult.get(0).equals(addResult))
4246
throw new IllegalStateException("Object not returned in ls!");
43-
byte[] catResult = ipfs.cat(merkleObject);
44-
byte[] getResult = ipfs.get(merkleObject);
47+
byte[] catResult = ipfs.cat(addResult.hash);
48+
byte[] getResult = ipfs.get(addResult.hash);
4549
if (!Arrays.equals(catResult, file.getContents()))
4650
throw new IllegalStateException("Different contents!");
47-
List<MerkleNode> pinRm = ipfs.pin.rm(merkleObject, true);
48-
if (!pinRm.get(0).equals(merkleObject))
51+
List<MerkleNode> pinRm = ipfs.pin.rm(addResult, true);
52+
if (!pinRm.get(0).equals(addResult))
4953
throw new IllegalStateException("Didn't remove file!");
5054
Object gc = ipfs.repo.gc();
5155
} catch (IOException e) {
@@ -56,8 +60,8 @@ public void fileTest(NamedStreamable file) {
5660
@org.junit.Test
5761
public void refsTest() {
5862
try {
59-
List<String> local = ipfs.refs.local();
60-
for (String ref: local) {
63+
List<Multihash> local = ipfs.refs.local();
64+
for (Multihash ref: local) {
6165
Object refs = ipfs.refs(ref, false);
6266
}
6367
} catch (IOException e) {
@@ -96,8 +100,8 @@ public void blockTest() {
96100
public void fileTest() {
97101
try {
98102
ipfs.repo.gc();
99-
List<String> local = ipfs.refs.local();
100-
for (String hash: local) {
103+
List<Multihash> local = ipfs.refs.local();
104+
for (Multihash hash: local) {
101105
Map ls = ipfs.file.ls(hash);
102106
}
103107
} catch (IOException e) {
@@ -110,7 +114,7 @@ public void nameTest() {
110114
try {
111115
MerkleNode pointer = new MerkleNode("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB");
112116
Map pub = ipfs.name.publish(pointer);
113-
String resolved = ipfs.name.resolve((String) pub.get("Name"));
117+
String resolved = ipfs.name.resolve(Multihash.fromBase58((String) pub.get("Name")));
114118
} catch (IOException e) {
115119
throw new RuntimeException(e);
116120
}
@@ -137,7 +141,7 @@ public void mountTest() {
137141
@org.junit.Test
138142
public void dhtTest() {
139143
try {
140-
MerkleNode pointer = new MerkleNode("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB");
144+
Multihash pointer = Multihash.fromBase58("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB");
141145
Map get = ipfs.dht.get(pointer);
142146
Map put = ipfs.dht.put("somekey", "somevalue");
143147
Map findprovs = ipfs.dht.findprovs(pointer);
@@ -160,7 +164,7 @@ public void statsTest() {
160164

161165
public void resolveTest() {
162166
try {
163-
String hash = "QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy";
167+
Multihash hash = Multihash.fromBase58("QmatmE9msSfkKxoffpHwNLNKgwZG8eT9Bud6YoPab52vpy");
164168
Map res = ipfs.resolve("ipns", hash, false);
165169
} catch (IOException e) {
166170
throw new RuntimeException(e);

0 commit comments

Comments
 (0)