01: package org.apache.lucene.store;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * 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: class RAMFile implements Serializable {
24:
25: private static final long serialVersionUID = 1l;
26:
27: private ArrayList buffers = new ArrayList();
28: long length;
29: RAMDirectory directory;
30: long sizeInBytes; // Only maintained if in a directory; updates synchronized on directory
31:
32: // This is publicly modifiable via Directory.touchFile(), so direct access not supported
33: private long lastModified = System.currentTimeMillis();
34:
35: // File used as buffer, in no RAMDirectory
36: RAMFile() {
37: }
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: synchronized long getLength() {
45: return length;
46: }
47:
48: 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: synchronized long getLastModified() {
54: return lastModified;
55: }
56:
57: synchronized void setLastModified(long lastModified) {
58: this .lastModified = lastModified;
59: }
60:
61: final synchronized byte[] addBuffer(int size) {
62: byte[] buffer = newBuffer(size);
63: if (directory != null)
64: synchronized (directory) { // Ensure addition of buffer and adjustment to directory size are atomic wrt directory
65: buffers.add(buffer);
66: directory.sizeInBytes += size;
67: sizeInBytes += size;
68: }
69: else
70: buffers.add(buffer);
71: return buffer;
72: }
73:
74: final synchronized byte[] getBuffer(int index) {
75: return (byte[]) buffers.get(index);
76: }
77:
78: 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: byte[] newBuffer(int size) {
89: return new byte[size];
90: }
91:
92: // Only valid if in a directory
93: long getSizeInBytes() {
94: synchronized (directory) {
95: return sizeInBytes;
96: }
97: }
98:
99: }
|