Skip to content
Denis Istratov edited this page Jul 30, 2023 · 12 revisions

RepoBuilder, create repo

To create a repository, use a special RepoBuilder class

Preparation

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.

Getting RepoBuilder instance

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:

Getting by File

File file = ...;
var repoBuilder = Repo.<String, User>builder(file);

The file will become a folder in which the repository data will be stored

Getting by Plugin and folder name

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();

Configuring RepoBuilder

Schema (required)

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)

Serializer

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

Schema

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.

see SchemaStrategy.java

Check examples:
MainPlugin.java
User.java
UserSchema.java

Binary (not recomended)

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

Optional RepoBuilder configuration

Logging

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)

Autosave

RepoBuilder#autoSave(long) - enables autosave and set autosave interval (in millis)

Sync

RepoBuilder#sync - wraps all methods as synchronized

cacheCollectionAll

RepoBuilder#cacheCollectionAll - enables the cache of the return value of the Repo#all() method

Concurrent

RepoBuilder#concurrent - uses ConcurrentHashMap for cache

MaxCacheSize

RepoBuilder#maxCacheSize - set max cache size. If the count of elements is exceeded, some of the data in the cache will be cleared

Build repo

If you have set all the required parameters, use RepoBuilder#build

Now you have an object of type MutableRepo!

Repo (description of methods)

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

MutableRepo (description of methods)

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

CacheRepo (description of methods)

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

YamlRepo

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))
);

Kotlin

see RepoBuilderKt