Skip to content

Commit

Permalink
Merge pull request #146 from autonomys/populate-cache-onarchival
Browse files Browse the repository at this point in the history
feat: populate cache on archival
  • Loading branch information
clostao authored Jan 27, 2025
2 parents 9aa8266 + 0334070 commit cbf9b3d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
8 changes: 6 additions & 2 deletions backend/__tests__/e2e/uploads/files.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
import { FileGateway } from '../../../src/services/dsn/fileGateway/index.js'
import { jest } from '@jest/globals'
import { downloadService } from '../../../src/services/download/index.js'
import { handleArchivedObjects } from '../../../src/services/upload/nodeRemover/index.js'

const files = [
{
Expand Down Expand Up @@ -315,14 +316,17 @@ files.map((file, index) => {
})

it('should be able to archive the object', async () => {
await ObjectUseCases.markAsArchived(cid)
await handleArchivedObjects()

const metadata = await metadataRepository.getMetadata(cid)
expect(metadata).not.toBeNull()
expect(metadata?.is_archived).toBe(true)

expect(memoryDownloadCache.has(cid)).toBe(true)
expect(await databaseDownloadCache.has(cid)).toBe(true)
})

it('should be able to remove the nodes', async () => {
it('should be able to downloaded from gateway', async () => {
await databaseDownloadCache.clear()
await memoryDownloadCache.clear()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ exports.down = function (db) {
return new Promise(function (resolve, reject) {
fs.readFile(filePath, { encoding: 'utf-8' }, function (err, data) {
if (err) return reject(err)
console.log('received data: ' + data)

resolve(data)
})
Expand Down
4 changes: 2 additions & 2 deletions backend/src/services/download/databaseDownloadCache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ const get = async function* (cid: string): AsyncIterable<Buffer> {
}
}

const has = async (cid: string) => {
const has = async (cid: string): Promise<boolean> => {
const entry = await registryRepository.getEntry(cid)

return !!entry
return Boolean(entry).valueOf()
}

export const clear = async () => {
Expand Down
4 changes: 3 additions & 1 deletion backend/src/services/upload/nodeRemover/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ObjectUseCases } from '../../../useCases/index.js'
import { safeCallback } from '../../../utils/safe.js'
import { downloadService } from '../../download/index.js'

const state = {
executing: false,
time: 10_000,
}

const handleArchivedObjects = safeCallback(async () => {
export const handleArchivedObjects = safeCallback(async () => {
if (state.executing) {
return
}
Expand All @@ -19,6 +20,7 @@ const handleArchivedObjects = safeCallback(async () => {
for (const cid of cids) {
const hasAllNodesArchived = await ObjectUseCases.hasAllNodesArchived(cid)
if (hasAllNodesArchived) {
await downloadService.download(cid)
await ObjectUseCases.markAsArchived(cid)
}
}
Expand Down

0 comments on commit cbf9b3d

Please sign in to comment.