Skip to content

Commit 486378b

Browse files
committed
More explicit types, and removed all MerkleNodes that should have been Multihashes
1 parent e602820 commit 486378b

File tree

2 files changed

+66
-33
lines changed

2 files changed

+66
-33
lines changed

src/org/ipfs/IPFS.java

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.stream.*;
77

88
public class IPFS {
9-
9+
public enum PinType {all, direct, indirect, recursive}
1010
public List<String> ObjectTemplates = Arrays.asList("unixfs-dir");
1111

1212
public final String host;
@@ -99,17 +99,30 @@ public List<Multihash> local() throws IOException {
9999
/* Pinning an object ensures a local copy of it is kept.
100100
*/
101101
class Pin {
102-
public Object add(MerkleNode merkleObject) throws IOException {
103-
return retrieveAndParse("pin/add?stream-channels=true&arg=" + merkleObject.hash);
102+
public List<Multihash> add(Multihash hash) throws IOException {
103+
return ((List<Object>)((Map)retrieveAndParse("pin/add?stream-channels=true&arg=" + hash)).get("Pinned"))
104+
.stream()
105+
.map(x -> Multihash.fromBase58((String)x))
106+
.collect(Collectors.toList());
107+
}
108+
109+
public Map<Multihash, Object> ls() throws IOException {
110+
return ls(PinType.direct);
111+
}
112+
113+
public Map<Multihash, Object> ls(PinType type) throws IOException {
114+
return ((Map<String, Object>)(((Map)retrieveAndParse("pin/ls?stream-channels=true&t="+type.name())).get("Keys"))).entrySet()
115+
.stream()
116+
.collect(Collectors.toMap(x -> Multihash.fromBase58(x.getKey()), x-> x.getValue()));
104117
}
105118

106-
public Object ls() throws IOException {
107-
return retrieveAndParse("pin/ls?stream-channels=true");
119+
public List<Multihash> rm(Multihash hash) throws IOException {
120+
return rm(hash, true);
108121
}
109122

110-
public List<MerkleNode> rm(MerkleNode merkleObject, boolean recursive) throws IOException {
111-
Map json = retrieveMap("pin/rm?stream-channels=true&r=" + recursive + "&arg=" + merkleObject.hash);
112-
return ((List<Object>) json.get("Pinned")).stream().map(x -> new MerkleNode((String) x)).collect(Collectors.toList());
123+
public List<Multihash> rm(Multihash hash, boolean recursive) throws IOException {
124+
Map json = retrieveMap("pin/rm?stream-channels=true&r=" + recursive + "&arg=" + hash);
125+
return ((List<Object>) json.get("Pinned")).stream().map(x -> Multihash.fromBase58((String) x)).collect(Collectors.toList());
113126
}
114127
}
115128

@@ -124,8 +137,8 @@ public Object gc() throws IOException {
124137
/* 'ipfs block' is a plumbing command used to manipulate raw ipfs blocks.
125138
*/
126139
class Block {
127-
public byte[] get(MerkleNode merkleObject) throws IOException {
128-
return retrieve("block/get?stream-channels=true&arg=" + merkleObject.hash);
140+
public byte[] get(Multihash hash) throws IOException {
141+
return retrieve("block/get?stream-channels=true&arg=" + hash);
129142
}
130143

131144
public List<MerkleNode> put(List<byte[]> data) throws IOException {
@@ -136,8 +149,8 @@ public List<MerkleNode> put(List<byte[]> data) throws IOException {
136149
return JSONParser.parseStream(res).stream().map(x -> MerkleNode.fromJSON((Map<String, Object>) x)).collect(Collectors.toList());
137150
}
138151

139-
public Map stat(MerkleNode merkleObject) throws IOException {
140-
return retrieveMap("block/stat?stream-channels=true&arg=" + merkleObject.hash);
152+
public Map stat(Multihash hash) throws IOException {
153+
return retrieveMap("block/stat?stream-channels=true&arg=" + hash);
141154
}
142155
}
143156

@@ -152,23 +165,23 @@ public List<MerkleNode> put(List<byte[]> data) throws IOException {
152165
return JSONParser.parseStream(res).stream().map(x -> MerkleNode.fromJSON((Map<String, Object>) x)).collect(Collectors.toList());
153166
}
154167

155-
public MerkleNode get(MerkleNode merkleObject) throws IOException {
156-
Map json = retrieveMap("object/get?stream-channels=true&arg=" + merkleObject.hash);
157-
json.put("Hash", merkleObject.hash.toBase58());
168+
public MerkleNode get(Multihash hash) throws IOException {
169+
Map json = retrieveMap("object/get?stream-channels=true&arg=" + hash);
170+
json.put("Hash", hash.toBase58());
158171
return MerkleNode.fromJSON(json);
159172
}
160173

161-
public MerkleNode links(MerkleNode merkleObject) throws IOException {
162-
Map json = retrieveMap("object/links?stream-channels=true&arg=" + merkleObject.hash);
174+
public MerkleNode links(Multihash hash) throws IOException {
175+
Map json = retrieveMap("object/links?stream-channels=true&arg=" + hash);
163176
return MerkleNode.fromJSON(json);
164177
}
165178

166-
public Map<String, Object> stat(MerkleNode merkleObject) throws IOException {
167-
return retrieveMap("object/stat?stream-channels=true&arg=" + merkleObject.hash);
179+
public Map<String, Object> stat(Multihash hash) throws IOException {
180+
return retrieveMap("object/stat?stream-channels=true&arg=" + hash);
168181
}
169182

170-
public byte[] data(MerkleNode merkleObject) throws IOException {
171-
return retrieve("object/data?stream-channels=true&arg=" + merkleObject.hash);
183+
public byte[] data(Multihash hash) throws IOException {
184+
return retrieve("object/data?stream-channels=true&arg=" + hash);
172185
}
173186

174187
public MerkleNode _new(Optional<String> template) throws IOException {
@@ -182,16 +195,16 @@ public MerkleNode _new(Optional<String> template) throws IOException {
182195
}
183196

184197
class Name {
185-
public Map publish(MerkleNode node) throws IOException {
186-
return publish(Optional.empty(), node);
198+
public Map publish(Multihash hash) throws IOException {
199+
return publish(Optional.empty(), hash);
187200
}
188201

189-
public Map publish(Optional<String> id, MerkleNode node) throws IOException {
190-
return retrieveMap("name/publish?arg=" + (id.isPresent() ? id+"&arg=" : "") + "/ipfs/"+node.hash);
202+
public Map publish(Optional<String> id, Multihash hash) throws IOException {
203+
return retrieveMap("name/publish?arg=" + (id.isPresent() ? id+"&arg=" : "") + "/ipfs/"+hash);
191204
}
192205

193-
public String resolve(Multihash addr) throws IOException {
194-
Map res = (Map) retrieveAndParse("name/resolve?arg=" + addr);
206+
public String resolve(Multihash hash) throws IOException {
207+
Map res = (Map) retrieveAndParse("name/resolve?arg=" + hash);
195208
return (String)res.get("Path");
196209
}
197210
}

test/org/ipfs/Test.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,35 @@ public void fileTest(NamedStreamable file) {
4848
byte[] getResult = ipfs.get(addResult.hash);
4949
if (!Arrays.equals(catResult, file.getContents()))
5050
throw new IllegalStateException("Different contents!");
51-
List<MerkleNode> pinRm = ipfs.pin.rm(addResult, true);
52-
if (!pinRm.get(0).equals(addResult))
51+
List<Multihash> pinRm = ipfs.pin.rm(addResult.hash, true);
52+
if (!pinRm.get(0).equals(addResult.hash))
5353
throw new IllegalStateException("Didn't remove file!");
5454
Object gc = ipfs.repo.gc();
5555
} catch (IOException e) {
5656
throw new RuntimeException(e);
5757
}
5858
}
5959

60+
@org.junit.Test
61+
public void pinTest() {
62+
try {
63+
MerkleNode file = ipfs.add(new NamedStreamable.ByteArrayWrapper("some data".getBytes()));
64+
Multihash hash = file.hash;
65+
Map<Multihash, Object> ls1 = ipfs.pin.ls(IPFS.PinType.all);
66+
boolean pinned = ls1.containsKey(hash);
67+
List<Multihash> rm = ipfs.pin.rm(hash);
68+
// second rm should not throw a http 500, but return an empty list
69+
// List<Multihash> rm2 = ipfs.pin.rm(hash);
70+
List<Multihash> add2 = ipfs.pin.add(hash);
71+
// adding something already pinned should succeed
72+
List<Multihash> add3 = ipfs.pin.add(hash);
73+
Map<Multihash, Object> ls = ipfs.pin.ls();
74+
System.out.println(ls);
75+
} catch (IOException e) {
76+
throw new RuntimeException(e);
77+
}
78+
}
79+
6080
@org.junit.Test
6181
public void refsTest() {
6282
try {
@@ -73,7 +93,7 @@ public void refsTest() {
7393
public void objectTest() {
7494
try {
7595
MerkleNode _new = ipfs.object._new(Optional.empty());
76-
MerkleNode pointer = new MerkleNode("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB");
96+
Multihash pointer = Multihash.fromBase58("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB");
7797
MerkleNode object = ipfs.object.get(pointer);
7898
List<MerkleNode> newPointer = ipfs.object.put(Arrays.asList(object.toJSONString().getBytes()));
7999
MerkleNode links = ipfs.object.links(pointer);
@@ -88,8 +108,8 @@ public void objectTest() {
88108
public void blockTest() {
89109
try {
90110
MerkleNode pointer = new MerkleNode("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB");
91-
Map stat = ipfs.block.stat(pointer);
92-
byte[] object = ipfs.block.get(pointer);
111+
Map stat = ipfs.block.stat(pointer.hash);
112+
byte[] object = ipfs.block.get(pointer.hash);
93113
List<MerkleNode> newPointer = ipfs.block.put(Arrays.asList("Some random data...".getBytes()));
94114
} catch (IOException e) {
95115
throw new RuntimeException(e);
@@ -113,7 +133,7 @@ public void fileTest() {
113133
public void nameTest() {
114134
try {
115135
MerkleNode pointer = new MerkleNode("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB");
116-
Map pub = ipfs.name.publish(pointer);
136+
Map pub = ipfs.name.publish(pointer.hash);
117137
String resolved = ipfs.name.resolve(Multihash.fromBase58((String) pub.get("Name")));
118138
} catch (IOException e) {
119139
throw new RuntimeException(e);

0 commit comments

Comments
 (0)