pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / store / RAMFile.java
1 package org.apache.lucene.store;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.util.ArrayList;
21 import java.io.Serializable;
22
23 /** @lucene.internal */
24 public class RAMFile implements Serializable {
25
26   private static final long serialVersionUID = 1l;
27
28   protected ArrayList<byte[]> buffers = new ArrayList<byte[]>();
29   long length;
30   RAMDirectory directory;
31   protected long sizeInBytes;
32
33   // This is publicly modifiable via Directory.touchFile(), so direct access not supported
34   private long lastModified = System.currentTimeMillis();
35
36   // File used as buffer, in no RAMDirectory
37   public RAMFile() {}
38   
39   RAMFile(RAMDirectory directory) {
40     this.directory = directory;
41   }
42
43   // For non-stream access from thread that might be concurrent with writing
44   public synchronized long getLength() {
45     return length;
46   }
47
48   protected synchronized void setLength(long length) {
49     this.length = length;
50   }
51
52   // For non-stream access from thread that might be concurrent with writing
53   public synchronized long getLastModified() {
54     return lastModified;
55   }
56
57   protected synchronized void setLastModified(long lastModified) {
58     this.lastModified = lastModified;
59   }
60
61   protected final byte[] addBuffer(int size) {
62     byte[] buffer = newBuffer(size);
63     synchronized(this) {
64       buffers.add(buffer);
65       sizeInBytes += size;
66     }
67
68     if (directory != null) {
69       directory.sizeInBytes.getAndAdd(size);
70     }
71     return buffer;
72   }
73
74   protected final synchronized byte[] getBuffer(int index) {
75     return buffers.get(index);
76   }
77
78   protected final synchronized int numBuffers() {
79     return buffers.size();
80   }
81
82   /**
83    * Expert: allocate a new buffer. 
84    * Subclasses can allocate differently. 
85    * @param size size of allocated buffer.
86    * @return allocated buffer.
87    */
88   protected byte[] newBuffer(int size) {
89     return new byte[size];
90   }
91
92   public synchronized long getSizeInBytes() {
93     return sizeInBytes;
94   }
95   
96 }