Skip to content

Commit

Permalink
Rename JoinedPageArray to SpilloverPageArray.
Browse files Browse the repository at this point in the history
  • Loading branch information
broneill committed Dec 12, 2024
1 parent 9fefe87 commit 5767e87
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,68 +29,66 @@
import org.cojen.tupl.util.Runner;

/**
* Joins {@link PageArray PageArrays} together in a sequential fashion. This is useful for
* Combines {@link PageArray PageArrays} together in a sequential fashion. This is useful for
* supporting overflow capacity when the first array fills up. In an emergency when all drives
* are full, a joined page array can be defined such that the second array is a remote file, or
* a {@link OpenOption#NON_DURABLE non-durable} file, or an anonymous {@link MappedPageArray}.
* With this configuration in place, delete non-essential entries and then {@link
* org.cojen.tupl.Database#compactFile compact} the database. After verifying that space is
* available again, the original page array configuration can be swapped back in.
* are full, a spillover page array can be defined such that the second array is a remote file,
* or a {@link OpenOption#NON_DURABLE non-durable} file, or an anonymous {@link
* MappedPageArray}. With this configuration in place, delete non-essential entries and then
* {@link org.cojen.tupl.Database#compactFile compact} the database. After verifying that space
* is available again, the original page array configuration can be swapped back in.
*
* @author Brian S O'Neill
*/
public class JoinedPageArray extends PageArray {
public class SpilloverPageArray extends PageArray {
/**
* @param first source for all pages lower than the join index
* @param joinIndex join index which separates the two page sources
* @param second source for all pages at or higher than the join index
* @throws IllegalArgumentException if page sizes don't match or if join index isn't
* @param first source for all pages lower than the spillover index
* @param spilloverIndex index which separates the two sources
* @param second source for all pages at or higher than the spillover index
* @throws IllegalArgumentException if page sizes don't match or if spillover index isn't
* greater than 0
* @throws IllegalStateException if the highest index of the first source is higher than the
* join index
* @throws IllegalStateException if the highest index of the first source is higher than
* the spillover index
*/
public static Supplier<PageArray> factory(Supplier<? extends PageArray> first,
long joinIndex,
long spilloverIndex,
Supplier<? extends PageArray> second)
{
return (CheckedSupplier<PageArray>) () -> {
return join(first.get(), joinIndex, second.get());
};
return (CheckedSupplier<PageArray>) () -> make(first.get(), spilloverIndex, second.get());
}

/**
* @param first source for all pages lower than the join index
* @param joinIndex join index which separates the two page sources
* @param second source for all pages at or higher than the join index
* @throws IllegalArgumentException if page sizes don't match or if join index isn't
* @param first source for all pages lower than the spillover index
* @param spilloverIndex index which separates the two sources
* @param second source for all pages at or higher than the spillover index
* @throws IllegalArgumentException if page sizes don't match or if spillover index isn't
* greater than 0
* @throws IllegalStateException if the highest index of the first source is higher than the
* join index
* @throws IllegalStateException if the highest index of the first source is higher than
* the spillover index
*/
public static PageArray join(PageArray first, long joinIndex, PageArray second)
public static PageArray make(PageArray first, long spilloverIndex, PageArray second)
throws IOException
{
if (first.pageSize() != second.pageSize() || joinIndex <= 0) {
if (first.pageSize() != second.pageSize() || spilloverIndex <= 0) {
throw new IllegalArgumentException();
}
long pageCount = first.pageCount();
if (pageCount > joinIndex) {
if (pageCount > spilloverIndex) {
throw new IllegalStateException
("First page array is too large: " + pageCount + " > " + joinIndex);
("First page array is too large: " + pageCount + " > " + spilloverIndex);
}
return new JoinedPageArray(first, joinIndex, second);
return new SpilloverPageArray(first, spilloverIndex, second);
}

private final PageArray mFirst, mSecond;
private final long mJoinIndex;
private final long mSpilloverIndex;
private final int mDirectPageSize;
private final boolean mReadOnly;

private JoinedPageArray(PageArray first, long joinIndex, PageArray second) {
private SpilloverPageArray(PageArray first, long spilloverIndex, PageArray second) {
super(first.pageSize());
mFirst = first;
mSecond = second;
mJoinIndex = joinIndex;
mSpilloverIndex = spilloverIndex;

int directPageSize = first.directPageSize();
if (second.directPageSize() != directPageSize) {
Expand Down Expand Up @@ -124,12 +122,12 @@ public boolean isEmpty() throws IOException {

@Override
public long pageCount() throws IOException {
return mJoinIndex + mSecond.pageCount();
return mSpilloverIndex + mSecond.pageCount();
}

@Override
public void truncatePageCount(long count) throws IOException {
long diff = count - mJoinIndex;
long diff = count - mSpilloverIndex;
if (diff > 0) {
mSecond.truncatePageCount(diff);
} else {
Expand All @@ -140,7 +138,7 @@ public void truncatePageCount(long count) throws IOException {

@Override
public void expandPageCount(long count) throws IOException {
long diff = count - mJoinIndex;
long diff = count - mSpilloverIndex;
if (diff > 0) {
mSecond.expandPageCount(diff);
} else {
Expand All @@ -151,10 +149,10 @@ public void expandPageCount(long count) throws IOException {
@Override
public long pageCountLimit() throws IOException {
long limit = mFirst.pageCountLimit();
if (limit < 0 || limit >= mJoinIndex) {
if (limit < 0 || limit >= mSpilloverIndex) {
limit = mSecond.pageCountLimit();
if (limit >= 0) {
limit += mJoinIndex;
limit += mSpilloverIndex;
}
}
return limit;
Expand All @@ -173,43 +171,43 @@ public void writePage(long index, long srcAddr, int offset) throws IOException {
@Override
public long evictPage(long index, long bufAddr) throws IOException {
PageArray pa;
if (index < mJoinIndex) {
if (index < mSpilloverIndex) {
pa = mFirst;
} else {
pa = mSecond;
index -= mJoinIndex;
index -= mSpilloverIndex;
}
return pa.evictPage(index, bufAddr);
}

@Override
public long directPageAddress(long index) throws IOException {
PageArray pa;
if (index < mJoinIndex) {
if (index < mSpilloverIndex) {
pa = mFirst;
} else {
pa = mSecond;
index -= mJoinIndex;
index -= mSpilloverIndex;
}
return pa.directPageAddress(index);
}

@Override
public long copyPage(long srcIndex, long dstIndex) throws IOException {
PageArray src;
if (srcIndex < mJoinIndex) {
if (srcIndex < mSpilloverIndex) {
src = mFirst;
} else {
src = mSecond;
srcIndex -= mJoinIndex;
srcIndex -= mSpilloverIndex;
}

PageArray dst;
if (dstIndex < mJoinIndex) {
if (dstIndex < mSpilloverIndex) {
dst = mFirst;
} else {
dst = mSecond;
dstIndex -= mJoinIndex;
dstIndex -= mSpilloverIndex;
}

if (src == dst) {
Expand All @@ -222,11 +220,11 @@ public long copyPage(long srcIndex, long dstIndex) throws IOException {
@Override
public long copyPageFromAddress(long srcAddr, long dstIndex) throws IOException {
PageArray pa;
if (dstIndex < mJoinIndex) {
if (dstIndex < mSpilloverIndex) {
pa = mFirst;
} else {
pa = mSecond;
dstIndex -= mJoinIndex;
dstIndex -= mSpilloverIndex;
}
return pa.copyPageFromAddress(srcAddr, dstIndex);
}
Expand Down Expand Up @@ -272,11 +270,11 @@ public void sync(boolean metadata) throws IOException {
@Override
public void syncPage(long index) throws IOException {
PageArray pa;
if (index < mJoinIndex) {
if (index < mSpilloverIndex) {
pa = mFirst;
} else {
pa = mSecond;
index -= mJoinIndex;
index -= mSpilloverIndex;
}
pa.syncPage(index);
}
Expand All @@ -296,11 +294,11 @@ private static interface Task {

private void action(long index, Task task) throws IOException {
PageArray pa;
if (index < mJoinIndex) {
if (index < mSpilloverIndex) {
pa = mFirst;
} else {
pa = mSecond;
index -= mJoinIndex;
index -= mSpilloverIndex;
}
task.perform(pa, index);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import org.cojen.tupl.DatabaseConfig;

import org.cojen.tupl.io.FilePageArray;
import org.cojen.tupl.io.JoinedPageArray;
import org.cojen.tupl.io.OpenOption;
import org.cojen.tupl.io.PageArray;
import org.cojen.tupl.io.SpilloverPageArray;

import static org.cojen.tupl.TestUtils.*;

Expand All @@ -39,15 +39,15 @@
*
* @author Brian S O'Neill
*/
public class CrudJoinedFileTest extends CrudTest {
public class CrudSpilloverFileTest extends CrudTest {
public static void main(String[] args) throws Exception {
org.junit.runner.JUnitCore.main(CrudJoinedFileTest.class.getName());
org.junit.runner.JUnitCore.main(CrudSpilloverFileTest.class.getName());
}

@Before
@Override
public void createTempDb() throws Exception {
Supplier<PageArray> factory = JoinedPageArray
Supplier<PageArray> factory = SpilloverPageArray
.factory(newPageArrayFactory(), 10, newPageArrayFactory());
var config = new DatabaseConfig().dataPageArray(factory);
mDb = newTempDatabase(getClass(), config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@

import org.cojen.tupl.DatabaseConfig;

import org.cojen.tupl.io.JoinedPageArray;
import org.cojen.tupl.io.MappedPageArray;
import org.cojen.tupl.io.OpenOption;
import org.cojen.tupl.io.PageArray;
import org.cojen.tupl.io.SpilloverPageArray;

import static org.cojen.tupl.TestUtils.*;

Expand All @@ -39,9 +39,9 @@
*
* @author Brian S. O'Neill
*/
public class CrudJoinedMappedFileTest extends CrudTest {
public class CrudSpilloverMappedFileTest extends CrudTest {
public static void main(String[] args) throws Exception {
org.junit.runner.JUnitCore.main(CrudJoinedMappedFileTest.class.getName());
org.junit.runner.JUnitCore.main(CrudSpilloverMappedFileTest.class.getName());
}

@Before
Expand All @@ -57,7 +57,7 @@ public void createTempDb() throws Exception {
var secondFile = new File(newTempBaseFile(getClass()).getPath() + ".db");
var second = MappedPageArray.factory(pageSize, 100000, secondFile, options);

Supplier<PageArray> factory = JoinedPageArray.factory(first, joinIndex, second);
Supplier<PageArray> factory = SpilloverPageArray.factory(first, joinIndex, second);
var config = new DatabaseConfig().dataPageArray(factory);
mDb = newTempDatabase(getClass(), config);
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/cojen/tupl/io/RestrictedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ public void accessChecks() throws Exception {
mm.var(FilePageArray.class).invoke("factory", 4096, fileVar, null))
.invoke("get");

mm = cm.addMethod(null, "JoinedPageArray").public_().static_();
mm = cm.addMethod(null, "SpilloverPageArray").public_().static_();
fileVar = mm.new_(File.class, "x");
mm.var(JoinedPageArray.class)
mm.var(SpilloverPageArray.class)
.invoke("factory",
mm.var(FilePageArray.class).invoke("factory", 4096, fileVar, null),
4096,
Expand All @@ -118,7 +118,7 @@ public void accessChecks() throws Exception {
invokeAndVerify(clazz, "MappedPageArray_1");
invokeAndVerify(clazz, "MappedPageArray_2");
invokeAndVerify(clazz, "StripedPageArray");
invokeAndVerify(clazz, "JoinedPageArray");
invokeAndVerify(clazz, "SpilloverPageArray");
invokeAndVerify(clazz, "zlib_1");
invokeAndVerify(clazz, "zlib_2");
invokeAndVerify(clazz, "lz4");
Expand Down

0 comments on commit 5767e87

Please sign in to comment.