<commit_before>"""
Essential implementation of the Store interface defined by RDF lib.
"""
import itertools
import logging
from django.db.utils import IntegrityError
import rdflib
from rdflib.store import VALID_STORE
from rdflib.term import Literal, Identifier, BNode
from rdflib_django import models
from rdflib_django.models import Namespace


DEFAULT_STORE = "Default Store"
GLOBAL_CONTEXT = BNode("Global Context")


DEFAULT_NAMESPACES = (
    ("xml", u"http://www.w3.org/XML/1998/namespace"),
    ("rdf", u"http://www.w3.org/1999/02/22-rdf-syntax-ns#"),
    ("rdfs", u"http://www.w3.org/2000/01/rdf-schema#")
)


log = logging.getLogger(__name__)


def _get_query_sets_for_object(o):
    """
    Determines the correct query set based on the object.

    If the object is a literal, it will return a query set over LiteralStatements.
    If the object is a URIRef or BNode, it will return a query set over Statements.
    If the object is unknown, it will return both the LiteralStatement and Statement query sets.

    This method always returns a list of size at least one.
    """
    if o:
        if isinstance(o, Literal):
            query_sets = [models.LiteralStatement.objects]
        else:
            query_sets = [models.Statement.objects]
    else:
        query_sets = [models.Statement.objects, models.LiteralStatement.objects]
    return query_sets


class DjangoStore(rdflib.store.Store):
    """
    RDFlib Store implementation the uses Django Models for storage and retrieval.

    >>> g = rdflib.Graph('Django')

    The implementation is context aware, and uses Django transactions.

    >>> g.store.context_aware
    True
    >>> g.store.transaction_aware
    False

    The implementation does not support formula's.

    >>> g.store.formula_aware
    False

    The implementation provides a single store with the identifier DEFAULT_STORE. This store
    is always present and needs not be opened.

    >>> g.store.identifier
    'Default Store'

    Using other stores is not allowed

    >>> g = DjangoStore(identifier='HelloWorld')
    Traceback (most recent call last):
        ...
    ValueError: multiple stores are not allowed


    """

    context_aware = True
    formula_aware = False
    transaction_aware = False

    def __init__(self, configuration=None, identifier=DEFAULT_STORE):
        if identifier and identifier != DEFAULT_STORE:
            raise ValueError("multiple stores are not allowed")

        self.identifier = DEFAULT_STORE
        self.default_context = None
        super(DjangoStore, self).__init__(configuration, identifier)
        self.open()

    def open(self, configuration=None, create=False):
        """
        Opens the underlying store. This is only necessary when opening
        a store with another identifier than the default identifier.

        >>> g = rdflib.Graph('Django')
        >>> g.open(configuration=None, create=False) == rdflib.store.VALID_STORE
        True
        """
        self.default_context = models.ContextRef.objects.get_or_create(identifier=GLOBAL_CONTEXT)[0]
#        for prefix, uri in DEFAULT_NAMESPACES:
#            if not Namespace.objects.filter(prefix=prefix).count():
#                try:
#                    Namespace(prefix=prefix, uri=uri).save()
#                except IntegrityError:
#                    raise SystemError("Someone registered namespace %s under prefix %s" % (uri, self.prefix(uri)))

        return VALID_STORE

    def destroy(self, configuration=None):
        """
        Completely destroys a store and all the contexts and triples in the store.

        >>> store = DjangoStore()
        >>> g = rdflib.Graph(store=store)
        >>> g.open(configuration=None, create=True) == rdflib.store.VALID_STORE
        True
        >>> g.open(configuration=None, create=False) == rdflib.store.VALID_STORE
        True
        >>> g.destroy(configuration=None)
        >>> g.open(configuration=None, create=False) == rdflib.store.VALID_STORE
        True
        """
        models.ContextRef.objects.all().delete()
        models.Statement.objects.all().delete()

    def _get_context_ref(self, context):
        """
        Returns a ContextRef for the specified context. Raises DoesNotExist if it does not exist.
        """
        if context is None:
            return self.default_context
        else:
            identifier = context.identifier if hasattr(context, 'identifier') else unicode(context)
            return models.ContextRef.objects.get(identifier=identifier)

    def _get_or_create_context_ref(self, context):
        """
        Returns a ContextRef for the specified context. Creates a new one if it does not yet exist.
        """
        if context is None:
            return self.default_context
        else:
            identifier = context.identifier if hasattr(context, 'identifier') else unicode(context)
            return models.ContextRef.objects.get_or_create(identifier=identifier)[0]

    def add(self, (s, p, o), context, quoted=False):
        """
        Adds a triple to the store.

        >>> from rdflib.term import URIRef<commit_msg>Optimizations based on existing initial data<commit_after>"""
Essential implementation of the Store interface defined by RDF lib.
"""
import itertools
import logging
from django.db.utils import IntegrityError
import rdflib
from rdflib.store import VALID_STORE
from rdflib.term import Literal, Identifier, BNode
from rdflib_django import models
from rdflib_django.models import Namespace


DEFAULT_STORE = "Default Store"
GLOBAL_CONTEXT = BNode("Global Context")


DEFAULT_NAMESPACES = (
    ("xml", u"http://www.w3.org/XML/1998/namespace"),
    ("rdf", u"http://www.w3.org/1999/02/22-rdf-syntax-ns#"),
    ("rdfs", u"http://www.w3.org/2000/01/rdf-schema#")
)


log = logging.getLogger(__name__)


def _get_query_sets_for_object(o):
    """
    Determines the correct query set based on the object.

    If the object is a literal, it will return a query set over LiteralStatements.
    If the object is a URIRef or BNode, it will return a query set over Statements.
    If the object is unknown, it will return both the LiteralStatement and Statement query sets.

    This method always returns a list of size at least one.
    """
    if o:
        if isinstance(o, Literal):
            query_sets = [models.LiteralStatement.objects]
        else:
            query_sets = [models.Statement.objects]
    else:
        query_sets = [models.Statement.objects, models.LiteralStatement.objects]
    return query_sets


class DjangoStore(rdflib.store.Store):
    """
    RDFlib Store implementation the uses Django Models for storage and retrieval.

    >>> g = rdflib.Graph('Django')

    The implementation is context aware, and uses Django transactions.

    >>> g.store.context_aware
    True
    >>> g.store.transaction_aware
    False

    The implementation does not support formula's.

    >>> g.store.formula_aware
    False

    The implementation provides a single store with the identifier DEFAULT_STORE. This store
    is always present and needs not be opened.

    >>> g.store.identifier
    'Default Store'

    Using other stores is not allowed

    >>> g = DjangoStore(identifier='HelloWorld')
    Traceback (most recent call last):
        ...
    ValueError: multiple stores are not allowed


    """

    context_aware = True
    formula_aware = False
    transaction_aware = False

    def __init__(self, configuration=None, identifier=DEFAULT_STORE):
        if identifier and identifier != DEFAULT_STORE:
            raise ValueError("multiple stores are not allowed")

        self.identifier = DEFAULT_STORE
        self.default_context = None
        super(DjangoStore, self).__init__(configuration, identifier)
        self.open()

    def open(self, configuration=None, create=False):
        """
        Opens the underlying store. This is only necessary when opening
        a store with another identifier than the default identifier.

        >>> g = rdflib.Graph('Django')
        >>> g.open(configuration=None, create=False) == rdflib.store.VALID_STORE
        True
        """
        return VALID_STORE

    def destroy(self, configuration=None):
        """
        Completely destroys a store and all the contexts and triples in the store.

        >>> store = DjangoStore()
        >>> g = rdflib.Graph(store=store)
        >>> g.open(configuration=None, create=True) == rdflib.store.VALID_STORE
        True
        >>> g.open(configuration=None, create=False) == rdflib.store.VALID_STORE
        True
        >>> g.destroy(configuration=None)
        >>> g.open(configuration=None, create=False) == rdflib.store.VALID_STORE
        True
        """
        models.Statement.objects.all().delete()
        models.LiteralStatement.objects.all().delete()
        models.ContextRef.objects.exclude(identifier=GLOBAL_CONTEXT).delete()

    def _get_context_ref(self, context):
        """
        Returns a ContextRef for the specified context. Raises DoesNotExist if it does not exist.
        """
        if context is None:
            return self.default_context
        else:
            identifier = context.identifier if hasattr(context, 'identifier') else unicode(context)
            return models.ContextRef.objects.get(identifier=identifier)

    def _get_or_create_context_ref(self, context):
        """
        Returns a ContextRef for the specified context. Creates a new one if it does not yet exist.
        """
        if context is None:
            return self.default_context
        else:
            identifier = context.identifier if hasattr(context, 'identifier') else unicode(context)
            return models.ContextRef.objects.get_or_create(identifier=identifier)[0]

    def add(self, (s, p, o), context, quoted=False):
        """
        Adds a triple to the store.

        >>> from rdflib.term import URIRef
{'fraction_non_alphanumeric': 0.09268243379826158, 'fraction_numerical': 0.004548211036992117, 'mean_word_length': 2.8028439661798616, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4862342', 'n_tokens_mistral': 2790, 'n_tokens_neox': 2657, 'n_words': 822}