vgritsenko 2004/02/02 06:33:15
Modified: java/src/org/apache/xindice/core/filer BTreeFiler.java
HashFiler.java Paged.java
java/src/org/apache/xindice/core/indexer NameIndexer.java
ValueIndexer.java
Log:
Add max-descriptors configuration attribute to all Filers based on Paged.
Make Paged configurable, remove configurable from all Paged implementations.
Add some javadoc.
Revision Changes Path
1.21 +6 -33
xml-xindice/java/src/org/apache/xindice/core/filer/BTreeFiler.java
Index: BTreeFiler.java
===================================================================
RCS file:
/home/cvs/xml-xindice/java/src/org/apache/xindice/core/filer/BTreeFiler.java,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- BTreeFiler.java 9 Jan 2004 05:08:22 -0000 1.20
+++ BTreeFiler.java 2 Feb 2004 14:33:15 -0000 1.21
@@ -67,8 +67,6 @@
import org.apache.xindice.core.data.Record;
import org.apache.xindice.core.data.RecordSet;
import org.apache.xindice.core.data.Value;
-import org.apache.xindice.util.Configurable;
-import org.apache.xindice.util.Configuration;
import java.io.DataInputStream;
import java.io.DataOutputStream;
@@ -85,17 +83,16 @@
*
* @version CVS $Revision$, $Date$
*/
-public final class BTreeFiler extends BTree implements Configurable, Filer {
+public final class BTreeFiler extends BTree implements Filer {
private static final Log log = LogFactory.getLog(BTreeFiler.class);
- // Page status
+ /**
+ * Record page status
+ */
protected static final byte RECORD = 20;
- private static final String PAGESIZE = "pagesize";
- private static final String MAXKEYSIZE = "maxkeysize";
- private Configuration config;
private BTreeFilerHeader fileHeader;
@@ -104,36 +101,12 @@
fileHeader = (BTreeFilerHeader) getFileHeader();
}
- public void setConfig(Configuration config) {
- this.config = config;
- }
-
- public Configuration getConfig() {
- return config;
- }
-
public void setLocation(File root, String location) {
setFile(new File(root, location + ".tbl"));
}
public String getName() {
return "BTreeFiler";
- }
-
- public boolean open() throws DBException {
- if (super.open()) {
- // These are the only properties that can be changed after
creation
- fileHeader.setMaxKeySize(config.getShortAttribute(MAXKEYSIZE,
fileHeader.getMaxKeySize()));
- return true;
- } else {
- return false;
- }
- }
-
- public boolean create() throws DBException {
- fileHeader.setPageSize(config.getIntAttribute(PAGESIZE,
fileHeader.getPageSize()));
- fileHeader.setMaxKeySize(config.getShortAttribute(MAXKEYSIZE,
fileHeader.getMaxKeySize()));
- return super.create();
}
public Record readRecord(Key key) throws DBException {
1.21 +12 -29
xml-xindice/java/src/org/apache/xindice/core/filer/HashFiler.java
Index: HashFiler.java
===================================================================
RCS file:
/home/cvs/xml-xindice/java/src/org/apache/xindice/core/filer/HashFiler.java,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- HashFiler.java 9 Jan 2004 05:08:22 -0000 1.20
+++ HashFiler.java 2 Feb 2004 14:33:15 -0000 1.21
@@ -67,8 +67,6 @@
import org.apache.xindice.core.data.Record;
import org.apache.xindice.core.data.RecordSet;
import org.apache.xindice.core.data.Value;
-import org.apache.xindice.util.Configurable;
-import org.apache.xindice.util.Configuration;
import java.io.DataInputStream;
import java.io.DataOutputStream;
@@ -91,16 +89,21 @@
* deprecated by BTreeFiler.
* @version CVS $Revision$, $Date$
*/
-public final class HashFiler extends Paged implements Configurable, Filer {
+public final class HashFiler extends Paged implements Filer {
private static final Log log = LogFactory.getLog(HashFiler.class);
+ /**
+ * Record page status
+ */
protected static final byte RECORD = 1;
+ /**
+ * Name of the configuration attribute pagecount
+ */
private static final String PAGECOUNT = "pagecount";
- private static final String PAGESIZE = "pagesize";
- private static final String MAXKEYSIZE = "maxkeysize";
- private Configuration config;
+
+
private HashFileHeader fileHeader;
@@ -109,14 +112,6 @@
fileHeader = (HashFileHeader) getFileHeader();
}
- public void setConfig(Configuration config) {
- this.config = config;
- }
-
- public Configuration getConfig() {
- return config;
- }
-
public void setLocation(File root, String location) {
setFile(new File(root, location + ".tbl"));
}
@@ -125,20 +120,8 @@
return "HashFiler";
}
- public boolean open() throws DBException {
- if (super.open()) {
- // These are the only properties that can be changed after
creation
- fileHeader.setMaxKeySize(config.getShortAttribute(MAXKEYSIZE,
fileHeader.getMaxKeySize()));
- return true;
- } else {
- return false;
- }
- }
-
public boolean create() throws DBException {
- fileHeader.setPageCount(config.getLongAttribute(PAGECOUNT,
fileHeader.getPageCount()));
- fileHeader.setPageSize(config.getIntAttribute(PAGESIZE,
fileHeader.getPageSize()));
- fileHeader.setMaxKeySize(config.getShortAttribute(MAXKEYSIZE,
fileHeader.getMaxKeySize()));
+ fileHeader.setPageCount(getConfig().getLongAttribute(PAGECOUNT,
fileHeader.getPageCount()));
return super.create();
}
1.26 +132 -27
xml-xindice/java/src/org/apache/xindice/core/filer/Paged.java
Index: Paged.java
===================================================================
RCS file:
/home/cvs/xml-xindice/java/src/org/apache/xindice/core/filer/Paged.java,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -r1.25 -r1.26
--- Paged.java 7 Jan 2004 04:02:04 -0000 1.25
+++ Paged.java 2 Feb 2004 14:33:15 -0000 1.26
@@ -66,6 +66,8 @@
import org.apache.xindice.core.DBObject;
import org.apache.xindice.core.data.Key;
import org.apache.xindice.core.data.Value;
+import org.apache.xindice.util.Configurable;
+import org.apache.xindice.util.Configuration;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -86,13 +88,31 @@
import java.lang.ref.WeakReference;
/**
- * Paged is a paged file foundation that is used by both the BTree
- * class and the HashFiler. It provides flexible paged I/O and
+ * Paged is a paged file implementation that is foundation for both the
+ * BTree class and the HashFiler. It provides flexible paged I/O and
* page caching functionality.
*
+ * Page has folowing configuration attributes:
+ * <ul>
+ * <li><strong>pagesize</strong>: Size of the page used by the paged file.
+ * Default page size is 4096 bytes. This parameter can be set only
+ * before paged file is created. Once it is created, this parameter
+ * can not be changed.</li>
+ * <li><strong>maxkeysize</strong>: Maximum allowed size of the key.
+ * Default maximum key size is 256 bytes.</li>
+ * <li><strong>max-descriptors</strong>: Defines maximum amount of
+ * simultaneously opened file descriptors this paged file can have.
+ * Several descriptors are needed to provide multithreaded access
+ * to the underlying file. Too large number will limit amount of
+ * collections you can open. Default value is 16
+ * ([EMAIL PROTECTED] #DEFAULT_DESCRIPTORS_MAX}).</li>
+ * </ul>
+ *
+ * FIXME: Currently it seems that maxkeysize is not used anywhere.
+ *
* @version CVS $Revision$, $Date$
*/
-public abstract class Paged implements DBObject {
+public abstract class Paged implements DBObject, Configurable {
private static final Log log = LogFactory.getLog(Paged.class);
@@ -103,18 +123,65 @@
private static final int MAX_DIRTY_SIZE = 128;
/**
- * The maximum number of open random access files we can have
+ * Name of the configuration attribute "pagesize"
+ */
+ private static final String CONFIG_PAGESIZE = "pagesize";
+
+ /**
+ * Name of the configuration attribute "maxkeysize"
+ */
+ private static final String CONFIG_KEYSIZE_MAX = "maxkeysize";
+
+ /**
+ * Name of the configuration attribute "max-descriptors"
+ */
+ private static final String CONFIG_DESCRIPTORS_MAX = "max-descriptors";
+
+ /**
+ * Default value of the "pagesize".
+ */
+ private static final int DEFAULT_PAGESIZE = 4096;
+
+ /**
+ * Default value of the "maxkeysize".
+ */
+ private static final int DEFAULT_KEYSIZE_MAX = 256;
+
+ /**
+ * Default value of the maximum number of open random access files paged
+ * can have. This number balances resources utilization and parallelism
of
+ * access to the paged file.
*/
- private static final int MAX_DESCRIPTORS = 16;
+ private static final int DEFAULT_DESCRIPTORS_MAX = 16;
- // Page status
+
+ /**
+ * Unused page status
+ */
protected static final byte UNUSED = 0;
+
+ /**
+ * Overflow page status
+ */
protected static final byte OVERFLOW = 126;
+
+ /**
+ * Deleted page status
+ */
protected static final byte DELETED = 127;
- // Page ID of non-existent page
+ /**
+ * Page ID of non-existent page
+ */
protected static final int NO_PAGE = -1;
+
+ /**
+ * Configuration of this paged instance
+ */
+ private Configuration config;
+
+ // TODO: This is not a cache right now, but a way to assure that only
one page instance at most exists in memory at all times.
/**
* Cache of recently read pages.
*
@@ -125,14 +192,18 @@
/**
* Cache of modified pages waiting to be written out.
- * Access synchronized by the [EMAIL PROTECTED] #dirtyLock}.
+ * Access is synchronized by the [EMAIL PROTECTED] #dirtyLock}.
*/
private Map dirty = new HashMap();
+
+ /**
+ * Lock for synchronizing access to the [EMAIL PROTECTED] #dirty} map.
+ */
private final Object dirtyLock = new Object();
/**
* Random access file descriptors cache.
- * Access to it and to [EMAIL PROTECTED] #descCount} is synchronized by
itself.
+ * Access to it and to [EMAIL PROTECTED] #descriptorsCount} is
synchronized by itself.
*/
private final Stack descriptors = new Stack();
@@ -140,7 +211,13 @@
* The number of random access file objects that exist, either in the
* cache [EMAIL PROTECTED] #descriptors}, or currently in use.
*/
- private int descCount;
+ private int descriptorsCount;
+
+ /**
+ * The maximum number of random access file objects that can be opened
+ * by this paged instance.
+ */
+ private int descriptorsMax;
/**
* Whether the file is opened or not.
@@ -159,6 +236,7 @@
public Paged() {
+ descriptorsMax = DEFAULT_DESCRIPTORS_MAX;
fileHeader = createFileHeader();
}
@@ -167,6 +245,14 @@
setFile(file);
}
+ public void setConfig(Configuration config) {
+ this.config = config;
+ }
+
+ public Configuration getConfig() {
+ return config;
+ }
+
/**
* setFile sets the file object for this Paged.
*
@@ -199,8 +285,8 @@
// Otherwise we need to get one some other way.
else {
// First try to create a new one if there's room
- if (descCount < MAX_DESCRIPTORS) {
- descCount++;
+ if (descriptorsCount < descriptorsMax) {
+ descriptorsCount++;
return new RandomAccessFile(file, "rw");
}
// Otherwise we have to wait for one to be released by
another thread.
@@ -245,7 +331,7 @@
// Synchronization is necessary as decrement operation is not
atomic
synchronized (descriptors) {
- descCount --;
+ descriptorsCount --;
}
}
}
@@ -494,6 +580,9 @@
return p;
}
+ /**
+ * @throws DBException COL_COLLECTION_CLOSED if paged file is closed
+ */
protected final void checkOpened() throws DBException {
if (!opened) {
throw new FilerException(FaultCodes.COL_COLLECTION_CLOSED,
@@ -510,11 +599,19 @@
return fileHeader;
}
+ /**
+ * @return True if this paged file exists
+ */
public boolean exists() {
return file.exists();
}
public boolean create() throws DBException {
+ fileHeader.setPageSize(config.getIntAttribute(CONFIG_PAGESIZE,
+
fileHeader.getPageSize()));
+ fileHeader.setMaxKeySize(config.getShortAttribute(CONFIG_KEYSIZE_MAX,
+
fileHeader.getMaxKeySize()));
+
RandomAccessFile raf = null;
try {
raf = getDescriptor();
@@ -532,9 +629,18 @@
public synchronized boolean open() throws DBException {
RandomAccessFile raf = null;
try {
+ // Initialize configurable parameters
+ descriptorsMax =
getConfig().getIntAttribute(CONFIG_DESCRIPTORS_MAX,
+ descriptorsMax);
+
if (exists()) {
raf = getDescriptor();
fileHeader.read();
+
+ // These are the only properties that can be changed after
creation
+
fileHeader.setMaxKeySize(config.getShortAttribute(CONFIG_KEYSIZE_MAX,
+
fileHeader.getMaxKeySize()));
+
opened = true;
} else {
opened = false;
@@ -556,14 +662,14 @@
flush();
synchronized (descriptors) {
- final int total = descCount;
+ final int total = descriptorsCount;
// Close descriptors in cache
while (!descriptors.empty()) {
closeDescriptor((RandomAccessFile)descriptors.pop());
}
// Attempt to close descriptors in use. Max wait time =
0.5s * MAX_DESCRIPTORS
- int n = descCount;
- while (descCount > 0 && n > 0) {
+ int n = descriptorsCount;
+ while (descriptorsCount > 0 && n > 0) {
descriptors.wait(500);
if (descriptors.isEmpty()) {
n--;
@@ -571,8 +677,8 @@
closeDescriptor((RandomAccessFile)descriptors.pop());
}
}
- if (descCount > 0) {
- log.warn(descCount + " out of " + total + " files
were not closed during close.");
+ if (descriptorsCount > 0) {
+ log.warn(descriptorsCount + " out of " + total + "
files were not closed during close.");
}
}
} catch (Exception e) {
@@ -803,7 +909,7 @@
/**
- * FileHeader
+ * Paged file's header
*/
public abstract class FileHeader {
private boolean dirty = false;
@@ -816,7 +922,7 @@
private long firstFreePage = -1;
private long lastFreePage = -1;
private byte pageHeaderSize = 64;
- private short maxKeySize = 256;
+ private short maxKeySize = DEFAULT_KEYSIZE_MAX;
private long recordCount;
public FileHeader() {
@@ -824,7 +930,7 @@
}
public FileHeader(long pageCount) {
- this(pageCount, 4096);
+ this(pageCount, DEFAULT_PAGESIZE);
}
public FileHeader(long pageCount, int pageSize) {
@@ -1051,9 +1157,8 @@
}
/**
- * PageHeader
+ * Paged file page's header
*/
-
public abstract static class PageHeader implements Streamable {
private boolean dirty = false;
private byte status = UNUSED;
@@ -1179,7 +1284,7 @@
}
/**
- * Page
+ * Paged file's page
*/
public final class Page implements Comparable {
/**
@@ -1213,7 +1318,7 @@
private int dataPos;
- public Page(Long pageNum) throws IOException {
+ public Page(Long pageNum) {
this.header = createPageHeader();
this.pageNum = pageNum;
this.offset = fileHeader.headerSize + (pageNum.longValue() *
fileHeader.pageSize);
1.13 +3 -15
xml-xindice/java/src/org/apache/xindice/core/indexer/NameIndexer.java
Index: NameIndexer.java
===================================================================
RCS file:
/home/cvs/xml-xindice/java/src/org/apache/xindice/core/indexer/NameIndexer.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- NameIndexer.java 12 Aug 2003 02:57:30 -0000 1.12
+++ NameIndexer.java 2 Feb 2004 14:33:15 -0000 1.13
@@ -91,10 +91,7 @@
private static final String NAME = "name";
private static final String PATTERN = "pattern";
- private static final String PAGESIZE = "pagesize";
- private static final String MAXKEYSIZE = "maxkeysize";
- private Configuration config;
private Collection collection;
private SymbolTable symbols;
@@ -102,34 +99,25 @@
private String pattern;
private boolean wildcard = false;
- private FileHeader fileHeader;
public NameIndexer() {
super();
- fileHeader = getFileHeader();
}
public void setConfig(Configuration config) {
- this.config = config;
+ super.setConfig(config);
try {
name = config.getAttribute(NAME);
pattern = config.getAttribute(PATTERN);
wildcard = pattern.indexOf('*') != -1;
- fileHeader.setPageSize(config.getIntAttribute(PAGESIZE,
fileHeader.getPageSize()));
- fileHeader.setMaxKeySize(config.getShortAttribute(MAXKEYSIZE,
fileHeader.getMaxKeySize()));
-
setLocation(name);
} catch (Exception e) {
if (log.isWarnEnabled()) {
log.warn("ignored exception", e);
}
}
- }
-
- public Configuration getConfig() {
- return config;
}
public String getName() {
1.17 +5 -16
xml-xindice/java/src/org/apache/xindice/core/indexer/ValueIndexer.java
Index: ValueIndexer.java
===================================================================
RCS file:
/home/cvs/xml-xindice/java/src/org/apache/xindice/core/indexer/ValueIndexer.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- ValueIndexer.java 7 Jan 2004 04:03:50 -0000 1.16
+++ ValueIndexer.java 2 Feb 2004 14:33:15 -0000 1.17
@@ -97,8 +97,6 @@
private static final String NAME = "name";
private static final String PATTERN = "pattern";
private static final String TYPE = "type";
- private static final String PAGESIZE = "pagesize";
- private static final String MAXKEYSIZE = "maxkeysize";
private static final int STRING = 0;
private static final int TRIMMED = 1;
@@ -121,7 +119,6 @@
private static final String CHAR_VAL = "char";
private static final String BOOLEAN_VAL = "boolean";
- private Configuration config;
private Collection collection;
private SymbolTable symbols;
@@ -131,16 +128,15 @@
private int typeSize = 32;
private boolean wildcard = false;
- private FileHeader fileHeader;
public ValueIndexer() {
super();
- fileHeader = getFileHeader();
- fileHeader.setPageSize(1024);
+ // Set default
+ getFileHeader().setPageSize(1024);
}
public void setConfig(Configuration config) {
- this.config = config;
+ super.setConfig(config);
try {
name = config.getAttribute(NAME);
@@ -179,19 +175,12 @@
typeSize = sizes[type];
- fileHeader.setPageSize(config.getIntAttribute(PAGESIZE,
fileHeader.getPageSize()));
- fileHeader.setMaxKeySize(config.getShortAttribute(MAXKEYSIZE,
fileHeader.getMaxKeySize()));
-
setLocation(name);
} catch (Exception e) {
if (log.isWarnEnabled()) {
log.warn("ignored exception", e);
}
}
- }
-
- public Configuration getConfig() {
- return config;
}
public String getName() {