Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add size() API to axon and stormtypes #2020

Merged
merged 1 commit into from Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions synapse/axon.py
Expand Up @@ -188,6 +188,10 @@ async def has(self, sha256):
await self._reqUserAllowed(('axon', 'has'))
return await self.cell.has(sha256)

async def size(self, sha256):
await self._reqUserAllowed(('axon', 'has'))
return await self.cell.size(sha256)

async def hashes(self, offs):
await self._reqUserAllowed(('axon', 'has'))
async for item in self.cell.hashes(offs):
Expand Down Expand Up @@ -344,6 +348,11 @@ async def upload(self):
async def has(self, sha256):
return self.axonslab.get(sha256, db=self.sizes) is not None

async def size(self, sha256):
byts = self.axonslab.get(sha256, db=self.sizes)
if byts is not None:
return int.from_bytes(byts, 'big')

async def metrics(self):
return dict(self.axonmetrics.items())

Expand Down
20 changes: 20 additions & 0 deletions synapse/lib/stormtypes.py
Expand Up @@ -1195,6 +1195,7 @@ def getObjLocals(self):
return {
'put': self._libBytesPut,
'has': self._libBytesHas,
'size': self._libBytesSize,
'upload': self._libBytesUpload,
}

Expand Down Expand Up @@ -1242,6 +1243,25 @@ async def _libBytesHas(self, sha256):
ret = await self.dyncall('axon', todo)
return ret

async def _libBytesSize(self, sha256):
'''
Return the size of the bytes stored in the Axon for the given sha256.

Args:
sha256 (str): The sha256 value to check.

Examples:

$size = $lib.bytes.size($sha256)

Returns:
int: The size of the file or $lib.null if the file is not found.
'''
await self.runt.snap.core.getAxon()
todo = s_common.todo('size', s_common.uhex(sha256))
ret = await self.dyncall('axon', todo)
return ret

async def _libBytesPut(self, byts):
'''
Save the given bytes variable to the Axon the Cortex is configured to use.
Expand Down
1 change: 1 addition & 0 deletions synapse/tests/test_axon.py
Expand Up @@ -70,6 +70,7 @@ async def runAxonTestBase(self, axon):
self.eq(b'asdfasdf', b''.join(bytz))

self.true(await axon.has(asdfhash))
self.eq(8, await axon.size(asdfhash))

logger.info('bbufhash test')

Expand Down
3 changes: 3 additions & 0 deletions synapse/tests/test_lib_stormtypes.py
Expand Up @@ -1534,6 +1534,9 @@ async def test_storm_lib_bytes(self):
nodes = await core.nodes(text, opts=opts)
self.len(2, nodes)

opts = {'vars': {'sha256': asdfhash_h}}
self.eq(8, await core.callStorm('return($lib.bytes.size($sha256))', opts=opts))

self.eq(nodes[0].ndef, ('test:int', 8))
self.eq(nodes[1].ndef, ('test:str', asdfhash_h))

Expand Down