Add RDF.Data.empty?/1

This commit is contained in:
Marcel Otto 2022-04-08 03:23:16 +02:00
parent 74f1d9b804
commit f9942b1315
3 changed files with 31 additions and 2 deletions

View file

@ -11,8 +11,11 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
- a `RDF.Graph` builder DSL available under the `RDF.Graph.build/2` function - a `RDF.Graph` builder DSL available under the `RDF.Graph.build/2` function
- `RDF.Graph.new/2` and `RDF.Graph.add/2` support the addition of `RDF.Dataset`s - `RDF.Graph.new/2` and `RDF.Graph.add/2` support the addition of `RDF.Dataset`s
- `RDF.Description.empty?/1`, `RDF.Graph.empty?/1` and `RDF.Dataset.empty?/1` which - `RDF.Description.empty?/1`, `RDF.Graph.empty?/1`, `RDF.Dataset.empty?/1` and
are significantly faster than `Enum.empty?/1` `RDF.Data.empty?/1` which are significantly faster than `Enum.empty?/1`
- By replacing all `Enum.empty?/1` uses over the RDF data structures with these
new `empty?/1` functions throughout the code base, several functions benefit
from this performance improvement.
- `RDF.Description.first/2` now has a `RDF.Description.first/3` variant which - `RDF.Description.first/2` now has a `RDF.Description.first/3` variant which
supports a default value supports a default value
- new guards in `RDF.Guards`: `is_statement/1` and `is_quad/1` - new guards in `RDF.Guards`: `is_statement/1` and `is_quad/1`

View file

@ -31,6 +31,11 @@ defprotocol RDF.Data do
""" """
def pop(data) def pop(data)
@doc """
Returns if the given RDF data structure is empty.
"""
def empty?(data)
@doc """ @doc """
Checks if the given statement exists within a RDF data structure. Checks if the given statement exists within a RDF data structure.
""" """
@ -175,6 +180,8 @@ defimpl RDF.Data, for: RDF.Description do
def pop(description), do: Description.pop(description) def pop(description), do: Description.pop(description)
def empty?(description), do: Description.empty?(description)
def include?(description, input, opts \\ []), def include?(description, input, opts \\ []),
do: Description.include?(description, input, opts) do: Description.include?(description, input, opts)
@ -275,6 +282,8 @@ defimpl RDF.Data, for: RDF.Graph do
def pop(graph), do: Graph.pop(graph) def pop(graph), do: Graph.pop(graph)
def empty?(graph), do: Graph.empty?(graph)
def include?(graph, input, opts \\ []), do: Graph.include?(graph, input, opts) def include?(graph, input, opts \\ []), do: Graph.include?(graph, input, opts)
def describes?(graph, subject), def describes?(graph, subject),
@ -343,6 +352,8 @@ defimpl RDF.Data, for: RDF.Dataset do
def pop(dataset), do: Dataset.pop(dataset) def pop(dataset), do: Dataset.pop(dataset)
def empty?(dataset), do: Dataset.empty?(dataset)
def include?(dataset, input, opts), do: Dataset.include?(dataset, input, opts) def include?(dataset, input, opts), do: Dataset.include?(dataset, input, opts)
def describes?(dataset, subject), def describes?(dataset, subject),

View file

@ -90,6 +90,11 @@ defmodule RDF.DataTest do
assert RDF.Data.pop(description) == Description.pop(description) assert RDF.Data.pop(description) == Description.pop(description)
end end
test "empty?", %{description: description} do
assert RDF.Data.empty?(description) == false
assert RDF.Data.empty?(RDF.description(EX.S)) == true
end
test "include?", %{description: description} do test "include?", %{description: description} do
assert RDF.Data.include?(description, {EX.S, EX.p1(), EX.O2}) assert RDF.Data.include?(description, {EX.S, EX.p1(), EX.O2})
refute RDF.Data.include?(description, {EX.Other, EX.p1(), EX.O2}) refute RDF.Data.include?(description, {EX.Other, EX.p1(), EX.O2})
@ -258,6 +263,11 @@ defmodule RDF.DataTest do
assert RDF.Data.pop(graph) == Graph.pop(graph) assert RDF.Data.pop(graph) == Graph.pop(graph)
end end
test "empty?", %{graph: graph} do
assert RDF.Data.empty?(graph) == false
assert RDF.Data.empty?(RDF.graph()) == true
end
test "include?", %{graph: graph} do test "include?", %{graph: graph} do
assert RDF.Data.include?(graph, {EX.S, EX.p1(), EX.O2}) assert RDF.Data.include?(graph, {EX.S, EX.p1(), EX.O2})
assert RDF.Data.include?(graph, {EX.S2, EX.p2(), EX.O3}) assert RDF.Data.include?(graph, {EX.S2, EX.p2(), EX.O3})
@ -419,6 +429,11 @@ defmodule RDF.DataTest do
assert RDF.Data.pop(dataset) == Dataset.pop(dataset) assert RDF.Data.pop(dataset) == Dataset.pop(dataset)
end end
test "empty?", %{dataset: dataset} do
assert RDF.Data.empty?(dataset) == false
assert RDF.Data.empty?(RDF.dataset()) == true
end
test "include?", %{dataset: dataset} do test "include?", %{dataset: dataset} do
assert RDF.Data.include?(dataset, {EX.S, EX.p1(), EX.O2}) assert RDF.Data.include?(dataset, {EX.S, EX.p1(), EX.O2})
assert RDF.Data.include?(dataset, {EX.S2, EX.p2(), EX.O3}) assert RDF.Data.include?(dataset, {EX.S2, EX.p2(), EX.O3})