Repository navigation
Repo
To create a repository, use a special RepoBuilder class
You must specify key and value types. For example we will store this class:
@Data
class User {
private final String name;
private final int age;
}In that case key has String type, value has User type.
Attention! If you not set schema with method RepoBuilder#schema. To create a repository, first of all (before set schema) you must set the repository folder (as File) or Plugin and folder name!
To get an instance of the RepoBuilder class, you can use one of three static methods of class Repo:
File file = ...;
var repoBuilder = Repo.<String, User>builder(file);The file will become a folder in which the repository data will be stored
Plugin plugin = ...;
String foldename = ...;
var repoBuilder = Repo.<String, User>builder(plugin, foldername);The same as in the first one, only the file will be received by name and relative to the plugin folder
Empty (is not recommended, because you will still have to specify the folder/plugin and the name in a more cumbersome way)
var repoBuilder = Repo.<String, User>builder();You must specify schema (it is responsible for how the repository will save data to a permanent memory)
You can do this using threemethods (choose one)
Repo.builder()
.serializer(CommonSerializer)You should provide a class that implements CommonSerializer. Your data will be stored in a file. (Each object is in a separate file).
see CommonSerializer.java serializer
Check examples:
MainPlugin.java
Purchase.java
PurchaseSerializer.java
Repo.builder()
.schema(SchemaStrategy)You can create your own implementation of the SchemaStrategy interface, for example, if you want to store data in a MySQL database.
Check examples:
MainPlugin.java
User.java
UserSchema.java
Repo.builder()
.binary()Although this method requires the least effort, but it is very unstable, so it is not recommended to use!
To use it, you just need to implements Serializable and add this field to your data class:
private static final long serialVersionUID = 6529685098267757690L;
Check example:
MainPlugin.java
You can enable logging or set your logger for repo:
RepoBuilder#logging() - enables logging
RepoBuilder#debugLogging() - enables logging (all messages are sent as debug)
RepoBuilder#logging(Logger) - set your logger and enables logging
RepoBuilder#debugLogging(Logger) - set your logger and enables logging (all messages are sent as debug)
RepoBuilder#autoSave(long) - enables autosave and set autosave interval (in millis)
RepoBuilder#sync - wraps all methods as synchronized
RepoBuilder#cacheCollectionAll - enables the cache of the return value of the Repo#all() method
RepoBuilder#concurrent - uses ConcurrentHashMap for cache
RepoBuilder#maxCacheSize - set max cache size. If the count of elements is exceeded, some of the data in the cache will be cleared
If you have set all the required parameters, use RepoBuilder#build
Now you have an object of type MutableRepo!
see Repo.java
Collection all - all elements in repo
Optional get(K) - get element by key from repo
Collection find(Predicate<V>) - find elements by predicate in repo
Optional findAny(Predicate<V>) - find one lement by predicate in repo
see MutableRepo.java
extends Repo
set(K, V); - save element into repo
remove(K) - remove element from repo
saveAll - save all elements in cache to permament memory
see CacheRepo.java
extends MutableRepo
You can cast return value from RepoBuilder#build to this type.
loadToCache(K) - load from permament memoryto cache by key
removeFromCache(K) - remove from cache by key
Implementation of immutable repo. Takes data from a file in YAML markup. Requires implementing ParseStrategy#fromYaml(FileConfiguration) to get the parse from the file.
For example:
Repo<String, List<Integer>> capacity = new YamlRepo<>(
new File(getDataFolder(), "capacity.yml"),
this,
conf -> conf.getKeys(false).stream().collect(Collectors.toMap(
value -> value,
conf::getIntegerList))
);see RepoBuilderKt