コード例 #1
0
        private Guid?AssertValidEtag(string key, Guid?etag, string op, TransactionInformation transactionInformation)
        {
            var readResult = storage.Documents.Read(new RavenJObject {
                { "key", key }
            });

            if (readResult != null)
            {
                StorageHelper.AssertNotModifiedByAnotherTransaction(storage, transactionStorageActions, key, readResult, transactionInformation);
                var existingEtag = new Guid(readResult.Key.Value <byte[]>("etag"));

                if (etag != null)
                {
                    if (existingEtag != etag)
                    {
                        if (etag.Value == Guid.Empty)
                        {
                            RavenJObject metadata;
                            ReadMetadata(key, existingEtag, readResult.Data, out metadata);
                            if (metadata.ContainsKey(Constants.RavenDeleteMarker) && metadata.Value <bool>(Constants.RavenDeleteMarker))
                            {
                                return(existingEtag);
                            }
                        }

                        throw new ConcurrencyException(op + " attempted on document '" + key +
                                                       "' using a non current etag")
                              {
                                  ActualETag   = etag.Value,
                                  ExpectedETag = existingEtag,
                                  Key          = key
                              };
                    }
                }
                return(existingEtag);
            }

            if (etag != null && etag != Guid.Empty)             // expected something to be there.
            {
                throw new ConcurrencyException("PUT attempted on document '" + key +
                                               "' using a non current etag (document deleted)")
                      {
                          ExpectedETag = etag.Value,
                          Key          = key
                      }
            }
            ;

            readResult = storage.DocumentsModifiedByTransactions.Read(new RavenJObject {
                { "key", key }
            });
            StorageHelper.AssertNotModifiedByAnotherTransaction(storage, transactionStorageActions, key, readResult, transactionInformation);

            if (readResult == null)
            {
                return(null);
            }

            return(new Guid(readResult.Key.Value <byte[]>("etag")));
        }
    }