batch_blob_store.js 902 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict'
  2. const BPromise = require('bluebird')
  3. /**
  4. * @constructor
  5. * @param {BlobStore} blobStore
  6. * @classdesc
  7. * Wrapper for BlobStore that pre-fetches blob metadata to avoid making one
  8. * database call per blob lookup.
  9. */
  10. function BatchBlobStore(blobStore) {
  11. this.blobStore = blobStore
  12. this.blobs = new Map()
  13. }
  14. /**
  15. * Pre-fetch metadata for the given blob hashes.
  16. *
  17. * @param {Array.<string>} hashes
  18. * @return {Promise}
  19. */
  20. BatchBlobStore.prototype.preload = function batchBlobStorePreload(hashes) {
  21. return BPromise.each(this.blobStore.getBlobs(hashes), blob => {
  22. this.blobs.set(blob.getHash(), blob)
  23. })
  24. }
  25. /**
  26. * @see BlobStore#getBlob
  27. */
  28. BatchBlobStore.prototype.getBlob = BPromise.method(
  29. function batchBlobStoreGetBlob(hash) {
  30. const blob = this.blobs.get(hash)
  31. if (blob) return blob
  32. return this.blobStore.getBlob(hash)
  33. }
  34. )
  35. module.exports = BatchBlobStore