dejvokep / boosted-yaml

A simple-to-use standalone Java library delivering boosted experience while working with YAML documents.
https://dejvokep.gitbook.io/boostedyaml/
Apache License 2.0
82 stars 8 forks source link

Document Versioning not working #28

Closed sznees closed 5 months ago

sznees commented 5 months ago

Hello there,

I'm completely new to this project and I'm trying it at the moment. I followed some instructions I found online and I can't get the BasicVersioning working. I guess I'm doing something wrong, but tbh I don't know what.

This is my test code:

    public static void main(String[] args) throws IOException {
        YamlDocument config = YamlDocument.create(new File("config.yml"), getResource("config.yml"),
                GeneralSettings.builder().setKeyFormat(GeneralSettings.KeyFormat.OBJECT).build(), LoaderSettings.builder().setAutoUpdate(true).build(), DumperSettings.DEFAULT,
                UpdaterSettings.builder().setVersioning(new BasicVersioning("config-version")).build());

        config.update();
        config.save();

        int ver = config.getInt("count");
        if (ver % 2 == 0) {
            config.set("books", 50);
        } else {
            config.set("books", 20);
        }
        config.set("count", ++ver);
        config.update();
        config.save();
    }

    public static @Nullable InputStream getResource(@NotNull String filename) {
        try {
            URL url = BoostedYamlTest.class.getClassLoader().getResource(filename);
            if (url == null) {
                return null;
            } else {
                URLConnection connection = url.openConnection();
                connection.setUseCaches(false);
                return connection.getInputStream();
            }
        } catch (IOException ex) {
            return null;
        }
    }

This is my default config that gets compiled with the Jar:

config-version: 1 count: 0 books: 19

Somehow the "config-version" just keeps on the value, that is already set.

dejvokep commented 5 months ago

Hi,

as updating a document makes sense to execute only when the default document has changed (the updater modifies the current document to comply with the structure of the default document - an update would be necessary exclusively if new options were added, some were removed, or the document was modified in any other way), to make it as effective as possible, versionings were introduced.

A versioning handles document revision identification via document version IDs adhering to a specific pattern. BasicVersioning defines a versioning pattern with the first ID being "1" and all following incremented by 1 (per ID). When you make a change in the document, you should change the ID inside the default document (that gets shipped with your software) to the next ID ("1" -> "2"). It doesn't necessarily have to change with every single modification - you can change it once for every release of your software (just like its version).

When calling the update() method (or with auto-update enabled), the versioning ensures the current document is out of date by comparing its ID with the version ID provided by the default document. If it is (e.g. "2" < "3" by the definition of BasicVersioning), it will update the document.

Updating a document involves copying over new options and removing the ones which are no longer needed (you can alter this behavior and ignore routes, define custom mappers... via the settings). After that's done, the updater will also update the document version ID to the one of the defaults to indicate it was updated.

To see it in action, try to create a document with ID "1" and then try to initialize a BoostedYAML document with defaults having some new option and ID "2". After calling update(), you should see the new option in the document and the ID changed to "2".

I hope I explained it clearly and will modify the wiki accordingly. Should you have any questions, please feel free to ask.

Thanks, have a great day. ~ dejvokep

sznees commented 5 months ago

Thank you so much!

I tried just like you said, it works perfectly fine and makes sense to me now. It is a smart way for production, i really like that feature!

Have a nice weekend!

dejvokep commented 5 months ago

You're welcome. Thank you and you too have a nice weekend.