<commit_before>/* Twine: SPARQL client helpers
 *
 * Author: <NAME> <<EMAIL>>
 *
 * Copyright (c) 2014-2015 BBC
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "p_libtwine.h"

static int twine_sparql_debug;

static char *twine_sparql_uri;
static char *twine_sparql_query_uri;
static char *twine_sparql_update_uri;
static char *twine_sparql_data_uri;

static int
twine_sparql_put_internal_(const char *uri, const char *triples, size_t length, const char *type, librdf_model *sourcemodel);

/* Internal: set defaults for SPARQL connections */
int
twine_sparql_defaults_(const char *base_uri, const char *query_uri, const char *update_uri, const char *data_uri, int verbose)
{
	if(base_uri)
	{
		twine_sparql_uri = strdup(base_uri);
		if(!twine_sparql_uri)
		{
			return -1;
		}
		twine_logf(LOG_DEBUG, "SPARQL endpoint is <%s>\n", twine_sparql_uri);
	}
	if(query_uri)
	{
		twine_sparql_query_uri = strdup(query_uri);
		if(!twine_sparql_query_uri)
		{
			return -1;
		}
		twine_logf(LOG_DEBUG, "SPARQL query endpoint is <%s>\n", twine_sparql_query_uri);
	}
	if(update_uri)
	{
		twine_sparql_update_uri = strdup(update_uri);
		if(!twine_sparql_update_uri)
		{
			return -1;
		}
		twine_logf(LOG_DEBUG, "SPARQL update endpoint is <%s>\n", twine_sparql_update_uri);
	}
	if(data_uri)
	{
		twine_sparql_data_uri = strdup(data_uri);
		if(!twine_sparql_data_uri)
		{
			return -1;
		}
		twine_logf(LOG_DEBUG, "SPARQL RESTful endpoint is <%s>\n", twine_sparql_data_uri);
	}
	twine_sparql_debug = verbose;
	return 0;
}

/* Public: Create a new SPARQL connection */
SPARQL *
twine_sparql_create(void)
{
	SPARQL *p;

	p = sparql_create(twine_sparql_uri);
	if(!p)
	{
		twine_logf(LOG_CRIT, "failed to create new SPARQL connection\n");
		return NULL;
	}
	sparql_set_logger(p, twine_logger_);
	sparql_set_verbose(p, twine_sparql_debug);
	if(twine_sparql_query_uri)
	{
		sparql_set_query_uri(p, twine_sparql_query_uri);
	}
	if(twine_sparql_update_uri)
	{
		sparql_set_update_uri(p, twine_sparql_update_uri);
	}
	if(twine_sparql_data_uri)
	{
		sparql_set_data_uri(p, twine_sparql_data_uri);
	}
	return p;
}

/* Public: Replace a graph from a Turtle buffer */
int
twine_sparql_put(const char *uri, const char *triples, size_t length)
{
	return twine_sparql_put_internal_(uri, triples, length, "text/turtle", NULL);
}

int
twine_sparql_put_format(const char *uri, const char *triples, size_t length, const char *type)
{
	return twine_sparql_put_internal_(uri, triples, length, type, NULL);
}

/* Public: Replace a graph from a librdf stream */
int
twine_sparql_put_stream(const char *uri, librdf_stream *stream)
{
	librdf_model *model;
	char *buf;
	size_t buflen;
	int r;

	model = twine_rdf_model_create();
	if(!model)
	{
		return -1;
	}
	if(librdf_model_add_statements(model, stream))
	{
		return -1;
	}
	buf = twine_rdf_model_ntriples(model, &buflen);
	if(!buf)
	{
		return -1;
	}
	r = twine_sparql_put_internal_(uri, buf, buflen, MIME_NTRIPLES, model);
	librdf_free_memory(buf);
	twine_rdf_model_destroy(model);
	return 0;
}

/* Public: Replace a graph from a librdf model */
int
twine_sparql_put_model(const char *uri, librdf_model *model)
{
	char *buf;
	size_t buflen;
	int r;

	buf = twine_rdf_model_ntriples(model, &buflen);
	if(!buf)
	{
		return -1;
	}
	r = twine_sparql_put_internal_(uri, buf, buflen, MIME_NTRIPLES, model);
	librdf_free_memory(buf);
	return r;
}

/* Perform an actual SPARQL PUT operation, dealing with pre-processors and
 * post-processors
 */<commit_msg>Ensure processing status is properly propagated (resolves #26)<commit_after>/* Twine: SPARQL client helpers
 *
 * Author: <NAME> <<EMAIL>>
 *
 * Copyright (c) 2014-2015 BBC
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "p_libtwine.h"

static int twine_sparql_debug;

static char *twine_sparql_uri;
static char *twine_sparql_query_uri;
static char *twine_sparql_update_uri;
static char *twine_sparql_data_uri;

static int
twine_sparql_put_internal_(const char *uri, const char *triples, size_t length, const char *type, librdf_model *sourcemodel);

/* Internal: set defaults for SPARQL connections */
int
twine_sparql_defaults_(const char *base_uri, const char *query_uri, const char *update_uri, const char *data_uri, int verbose)
{
	if(base_uri)
	{
		twine_sparql_uri = strdup(base_uri);
		if(!twine_sparql_uri)
		{
			return -1;
		}
		twine_logf(LOG_DEBUG, "SPARQL endpoint is <%s>\n", twine_sparql_uri);
	}
	if(query_uri)
	{
		twine_sparql_query_uri = strdup(query_uri);
		if(!twine_sparql_query_uri)
		{
			return -1;
		}
		twine_logf(LOG_DEBUG, "SPARQL query endpoint is <%s>\n", twine_sparql_query_uri);
	}
	if(update_uri)
	{
		twine_sparql_update_uri = strdup(update_uri);
		if(!twine_sparql_update_uri)
		{
			return -1;
		}
		twine_logf(LOG_DEBUG, "SPARQL update endpoint is <%s>\n", twine_sparql_update_uri);
	}
	if(data_uri)
	{
		twine_sparql_data_uri = strdup(data_uri);
		if(!twine_sparql_data_uri)
		{
			return -1;
		}
		twine_logf(LOG_DEBUG, "SPARQL RESTful endpoint is <%s>\n", twine_sparql_data_uri);
	}
	twine_sparql_debug = verbose;
	return 0;
}

/* Public: Create a new SPARQL connection */
SPARQL *
twine_sparql_create(void)
{
	SPARQL *p;

	p = sparql_create(twine_sparql_uri);
	if(!p)
	{
		twine_logf(LOG_CRIT, "failed to create new SPARQL connection\n");
		return NULL;
	}
	sparql_set_logger(p, twine_logger_);
	sparql_set_verbose(p, twine_sparql_debug);
	if(twine_sparql_query_uri)
	{
		sparql_set_query_uri(p, twine_sparql_query_uri);
	}
	if(twine_sparql_update_uri)
	{
		sparql_set_update_uri(p, twine_sparql_update_uri);
	}
	if(twine_sparql_data_uri)
	{
		sparql_set_data_uri(p, twine_sparql_data_uri);
	}
	return p;
}

/* Public: Replace a graph from a Turtle buffer */
int
twine_sparql_put(const char *uri, const char *triples, size_t length)
{
	return twine_sparql_put_internal_(uri, triples, length, "text/turtle", NULL);
}

int
twine_sparql_put_format(const char *uri, const char *triples, size_t length, const char *type)
{
	return twine_sparql_put_internal_(uri, triples, length, type, NULL);
}

/* Public: Replace a graph from a librdf stream */
int
twine_sparql_put_stream(const char *uri, librdf_stream *stream)
{
	librdf_model *model;
	char *buf;
	size_t buflen;
	int r;

	model = twine_rdf_model_create();
	if(!model)
	{
		return -1;
	}
	if(librdf_model_add_statements(model, stream))
	{
		return -1;
	}
	buf = twine_rdf_model_ntriples(model, &buflen);
	if(!buf)
	{
		return -1;
	}
	r = twine_sparql_put_internal_(uri, buf, buflen, MIME_NTRIPLES, model);
	librdf_free_memory(buf);
	twine_rdf_model_destroy(model);
	return r;
}

/* Public: Replace a graph from a librdf model */
int
twine_sparql_put_model(const char *uri, librdf_model *model)
{
	char *buf;
	size_t buflen;
	int r;

	buf = twine_rdf_model_ntriples(model, &buflen);
	if(!buf)
	{
		return -1;
	}
	r = twine_sparql_put_internal_(uri, buf, buflen, MIME_NTRIPLES, model);
	librdf_free_memory(buf);
	return r;
}

/* Perform an actual SPARQL PUT operation, dealing with pre-processors and
 * post-processors
 */
{'fraction_non_alphanumeric': 0.1057609630266552, 'fraction_numerical': 0.005527576464807763, 'mean_word_length': 4.917151162790698, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '4898373', 'n_tokens_mistral': 3428, 'n_tokens_neox': 3304, 'n_words': 755}