Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SequentialDataBase as a better SimpleDatabase #129

Open
wants to merge 5 commits into
base: 1.12
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/main/java/betterquesting/api2/storage/SequentialDataBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package betterquesting.api2.storage;

import java.util.BitSet;

/**
* a database implementation specialized for ids that're sequential, dense and not randomly generated
* <p>
* see {@link RandomIndexDatabase} for database specialized in handling randomly generated ids
*/
public class SequentialDataBase<T> extends AbstractDatabase<T> {

private final BitSet ids = new BitSet();
/**
* the "smallest acceptable" id, any id smaller than this is considered as used and can
* be skipped when searching for new id, this condition can be maintained by:
* - refresh lowerBound after new id is generated: {@link #nextID()}
* - refresh lowerBound after one id is removed: {@link #removeID(int)}
*/
private int lowerBound = 0;

@Override
public synchronized int nextID() {
int next = ids.nextClearBit(lowerBound);
lowerBound = next + 1;
return next;
}

@Override
public synchronized DBEntry<T> add(int id, T value) {
DBEntry<T> result = super.add(id, value);
// Don't add when an exception is thrown
ids.set(id);
// lowerBound = id; //no, lowerBound will not be refreshed here, delay to next `nextID()` call instead
return result;
}

@Override
public synchronized boolean removeID(int key) {
boolean result = super.removeID(key);
if (result) {
ids.clear(key);
lowerBound = Math.min(key, lowerBound);
}
return result;
}

@Override
public synchronized void reset() {
super.reset();
ids.clear();
}
}
38 changes: 4 additions & 34 deletions src/main/java/betterquesting/api2/storage/SimpleDatabase.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,8 @@
package betterquesting.api2.storage;

import java.util.BitSet;
import java.util.Collections;
import java.util.List;
import java.util.TreeMap;

public class SimpleDatabase<T> extends AbstractDatabase<T> {

private final BitSet idMap = new BitSet();

@Override
public synchronized int nextID() {
return idMap.nextClearBit(0);
}

@Override
public synchronized DBEntry<T> add(int id, T value) {
DBEntry<T> result = super.add(id, value);
// Don't add when an exception is thrown
idMap.set(id);
return result;
}

@Override
public synchronized boolean removeID(int key) {
boolean result = super.removeID(key);
if (result) idMap.clear(key);
return result;
}

@Override
public synchronized void reset() {
super.reset();
idMap.clear();
}
/**
* @see SequentialDataBase
*/
public class SimpleDatabase<T> extends SequentialDataBase<T> {

}