applied latest patch from Andi Vajda

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150987 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2004-06-01 09:27:03 +00:00
parent 04c866d63a
commit 281ccaa902
5 changed files with 72 additions and 53 deletions

View File

@ -99,27 +99,32 @@ public class Block extends Object {
protected void seek(long position) protected void seek(long position)
throws IOException throws IOException
{ {
position = position >>> DbOutputStream.BLOCK_SHIFT;
byte[] data = key.getData(); byte[] data = key.getData();
int last = data.length - 1; int index = data.length - 8;
for (int i = 0; i < 8; i++) { position >>>= DbOutputStream.BLOCK_SHIFT;
data[last - i] = (byte) (position & 0xff);
position >>>= 8; data[index + 0] = (byte) (0xff & (position >>> 56));
} data[index + 1] = (byte) (0xff & (position >>> 48));
data[index + 2] = (byte) (0xff & (position >>> 40));
data[index + 3] = (byte) (0xff & (position >>> 32));
data[index + 4] = (byte) (0xff & (position >>> 24));
data[index + 5] = (byte) (0xff & (position >>> 16));
data[index + 6] = (byte) (0xff & (position >>> 8));
data[index + 7] = (byte) (0xff & (position >>> 0));
} }
protected void get(Db blocks, DbTxn txn) protected void get(Db blocks, DbTxn txn, int flags)
throws IOException throws IOException
{ {
try { try {
blocks.get(txn, key, data, 0); blocks.get(txn, key, data, flags);
} catch (DbException e) { } catch (DbException e) {
throw new IOException(e.getMessage()); throw new IOException(e.getMessage());
} }
} }
protected void put(Db blocks, DbTxn txn) protected void put(Db blocks, DbTxn txn, int flags)
throws IOException throws IOException
{ {
try { try {

View File

@ -87,6 +87,7 @@ public class DbDirectory extends Directory {
protected Db files, blocks; protected Db files, blocks;
protected DbTxn txn; protected DbTxn txn;
protected int flags;
/** /**
* Instantiate a DbDirectory. The same threading rules that apply to * Instantiate a DbDirectory. The same threading rules that apply to
@ -97,13 +98,15 @@ public class DbDirectory extends Directory {
* <code>null</code>. * <code>null</code>.
* @param files a db handle to store file records. * @param files a db handle to store file records.
* @param blocks a db handle to store file data blocks. * @param blocks a db handle to store file data blocks.
* @param flags flags used for db read operations.
*/ */
public DbDirectory(DbTxn txn, Db files, Db blocks) public DbDirectory(DbTxn txn, Db files, Db blocks, int flags)
{ {
super(); super();
this.txn = txn; this.txn = txn;
this.flags = flags;
this.files = files; this.files = files;
this.blocks = blocks; this.blocks = blocks;
} }
@ -116,19 +119,19 @@ public class DbDirectory extends Directory {
public OutputStream createFile(String name) public OutputStream createFile(String name)
throws IOException throws IOException
{ {
return new DbOutputStream(files, blocks, txn, name, true); return new DbOutputStream(files, blocks, txn, flags, name, true);
} }
public void deleteFile(String name) public void deleteFile(String name)
throws IOException throws IOException
{ {
new File(name).delete(files, blocks, txn); new File(name).delete(files, blocks, txn, flags);
} }
public boolean fileExists(String name) public boolean fileExists(String name)
throws IOException throws IOException
{ {
return new File(name).exists(files, txn); return new File(name).exists(files, txn, flags);
} }
public long fileLength(String name) public long fileLength(String name)
@ -136,7 +139,7 @@ public class DbDirectory extends Directory {
{ {
File file = new File(name); File file = new File(name);
if (file.exists(files, txn)) if (file.exists(files, txn, flags))
return file.getLength(); return file.getLength();
throw new IOException("File does not exist: " + name); throw new IOException("File does not exist: " + name);
@ -147,7 +150,7 @@ public class DbDirectory extends Directory {
{ {
File file = new File(name); File file = new File(name);
if (file.exists(files, txn)) if (file.exists(files, txn, flags))
return file.getTimeModified(); return file.getTimeModified();
throw new IOException("File does not exist: " + name); throw new IOException("File does not exist: " + name);
@ -167,9 +170,10 @@ public class DbDirectory extends Directory {
data.setPartialLength(0); data.setPartialLength(0);
data.setFlags(Db.DB_DBT_PARTIAL); data.setFlags(Db.DB_DBT_PARTIAL);
cursor = files.cursor(txn, 0); cursor = files.cursor(txn, flags);
if (cursor.get(key, data, Db.DB_SET_RANGE) != Db.DB_NOTFOUND) if (cursor.get(key, data,
Db.DB_SET_RANGE | flags) != Db.DB_NOTFOUND)
{ {
ByteArrayInputStream buffer = ByteArrayInputStream buffer =
new ByteArrayInputStream(key.getData()); new ByteArrayInputStream(key.getData());
@ -179,7 +183,8 @@ public class DbDirectory extends Directory {
in.close(); in.close();
list.add(name); list.add(name);
while (cursor.get(key, data, Db.DB_NEXT) != Db.DB_NOTFOUND) { while (cursor.get(key, data,
Db.DB_NEXT | flags) != Db.DB_NOTFOUND) {
buffer = new ByteArrayInputStream(key.getData()); buffer = new ByteArrayInputStream(key.getData());
in = new DataInputStream(buffer); in = new DataInputStream(buffer);
name = in.readUTF(); name = in.readUTF();
@ -202,7 +207,7 @@ public class DbDirectory extends Directory {
public InputStream openFile(String name) public InputStream openFile(String name)
throws IOException throws IOException
{ {
return new DbInputStream(files, blocks, txn, name); return new DbInputStream(files, blocks, txn, flags, name);
} }
public Lock makeLock(String name) public Lock makeLock(String name)
@ -213,7 +218,7 @@ public class DbDirectory extends Directory {
public void renameFile(String from, String to) public void renameFile(String from, String to)
throws IOException throws IOException
{ {
new File(from).rename(files, blocks, txn, to); new File(from).rename(files, blocks, txn, flags, to);
} }
public void touchFile(String name) public void touchFile(String name)
@ -222,9 +227,9 @@ public class DbDirectory extends Directory {
File file = new File(name); File file = new File(name);
long length = 0L; long length = 0L;
if (file.exists(files, txn)) if (file.exists(files, txn, flags))
length = file.getLength(); length = file.getLength();
file.modify(files, txn, length, System.currentTimeMillis()); file.modify(files, txn, flags, length, System.currentTimeMillis());
} }
} }

View File

@ -76,8 +76,10 @@ public class DbInputStream extends InputStream {
protected Block block; protected Block block;
protected DbTxn txn; protected DbTxn txn;
protected Db files, blocks; protected Db files, blocks;
protected int flags;
protected DbInputStream(Db files, Db blocks, DbTxn txn, String name) protected DbInputStream(Db files, Db blocks, DbTxn txn, int flags,
String name)
throws IOException throws IOException
{ {
super(); super();
@ -85,15 +87,16 @@ public class DbInputStream extends InputStream {
this.files = files; this.files = files;
this.blocks = blocks; this.blocks = blocks;
this.txn = txn; this.txn = txn;
this.flags = flags;
this.file = new File(name); this.file = new File(name);
if (!file.exists(files, txn)) if (!file.exists(files, txn, flags))
throw new IOException("File does not exist: " + name); throw new IOException("File does not exist: " + name);
length = file.getLength(); length = file.getLength();
block = new Block(file); block = new Block(file);
block.get(blocks, txn); block.get(blocks, txn, flags);
} }
public Object clone() public Object clone()
@ -103,7 +106,7 @@ public class DbInputStream extends InputStream {
clone.block = new Block(file); clone.block = new Block(file);
clone.block.seek(position); clone.block.seek(position);
clone.block.get(blocks, txn); clone.block.get(blocks, txn, flags);
return clone; return clone;
} catch (IOException e) { } catch (IOException e) {
@ -134,7 +137,7 @@ public class DbInputStream extends InputStream {
position += blockLen; position += blockLen;
block.seek(position); block.seek(position);
block.get(blocks, txn); block.get(blocks, txn, flags);
blockPos = 0; blockPos = 0;
} }
@ -155,7 +158,7 @@ public class DbInputStream extends InputStream {
(position >>> DbOutputStream.BLOCK_SHIFT)) (position >>> DbOutputStream.BLOCK_SHIFT))
{ {
block.seek(pos); block.seek(pos);
block.get(blocks, txn); block.get(blocks, txn, flags);
} }
position = pos; position = pos;

View File

@ -84,8 +84,9 @@ public class DbOutputStream extends OutputStream {
protected Block block; protected Block block;
protected DbTxn txn; protected DbTxn txn;
protected Db files, blocks; protected Db files, blocks;
protected int flags;
protected DbOutputStream(Db files, Db blocks, DbTxn txn, protected DbOutputStream(Db files, Db blocks, DbTxn txn, int flags,
String name, boolean create) String name, boolean create)
throws IOException throws IOException
{ {
@ -94,13 +95,14 @@ public class DbOutputStream extends OutputStream {
this.files = files; this.files = files;
this.blocks = blocks; this.blocks = blocks;
this.txn = txn; this.txn = txn;
this.flags = flags;
file = new File(files, blocks, txn, name, create); file = new File(files, blocks, txn, flags, name, create);
block = new Block(file); block = new Block(file);
length = file.getLength(); length = file.getLength();
seek(length); seek(length);
block.get(blocks, txn); block.get(blocks, txn, flags);
} }
public void close() public void close()
@ -108,9 +110,9 @@ public class DbOutputStream extends OutputStream {
{ {
flush(); flush();
if (length > 0) if (length > 0)
block.put(blocks, txn); block.put(blocks, txn, flags);
file.modify(files, txn, length, System.currentTimeMillis()); file.modify(files, txn, flags, length, System.currentTimeMillis());
} }
protected void flushBuffer(byte[] b, int len) protected void flushBuffer(byte[] b, int len)
@ -123,14 +125,14 @@ public class DbOutputStream extends OutputStream {
int blockLen = BLOCK_LEN - blockPos; int blockLen = BLOCK_LEN - blockPos;
System.arraycopy(b, offset, block.getData(), blockPos, blockLen); System.arraycopy(b, offset, block.getData(), blockPos, blockLen);
block.put(blocks, txn); block.put(blocks, txn, flags);
len -= blockLen; len -= blockLen;
offset += blockLen; offset += blockLen;
position += blockLen; position += blockLen;
block.seek(position); block.seek(position);
block.get(blocks, txn); block.get(blocks, txn, flags);
blockPos = 0; blockPos = 0;
} }
@ -167,9 +169,9 @@ public class DbOutputStream extends OutputStream {
position = pos; position = pos;
else else
{ {
block.put(blocks, txn); block.put(blocks, txn, flags);
block.seek(pos); block.seek(pos);
block.get(blocks, txn); block.get(blocks, txn, flags);
position = pos; position = pos;
} }
} }

View File

@ -92,12 +92,13 @@ public class File extends Object {
data.setFlags(Db.DB_DBT_USERMEM); data.setFlags(Db.DB_DBT_USERMEM);
} }
protected File(Db files, Db blocks, DbTxn txn, String name, boolean create) protected File(Db files, Db blocks, DbTxn txn, int flags,
String name, boolean create)
throws IOException throws IOException
{ {
this(name); this(name);
if (!exists(files, txn)) if (!exists(files, txn, flags))
{ {
if (!create) if (!create)
throw new IOException("File does not exist: " + name); throw new IOException("File does not exist: " + name);
@ -122,7 +123,8 @@ public class File extends Object {
uuid[8] = (byte) ((byte) 0x80 | uuid[8] = (byte) ((byte) 0x80 |
(uuid[8] & (byte) 0x3f)); (uuid[8] & (byte) 0x3f));
System.arraycopy(uuid, 0, key.getData(), 0, 16); System.arraycopy(uuid, 0, key.getData(), 0, 16);
} while (blocks.get(txn, key, data, 0) != Db.DB_NOTFOUND); } while (blocks.get(txn, key, data,
flags) != Db.DB_NOTFOUND);
} catch (DbException e) { } catch (DbException e) {
throw new IOException(e.getMessage()); throw new IOException(e.getMessage());
} }
@ -172,11 +174,11 @@ public class File extends Object {
return timeModified; return timeModified;
} }
protected boolean exists(Db files, DbTxn txn) protected boolean exists(Db files, DbTxn txn, int flags)
throws IOException throws IOException
{ {
try { try {
if (files.get(txn, key, data, 0) == Db.DB_NOTFOUND) if (files.get(txn, key, data, flags) == Db.DB_NOTFOUND)
return false; return false;
} catch (DbException e) { } catch (DbException e) {
throw new IOException(e.getMessage()); throw new IOException(e.getMessage());
@ -196,7 +198,8 @@ public class File extends Object {
return true; return true;
} }
protected void modify(Db files, DbTxn txn, long length, long timeModified) protected void modify(Db files, DbTxn txn, int flags,
long length, long timeModified)
throws IOException throws IOException
{ {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(32); ByteArrayOutputStream buffer = new ByteArrayOutputStream(32);
@ -219,10 +222,10 @@ public class File extends Object {
this.timeModified = timeModified; this.timeModified = timeModified;
} }
protected void delete(Db files, Db blocks, DbTxn txn) protected void delete(Db files, Db blocks, DbTxn txn, int flags)
throws IOException throws IOException
{ {
if (!exists(files, txn)) if (!exists(files, txn, flags))
throw new IOException("File does not exist: " + getName()); throw new IOException("File does not exist: " + getName());
Dbc cursor = null; Dbc cursor = null;
@ -241,15 +244,15 @@ public class File extends Object {
cursorData.setPartialLength(0); cursorData.setPartialLength(0);
cursorData.setFlags(Db.DB_DBT_PARTIAL); cursorData.setFlags(Db.DB_DBT_PARTIAL);
cursor = blocks.cursor(txn, 0); cursor = blocks.cursor(txn, flags);
if (cursor.get(cursorKey, cursorData, if (cursor.get(cursorKey, cursorData,
Db.DB_SET_RANGE) != Db.DB_NOTFOUND) Db.DB_SET_RANGE | flags) != Db.DB_NOTFOUND)
{ {
cursor.delete(0); cursor.delete(0);
while (cursor.get(cursorKey, cursorData, Db.DB_NEXT) != while (cursor.get(cursorKey, cursorData,
Db.DB_NOTFOUND) { Db.DB_NEXT | flags) != Db.DB_NOTFOUND) {
for (int i = 0; i < bytes.length; i++) for (int i = 0; i < bytes.length; i++)
if (bytes[i] != cursorBytes[i]) if (bytes[i] != cursorBytes[i])
return; return;
@ -268,16 +271,17 @@ public class File extends Object {
} }
} }
protected void rename(Db files, Db blocks, DbTxn txn, String name) protected void rename(Db files, Db blocks, DbTxn txn, int flags,
String name)
throws IOException throws IOException
{ {
if (!exists(files, txn)) if (!exists(files, txn, flags))
throw new IOException("File does not exist: " + getName()); throw new IOException("File does not exist: " + getName());
File newFile = new File(name); File newFile = new File(name);
if (newFile.exists(files, txn)) if (newFile.exists(files, txn, flags))
newFile.delete(files, blocks, txn); newFile.delete(files, blocks, txn, flags);
try { try {
files.delete(txn, key, 0); files.delete(txn, key, 0);