Skip to content

Commit

Permalink
SYN-6652: --deledges option to delnode (#3503)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSquires authored Jan 23, 2024
1 parent 0e162b1 commit e4621cb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
21 changes: 21 additions & 0 deletions synapse/lib/storm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4567,12 +4567,15 @@ def getArgParser(self):
pars.add_argument('--delbytes', default=False, action='store_true',
help='For file:bytes nodes, remove the bytes associated with the '
'sha256 property from the axon as well if present.')
pars.add_argument('--deledges', default=False, action='store_true',
help='Delete N2 light edges before deleting the node.')
return pars

async def execStormCmd(self, runt, genr):

force = await s_stormtypes.tobool(self.opts.force)
delbytes = await s_stormtypes.tobool(self.opts.delbytes)
deledges = await s_stormtypes.tobool(self.opts.deledges)

if force:
if runt.user is not None and not runt.isAdmin():
Expand All @@ -4592,6 +4595,24 @@ async def execStormCmd(self, runt, genr):

runt.layerConfirm(('node', 'del', node.form.name))

if deledges:
async with await s_spooled.Set.anit(dirn=self.runt.snap.core.dirn) as edges:
seenverbs = set()

async for (verb, n2iden) in node.iterEdgesN2():
if verb not in seenverbs:
runt.layerConfirm(('node', 'edge', 'del', verb))
seenverbs.add(verb)
await edges.add((verb, n2iden))

async with self.runt.snap.getEditor() as editor:
async for (verb, n2iden) in edges:
n2 = await editor.getNodeByBuid(s_common.uhex(n2iden))
if n2 is not None:
if await n2.delEdge(verb, node.iden()) and len(editor.protonodes) >= 1000:
await self.runt.snap.applyNodeEdits(editor.getNodeEdits())
editor.protonodes.clear()

if delbytes and node.form.name == 'file:bytes':
sha256 = node.props.get('sha256')
if sha256:
Expand Down
53 changes: 53 additions & 0 deletions synapse/tests/test_lib_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,59 @@ async def test_node_delete(self):
msgs = await core.stormlist(edgeq)
self.len(1, [m for m in msgs if m[0] == 'print'])

q = '''
[test:str=delfoo test:str=delbar]
{ test:str=delfoo [ +(bar)> { test:str=delbar } ] }
{ test:str=delbar [ +(foo)> { test:str=delfoo } ] }
'''
nodes = await core.nodes(q)
self.len(2, nodes)

foo, bar = nodes
fooedges = [edge async for edge in foo.iterEdgesN1()]
baredges = [edge async for edge in bar.iterEdgesN1()]

self.len(2, fooedges)
self.len(2, baredges)

msgs = await core.stormlist('test:str=delfoo | delnode')
self.stormIsInErr('Other nodes still have light edges to this node.', msgs)

nodes = await core.nodes('test:str=delfoo')
self.len(1, nodes)

msgs = await core.stormlist('test:str=delfoo | delnode --deledges')
self.stormHasNoWarnErr(msgs)

nodes = await core.nodes('test:str=delfoo')
self.len(0, nodes)

msgs = await core.stormlist('test:str=delbar | delnode')
self.stormHasNoWarnErr(msgs)

nodes = await core.nodes('[test:str=delfoo]')
self.len(1, nodes)
foo = nodes[0]

q = '''
for $ii in $lib.range(1200) {
$valu = `bar{$ii}`
[ test:str=$valu +(foo)> { test:str=delfoo } ]
}
'''
msgs = await core.stormlist(q)
self.stormHasNoWarnErr(msgs)

fooedges = [edge async for edge in foo.iterEdgesN2()]

self.len(1200, fooedges)

msgs = await core.stormlist('test:str=delfoo | delnode --deledges')
self.stormHasNoWarnErr(msgs)

nodes = await core.nodes('test:str=delfoo')
self.len(0, nodes)

async def test_node_remove_missing_basetag(self):

async with self.getTestCore() as core:
Expand Down

0 comments on commit e4621cb

Please sign in to comment.