Skip to content

Commit 788ba9c

Browse files
committed
Implement new pin update call
1 parent eb3e2c1 commit 788ba9c

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/main/java/io/ipfs/api/IPFS.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ public List<Multihash> rm(Multihash hash, boolean recursive) throws IOException
176176
Map json = retrieveMap("pin/rm?stream-channels=true&r=" + recursive + "&arg=" + hash);
177177
return ((List<Object>) json.get("Pins")).stream().map(x -> Cid.decode((String) x)).collect(Collectors.toList());
178178
}
179+
180+
public Object update(Multihash existing, Multihash modified, boolean unpin) throws IOException {
181+
Object obj = retrieveAndParse("pin/update?stream-channels=true&arg=" + existing + "&arg=" + modified + "&unpin=" + unpin);
182+
return obj;
183+
}
179184
}
180185

181186
/* 'ipfs repo' is a plumbing command used to manipulate the repo.

src/test/java/io/ipfs/api/APITest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,37 @@ public void pinTest() {
201201
}
202202
}
203203

204+
@org.junit.Test
205+
public void pinUpdate() {
206+
try {
207+
MerkleNode child1 = ipfs.add(new NamedStreamable.ByteArrayWrapper("some data".getBytes()));
208+
Multihash hashChild1 = child1.hash;
209+
System.out.println("child1: " + hashChild1);
210+
211+
CborObject.CborMerkleLink root1 = new CborObject.CborMerkleLink(hashChild1);
212+
MerkleNode root1Res = ipfs.block.put(Collections.singletonList(root1.toByteArray()), Optional.of("cbor")).get(0);
213+
System.out.println("root1: " + root1Res.hash);
214+
ipfs.pin.add(root1Res.hash);
215+
216+
CborObject.CborList root2 = new CborObject.CborList(Arrays.asList(new CborObject.CborMerkleLink(hashChild1), new CborObject.CborLong(42)));
217+
MerkleNode root2Res = ipfs.block.put(Collections.singletonList(root2.toByteArray()), Optional.of("cbor")).get(0);
218+
ipfs.pin.update(root1Res.hash, root2Res.hash, true);
219+
220+
Map<Multihash, Object> ls = ipfs.pin.ls(IPFS.PinType.all);
221+
boolean childPresent = ls.containsKey(hashChild1);
222+
if (!childPresent)
223+
throw new IllegalStateException("Child not present!");
224+
225+
ipfs.repo.gc();
226+
Map<Multihash, Object> ls2 = ipfs.pin.ls(IPFS.PinType.all);
227+
boolean childPresentAfterGC = ls2.containsKey(hashChild1);
228+
if (!childPresentAfterGC)
229+
throw new IllegalStateException("Child not present!");
230+
} catch (IOException e) {
231+
throw new RuntimeException(e);
232+
}
233+
}
234+
204235
@org.junit.Test
205236
public void indirectPinTest() {
206237
try {

0 commit comments

Comments
 (0)