0 votes
asked in MicroStream for Java by (160 points)

Hi, 

Sorry, this might be a very stupid mistake on my behalf but I can't even get basic persistence to work with Spring. 

I have a very simple example: 

@Slf4j
@Component
public class RootRepositoryImpl implements RootRepository {

  private final EmbeddedStorageManager storageManager;

  public RootRepositoryImpl() {
    this.storageManager = EmbeddedStorage.start();

    if (storageManager.root() == null) storageManager.setRoot(new Root());
  }

  @Override
  public long update() {
    return this.storageManager.storeRoot();
  }

  @Override
  public Root find() {
    return (Root) this.storageManager.root();
  }
}

And in another class, I modify the root object 

  @PostMapping("/delete-small-files")
  public Mono<Long> setDeleteSmallFiles(@RequestBody boolean value) {
    Root root = rootRepository.find();
    root.getEnabledProperties().setDeleteSmallFiles(value);

    return Mono.just(rootRepository.update());
  }

The first time I execute this, it updates and saves the changed value. However, running it again after restarting and the old value persists. 

I'm using versions:

	implementation 'one.microstream:storage.embedded:04.00.00-MS-GA'
	implementation 'one.microstream:storage.embedded.configuration:04.00.00-MS-GA'

Where have I gone wrong here? 

1 Answer

+1 vote
answered by (9.7k points)
selected by
 
Best answer

Hello,

To get it working you need to consider some rules regarding the storage (see below)

This is what happens in your case:

  1. Run:
    A new and empty storage is created.
    “storage.storeRoot()” saves your root-object and all its referenced objects because they had never been stored before.
     
  2. Run:
    An existing storage is found and loaded; you get the root-Object and all referenced objects you persisted.
    The call “.setDeleteSmallFiles(value)” seems to modify a Child-Object of the root.
    The “.storeRoot()” now only persists the rootObject but not its children because the children have already been persisted.

    You need to store whatever the “setDeleteSmallFiles(value)” modified!

e.g.:

if you add a new item to a list:

myRoot.myList.add(new Item())
storage.store(myRoot.myList);

if you modify an already stored item:

myRoot.myList.get(0).setInt(10)
storage.store(myRoot.myList.get(0));

 

General storing rules:

  1. New Objects:
    Store the Object(s) that reference to the new Object
    All referenced objects that had not been persisted before will be saved too (rule 4)
     
  2. Modified Objects:
    Store the Object itself
     
  3. Immutable Objects:
    e.g. Strings
    Immutable objects cannot be modified only replaced with a new instance. Therefore rule 1 applies to them.
     
  4. Referenced instances will be saved only if they had not been persisted before.
    To store them again it must be done explicitly (rule 2).

See: https://manual.docs.microstream.one/data-store/storing-data

best regards

Notes: Every question must be a separate forum post. Headline: Formulate your question shortly and precisely. Thank you!
Powered by Question2Answer
...