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
RAMFile.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 
20 namespace Lucene.Net.Store
21 {
22 
23  [Serializable]
24  public class RAMFile
25  {
26 
27  private const long serialVersionUID = 1L;
28 
29  protected System.Collections.Generic.List<byte[]> buffers = new System.Collections.Generic.List<byte[]>();
30  internal long length;
31  internal RAMDirectory directory;
32  internal long sizeInBytes;
33 
34  // This is publicly modifiable via Directory.touchFile(), so direct access not supported
35  private long lastModified = (DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond);
36 
37  // File used as buffer, in no RAMDirectory
38  public /*internal*/ RAMFile()
39  {
40  }
41 
42  public /*internal*/ RAMFile(RAMDirectory directory)
43  {
44  this.directory = directory;
45  }
46 
47  // For non-stream access from thread that might be concurrent with writing
48 
49  internal virtual long Length
50  {
51  get
52  {
53  lock (this)
54  {
55  return length;
56  }
57  }
58  set
59  {
60  lock (this)
61  {
62  this.length = value;
63  }
64  }
65  }
66 
67  // For non-stream access from thread that might be concurrent with writing
68 
69  internal virtual long LastModified
70  {
71  get
72  {
73  lock (this)
74  {
75  return lastModified;
76  }
77  }
78  set
79  {
80  lock (this)
81  {
82  this.lastModified = value;
83  }
84  }
85  }
86 
87  internal byte[] AddBuffer(int size)
88  {
89  byte[] buffer = NewBuffer(size);
90  lock (this)
91  {
92  buffers.Add(buffer);
93  sizeInBytes += size;
94  }
95 
96  if (directory != null)
97  {
98  lock (directory) //{{DIGY}} what if directory gets null in the mean time?
99  {
100  directory.internalSizeInBytes += size;
101  }
102  }
103 
104  return buffer;
105  }
106 
107  public /*internal*/ byte[] GetBuffer(int index)
108  {
109  lock (this)
110  {
111  return buffers[index];
112  }
113  }
114 
115  public /*internal*/ int NumBuffers()
116  {
117  lock (this)
118  {
119  return buffers.Count;
120  }
121  }
122 
123  /// <summary> Expert: allocate a new buffer.
124  /// Subclasses can allocate differently.
125  /// </summary>
126  /// <param name="size">size of allocated buffer.
127  /// </param>
128  /// <returns> allocated buffer.
129  /// </returns>
130  public /*internal*/ virtual byte[] NewBuffer(int size)
131  {
132  return new byte[size];
133  }
134 
135 
136  public virtual long SizeInBytes
137  {
138  get
139  {
140  lock (this)
141  {
142  return sizeInBytes;
143  }
144  }
145  }
146  }
147 }