summaryrefslogtreecommitdiff
path: root/platform/util/src/com/intellij/util/io/RandomAccessDataFile.java
blob: 9a297bcef6d1ed64ece14231e185ea6f47353024 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * @author max
 */
package com.intellij.util.io;

import com.intellij.openapi.Forceable;
import com.intellij.openapi.diagnostic.Logger;
import org.jetbrains.annotations.NotNull;

import java.io.*;
import java.nio.ByteBuffer;

public class RandomAccessDataFile implements Forceable, Closeable {
  protected static final Logger LOG = Logger.getInstance("#com.intellij.util.io.RandomAccessDataFile");

  private static final OpenChannelsCache ourCache = new OpenChannelsCache(150, "rw");
  private static int ourFilesCount = 0;

  private final int myCount = ourFilesCount++;
  private final File myFile;
  private final PagePool myPool;
  private long lastSeek = -1l;

  private final byte[] myTypedIOBuffer = new byte[8];

  private final FileWriter log;

  private volatile long mySize;
  private volatile boolean myIsDirty = false;
  private volatile boolean myIsDisposed = false;

  private static final boolean DEBUG = false;

  public RandomAccessDataFile(final File file) throws IOException {
    this(file, PagePool.SHARED);
  }

  public RandomAccessDataFile(final File file, final PagePool pool) throws IOException {
    myPool = pool;
    myFile = file;
    if (!file.exists()) {
      throw new FileNotFoundException(file.getPath() + " does not exist");
    }

    mySize = file.length();
    if (DEBUG) {
      log = new FileWriter(file.getPath() + ".log");
    }
    else {
      log = null;
    }
  }

  @NotNull
  public File getFile() {
    return myFile;
  }

  public void put(long addr, byte[] bytes, int off, int len) {
    assertNotDisposed();

    myIsDirty = true;
    mySize = Math.max(mySize, addr + len);

    while (len > 0) {
      final Page page = myPool.alloc(this, addr);
      int written = page.put(addr, bytes, off, len);
      len -= written;
      addr += written;
      off += written;
    }
  }

  public void get(long addr, byte[] bytes, int off, int len) {
    assertNotDisposed();

    while (len > 0) {
      final Page page = myPool.alloc(this, addr);
      int read = page.get(addr, bytes, off, len);
      len -= read;
      addr += read;
      off += read;
    }
  }

  private void releaseFile() {
    ourCache.releaseChannel(myFile);
  }

  private RandomAccessFile getRandomAccessFile() throws FileNotFoundException {
    return ourCache.getChannel(myFile);
  }

  public void putInt(long addr, int value) {
    Bits.putInt(myTypedIOBuffer, 0, value);
    put(addr, myTypedIOBuffer, 0, 4);
  }

  public int getInt(long addr) {
    get(addr, myTypedIOBuffer, 0, 4);
    return Bits.getInt(myTypedIOBuffer, 0);
  }

  public void putLong(long addr, long value) {
    Bits.putLong(myTypedIOBuffer, 0, value);
    put(addr, myTypedIOBuffer, 0, 8);
  }

  public void putByte(final long addr, final byte b) {
    myTypedIOBuffer[0] = b;
    put(addr, myTypedIOBuffer, 0, 1);
  }

  public byte getByte(long addr) {
    get(addr, myTypedIOBuffer, 0, 1);
    return myTypedIOBuffer[0];
  }

  public long getLong(long addr) {
    get(addr, myTypedIOBuffer, 0, 8);
    return Bits.getLong(myTypedIOBuffer, 0);
  }

  public String getUTF(long addr) {
    try {
      int len = getInt(addr);
      byte[] bytes = new byte[len];
      get(addr + 4, bytes, 0, len);
      return new String(bytes, "UTF-8");
    }
    catch (UnsupportedEncodingException e) {
      // Can't be
      return "";
    }
  }

  public void putUTF(long addr, String value) {
    try {
      final byte[] bytes = value.getBytes("UTF-8");
      putInt(addr, bytes.length);
      put(addr + 4, bytes, 0, bytes.length);
    }
    catch (UnsupportedEncodingException e) {
      // Can't be
    }
  }

  public long length() {
    assertNotDisposed();
    return mySize;
  }

  public long physicalLength() {
    assertNotDisposed();

    long res;

    try {
      RandomAccessFile file = getRandomAccessFile();
      try {
        synchronized (file) {
          res = file.length();
        }
      }
      finally {
        releaseFile();
      }
    }
    catch (IOException e) {
      return 0;
    }
    return res;
  }

  public void dispose() {
    if (myIsDisposed) return;
    myPool.flushPages(this);
    ourCache.closeChannel(myFile);

    myIsDisposed = true;
  }

  @Override
  public void close() {
    dispose();
  }

  @Override
  public void force() {
    assertNotDisposed();
    if (isDirty()) {
      myPool.flushPages(this);
      myIsDirty = false;
    }
  }

  public void flushSomePages(int maxPagesToFlush) {
    assertNotDisposed();
    if (isDirty()) {
      myIsDirty = !myPool.flushPages(this, maxPagesToFlush);
    }
  }

  @Override
  public boolean isDirty() {
    assertNotDisposed();
    return myIsDirty;
  }

  public boolean isDisposed() {
    return myIsDisposed;
  }

  private void assertNotDisposed() {
    if (myIsDisposed) {
      LOG.assertTrue(false, "storage file is disposed: " + myFile);
    }
  }

  public static int totalReads = 0;
  public static long totalReadBytes = 0;

  public static int seekcount = 0;
  public static int totalWrites = 0;
  public static long totalWriteBytes = 0;

  void loadPage(final Page page) {
    assertNotDisposed();
    try {
      final RandomAccessFile file = getRandomAccessFile();
      try {
        synchronized (file) {
          seek(file, page.getOffset());
          final ByteBuffer buf = page.getBuf();

          totalReads++;
          totalReadBytes += Page.PAGE_SIZE;

          if (DEBUG) {
            log.write("Read at: \t" + page.getOffset() + "\t len: " + Page.PAGE_SIZE + ", size: " + mySize + "\n");
          }
          file.read(buf.array(), 0, Page.PAGE_SIZE);
          lastSeek += Page.PAGE_SIZE;
        }
      }
      finally {
        releaseFile();
      }
    }
    catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  void flushPage(final Page page, int start, int end) {
    assertNotDisposed();
    try {
      flush(page.getBuf(), page.getOffset() + start, start, end - start);
    }
    catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  private void flush(final ByteBuffer buf, final long fileOffset, final int bufOffset, int length) throws IOException {
    if (fileOffset + length > mySize) {
      length = (int)(mySize - fileOffset);
    }

    final RandomAccessFile file = getRandomAccessFile();
    try {
      synchronized (file) {
        seek(file, fileOffset);

        totalWrites++;
        totalWriteBytes += length;

        if (DEBUG) {
          log.write("Write at: \t" + fileOffset + "\t len: " + length + ", size: " + mySize + ", filesize: " + file.length() + "\n");
        }
        file.write(buf.array(), bufOffset, length);
        lastSeek += length;
      }
    }
    finally {
      releaseFile();
    }
  }

  private void seek(final RandomAccessFile file, final long fileOffset) throws IOException {
    if (DEBUG) {
      if (lastSeek != -1L && fileOffset != lastSeek) {
        long delta = fileOffset - lastSeek;
        seekcount++;
        log.write("Seeking: " + delta + "\n");
      }
      lastSeek = fileOffset;
    }

    file.seek(fileOffset);
  }

  public int hashCode() {
    return myCount;
  }

  @Override
  public synchronized String toString() {
    return "RandomAccessFile[" + myFile + ", dirty=" + myIsDirty + "]";
  }
}