OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [tools/] [gnu/] [classpath/] [tools/] [rmiregistry/] [PersistentHashTable.java] - Blame information for rev 779

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 779 jeremybenn
/* PersistentHasthable.java -- Persistent hash table.
2
   Copyright (C) 2006 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version. */
37
 
38
package gnu.classpath.tools.rmiregistry;
39
 
40
import gnu.classpath.tools.common.Persistent;
41
 
42
import java.io.BufferedInputStream;
43
import java.io.BufferedOutputStream;
44
import java.io.File;
45
import java.io.FileInputStream;
46
import java.io.FileOutputStream;
47
import java.io.ObjectInputStream;
48
import java.io.ObjectOutputStream;
49
import java.io.Serializable;
50
import java.util.Hashtable;
51
import java.util.Map;
52
import java.util.TimerTask;
53
 
54
/**
55
 * The persistent hash table. The changes are written to dist after
56
 * SAVE_AT_MOST_AFTER time from the latest database change or at most after
57
 * ALWAYS_UPDATE, if the database is updated very frequently. To ensure that no
58
 * information is lost, the shutdown method must be called before exit.
59
 *
60
 * @author Audrius Meskauskas (audriusa@bioinformatics.org)
61
 */
62
public class PersistentHashTable
63
  extends Hashtable
64
  implements Serializable, Persistent
65
{
66
 
67
  /**
68
   * Use serialVersionUID for interoperability
69
   */
70
  private static final long serialVersionUID = 1;
71
 
72
  class WriteToDiskTask extends TimerTask
73
  {
74
    /**
75
     * Save the database.
76
     */
77
    public void run()
78
    {
79
      writeContent();
80
      sheduled = null;
81
    }
82
  }
83
 
84
  /**
85
   * The database file.
86
   */
87
  File database;
88
 
89
  /**
90
   * The currently sheduled write to disk task, null if none.
91
   */
92
  WriteToDiskTask sheduled = null;
93
 
94
  /**
95
   * The time, when the disk database was last updated.
96
   */
97
  long lastUpdated;
98
 
99
  /**
100
   * Setting to false prevents the automated disk update.
101
   * The initial value is true to prevent writing while reading and is set
102
   * to false in createInstance.
103
   */
104
  transient boolean ready;
105
 
106
  /**
107
   * Use static method to obtain the instance.
108
   */
109
  private PersistentHashTable(File file)
110
  {
111
    if (file == null)
112
      throw new NullPointerException("Null file provided");
113
    database = file;
114
  }
115
 
116
  /**
117
   * Create a new persistent table that stores its information into the given
118
   * file.
119
   *
120
   * @param file
121
   *          the file, where the table stores its information.
122
   * @param coldStart
123
   *          if true, the existing file with this name will be erased and
124
   *          ignored. Otherwise, it will be assumed that the file contains the
125
   *          persistent table information.
126
   */
127
  public static Map createInstance(File file, boolean coldStart)
128
  {
129
    try
130
      {
131
        PersistentHashTable k2v;
132
        System.out.println ("Here1");
133
        if (file.exists())
134
          {
135
        System.out.println ("Here2");
136
            if (coldStart)
137
              {
138
        System.out.println ("Here2.5");
139
                file.delete();
140
                k2v = new PersistentHashTable(file);
141
              }
142
            else
143
              {
144
        System.out.println ("Here3");
145
                FileInputStream fi = new FileInputStream(file);
146
        System.out.println ("Here3.1");
147
                BufferedInputStream b = new BufferedInputStream(fi);
148
        System.out.println ("Here3.2");
149
                ObjectInputStream oin = new ObjectInputStream(b);
150
        System.out.println ("Here3.3");
151
 
152
        System.out.println ("Here4");
153
                k2v = (PersistentHashTable) oin.readObject();
154
                oin.close();
155
        System.out.println ("Here5");
156
              }
157
          }
158
        else
159
          {
160
        System.out.println ("Here6");
161
          k2v = new PersistentHashTable(file);
162
        System.out.println ("Here7");
163
          }
164
 
165
        System.out.println ("Here8");
166
        k2v.ready = true;
167
        return k2v;
168
      }
169
    catch (Exception ioex)
170
      {
171
        InternalError ierr = new InternalError("Unable to intialize with file "
172
                                               + file);
173
        ierr.initCause(ioex);
174
        throw ierr;
175
      }
176
  }
177
 
178
 
179
  /**
180
   * Write the database content to the disk.
181
   */
182
  public synchronized void writeContent()
183
  {
184
    try
185
      {
186
        FileOutputStream fou = new FileOutputStream(database);
187
        BufferedOutputStream b = new BufferedOutputStream(fou);
188
        ObjectOutputStream oout = new ObjectOutputStream(b);
189
        oout.writeObject(this);
190
        oout.close();
191
      }
192
    catch (Exception ioex)
193
      {
194
        InternalError ierr = new InternalError(
195
          "Failed to write database to disk: "+ database);
196
        ierr.initCause(ioex);
197
        throw ierr;
198
      }
199
  }
200
 
201
  /**
202
   * Mark the modified database as modified. The database will be written after
203
   * several seconds, unless another modification occurs.
204
   */
205
  public void markDirty()
206
  {
207
    if (System.currentTimeMillis() - lastUpdated > ALWAYS_UPDATE)
208
      {
209
        // Force storing to disk under intensive operation.
210
        writeContent();
211
        lastUpdated = System.currentTimeMillis();
212
        if (sheduled != null)
213
          {
214
            sheduled.cancel();
215
            sheduled = null;
216
          }
217
      }
218
    else
219
      {
220
        // Otherwise coalesce the disk database copy update events.
221
        if (sheduled != null)
222
          sheduled.cancel();
223
        sheduled = new WriteToDiskTask();
224
        timer.schedule(sheduled, SAVE_AT_MOST_AFTER);
225
      }
226
  }
227
 
228
  /**
229
   * Save the current database state to the disk before exit.
230
   */
231
  public void shutdown()
232
  {
233
    if (sheduled != null)
234
      {
235
        writeContent();
236
        sheduled = null;
237
      }
238
  }
239
 
240
  /**
241
   * Update the memory maps and mark as should be written to the disk.
242
   */
243
  public Object put(Object key, Object value)
244
  {
245
    super.put(key, value);
246
    if (ready)
247
      markDirty();
248
    return value;
249
  }
250
 
251
  /**
252
   * Update the memory maps and mark as should be written to the disk.
253
   */
254
  public Object remove(Object key)
255
  {
256
    Object removed = super.remove(key);
257
    if (ready)
258
      markDirty();
259
    return removed;
260
  }
261
 
262
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.