Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 2e9c43658e374d290a2de15d25134ac8 > files > 712

db4o-doc-8.0-1.fc15.i686.rpm

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="2" MadCap:lastHeight="120" MadCap:lastWidth="624" MadCap:disableMasterStylesheet="true" MadCap:tocPath="Configuration|File Configuration|Storage|Pluggable Storage" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem">
    <head><title>Logging Storage	</title>
        <script type="text/javascript">/* <![CDATA[ */
window.onload = function(){
	var pathToFlash = $('html').attr('MadCap:PathToHelpSystem') + 'Content/Resources/Code/ZeroClipboard.swf';
	ZeroClipboard.setMoviePath(pathToFlash);
			
	function bindToClipBord(element,content){
		var clip = new ZeroClipboard.Client();
		clip.setText(content);
		clip.glue(element);
	};
		
	if(location.protocol==='file:'){
		$('.copylink-marker').remove();
	} else{
			$('.copylink-marker').each(function(){
				var text = $(this).parent().parent().children('.prettyprint').html();
				$(this).hover(function(){
					bindToClipBord(this,text);
				},
				function(){});
			});	
	}		
	prettyPrint();	
};
                /* ]]> */</script>
        <link href="../../../../SkinSupport/MadCap.css" rel="stylesheet" />
        <link href="../../../../Resources/Stylesheets/OnlineStyle.css" rel="stylesheet" />
        <script src="../../../../SkinSupport/MadCapAll.js">
        </script>
        <script src="../../../../Resources/Code/prettify.js">
        </script>
        <script src="../../../../Resources/Code/lang-vb.js">
        </script>
        <script src="../../../../Resources/Code/jquery.min.js">
        </script>
        <script src="../../../../Resources/Code/ZeroClipboard.js">
        </script>
    </head>
    <body>
        <p class="MCWebHelpFramesetLink" style="display: none;"><a href="../../../../../index_CSH.html#configuration/file/storage/pluggable_storage/logging_storage.htm" style="">Open topic with navigation</a>
        </p>
        <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../../../../configuration.htm">Configuration</a><span class="MCBreadcrumbsDivider"> &gt; </span><a class="MCBreadcrumbsLink" href="../../../file_configuration.htm">File Configuration</a><span class="MCBreadcrumbsDivider"> &gt; </span><a class="MCBreadcrumbsLink" href="../../storage.htm">Storage</a><span class="MCBreadcrumbsDivider"> &gt; </span><span class="MCBreadcrumbs">Logging Storage</span>
        </div>
        <p>
            <script type="text/javascript">/*<![CDATA[*/document.write('<a href="' + location.href +'">');
				document.write("Direct Link");
			document.write('</a>');/*]]>*/</script>
        </p>
        <p>
        </p>
        <h1>Logging Storage</h1>
        <p>In this example we will implement a simple file base
storage, which will log messages about each IO operation. In the implementation
you can see that most of the functionality is derived from StorageDecorator and
BinDecorator classes with additional logging added:</p>
        <p MadCap:conditions="Global.Primary:java" />
        <p MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages">
            <pre class="prettyprint" xml:space="preserve">LoggingStorage.cs
/**//* Copyright (C) 2004 - 2009  Versant Corporation http://www.versant.com */
using System;
using Db4objects.Db4o.IO;

namespace Db4odoc.<span class="MCPopup"><a href="javascript:void(0);" class="MCPopupSpot" onclick="FMCPopup( event, this ); return false;" MadCap:src="../../storage.htm">Storage<img style="border: none;margin-left: 5px;" src="../../../../SkinSupport/ExpandingClosed.gif" MadCap:altsrc="../../../../SkinSupport/ExpandingOpen.gif" class="MCExpandingIcon" onload="if ( typeof( FMCPreloadImage ) == 'function' ) { FMCPreloadImage( '../../../../SkinSupport/ExpandingOpen.gif' ); }" /></a></span>
 {
    class LoggingStorage : StorageDecorator
     {
        // delegate to a normal file storage
        public LoggingStorage()
            : base(new FileStorage())
         {

        }

        // use submitted storage as a delegate
        public LoggingStorage(IStorage storage)
            : base(storage)
         {

        }

        /**//**
         * opens a Bin for the given URI.
         */
        public override IBin Open(BinConfiguration config)
         {
            IBin storage = base.Open(config);
            if (config.ReadOnly())
             {
                return new ReadOnlyBin(new LoggingBin(storage));
            }
            return new LoggingBin(storage);
        }

        /**//// &lt;summary&gt;
        /// LoggingBin implementation. Allows to log information
      /// for each IO operation
        /// &lt;/summary&gt;
        class LoggingBin : BinDecorator
         {

            public LoggingBin(IBin bin)
                : base(bin)
             {

            }

            // delegate to the base class and log a message
            public override void Close()
             {
                base.Close();
                System.Console.WriteLine(string.Format
("{0} LoggingStorage: File closed", DateTime.Now.ToString()));
            }

            // log a message, then delegate
            public override int Read(long pos, byte[] buffer, int length)
             {
                System.Console.WriteLine(
string.Format("{0} LoggingStorage: Reading {1} bytes and {2} position", 
DateTime.Now.ToString(), length, pos));
                return base.Read(pos, buffer, length);

            }

            // log a message, then delegate
            public override void Write(long pos, byte[] buffer, int length)
             {
                System.Console.WriteLine(
string.Format("{0} LoggingStorage: Writing {1} bytes and {2} position", 
DateTime.Now.ToString(), length, pos));
                base.Write(pos, buffer, length);
            }

            // log a message, then delegate
            public override void Sync()
             {
                System.Console.WriteLine(
string.Format("{0} LoggingStorage: Syncing", DateTime.Now.ToString()));
                base.Sync();
            }

        }
    }
}</pre>
        </p>
        <p MadCap:conditions="Global.Primary:cs" />
        <p MadCap:conditions="Primary..NET,Primary.VB.NET,Primary.All languages">
            <pre class="prettyprint lang-vb" xml:space="preserve">LoggingStorage.vb
' Copyright (C) 2004 - 2009  Versant Corporation http://www.versant.com 

Imports System
Imports Db4objects.Db4o.IO

Namespace Db4odoc.Storage
    Class LoggingStorage
        Inherits StorageDecorator
        Public Sub New()
            ' delegate to a normal file storage
            MyBase.New(New FileStorage())

        End Sub

        Public Sub New(ByVal storage As IStorage)
            ' use submitted storage as a delegate
            MyBase.New(storage)

        End Sub

        '
        ' opens a logging bin for the given URI.
        '         
        Public Overloads Overrides Function Open(ByVal config _ 
As BinConfiguration) As IBin
            Dim storage As IBin = MyBase.Open(config)
            If config.[ReadOnly]() Then
                Return New ReadOnlyBin(New LoggingBin(storage))
            End If
            Return New LoggingBin(storage)
        End Function

        ' LoggingBin implementation. Allows to log information
        ' for each IO operation
        Private Class LoggingBin
            Inherits BinDecorator

            Public Sub New(ByVal bin As IBin)
                MyBase.New(bin)

            End Sub

            ' delegate to the base class and log a message
            Public Overloads Overrides Sub Close()
                MyBase.Close()
                System.Console.WriteLine( _ 
String.Format("{0} LoggingStorage: File closed", DateTime.Now.ToString()))
            End Sub

            ' log a message, then delegate
            Public Overloads Overrides Function Read(ByVal _ 
pos As Long, ByVal buffer As Byte(), _  
ByVal length As Integer) As Integer
                System.Console.WriteLine( _ 
String.Format("{0} LoggingStorage: Reading {1} bytes and {2} position", _ 
DateTime.Now.ToString(), length, pos))

                Return MyBase.Read(pos, buffer, length)
            End Function

            ' log a message, then delegate
            Public Overloads Overrides Sub Write(ByVal pos As Long, _ 
ByVal buffer As Byte(), ByVal length As Integer)
                System.Console.WriteLine(_ 
String.Format("{0} LoggingStorage: Writing {1} bytes and {2} position", _ 
DateTime.Now.ToString(), length, pos))
                MyBase.Write(pos, buffer, length)
            End Sub

            ' log a message, then delegate
            Public Overloads Overrides Sub Sync()
                System.Console.WriteLine( _ 
String.Format("{0} LoggingStorage: Syncing", DateTime.Now.ToString()))
                MyBase.Sync()
            End Sub

        End Class
    End Class
End Namespace</pre>
        </p>
        <p MadCap:conditions="Global.Primary:vb" />
        <p>Use the LoggingStorage implementation with the following
code (you can find a working example if you download LoggingStorage class).</p>
        <p MadCap:conditions="Global.Primary:java" />
        <p MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages">
            <p>C#:</p>
            <p><code>config.File.Storage(new LoggingStorage());</code>
            </p>
        </p>
        <p MadCap:conditions="Global.Primary:cs" />
        <p MadCap:conditions="Primary..NET,Primary.VB.NET,Primary.All languages">
            <p>VB.NET:</p>
            <p><code>config.File.Storage(New LoggingStorage())</code>
            </p>
        </p>
        <p MadCap:conditions="Global.Primary:vb" />
        <p MadCap:conditions="Primary.Online">Download example code:</p>
        <p MadCap:conditions="Primary.Online">
            <MadCap:conditionalText MadCap:conditions="Primary..NET,Primary.VB.NET,Primary.All languages"><a href="storagevb.zip">VB.NET </a>
            </MadCap:conditionalText>
            <MadCap:conditionalText MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"><a href="storagecs.zip">c# </a>
            </MadCap:conditionalText>
        </p>
        <script type="text/javascript" src="../../../../SkinSupport/MadCapBodyEnd.js">
        </script>
    </body>
</html>