From f9942b13158c5e4a522d31ffa87a4c9cf0e78e1a Mon Sep 17 00:00:00 2001 From: Marcel Otto Date: Fri, 8 Apr 2022 03:23:16 +0200 Subject: [PATCH] Add RDF.Data.empty?/1 --- CHANGELOG.md | 7 +++++-- lib/rdf/data.ex | 11 +++++++++++ test/unit/data_test.exs | 15 +++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49e2a8d..8562880 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 - `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 - are significantly faster than `Enum.empty?/1` +- `RDF.Description.empty?/1`, `RDF.Graph.empty?/1`, `RDF.Dataset.empty?/1` and + `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 supports a default value - new guards in `RDF.Guards`: `is_statement/1` and `is_quad/1` diff --git a/lib/rdf/data.ex b/lib/rdf/data.ex index c9cd831..f0372b6 100644 --- a/lib/rdf/data.ex +++ b/lib/rdf/data.ex @@ -31,6 +31,11 @@ defprotocol RDF.Data do """ def pop(data) + @doc """ + Returns if the given RDF data structure is empty. + """ + def empty?(data) + @doc """ 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 empty?(description), do: Description.empty?(description) + def 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 empty?(graph), do: Graph.empty?(graph) + def include?(graph, input, opts \\ []), do: Graph.include?(graph, input, opts) def describes?(graph, subject), @@ -343,6 +352,8 @@ defimpl RDF.Data, for: RDF.Dataset do 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 describes?(dataset, subject), diff --git a/test/unit/data_test.exs b/test/unit/data_test.exs index 068c945..a20d17e 100644 --- a/test/unit/data_test.exs +++ b/test/unit/data_test.exs @@ -90,6 +90,11 @@ defmodule RDF.DataTest do assert RDF.Data.pop(description) == Description.pop(description) 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 assert RDF.Data.include?(description, {EX.S, 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) 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 assert RDF.Data.include?(graph, {EX.S, EX.p1(), EX.O2}) 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) 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 assert RDF.Data.include?(dataset, {EX.S, EX.p1(), EX.O2}) assert RDF.Data.include?(dataset, {EX.S2, EX.p2(), EX.O3})