/*
 * rdfserver.h
 * Interface to the rdf server
 *
 * Copyright (C) Leni Mayo 2002
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License, version 2, as
 *	published by the Free Software Foundation.
 *	
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *	
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#ifndef _RDFSERVER_H_
#define _RDFSERVER_H_

#include "rdfcommon.h"
#include "rdfconsturi.h"

typedef enum
{
	eStorageCodeDiskCreate,
	eStorageCodeDiskOpenR,
	eStorageCodeDiskOpenRW,
	eStorageCodeMemory,
} StorageCode;

int truncateStorage(const char *pszName);

/*
 * In an early implementation, we parsed a bunch of uris into models
 * during server-startup and used them later, during execution of a callback.
 * RdfServer contained a GSList of ServiceCatalog, populated during started
 * and referenced in the callback.
 *
 * The idea was that parsing during server startup is probably more
 * time-efficient than parsing during the execution of a callback
 * when the boot-process is likely to block and wait on a response.
 */
typedef struct
{
	librdf_model   *pModel;
	librdf_storage *pStorage;
	char           *pszFilePrefix;
	GSList         *pList;    /* contains ServiceCatalogMetaData */
} ServiceCatalog;

/*
 * meta-data (eg eSerelMtime or eSerelSourceUri) 
 * associated with ServiceCatalog
 */
typedef struct
{
	RdfProperty code;
	char     *pszValue;
} ServiceCatalogMetaData;

typedef enum 
{
	eRdfModelRecentBoot  = 0,
	eRdfModelDefinitions = 1,
	eRdfModelCodeMax     = 2
} RdfModelCode;

typedef enum
{
	eRdfServerStateInitial,
	eRdfServerStatePopulateFailed,
	eRdfServerStatePopulateSuccess
} RdfServerState;

typedef struct 
{
	ServiceCatalog table[eRdfModelCodeMax];
	librdf_parser  *pParser;
	const char     *pszBaseUri;
	librdf_uri     *pBaseUri;
	RdfServerState state;
	int            fHistoryUsed;     /* set by --populate, used by --query verify, FALSE if history was rebuilt, TRUE otherwise */
	char           *pszErrorMessage; /* set by --populate, used by --query verify */
} RdfServer;

RdfServer * rdfServerNew();
void rdfServerDelete(RdfServer *pRdfServer);

ServiceCatalog * serviceCatalogNew(librdf_model *pModel, librdf_storage *pStorage);
void             serviceCatalogDelete(ServiceCatalog *pServiceCatalog);
ServiceCatalog * serviceCatalogFromUri(RdfServer *pRdfServer, const char *pszUriForParser, const char *pszUriForLabel);
int serviceCatalogInitialise(ServiceCatalog *pServiceCatalog);

ServiceCatalogMetaData * serviceCatalogMetaDataNew(RdfProperty code, char *pszValue);
void                     serviceCatalogMetaDataDelete(ServiceCatalogMetaData *p);
const char *             serviceCatalogMetaDataValue(ServiceCatalog *pServiceCatalog, RdfProperty code);

int initialiseModelAndStorageShortTerm(librdf_model **ppModel, librdf_storage **ppStorage);

char * mtimeFromUri(const char *pszUri);

GSList *newListFromLists(GSList *pList1, GSList *pList2);

int parseUriIntoModel(RdfServer *pRdfServer, librdf_model *pModel, const char *pszUri, const char *pszBaseUri);

void   rdfServerSetDataDirectory(const char *pszDirectory);
char * rdfServerGetDataDirectory();

void     rdfServerSetUriSeq(const char *pszUriSeq);
GSList * rdfServerGetUriSeq();

#endif /* _RDFSERVER_H_ */
