订阅所有JSP/Servlet的日志 订阅 | 这是最新一篇日志 上一篇 | 下一篇日志 下一篇 ]
JAVA技术

StrongHashTable

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
package org.garret.perst.impl;
 
import org.garret.perst.IPersistent;
 
public class StrongHashTable
    implements OidHashTable
{
    static class Entry
    {
 
        Entry next;
        IPersistent obj;
        int oid;
 
        Entry(int i, IPersistent ipersistent, Entry entry)
        {
            next = entry;
            oid = i;
            obj = ipersistent;
        }
    }
 
 
    Entry table[];
    static final float loadFactor = 0.75F;
    int count;
    int threshold;
    boolean flushing;
    static final int MODIFIED_BUFFER_SIZE = 1024;
    IPersistent modified[];
    int nModified;
 
    public StrongHashTable(int i)
    {
        threshold = (int)((float)i * 0.75F);
        if(i != 0)
            table = new Entry[i];
        modified = new IPersistent[1024];
    }
 
    public synchronized boolean remove(int i)
    {
        Entry aentry[] = table;
        int j = (i & 0x7fffffff) % aentry.length;
        Entry entry = aentry[j];
        Entry entry1 = null;
        for(; entry != null; entry = entry.next)
        {
            if(entry.oid == i)
            {
                entry.obj = null;
                count--;
                if(entry1 != null)
                    entry1.next = entry.next;
                else
                    aentry[j] = entry.next;
                return true;
            }
            entry1 = entry;
        }
 
        return false;
    }
 
    public synchronized void put(int i, IPersistent ipersistent)
    {
        Entry aentry[] = table;
        int j = (i & 0x7fffffff) % aentry.length;
        for(Entry entry = aentry[j]; entry != null; entry = entry.next)
            if(entry.oid == i)
            {
                entry.obj = ipersistent;
                return;
            }
 
        if(count >= threshold && !flushing)
        {
            rehash();
            aentry = table;
            j = (i & 0x7fffffff) % aentry.length;
        }
        aentry[j] = new Entry(i, ipersistent, aentry[j]);
        count++;
    }
 
    public synchronized IPersistent get(int i)
    {
        Entry aentry[] = table;
        int j = (i & 0x7fffffff) % aentry.length;
        for(Entry entry = aentry[j]; entry != null; entry = entry.next)
            if(entry.oid == i)
                return entry.obj;
 
        return null;
    }
 
    void rehash()
    {
        int i = table.length;
        Entry aentry[] = table;
        int k = i * 2 + 1;
        Entry aentry1[] = new Entry[k];
        threshold = (int)((float)k * 0.75F);
        table = aentry1;
        for(int j = i; --j >= 0;)
        {
            Entry entry = aentry[j];
            while(entry != null) 
            {
                Entry entry1 = entry;
                entry = entry.next;
                int l = (entry1.oid & 0x7fffffff) % k;
                entry1.next = aentry1[l];
                aentry1[l] = entry1;
            }
        }
 
    }
 
    public synchronized void flush()
    {
        if(nModified < 1024)
        {
            IPersistent aipersistent[] = modified;
            int i = nModified;
            do
            {
                if(--i < 0)
                    break;
                IPersistent ipersistent = aipersistent[i];
                if(ipersistent.isModified())
                    ipersistent.store();
            } while(true);
        } else
        {
            Entry aentry[] = table;
            flushing = true;
            for(int j = 0; j < aentry.length; j++)
            {
                for(Entry entry = aentry[j]; entry != null; entry = entry.next)
                    if(entry.obj.isModified())
                        entry.obj.store();
 
            }
 
            flushing = false;
            if(count >= threshold)
                rehash();
        }
        nModified = 0;
    }
 
    public synchronized void clear()
    {
        Entry aentry[] = table;
        for(int i = 0; i < aentry.length; i++)
            aentry[i] = null;
 
        count = 0;
        nModified = 0;
    }
 
    public synchronized void invalidate()
    {
        for(int i = 0; i < table.length; i++)
        {
            for(Entry entry = table[i]; entry != null; entry = entry.next)
                if(entry.obj.isModified())
                    entry.obj.invalidate();
 
            table[i] = null;
        }
 
        count = 0;
        nModified = 0;
    }
 
    public synchronized void setDirty(IPersistent ipersistent)
    {
        if(nModified < 1024)
            modified[nModified++] = ipersistent;
    }
 
    public void clearDirty(IPersistent ipersistent)
    {
    }
 
    public int size()
    {
        return count;
    }
 
    public void preprocess()
    {
    }
}


平均得分
(0 次评分)





文章来自: 本站原创
标签: Hashtable 
评论: 0 | 查看次数: 1819
发表评论
昵 称:  登录
内 容:
选 项:
字数限制 1000 字 | UBB代码 开启 | [img]标签 开启