From 281ccaa902669ea8e1fe9e2345ecc6537f2ab293 Mon Sep 17 00:00:00 2001 From: Erik Hatcher Date: Tue, 1 Jun 2004 09:27:03 +0000 Subject: [PATCH] applied latest patch from Andi Vajda git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150987 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/lucene/store/db/Block.java | 23 +++++++----- .../apache/lucene/store/db/DbDirectory.java | 31 +++++++++------- .../apache/lucene/store/db/DbInputStream.java | 15 ++++---- .../lucene/store/db/DbOutputStream.java | 20 ++++++----- .../java/org/apache/lucene/store/db/File.java | 36 ++++++++++--------- 5 files changed, 72 insertions(+), 53 deletions(-) diff --git a/sandbox/contributions/db/src/java/org/apache/lucene/store/db/Block.java b/sandbox/contributions/db/src/java/org/apache/lucene/store/db/Block.java index 6897bd1e46c..176dd689d3e 100644 --- a/sandbox/contributions/db/src/java/org/apache/lucene/store/db/Block.java +++ b/sandbox/contributions/db/src/java/org/apache/lucene/store/db/Block.java @@ -99,27 +99,32 @@ public class Block extends Object { protected void seek(long position) throws IOException { - position = position >>> DbOutputStream.BLOCK_SHIFT; byte[] data = key.getData(); - int last = data.length - 1; + int index = data.length - 8; - for (int i = 0; i < 8; i++) { - data[last - i] = (byte) (position & 0xff); - position >>>= 8; - } + position >>>= DbOutputStream.BLOCK_SHIFT; + + 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 { try { - blocks.get(txn, key, data, 0); + blocks.get(txn, key, data, flags); } catch (DbException e) { throw new IOException(e.getMessage()); } } - protected void put(Db blocks, DbTxn txn) + protected void put(Db blocks, DbTxn txn, int flags) throws IOException { try { diff --git a/sandbox/contributions/db/src/java/org/apache/lucene/store/db/DbDirectory.java b/sandbox/contributions/db/src/java/org/apache/lucene/store/db/DbDirectory.java index 2052f7ab827..f4495525f2f 100644 --- a/sandbox/contributions/db/src/java/org/apache/lucene/store/db/DbDirectory.java +++ b/sandbox/contributions/db/src/java/org/apache/lucene/store/db/DbDirectory.java @@ -87,6 +87,7 @@ public class DbDirectory extends Directory { protected Db files, blocks; protected DbTxn txn; + protected int flags; /** * Instantiate a DbDirectory. The same threading rules that apply to @@ -97,13 +98,15 @@ public class DbDirectory extends Directory { * null. * @param files a db handle to store file records. * @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(); this.txn = txn; + this.flags = flags; this.files = files; this.blocks = blocks; } @@ -116,19 +119,19 @@ public class DbDirectory extends Directory { public OutputStream createFile(String name) throws IOException { - return new DbOutputStream(files, blocks, txn, name, true); + return new DbOutputStream(files, blocks, txn, flags, name, true); } public void deleteFile(String name) throws IOException { - new File(name).delete(files, blocks, txn); + new File(name).delete(files, blocks, txn, flags); } public boolean fileExists(String name) throws IOException { - return new File(name).exists(files, txn); + return new File(name).exists(files, txn, flags); } public long fileLength(String name) @@ -136,7 +139,7 @@ public class DbDirectory extends Directory { { File file = new File(name); - if (file.exists(files, txn)) + if (file.exists(files, txn, flags)) return file.getLength(); throw new IOException("File does not exist: " + name); @@ -147,7 +150,7 @@ public class DbDirectory extends Directory { { File file = new File(name); - if (file.exists(files, txn)) + if (file.exists(files, txn, flags)) return file.getTimeModified(); throw new IOException("File does not exist: " + name); @@ -167,9 +170,10 @@ public class DbDirectory extends Directory { data.setPartialLength(0); 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 = new ByteArrayInputStream(key.getData()); @@ -179,7 +183,8 @@ public class DbDirectory extends Directory { in.close(); 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()); in = new DataInputStream(buffer); name = in.readUTF(); @@ -202,7 +207,7 @@ public class DbDirectory extends Directory { public InputStream openFile(String name) throws IOException { - return new DbInputStream(files, blocks, txn, name); + return new DbInputStream(files, blocks, txn, flags, name); } public Lock makeLock(String name) @@ -213,7 +218,7 @@ public class DbDirectory extends Directory { public void renameFile(String from, String to) throws IOException { - new File(from).rename(files, blocks, txn, to); + new File(from).rename(files, blocks, txn, flags, to); } public void touchFile(String name) @@ -222,9 +227,9 @@ public class DbDirectory extends Directory { File file = new File(name); long length = 0L; - if (file.exists(files, txn)) + if (file.exists(files, txn, flags)) length = file.getLength(); - file.modify(files, txn, length, System.currentTimeMillis()); + file.modify(files, txn, flags, length, System.currentTimeMillis()); } } diff --git a/sandbox/contributions/db/src/java/org/apache/lucene/store/db/DbInputStream.java b/sandbox/contributions/db/src/java/org/apache/lucene/store/db/DbInputStream.java index 0d88feef713..599306a6aac 100644 --- a/sandbox/contributions/db/src/java/org/apache/lucene/store/db/DbInputStream.java +++ b/sandbox/contributions/db/src/java/org/apache/lucene/store/db/DbInputStream.java @@ -76,8 +76,10 @@ public class DbInputStream extends InputStream { protected Block block; protected DbTxn txn; 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 { super(); @@ -85,15 +87,16 @@ public class DbInputStream extends InputStream { this.files = files; this.blocks = blocks; this.txn = txn; + this.flags = flags; this.file = new File(name); - if (!file.exists(files, txn)) + if (!file.exists(files, txn, flags)) throw new IOException("File does not exist: " + name); length = file.getLength(); block = new Block(file); - block.get(blocks, txn); + block.get(blocks, txn, flags); } public Object clone() @@ -103,7 +106,7 @@ public class DbInputStream extends InputStream { clone.block = new Block(file); clone.block.seek(position); - clone.block.get(blocks, txn); + clone.block.get(blocks, txn, flags); return clone; } catch (IOException e) { @@ -134,7 +137,7 @@ public class DbInputStream extends InputStream { position += blockLen; block.seek(position); - block.get(blocks, txn); + block.get(blocks, txn, flags); blockPos = 0; } @@ -155,7 +158,7 @@ public class DbInputStream extends InputStream { (position >>> DbOutputStream.BLOCK_SHIFT)) { block.seek(pos); - block.get(blocks, txn); + block.get(blocks, txn, flags); } position = pos; diff --git a/sandbox/contributions/db/src/java/org/apache/lucene/store/db/DbOutputStream.java b/sandbox/contributions/db/src/java/org/apache/lucene/store/db/DbOutputStream.java index 3f4af53c9f5..9b667acf716 100644 --- a/sandbox/contributions/db/src/java/org/apache/lucene/store/db/DbOutputStream.java +++ b/sandbox/contributions/db/src/java/org/apache/lucene/store/db/DbOutputStream.java @@ -84,8 +84,9 @@ public class DbOutputStream extends OutputStream { protected Block block; protected DbTxn txn; 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) throws IOException { @@ -94,13 +95,14 @@ public class DbOutputStream extends OutputStream { this.files = files; this.blocks = blocks; 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); length = file.getLength(); seek(length); - block.get(blocks, txn); + block.get(blocks, txn, flags); } public void close() @@ -108,9 +110,9 @@ public class DbOutputStream extends OutputStream { { flush(); 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) @@ -123,14 +125,14 @@ public class DbOutputStream extends OutputStream { int blockLen = BLOCK_LEN - blockPos; System.arraycopy(b, offset, block.getData(), blockPos, blockLen); - block.put(blocks, txn); + block.put(blocks, txn, flags); len -= blockLen; offset += blockLen; position += blockLen; block.seek(position); - block.get(blocks, txn); + block.get(blocks, txn, flags); blockPos = 0; } @@ -167,9 +169,9 @@ public class DbOutputStream extends OutputStream { position = pos; else { - block.put(blocks, txn); + block.put(blocks, txn, flags); block.seek(pos); - block.get(blocks, txn); + block.get(blocks, txn, flags); position = pos; } } diff --git a/sandbox/contributions/db/src/java/org/apache/lucene/store/db/File.java b/sandbox/contributions/db/src/java/org/apache/lucene/store/db/File.java index ff4f1bddd59..465f6bfcd70 100644 --- a/sandbox/contributions/db/src/java/org/apache/lucene/store/db/File.java +++ b/sandbox/contributions/db/src/java/org/apache/lucene/store/db/File.java @@ -92,12 +92,13 @@ public class File extends Object { 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 { this(name); - if (!exists(files, txn)) + if (!exists(files, txn, flags)) { if (!create) 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) 0x3f)); 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) { throw new IOException(e.getMessage()); } @@ -172,11 +174,11 @@ public class File extends Object { return timeModified; } - protected boolean exists(Db files, DbTxn txn) + protected boolean exists(Db files, DbTxn txn, int flags) throws IOException { try { - if (files.get(txn, key, data, 0) == Db.DB_NOTFOUND) + if (files.get(txn, key, data, flags) == Db.DB_NOTFOUND) return false; } catch (DbException e) { throw new IOException(e.getMessage()); @@ -196,7 +198,8 @@ public class File extends Object { 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 { ByteArrayOutputStream buffer = new ByteArrayOutputStream(32); @@ -219,10 +222,10 @@ public class File extends Object { 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 { - if (!exists(files, txn)) + if (!exists(files, txn, flags)) throw new IOException("File does not exist: " + getName()); Dbc cursor = null; @@ -241,15 +244,15 @@ public class File extends Object { cursorData.setPartialLength(0); cursorData.setFlags(Db.DB_DBT_PARTIAL); - cursor = blocks.cursor(txn, 0); + cursor = blocks.cursor(txn, flags); if (cursor.get(cursorKey, cursorData, - Db.DB_SET_RANGE) != Db.DB_NOTFOUND) + Db.DB_SET_RANGE | flags) != Db.DB_NOTFOUND) { cursor.delete(0); - while (cursor.get(cursorKey, cursorData, Db.DB_NEXT) != - Db.DB_NOTFOUND) { + while (cursor.get(cursorKey, cursorData, + Db.DB_NEXT | flags) != Db.DB_NOTFOUND) { for (int i = 0; i < bytes.length; i++) if (bytes[i] != cursorBytes[i]) 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 { - if (!exists(files, txn)) + if (!exists(files, txn, flags)) throw new IOException("File does not exist: " + getName()); File newFile = new File(name); - if (newFile.exists(files, txn)) - newFile.delete(files, blocks, txn); + if (newFile.exists(files, txn, flags)) + newFile.delete(files, blocks, txn, flags); try { files.delete(txn, key, 0);