-
-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Patrick Reinhart <patrick@reini.net>
- Loading branch information
Showing
9 changed files
with
267 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
exist-core/src/main/java/org/exist/util/io/ContentFilePool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* eXist-db Open Source Native XML Database | ||
* Copyright (C) 2001 The eXist-db Authors | ||
* | ||
* info@exist-db.org | ||
* http://www.exist-db.org | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
package org.exist.util.io; | ||
|
||
import org.apache.commons.pool2.impl.GenericObjectPool; | ||
import org.apache.commons.pool2.impl.GenericObjectPoolConfig; | ||
import org.exist.util.Configuration; | ||
|
||
/** | ||
* @author <a href="mailto:patrick@reini.net">Patrick Reinhart</a> | ||
*/ | ||
public final class ContentFilePool extends GenericObjectPool<ContentFile> { | ||
public static final String PROPERTY_POOL_SIZE = "content-file.pool.size"; | ||
public static final String PROPERTY_IN_MEMORY_SIZE = "content-file.in-memory-size"; | ||
|
||
public ContentFilePool(final TemporaryFileManager tempFileManager, final Configuration config, final int maxIdle) { | ||
super(new ContentFilePoolObjectFactory(tempFileManager, toInMemorySize(config)), toPoolConfig(config, maxIdle)); | ||
} | ||
|
||
private static int toInMemorySize(Configuration config) { | ||
return config.getInteger(PROPERTY_IN_MEMORY_SIZE, VirtualTempPath.DEFAULT_IN_MEMORY_SIZE); | ||
} | ||
|
||
private static GenericObjectPoolConfig<ContentFile> toPoolConfig(final Configuration config, final int maxIdle) { | ||
final GenericObjectPoolConfig<ContentFile> poolConfig = new GenericObjectPoolConfig<>(); | ||
poolConfig.setBlockWhenExhausted(false); | ||
poolConfig.setLifo(true); | ||
poolConfig.setMaxIdle(maxIdle); | ||
poolConfig.setMaxTotal(config.getInteger(PROPERTY_POOL_SIZE)); | ||
poolConfig.setJmxNameBase("org.exist.management.exist:type=VirtualTempPathPool"); | ||
return poolConfig; | ||
} | ||
|
||
@Override | ||
public ContentFile borrowObject() { | ||
try { | ||
return super.borrowObject(); | ||
} catch (Exception e) { | ||
throw new IllegalStateException("Error while borrowing ContentFile", e); | ||
} | ||
} | ||
|
||
@Override | ||
public void returnObject(ContentFile obj) { | ||
if (obj == null) { | ||
return; | ||
} | ||
try { | ||
obj.close(); | ||
super.returnObject(obj); | ||
} catch (Exception e) { | ||
throw new IllegalStateException("Error while returning ContentFile", e); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
exist-core/src/main/java/org/exist/util/io/ContentFilePoolObjectFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* eXist-db Open Source Native XML Database | ||
* Copyright (C) 2001 The eXist-db Authors | ||
* | ||
* info@exist-db.org | ||
* http://www.exist-db.org | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
package org.exist.util.io; | ||
|
||
import org.apache.commons.pool2.BasePooledObjectFactory; | ||
import org.apache.commons.pool2.PooledObject; | ||
import org.apache.commons.pool2.impl.DefaultPooledObject; | ||
|
||
/** | ||
* @author <a href="mailto:patrick@reini.net">Patrick Reinhart</a> | ||
*/ | ||
public final class ContentFilePoolObjectFactory extends BasePooledObjectFactory<ContentFile> { | ||
private final TemporaryFileManager tempFileManager; | ||
private final int inMemorySize; | ||
|
||
public ContentFilePoolObjectFactory(final TemporaryFileManager tempFileManager, int inMemorySize) { | ||
this.tempFileManager = tempFileManager; | ||
this.inMemorySize = inMemorySize; | ||
} | ||
|
||
@Override | ||
public ContentFile create() throws Exception { | ||
return new VirtualTempPath(inMemorySize, tempFileManager); | ||
} | ||
|
||
@Override | ||
public PooledObject<ContentFile> wrap(ContentFile obj) { | ||
return new DefaultPooledObject<>(obj); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.