Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 72000bd1a30b076d386c085d03d7296e3cfa4e42 (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
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
/***************************************************************************
 * 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.File;
import java.io.IOException;
import java.io.RandomAccessFile;

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

  private RandomAccessFile randomAccessFile;

  private ExtendedDataInput input;

  private ExtendedDataOutput output;

  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);
      input = new DataInputExtender(randomAccessFile);
      output = new DataOutputExtender(randomAccessFile);
      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 long getValuePosition(long index)
  {
    return getPosition(index) + getKeySize();
  }

  public K getMaxKey()
  {
    if (entryCount == 0)
    {
      return null;
    }

    return getKey(entryCount - 1);
  }

  public K getKey(long index)
  {
    try
    {
      long pos = getPosition(index);
      randomAccessFile.seek(pos);
      return readKey(input);
    }
    catch (IOException ex)
    {
      throw new IORuntimeException(ex);
    }
  }

  public V getValue(long index)
  {
    try
    {
      long pos = getValuePosition(index);
      randomAccessFile.seek(pos);
      return readValue(input);
    }
    catch (IOException ex)
    {
      throw new IORuntimeException(ex);
    }
  }

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

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

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

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

        randomAccessFile.seek(getPosition(i));
        writeKey(output, k);
        randomAccessFile.seek(getValuePosition(i));
        writeValue(output, v);
      }

      ++entryCount;
      randomAccessFile.seek(getPosition(index));
      writeKey(output, key);
      randomAccessFile.seek(getValuePosition(index));
      writeValue(output, 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(getPosition(mid));
      Comparable<K> midVal = readKey(input);
      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(ExtendedDataInput in) throws IOException;

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

  public abstract int getValueSize();

  protected abstract V readValue(ExtendedDataInput in) throws IOException;

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

Back to the top