forked from Funwayguy/BetterQuesting
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Abstract database * Newly created `questID` will be random * Quest importers try to use the old quest id * Use `RandomIndexDatabase.nextID()` * Copy quest id button * Remove quest id from chat * Close popup on copy * Require edit mode and advanced info to view quest id --------- Co-authored-by: KatatsumuriPan <[email protected]>
- Loading branch information
1 parent
c31d4d8
commit 5409ade
Showing
16 changed files
with
216 additions
and
149 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
127 changes: 127 additions & 0 deletions
127
src/main/java/betterquesting/api2/storage/AbstractDatabase.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,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 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
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
22 changes: 22 additions & 0 deletions
22
src/main/java/betterquesting/api2/storage/RandomIndexDatabase.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,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.