Migrating from maplib to fastrdf

fastrdf is the continuation of maplib. It is the same library with the same API: Model, the templates, SPARQL, SHACL, Datalog and the virtualization work as before. What changed is the name, everywhere the name shows, and the license.

A migration has four parts:

  1. Install fastrdf and change the imports.

  2. Change the namespaces in queries, templates and shapes.

  3. Rewrite the maplib IRIs in stored data.

  4. Rename the environment variables.

Read the license first: it decides whether you may use fastrdf where you used maplib.

What changed at a glance

maplib

fastrdf

Package

pip install maplib

pip install fastrdf

Import

from maplib import Model

from fastrdf import Model

Python

3.10 or newer

3.12 or newer

License

Apache 2.0, with a separate Enterprise Edition

Business Source License 1.1, one package with every feature

Vocabulary

https://datatreehouse.github.io/maplib/vocab#

https://datatreehouse.github.io/fastrdf/vocab#

Default prefix

maplib:

fastrdf:

Environment variables

MAPLIB_*

FASTRDF_*

Documentation

https://datatreehouse.github.io/maplib/

https://datatreehouse.github.io/fastrdf/

The license

maplib is open source under the Apache License 2.0, and its Enterprise Edition was a separate package. fastrdf is one package under the Business Source License 1.1, which is not an open source license:

  • Anyone may use fastrdf for free outside production: evaluation, development, testing and research.

  • Accredited educational institutions may also use it for free in production.

  • All other production use requires a commercial license from Data Treehouse AS, info@data-treehouse.com.

  • Each version becomes available under the Apache License 2.0 four years after it is published.

pip install fastrdf installs the whole library, including what was maplib Enterprise Edition. There is no Enterprise Edition of fastrdf. The released versions of maplib stay available under their own license.

1. The package and the imports

pip uninstall maplib
pip install fastrdf

The module is fastrdf, and every submodule kept its name:

maplib

fastrdf

import maplib

import fastrdf

from maplib import Model, Template, IRI, ...

from fastrdf import Model, Template, IRI, ...

maplib.virtualization

fastrdf.virtualization

maplib.config

fastrdf.config

maplib.storage

fastrdf.storage

maplib.opcua

fastrdf.opcua

maplib.cim

fastrdf.cim

python -m maplib.serve

python -m fastrdf.serve

In most code, replacing maplib with fastrdf in the import lines is the whole change. The names of classes, methods and arguments are the same.

fastrdf needs Python 3.12 or newer, where maplib ran on 3.10. It also installs the packages of the Graph portal that Model.explore() runs, such as FastAPI and pandas.

The file extension of serialized models is .fastrdf by convention, where it was .maplib. It is only a convention: Model.deserialize reads a file by its content. A file that fastrdf writes starts with the bytes FASTRDF, where a file from maplib starts with MAPLIB. fastrdf reads both, so the serialized models that maplib wrote still open; maplib does not read the files that fastrdf writes. See maplib IRIs in stored data for the IRIs inside them.

2. Namespaces in queries, templates and shapes

fastrdf does not read IRIs in the maplib namespaces as its own. An IRI such as https://datatreehouse.github.io/maplib/vocab#shortestPath is an ordinary IRI to fastrdf: a query that uses it as a magic predicate matches triples with that predicate, finds none, and gives no solutions and no error.

Replace the namespaces in every SPARQL query, stOTTR template, SHACL shape, Datalog rule and mapping that you keep:

maplib

fastrdf

What it holds

https://datatreehouse.github.io/maplib/vocab#

https://datatreehouse.github.io/fastrdf/vocab#

The vocabulary: functions, magic predicates, time series terms, the default and transient graphs. See the vocabulary

https://datatreehouse.github.io/maplib/config#

https://datatreehouse.github.io/fastrdf/config#

The config graph

urn:maplib_default:

urn:fastrdf_default:

Predicates and templates that map_default generates

urn:maplib_osipi:

urn:fastrdf_osipi:

The model that map_osipi and map_pi_points write

urn:maplib_insights_hub:

urn:fastrdf_insights_hub:

The model that map_insights_hub writes

urn:maplib:

urn:fastrdf:

Generated IRIs, e.g. of the nodes of map_json and map_xml, of validation reports and of config items

The prefix that is known without a declaration is now fastrdf:, not maplib:. A query that used maplib: without declaring it now fails to parse; a query that declares PREFIX maplib: <https://datatreehouse.github.io/maplib/vocab#> parses and finds nothing. Either change the prefix, or keep the name and point it at the new namespace:

# before
PREFIX maplib: <https://datatreehouse.github.io/maplib/vocab#>
SELECT ?t ?len WHERE { ex:a maplib:shortestPath [ maplib:object ?t ; maplib:predicate ex:p ; maplib:length ?len ] }

# after
PREFIX fastrdf: <https://datatreehouse.github.io/fastrdf/vocab#>
SELECT ?t ?len WHERE { ex:a fastrdf:shortestPath [ fastrdf:object ?t ; fastrdf:predicate ex:p ; fastrdf:length ?len ] }

The same holds in Python for the constants: maplib.DEFAULT_GRAPH and maplib.TRANSIENT_GRAPH are fastrdf.DEFAULT_GRAPH and fastrdf.TRANSIENT_GRAPH, with IRIs in the new namespace. Code that uses the constants needs no other change; code that has the IRIs as strings does.

One older namespace is still read: IRIs in the former chrontext namespace, https://github.com/DataTreehouse/chrontext#, are read as the term with the same name in the fastrdf vocabulary, in queries, in templates and as predicates of data. Queries and data that use chrontext:hasTimeseries and the other time series terms keep working.

3. maplib IRIs in stored data

Data that maplib wrote may hold IRIs in the maplib namespaces: the time series terms (hasResource, hasExternalId, hasTimeseries), a config graph, the models of map_osipi and map_insights_hub, validation reports, and the predicates of map_default. fastrdf does not recognise them. This applies to RDF files and to serialized models alike.

For data that you can build again, run the mappings again with fastrdf. For other data, read it with fastrdf and rewrite the IRIs with a SPARQL update, one graph at a time:

from fastrdf import Model

NAMESPACES = {
    "https://datatreehouse.github.io/maplib/vocab#": "https://datatreehouse.github.io/fastrdf/vocab#",
    "https://datatreehouse.github.io/maplib/config#": "https://datatreehouse.github.io/fastrdf/config#",
    "urn:maplib_default:": "urn:fastrdf_default:",
    "urn:maplib_osipi:": "urn:fastrdf_osipi:",
    "urn:maplib_insights_hub:": "urn:fastrdf_insights_hub:",
}


def rename_namespace(m, old, new, graph=None):
    """Moves the predicates and the IRI objects of a graph from one namespace to another."""
    m.update(f"""
        DELETE {{ ?s ?p ?o }} INSERT {{ ?s ?p2 ?o }} WHERE {{
            ?s ?p ?o . FILTER(STRSTARTS(STR(?p), "{old}"))
            BIND(IRI(CONCAT("{new}", STRAFTER(STR(?p), "{old}"))) AS ?p2) }}""", graph=graph)
    m.update(f"""
        DELETE {{ ?s ?p ?o }} INSERT {{ ?s ?p ?o2 }} WHERE {{
            ?s ?p ?o . FILTER(isIRI(?o) && STRSTARTS(STR(?o), "{old}"))
            BIND(IRI(CONCAT("{new}", STRAFTER(STR(?o), "{old}"))) AS ?o2) }}""", graph=graph)


m = Model.deserialize("model.maplib")   # or m = Model(); m.read("data.ttl")
for old, new in NAMESPACES.items():
    rename_namespace(m, old, new)       # the default graph; pass graph="..." for each named graph
m.serialize("model.fastrdf")

Notes:

  • The update covers predicates and objects. Subjects in a maplib namespace are rare; the config items urn:maplib:config:... are the ones to look for. Rewrite them the same way with a third update on ?s, or build the config graph again with fastrdf.config.

  • IRIs in urn:maplib: that maplib generated for nodes, e.g. with map_json, are only names. They can stay as they are, as long as the data is not mixed with nodes that fastrdf generates anew from the same source.

  • Templates that were stored in a serialized model keep the IRIs they were written with. Add the templates again from their source after changing the namespaces there.

  • With hybrid and disk storage, fastrdf keeps its working files in a folder named fastrdf_storage inside the storage_folder you give, where maplib used maplib_storage. The folder is emptied each time a model is created, so there is nothing in it to migrate: delete the maplib_storage folder that maplib left behind to get its disk space back. fastrdf does not remove it.

To see whether a model has IRIs left in a maplib namespace:

m.query("""
    SELECT ?p (COUNT(*) AS ?n) WHERE {
        ?s ?p ?o . FILTER(CONTAINS(STR(?p), "maplib"))
    } GROUP BY ?p""")

4. Environment variables

Every environment variable changed its prefix from MAPLIB_ to FASTRDF_, and the old names are not read. A setting under its old name has no effect and gives no warning.

maplib

fastrdf

MAPLIB_POLARS_ENGINE

FASTRDF_POLARS_ENGINE

MAPLIB_DUCKDB_SANDBOX

FASTRDF_DUCKDB_SANDBOX

MAPLIB_DUCKDB_ALLOWED_DIRECTORIES

FASTRDF_DUCKDB_ALLOWED_DIRECTORIES

MAPLIB_SPATIAL_EXTENSION

FASTRDF_SPATIAL_EXTENSION

MAPLIB_REMOTE_CACHE_BYTES

FASTRDF_REMOTE_CACHE_BYTES

MAPLIB_REMOTE_CACHE_GRACE_SECONDS

FASTRDF_REMOTE_CACHE_GRACE_SECONDS

MAPLIB_TABLE_STATISTICS

FASTRDF_TABLE_STATISTICS

MAPLIB_SEGMENT_FILTER_BITS

FASTRDF_SEGMENT_FILTER_BITS

MAPLIB_INL, MAPLIB_INL_*

FASTRDF_INL, FASTRDF_INL_*

MAPLIB_LATERAL_PER_ROW

FASTRDF_LATERAL_PER_ROW

MAPLIB_CSVW_NS

FASTRDF_CSVW_NS

MAPLIB_AUTH_TOKEN

FASTRDF_AUTH_TOKEN

Check container definitions, deployment scripts, .env files and CI configuration for MAPLIB_. A server started with python -m fastrdf.serve that still has its token in MAPLIB_AUTH_TOKEN runs without authentication.

Changes that are not part of the rename

fastrdf also moves on from the last maplib it was built from. The change log has every change; the breaking ones so far:

  • Model.get_predicate_iris and Model.get_predicate are removed. Query the predicates with SPARQL instead, e.g. SELECT DISTINCT ?p WHERE { ?s ?p ?o }, or summarise a model with Model.describe.

A checklist

  • [ ] The license allows your use, or you have a commercial license.

  • [ ] Python is 3.12 or newer.

  • [ ] maplib is uninstalled and fastrdf installed; the imports are changed.

  • [ ] No query, template, shape, rule or mapping has maplib in a namespace, and none uses the prefix maplib: for the old namespace.

  • [ ] Stored data has no IRIs in a maplib namespace, or they are known to be only names.

  • [ ] No MAPLIB_ environment variable is set anywhere, the authentication token in particular.

  • [ ] Links to the documentation and to vocabulary IRIs point at https://datatreehouse.github.io/fastrdf/.