Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
CheckSumIndexOutput.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 using Lucene.Net.Support;
20 using CRC32 = Lucene.Net.Support.CRC32;
21 
22 namespace Lucene.Net.Store
23 {
24 
25  /// <summary>Writes bytes through to a primary IndexOutput, computing
26  /// checksum. Note that you cannot use seek().
27  /// </summary>
29  {
30  internal IndexOutput main;
31  internal IChecksum digest;
32 
33  private bool isDisposed;
34 
36  {
37  this.main = main;
38  digest = new CRC32();
39  }
40 
41  public override void WriteByte(byte b)
42  {
43  digest.Update(b);
44  main.WriteByte(b);
45  }
46 
47  public override void WriteBytes(byte[] b, int offset, int length)
48  {
49  digest.Update(b, offset, length);
50  main.WriteBytes(b, offset, length);
51  }
52 
53  public virtual long Checksum
54  {
55  get { return digest.Value; }
56  }
57 
58  public override void Flush()
59  {
60  main.Flush();
61  }
62 
63  protected override void Dispose(bool disposing)
64  {
65  if (isDisposed) return;
66 
67  if (disposing)
68  {
69  main.Close();
70  }
71 
72  isDisposed = true;
73  }
74 
75  public override long FilePointer
76  {
77  get { return main.FilePointer; }
78  }
79 
80  public override void Seek(long pos)
81  {
82  throw new System.SystemException("not allowed");
83  }
84 
85  /// <summary> Starts but does not complete the commit of this file (=
86  /// writing of the final checksum at the end). After this
87  /// is called must call <see cref="FinishCommit" /> and the
88  /// <see cref="Dispose" /> to complete the commit.
89  /// </summary>
90  public virtual void PrepareCommit()
91  {
92  long checksum = Checksum;
93  // Intentionally write a mismatched checksum. This is
94  // because we want to 1) test, as best we can, that we
95  // are able to write a long to the file, but 2) not
96  // actually "commit" the file yet. This (prepare
97  // commit) is phase 1 of a two-phase commit.
98  long pos = main.FilePointer;
99  main.WriteLong(checksum - 1);
100  main.Flush();
101  main.Seek(pos);
102  }
103 
104  /// <summary>See <see cref="PrepareCommit" /> </summary>
105  public virtual void FinishCommit()
106  {
107  main.WriteLong(Checksum);
108  }
109 
110  public override long Length
111  {
112  get { return main.Length; }
113  }
114  }
115 }