StrongHashTable
作者:anotherbug 日期:2007-09-14 20:13:08
1 | 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 次评分)
评论: 0 | 查看次数: 1819
发表评论
订阅
上一篇
|

文章来自:
标签: 




