Commit 075297d3 authored by Vladislav Bashkirev's avatar Vladislav Bashkirev

Implement Pin#check method

Method checks if all attachments found in Redis are pinned.
parent 6d1680bf
...@@ -129,6 +129,7 @@ to pin attachments. ...@@ -129,6 +129,7 @@ to pin attachments.
* [Pin](#Pin) * [Pin](#Pin)
* [new Pin()](#new_Pin_new) * [new Pin()](#new_Pin_new)
* [.all()](#Pin+all)<code>Promise</code> * [.all()](#Pin+all)<code>Promise</code>
* [.check()](#Pin+check)<code>Promise</code>
<a name="new_Pin_new"></a> <a name="new_Pin_new"></a>
...@@ -142,6 +143,14 @@ Pins attachments to local storage for which there are records in Redis. ...@@ -142,6 +143,14 @@ Pins attachments to local storage for which there are records in Redis.
**Kind**: instance method of [<code>Pin</code>](#Pin) **Kind**: instance method of [<code>Pin</code>](#Pin)
**Returns**: <code>Promise</code> - Number of pinned attachments. **Returns**: <code>Promise</code> - Number of pinned attachments.
<a name="Pin+check"></a>
### pin.check() ⇒ <code>Promise</code>
Checks if IPFS objects for hashes found in Redis are stored locally.
**Kind**: instance method of [<code>Pin</code>](#Pin)
**Returns**: <code>Promise</code> - Resolves with array of unpinned hashes or rejects with
error. If array is empty, all hashes are pinned.
<a name="Cleaner"></a> <a name="Cleaner"></a>
## Cleaner ## Cleaner
......
...@@ -148,6 +148,7 @@ to pin attachments. ...@@ -148,6 +148,7 @@ to pin attachments.
* [Pin](#Pin) * [Pin](#Pin)
* [new Pin()](#new_Pin_new) * [new Pin()](#new_Pin_new)
* [.all()](#Pin+all)<code>Promise</code> * [.all()](#Pin+all)<code>Promise</code>
* [.check()](#Pin+check)<code>Promise</code>
<a name="new_Pin_new"></a> <a name="new_Pin_new"></a>
...@@ -161,6 +162,14 @@ Pins attachments to local storage for which there are records in Redis. ...@@ -161,6 +162,14 @@ Pins attachments to local storage for which there are records in Redis.
**Kind**: instance method of [<code>Pin</code>](#Pin) **Kind**: instance method of [<code>Pin</code>](#Pin)
**Returns**: <code>Promise</code> - Number of pinned attachments. **Returns**: <code>Promise</code> - Number of pinned attachments.
<a name="Pin+check"></a>
### pin.check() ⇒ <code>Promise</code>
Checks if IPFS objects for hashes found in Redis are stored locally.
**Kind**: instance method of [<code>Pin</code>](#Pin)
**Returns**: <code>Promise</code> - Resolves with array of unpinned hashes or rejects with
error. If array is empty, all hashes are pinned.
<a name="Cleaner"></a> <a name="Cleaner"></a>
## Cleaner ## Cleaner
......
...@@ -47,6 +47,28 @@ class Pin { ...@@ -47,6 +47,28 @@ class Pin {
throw error; throw error;
}); });
} }
/**
* Checks if IPFS objects for hashes found in Redis are stored locally.
* @return {Promise} Resolves with array of unpinned hashes or rejects with
* error. If array is empty, all hashes are pinned.
*/
check() {
logging.info('Checking if all attachments are locally stored');
return Promise.join(
this.ipfs.refs.local(),
redis.keysAsync(this.prefix + '*')
).spread((refs, keys) => {
let hashes = keys.map((key) => key.split('_')[1]);
logging.info(`Found ${hashes.length} attachments
and ${refs.length} local objects`);
return hashes.filter(refs.includes);
}).catch((error) => {
throw error;
});
}
} }
module.exports = Pin; module.exports = Pin;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment