Skip to content

Commit

Permalink
Util: start using the GenQuery2 parser
Browse files Browse the repository at this point in the history
  • Loading branch information
lwesterhof committed Dec 18, 2024
1 parent 966dbd5 commit 4b477d0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
30 changes: 16 additions & 14 deletions util/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import itertools
import json
from functools import reduce
from typing import Iterable, List, Tuple

import genquery
Expand All @@ -18,9 +17,12 @@

def exists(ctx: rule.Context, path: str) -> bool:
"""Check if a collection with the given path exists."""
return len(list(genquery.row_iterator(
"COLL_ID", "COLL_NAME = '{}'".format(path),
genquery.AS_LIST, ctx))) > 0
return len(list(
genquery.Query(ctx,
"COLL_ID",
f"COLL_NAME = '{path}'",
output=genquery.AS_LIST,
parser=genquery.Parser.GENQUERY2))) > 0


def owner(ctx: rule.Context, path: str) -> Tuple[str, str] | None:
Expand All @@ -46,16 +48,16 @@ def empty(ctx: rule.Context, path: str) -> bool:

def size(ctx: rule.Context, path: str) -> int:
"""Get a collection's size in bytes."""
def func(x: int, row: List) -> int:
return x + int(row[1])

return reduce(func,
itertools.chain(genquery.row_iterator("DATA_ID, DATA_SIZE",
"COLL_NAME like '{}'".format(path),
genquery.AS_LIST, ctx),
genquery.row_iterator("DATA_ID, DATA_SIZE",
"COLL_NAME like '{}/%'".format(path),
genquery.AS_LIST, ctx)), 0)
return sum(
row[1]
for row in genquery.Query(
ctx,
"DATA_ID, DATA_SIZE",
f"COLL_NAME like '{path}' or COLL_NAME like '{path}/%'",
output=genquery.AS_LIST,
parser=genquery.Parser.GENQUERY2,
)
)


def data_count(ctx: rule.Context, path: str, recursive: bool = True) -> int:
Expand Down
11 changes: 7 additions & 4 deletions util/data_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@

def exists(ctx: rule.Context, path: str) -> bool:
"""Check if a data object with the given path exists."""
return len(list(genquery.row_iterator(
"DATA_ID",
"COLL_NAME = '%s' AND DATA_NAME = '%s'" % pathutil.chop(path),
genquery.AS_LIST, ctx))) > 0
coll_name, data_name = pathutil.chop(path)
return len(list(
genquery.Query(ctx,
"DATA_ID",
f"COLL_NAME = '{coll_name}' AND DATA_NAME = '{data_name}'",
output=genquery.AS_LIST,
parser=genquery.Parser.GENQUERY2))) > 0


def get_properties(ctx: rule.Context, data_id: str, resource: str) -> Dict | None:
Expand Down

0 comments on commit 4b477d0

Please sign in to comment.