forked from Funwayguy/BetterQuesting
-
Notifications
You must be signed in to change notification settings - Fork 25
Random quest ID #110
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
Closed
KatatsumuriPan
wants to merge
4
commits into
CleanroomMC:1.12
from
KatatsumuriPan:1.12-random-quest-id
Closed
Random quest ID #110
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
src/main/java/betterquesting/api2/storage/AbstractDatabase.java
This file contains hidden or 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,127 @@ | ||
| package betterquesting.api2.storage; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.TreeMap; | ||
|
|
||
| public abstract class AbstractDatabase<T> implements IDatabase<T> { | ||
|
|
||
| /** | ||
| * If the cache size would somehow exceed 24MB (on 64bit machines) we stop. | ||
| */ | ||
| public static int CACHE_MAX_SIZE = 24 * 1024 * 1024 / 8; | ||
|
|
||
| /** | ||
| * If {@code mapDB.size < SPARSE_RATIO * (mapDB.lastKey() - mapDB.firstKey())} the database will be considered | ||
| * sparse and an cache array won't be built to save memory. | ||
| * <p> | ||
| * Under this sparsity a 10k element database will roughly result in a 0.5MB cache which is more than enough reasonable. | ||
| */ | ||
| public static double SPARSE_RATIO = 0.15d; | ||
|
|
||
| final TreeMap<Integer, T> mapDB = new TreeMap<>(); | ||
|
|
||
| private LookupLogicType type = null; | ||
| private LookupLogic<T> logic = null; | ||
|
|
||
| private LookupLogic<T> getLookupLogic() { | ||
| if (type != null) | ||
| return logic; | ||
| LookupLogicType newType = LookupLogicType.determine(this); | ||
| type = newType; | ||
| logic = newType.get(this); | ||
| return logic; | ||
| } | ||
|
|
||
| private void updateLookupLogic() { | ||
| if (type == null) | ||
| return; | ||
| LookupLogicType newType = LookupLogicType.determine(this); | ||
| if (newType != type) { | ||
| type = null; | ||
| logic = null; | ||
| } else { | ||
| logic.onDataChange(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized DBEntry<T> add(int id, T value) { | ||
| if (value == null) { | ||
| throw new NullPointerException("Value cannot be null"); | ||
| } else if (id < 0) { | ||
| throw new IllegalArgumentException("ID cannot be negative"); | ||
| } else { | ||
| if (mapDB.putIfAbsent(id, value) == null) { | ||
| updateLookupLogic(); | ||
| return new DBEntry<>(id, value); | ||
| } else { | ||
| throw new IllegalArgumentException("ID or value is already contained within database"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized boolean removeID(int key) { | ||
| if (key < 0) | ||
| return false; | ||
|
|
||
| if (mapDB.remove(key) != null) { | ||
| updateLookupLogic(); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized boolean removeValue(T value) { | ||
| return value != null && removeID(getID(value)); | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized int getID(T value) { | ||
| if (value == null) | ||
| return -1; | ||
|
|
||
| for (DBEntry<T> entry : getEntries()) { | ||
| if (entry.getValue() == value) | ||
| return entry.getID(); | ||
| } | ||
|
|
||
| return -1; | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized T getValue(int id) { | ||
| if (id < 0 || mapDB.size() <= 0) | ||
| return null; | ||
| return mapDB.get(id); | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized int size() { | ||
| return mapDB.size(); | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized void reset() { | ||
| mapDB.clear(); | ||
| type = null; | ||
| logic = null; | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized List<DBEntry<T>> getEntries() { | ||
| return mapDB.isEmpty() ? Collections.emptyList() : getLookupLogic().getRefCache(); | ||
| } | ||
|
|
||
| /** | ||
| * First try to use array cache. | ||
| * If memory usage would be too high try use sort merge join if keys is large. | ||
| * Otherwise look up each key separately via {@link TreeMap#get(Object)}. | ||
| */ | ||
| @Override | ||
| public synchronized List<DBEntry<T>> bulkLookup(int... keys) { | ||
| return mapDB.isEmpty() || keys.length == 0 ? Collections.emptyList() : getLookupLogic().bulkLookup(keys); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
22 changes: 22 additions & 0 deletions
22
src/main/java/betterquesting/api2/storage/RandomIndexDatabase.java
This file contains hidden or 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,22 @@ | ||
| package betterquesting.api2.storage; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| public class RandomIndexDatabase<T> extends AbstractDatabase<T> { | ||
|
|
||
| private final Random random = new Random(); | ||
|
|
||
| @Override | ||
| public synchronized int nextID() { | ||
| int id; | ||
| do { | ||
| // id >= 0 | ||
| id = random.nextInt() & 0x7fff_ffff; | ||
| } | ||
| // The new id doesn't conflict with existing ones. | ||
| // However, new ids created by different players could conflict with each other. | ||
| while (mapDB.containsKey(id)); | ||
| return id; | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.