/// <summary>
        /// Commits the provided transaction
        /// </summary>
        public void CommitTransaction(ref TransactionToken token)
        {
            AssertionFailedException.Assert(token.State == StateOpen);
            token.State = StateCommitted;

            MemoryStream buffer = token.Object as MemoryStream;

            if (buffer == null)
            {
                return; // nothing to commit
            }
            byte[] bytes = buffer.GetBuffer();
            Crc32  crc   = new Crc32();

            crc.Add(bytes, 4, (int)buffer.Position - 4);
            PrimitiveSerializer.Int32.WriteTo(crc.Value, buffer);

            int len = (int)buffer.Position;

            PrimitiveSerializer.Int32.WriteTo((0xee << 24) + len, buffer);
            buffer.Position = 0;
            PrimitiveSerializer.Int32.WriteTo((0xbb << 24) + len, buffer);
            bytes = buffer.GetBuffer();

            WriteBytes(bytes, 0, len + 4);
        }
        private void Write(ref TransactionToken token, OperationCode operation, TKey key, TValue value)
        {
            AssertionFailedException.Assert(token.State == StateOpen);
            MemoryStream buffer = token.Object as MemoryStream;

            if (buffer == null)
            {
                token.Object = buffer = new MemoryStream();
                PrimitiveSerializer.Int32.WriteTo(0, buffer);
                PrimitiveSerializer.Int32.WriteTo(unchecked ((int)token.Handle), buffer);
                PrimitiveSerializer.Int16.WriteTo(0, buffer);
            }

            PrimitiveSerializer.Int16.WriteTo((short)operation, buffer);
            _options.KeySerializer.WriteTo(key, buffer);
            if (operation != OperationCode.Remove)
            {
                _options.ValueSerializer.WriteTo(value, buffer);
            }

            //Increment the operation counter at offset 8
            long pos = buffer.Position;

            buffer.Position = 8;
            short count = PrimitiveSerializer.Int16.ReadFrom(buffer);

            buffer.Position = 8;
            PrimitiveSerializer.Int16.WriteTo(++count, buffer);

            buffer.Position = pos;
        }
 public void RemoveValue(TKey key)
 {
     if (_cache.Options.LogFile != null)
     {
         if (!_hasLogToken)
         {
             _logToken = _cache.Options.LogFile.BeginTransaction();
         }
         _hasLogToken = true;
         _cache.Options.LogFile.RemoveValue(ref _logToken, key);
     }
 }
 public void UpdateValue(TKey key, TValue value)
 {
     if (_cache.Options.LogFile != null)
     {
         if (!_hasLogToken)
         {
             _logToken = _cache.Options.LogFile.BeginTransaction();
         }
         _hasLogToken = true;
         _cache.Options.LogFile.UpdateValue(ref _logToken, key, value);
     }
 }
        /// <summary>
        /// Abandons the provided transaction
        /// </summary>
        public void RollbackTransaction(ref TransactionToken token)
        {
            if (token.State == StateRolledback)
            {
                return;
            }

            AssertionFailedException.Assert(token.State == StateOpen);
            token.State = StateRolledback;
            MemoryStream buffer = token.Object as MemoryStream;

            if (buffer != null)
            {
                buffer.Dispose();
            }
            token.Object = null;
            token.Handle = 0;
        }
 /// <summary> The provided key/value pair was removed in the provided transaction </summary>
 public void RemoveValue(ref TransactionToken token, TKey key)
 {
     Write(ref token, OperationCode.Remove, key, default(TValue));
 }
 /// <summary> The provided key/value pair was updated in the provided transaction </summary>
 public void UpdateValue(ref TransactionToken token, TKey key, TValue value)
 {
     Write(ref token, OperationCode.Update, key, value);
 }
 /// <summary> The provided key/value pair was added in the provided transaction </summary>
 public void AddValue(ref TransactionToken token, TKey key, TValue value)
 {
     Write(ref token, OperationCode.Add, key, value);
 }
 public NodeTransaction(NodeCacheBase cache)
 {
     _cache       = cache;
     _hasLogToken = false;
     _logToken    = new TransactionToken();
 }