Esempio n. 1
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())
                    .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
                        entity.SetProperty(Constants.VersionPredicateUri, 1);
                    }
                    else
                    {
                        var intVersion = Convert.ToInt32(version);
                        // inc version
                        intVersion++;
                        entity.SetProperty(Constants.VersionPredicateUri, intVersion);
                        Preconditions.Add(new Triple
                        {
                            Subject   = subject,
                            Predicate = Constants.VersionPredicateUri,
                            Object    = version.ToString(),
                            IsLiteral = true,
                            DataType  = RdfDatatypes.Integer,
                            LangCode  = null,
                            Graph     = VersionGraphUri
                        });
                    }
                }
            }

            try
            {
                Client.ApplyTransaction(Preconditions, DeletePatterns, AddTriples, UpdateGraphUri);
            }
            catch (TransactionPreconditionsFailedException)
            {
                Preconditions.Clear();
                throw;
            }

            // 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.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);
                }
                throw new BrightstarClientException(status.ExceptionDetail != null && !String.IsNullOrEmpty(status.ExceptionDetail.Message) ? status.ExceptionDetail.Message : "The transaction encountered an error");
            }

            // reset changes
            ResetTransactionData();
        }
Esempio n. 3
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();
        }