public void ApplyTransaction(IList<Triple> preconditions, IList<Triple> deletePatterns, IList<Triple> inserts, 
            string updateGraphUri )
        {
            var deleteData = new StringWriter();
            var dw = new BrightstarTripleSinkAdapter(new NQuadsWriter(deleteData, updateGraphUri));
            foreach (Triple triple in deletePatterns)
            {
                dw.Triple(triple);
            }
            deleteData.Close();

            var addData = new StringWriter();
            var aw = new BrightstarTripleSinkAdapter(new NQuadsWriter(addData, updateGraphUri));
            foreach (Triple triple in inserts)
            {
                aw.Triple(triple);
            }
            addData.Close();

            var preconditionsData = new StringWriter();
            var pw = new BrightstarTripleSinkAdapter(new NQuadsWriter(preconditionsData, updateGraphUri));
            foreach (var triple in preconditions)
            {
                pw.Triple(triple);
            }
            preconditionsData.Close();

            PostTransaction(preconditionsData.ToString(), deleteData.ToString(), addData.ToString(), updateGraphUri);
        }
 private static string SerializeTriples(IEnumerable<Triple> triples)
 {
     if (triples == null) return string.Empty;
     using (var writer = new StringWriter())
     {
         var sink = new BrightstarTripleSinkAdapter(new NQuadsWriter(writer));
         foreach(var t in triples) sink.Triple(t);
         writer.Close();
         return writer.ToString();
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Exports all triples to the stream provided
 /// </summary>
 /// <param name="store">The store to export triples from </param>
 /// <param name="output">The stream to write the triples to</param>
 /// <param name="graphs">OPTIONAL: The URIs of the graph(s) to be exported. Pass NULL to export all graphs</param>
 public static void Export(this IStore store, Stream output, IEnumerable<string> graphs = null)
 {
     IEnumerable<Triple> triples = store.Match(null, null, null, graphs: graphs);
     using (var sw = new StreamWriter(output))
     {
         var ntripleWriter = new BrightstarTripleSinkAdapter(new NTriplesWriter(sw));
         foreach (Triple triple in triples)
         {
             ntripleWriter.Triple(triple);
         }
         sw.Flush();
     }
 }
Ejemplo n.º 4
0
        private static void RunExport(object jobData)
        {
            var exportJob = jobData as ExportJob;
            if (exportJob == null) return;
            try
            {
                var storeDirectory = exportJob._storeWorker.WriteStore.DirectoryPath;
                var exportDirectory = Path.Combine(Path.GetDirectoryName(storeDirectory), "import");
#if PORTABLE
                var persistenceManager = PlatformAdapter.Resolve<IPersistenceManager>();
                if (!persistenceManager.DirectoryExists(exportDirectory)) persistenceManager.CreateDirectory(exportDirectory);
                var filePath = Path.Combine(exportDirectory, exportJob._outputFileName);
                using (var stream = persistenceManager.GetOutputStream(filePath, FileMode.Create))
#else
                if (!Directory.Exists(exportDirectory)) Directory.CreateDirectory(exportDirectory);
                var filePath = Path.Combine(exportDirectory, exportJob._outputFileName);
                Logging.LogDebug("Export file path calculated as '{0}'", filePath);
                using (var stream = File.Open(filePath, FileMode.Create, FileAccess.Write))
#endif
                {
                    string[] graphs = String.IsNullOrEmpty(exportJob._graphUri)
                                          ? null
                                          : new[] {exportJob._graphUri};
                    var triples = exportJob._storeWorker.ReadStore.Match(null, null, null, graphs:graphs);
                    var sw = new StreamWriter(stream);
                    var sink = GetWriterSink(exportJob._exportFormat, sw);
                    var nw = new BrightstarTripleSinkAdapter(sink);
                    foreach (var triple in triples)
                    {
                        nw.Triple(triple);
                    }
                    sink.Close();
                    sw.Flush();

#if !PORTABLE
                    stream.Flush(true);
                    stream.Close();
#endif
                }
                exportJob._successCallback(exportJob._jobId);
            }
            catch (Exception ex)
            {
                Logging.LogError(BrightstarEventId.ExportDataError, "Error Exporting Data {0} {1}", ex.Message, ex.StackTrace);
                exportJob._errorCallback(exportJob._jobId, ex);
            }

        }
Ejemplo n.º 5
0
        private static void RunExport(object jobData)
        {
            var exportJob = jobData as ExportJob;
            if (exportJob == null) return;
            try
            {
                var storeDirectory = exportJob._storeWorker.WriteStore.DirectoryPath;
                var exportDirectory = Path.Combine(storeDirectory, ".." + Path.DirectorySeparatorChar + "import");
                if (!Directory.Exists(exportDirectory)) Directory.CreateDirectory(exportDirectory);
                var filePath = Path.Combine(exportDirectory, exportJob._outputFileName);
                Logging.LogDebug("Export file path calculated as '{0}'", filePath);
                using (var stream = File.Open(filePath, FileMode.Create, FileAccess.Write))
                {
                    string[] graphs = String.IsNullOrEmpty(exportJob._graphUri)
                                          ? null
                                          : new[] {exportJob._graphUri};
                    var triples = exportJob._storeWorker.ReadStore.Match(null, null, null, graphs:graphs);
                    var sw = new StreamWriter(stream);
                    var nw = new BrightstarTripleSinkAdapter(new NTriplesWriter(sw));
                    foreach (var triple in triples)
                    {
                        nw.Triple(triple);
                    }
                    sw.Flush();
                    stream.Flush(true);
                    stream.Close();
                }
                exportJob._successCallback(exportJob._jobId);
            }
            catch (Exception ex)
            {
                Logging.LogError(BrightstarEventId.ExportDataError, "Error Exporting Data {0} {1}", ex.Message, ex.StackTrace);
                exportJob._errorCallback(exportJob._jobId, ex);
            }

        }
        /// <summary>
        /// Commits all changes. Waits for the operation to complete.
        /// </summary>
        protected override void DoSaveChanges()
        {
            if (_optimisticLockingEnabled)
            {
                // get subject entity and see if there is a version triple
                var subjects =
                    AddTriples.Select(x => x.Subject)
                              .Distinct()
                              .Union(DeletePatterns.Select(x => x.Subject).Distinct())
                              .Except(new[] {Constants.WildcardUri}).ToList();
                foreach (var subject in subjects)
                {
                    var entity = LookupDataObject(subject);
                    if (entity == null) throw new BrightstarClientException("No Entity Found for Subject " + subject);

                    var version = entity.GetPropertyValue(Constants.VersionPredicateUri);
                    if (version == null)
                    {
                        // no existing version information so assume this is the first time using it with 1
                        entity.SetProperty(Constants.VersionPredicateUri, 1);
                    }
                    else
                    {
                        var intVersion = Convert.ToInt32(version);
                        // inc version
                        intVersion++;
                        entity.SetProperty(Constants.VersionPredicateUri, intVersion);
                        Preconditions.Add(new Triple
                                                {
                                                    Graph = VersionGraphUri,
                                                    DataType = RdfDatatypes.Integer,
                                                    IsLiteral = true,
                                                    LangCode = null,
                                                    Object = version.ToString(),
                                                    Predicate = Constants.VersionPredicateUri,
                                                    Subject = subject
                                                });
                    }
                }
            }

            var deleteData = new StringWriter();
            var dw = new BrightstarTripleSinkAdapter(new NQuadsWriter(deleteData, UpdateGraphUri));
            foreach (var triple in DeletePatterns)
            {
                dw.Triple(triple);
            }
            deleteData.Close();

            var addData = new StringWriter();
            var aw = new BrightstarTripleSinkAdapter(new NQuadsWriter(addData, UpdateGraphUri));
            foreach (var triple in AddTriples)
            {
                aw.Triple(triple);               
            }
            addData.Close();

            var preconditionsData = new StringWriter();
            var pw = new BrightstarTripleSinkAdapter(new NQuadsWriter(preconditionsData, UpdateGraphUri));
            foreach (var triple in Preconditions)
            {
                pw.Triple(triple);
            }
            preconditionsData.Close();

            var jobId = _serverCore.ProcessTransaction(_storeName, preconditionsData.ToString(), deleteData.ToString(), addData.ToString(), UpdateGraphUri);
            var status = _serverCore.GetJobStatus(_storeName, jobId.ToString());
            while (!(status.JobStatus == JobStatus.CompletedOk || status.JobStatus == JobStatus.TransactionError))
            {
                // wait for completion.
#if !PORTABLE
                Thread.Sleep(5);
#endif
                status = _serverCore.GetJobStatus(_storeName, jobId.ToString());
            }

            if (status.JobStatus == JobStatus.TransactionError)
            {
                if (status.ExceptionDetail.Type.Equals(typeof(PreconditionFailedException).FullName))
                {
                    var failedTriples =
                        status.ExceptionDetail.Message.Substring(status.ExceptionDetail.Message.IndexOf("\n") + 1);
                    Preconditions.Clear();
                    throw new TransactionPreconditionsFailedException(failedTriples);
                }
                // todo: fix me and report inner exception
                throw new BrightstarClientException(status.ExceptionDetail != null ? status.ExceptionDetail.Message : "The transaction encountered an error");
            }

            // reset changes
            ResetTransactionData();
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Commits all changes. Waits for the operation to complete.
        /// </summary>
        protected override void DoSaveChanges()
        {
            if (_optimisticLockingEnabled)
            {
                // get subject entity and see if there is a version triple
                var subjects = AddTriples.Select(x => x.Subject).Distinct().Union(DeletePatterns.Select(x => x.Subject).Distinct()).ToList();
                foreach (var subject in subjects)
                {
                    var entity = LookupDataObject(subject);
                    if (entity == null) throw new BrightstarClientException("No Entity Found for Subject " + subject);
                    var version = entity.GetPropertyValue(Constants.VersionPredicateUri);
                    if (version == null)
                    {
                        // no existing version information so assume this is the first
                        entity.SetProperty(Constants.VersionPredicateUri, 1);
                    }
                    else
                    {
                        var intVersion = Convert.ToInt32(version);
                        // inc version
                        intVersion++;
                        entity.SetProperty(Constants.VersionPredicateUri, intVersion);
                        Preconditions.Add(new Triple { Graph = Constants.DefaultGraphUri, DataType = RdfDatatypes.Integer, IsLiteral = true, LangCode = null, Object = version.ToString(), Predicate = Constants.VersionPredicateUri, Subject = subject });
                    }
                }
            }

            var deleteData = new StringWriter();
            var dw = new BrightstarTripleSinkAdapter(new NTriplesWriter(deleteData));
            foreach (Triple triple in DeletePatterns)
            {
                dw.Triple(triple);
            }
            deleteData.Close();

            var addData = new StringWriter();
            var aw = new BrightstarTripleSinkAdapter(new NTriplesWriter(addData));
            foreach (Triple triple in AddTriples)
            {
                aw.Triple(triple);
            }
            addData.Close();

            var preconditionsData = new StringWriter();
            var pw = new BrightstarTripleSinkAdapter(new NTriplesWriter(preconditionsData));
            foreach (var triple in Preconditions)
            {
                pw.Triple(triple);
            }
            preconditionsData.Close();

            PostTransaction(preconditionsData.ToString(), deleteData.ToString(), addData.ToString());

            // reset changes
            ResetTransactionData();
        }
        /// <summary>
        /// Commits all changes. Waits for the operation to complete.
        /// </summary>
        protected override void DoSaveChanges()
        {
            if (_optimisticLockingEnabled)
            {
                // get subject entity and see if there is a version triple
                var subjects = AddTriples.Subjects
                    .Union(DeletePatterns.Subjects)
                    .Except(new[] {Constants.WildcardUri}).ToList();
                foreach (var subject in subjects)
                {
                    var entity = LookupDataObject(subject);
                    if (entity == null) throw new BrightstarClientException("No Entity Found for Subject " + subject);

                    var version = entity.GetPropertyValue(Constants.VersionPredicateUri);
                    if (version == null)
                    {
                        // no existing version information so assume this is the first time using it with 1
                        entity.SetProperty(Constants.VersionPredicateUri, 1);
                    }
                    else
                    {
                        var intVersion = Convert.ToInt32(version);
                        // inc version
                        intVersion++;
                        entity.SetProperty(Constants.VersionPredicateUri, intVersion);
                        Preconditions.Add(new Triple
                                                {
                                                    Graph = VersionGraphUri,
                                                    DataType = RdfDatatypes.Integer,
                                                    IsLiteral = true,
                                                    LangCode = null,
                                                    Object = version.ToString(),
                                                    Predicate = Constants.VersionPredicateUri,
                                                    Subject = subject
                                                });
                    }
                }
            }

            var deleteData = new StringWriter();
            var dw = new BrightstarTripleSinkAdapter(new NQuadsWriter(deleteData));
            foreach (var triple in DeletePatterns.Items)
            {
                dw.Triple(triple);
            }
            deleteData.Close();

            var addData = new StringWriter();
            var aw = new BrightstarTripleSinkAdapter(new NQuadsWriter(addData));
            foreach (var triple in AddTriples.Items)
            {
                aw.Triple(triple);               
            }
            addData.Close();

            var preconditionsData = new StringWriter();
            var pw = new BrightstarTripleSinkAdapter(new NQuadsWriter(preconditionsData));
            foreach (var triple in Preconditions.Items)
            {
                pw.Triple(triple);
            }
            preconditionsData.Close();

            var nePreconditionsData = new StringWriter();
            var nw = new BrightstarTripleSinkAdapter(new NQuadsWriter(nePreconditionsData));
            foreach (var triple in NonExistencePreconditions.Items)
            {
                nw.Triple(triple);
            }
            nePreconditionsData.Close();

            var jobId = _serverCore.ProcessTransaction(_storeName, preconditionsData.ToString(), 
                nePreconditionsData.ToString(),
                deleteData.ToString(), addData.ToString(), UpdateGraphUri);
            var status = _serverCore.GetJobStatus(_storeName, jobId.ToString());
            status.WaitEvent.WaitOne();
//            while (!(status.JobStatus == JobStatus.CompletedOk || status.JobStatus == JobStatus.TransactionError))
//            {
//                // wait for completion.
//#if !PORTABLE
//                Thread.Sleep(5);
//#endif
//                status = _serverCore.GetJobStatus(_storeName, jobId.ToString());
//            }

            if (status.JobStatus == JobStatus.TransactionError)
            {
                if (status.ExceptionDetail.Type.Equals(typeof(PreconditionFailedException).FullName))
                {
                    Preconditions.Clear();
                    throw TransactionPreconditionsFailedException.FromExceptionDetail(status.ExceptionDetail);
                }
                throw new BrightstarClientException(status.ExceptionDetail != null  && !String.IsNullOrEmpty(status.ExceptionDetail.Message) ? status.ExceptionDetail.Message : "The transaction encountered an error");
            }
            if (status.JobStatus != JobStatus.CompletedOk)
            {
                throw new BrightstarClientException("Unexpected job status on completion: " + status.JobStatus + ". Last message was: " + status.Information);
            }

            // reset changes
            ResetTransactionData();
        }
Ejemplo n.º 9
0
 public void ExportData(Stream stream, string graphUri)
 {
     try
     {
         var triples = ReadStore.Match(null, null, null, graph: graphUri);
         using (var sw = new StreamWriter(stream))
         {
             var nw = new BrightstarTripleSinkAdapter(new NTriplesWriter(sw));
             foreach (var triple in triples)
             {
                 nw.Triple(triple);
             }
             sw.Flush();
         }
     }
     catch (ReadWriteStoreModifiedException)
     {
         Logging.LogError(BrightstarEventId.ExportDataError, "Store was modified while export was running.");
     }
     catch (Exception ex)
     {
         Logging.LogError(BrightstarEventId.ExportDataError, "Error Exporting Data {0} {1}", ex.Message,
                          ex.StackTrace);
     }
 }