Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 244ec56751e6d0fb446c2c1e9824c443bdd82141 (plain) (blame)
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
/***************************************************************************
 * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/
package org.eclipse.net4j.util.io;

import java.io.Closeable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * @author Eike Stepper
 */
public abstract class SortedFileMap<K extends Comparable, V> implements Closeable
{
  private File file;

  private RandomAccessFile randomAccessFile;

  private long entrySize;

  private long entryCount;

  /**
   * @see RandomAccessFile#RandomAccessFile(File, String)
   */
  public SortedFileMap(File file, String mode)
  {
    try
    {
      this.file = file;
      randomAccessFile = new RandomAccessFile(file, mode);
      entrySize = getKeySize() + getValueSize();
      entryCount = randomAccessFile.length() / entrySize;
    }
    catch (IOException ex)
    {
      throw new IORuntimeException(ex);
    }
  }

  public void close() throws IOException
  {
    IOUtil.close(randomAccessFile);
  }

  public File getFile()
  {
    return file;
  }

  public RandomAccessFile getRandomAccessFile()
  {
    return randomAccessFile;
  }

  public long getEntryCount()
  {
    return entryCount;
  }

  public int getEntrySize()
  {
    return (int)entrySize;
  }

  public long getPosition(long index)
  {
    return index * entrySize;
  }

  public V get(K key)
  {
    try
    {
      long index = search(key);
      if (index < 0)
      {
        return null;
      }

      return readValue(randomAccessFile);
    }
    catch (IOException ex)
    {
      throw new IORuntimeException(ex);
    }
  }

  public V put(K key, V value)
  {
    try
    {
      long index = search(key);
      if (index >= 0)
      {
        long pos = randomAccessFile.getFilePointer();
        V oldValue = readValue(randomAccessFile);
        randomAccessFile.seek(pos);
        writeValue(randomAccessFile, value);
        return oldValue;
      }

      index = -index - 1;
      for (long i = entryCount; i > index; --i)
      {
        randomAccessFile.seek((i - 1) * entrySize);
        K k = readKey(randomAccessFile);
        V v = readValue(randomAccessFile);

        randomAccessFile.seek(i * entrySize);
        writeKey(randomAccessFile, k);
        writeValue(randomAccessFile, v);
      }

      ++entryCount;
      randomAccessFile.seek(getPosition(index));
      writeKey(randomAccessFile, key);
      writeValue(randomAccessFile, value);
      return null;
    }
    catch (IOException ex)
    {
      throw new IORuntimeException(ex);
    }
  }

  /**
   * @return The index of the entry with the given key if the key exists,
   *         <code>-(insertionIndex + 1)</code> otherwise.
   */
  protected long search(K key) throws IOException
  {
    long low = 0;
    long high = entryCount - 1;

    while (low <= high)
    {
      long mid = low + high >> 1;
      randomAccessFile.seek(mid * entrySize);
      Comparable midVal = readKey(randomAccessFile);
      int cmp = midVal.compareTo(key);

      if (cmp < 0)
        low = mid + 1;
      else if (cmp > 0)
        high = mid - 1;
      else
        return mid; // key found
    }
    return -(low + 1); // key not found.
  }

  public abstract int getKeySize();

  protected abstract K readKey(DataInput in) throws IOException;

  protected abstract void writeKey(DataOutput out, K key) throws IOException;

  public abstract int getValueSize();

  protected abstract V readValue(DataInput in) throws IOException;

  protected abstract void writeValue(DataOutput out, V value) throws IOException;
}

Back to the top