Microstream (spring boot) 为什么总是加载第一个版本的数据?

huangapple go评论53阅读模式
英文:

Why microstream (spring boot) always loads the 1th version of data?

问题

以下是您要翻译的内容:

I'm trying to use microstream (spring boot). I wrote a test class as demo. If I start the demo first time, it creates the session object and stores it in embedded storage. The second time session object is loaded again and "numValue" attribute is incremented. But all following runs, the numValue of stream is always 0. What do I missing?

There is the demo class

package test.microstream;

import jakarta.annotation.PostConstruct;
import lombok.AllArgsConstructor;
import lombok Data;
import lombok ToString;
import lombok.extern.slf4j.Slf4j;
import one.microstream.integrations.spring.boot.types.Storage;
import one.microstream.storage.types.StorageManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot ApplicationArguments;
import org.springframework.boot ApplicationRunner;
import org.springframework.boot SpringApplication;
import org.springframework.boot.autoconfigure SpringBootApplication;
import org.springframework context.annotation Bean;

import java.util HashMap;
import java.util Map;

SpringBootApplication
@Slf4j
public class MicrostreamApplication {

    public static void main(String[] args) {
        SpringApplication.run(MicrostreamApplication.class, args);
    }

    @AllArgsConstructor
    @Data
    @ToString
    public static class DemoSession {
        String account;
        int numValue;
    }

    @Data
    @ToString
    @Storage
    public static class DemoStorage {
        Map<String, DemoSession> sessions;

        @PostConstruct
        public void init() {
            if(sessions == null) {
                sessions = new HashMap<>();
            }
        }

        public void setSession(DemoSession session) {
            sessions.put(session.getAccount(), session);
        }

        public DemoSession getSession(String account) {
            return sessions.get(account);
        }
    }

    @Autowired
    DemoStorage storage;

    @Autowired
    StorageManager storageManager;

    @Bean
    public ApplicationRunner runner() {
        return new ApplicationRunner() {
            @Override
            public void run(ApplicationArguments args) throws Exception {
                log.info("root {}", storageManager.root());
                log.info("storage {}", storage);
                var session = storage.getSession("acc");
                if(session is null) {
                    session = new DemoSession("acc", 0);
                    log.info("create session {}", session);
                    storage.setSession(session);
                    storageManager.storeRoot();
                } else {
                    log.info("loaded session {}", session);
                    session.setNumValue(session.getNumValue()+1);
                    log.info("update session {}", session);
                    storageManager.storeRoot();
                }
                log.info("after all");
                log.info("root {}", storageManager.root());
                log.info("storage {}", storage);
            }
        };
    }
}

There are outputs of the programm

2023-02-14T21:35:32.947+01:00 INFO 163817 --- [ main] one.microstream.util.logging.Logging : MicroStream Version 08.00.00-MS-EA2
2023-02-14T21:35:33.089+01:00 INFO 163817 --- [ main] .s.e.t.EmbeddedStorageFoundation$Default : Creating embedded storage manager
2023-02-14T21:35:33.206+01:00 INFO 163817 --- [ main] .t.PersistenceTypeHandlerManager$Default : Initializing type handler manager
2023-02-14T21:35:33.266+01:00 INFO 163817 --- [ main] o.m.s.e.t.EmbeddedStorageManager$Default : Starting embedded storage manager
2023-02-14T21:35:33.268+01:00 INFO 163817 --- [ main] o.m.storage.types.StorageSystem$Default : Starting storage system
2023-02-14T21:35:33.269+01:00 INFO 163817 --- [ main] .m.s.t.StorageStructureValidator$Default : Storage structure validated successfully.
2023-02-14T21:35:33.295+01:00 INFO 163817 --- [ main] .t.PersistenceTypeHandlerManager$Default : Initializing type handler manager
2023-02-14T21:35:33.308+01:00 INFO 163817 --- [ main] o.m.s.e.t.EmbeddedStorageManager$Default : Embedded storage manager initialized
2023-02-14T21:35:33.366+01:00 INFO 163817 --- [ main] t.microstream.MicrostreamApplication : Started MicrostreamApplication in 0.963 seconds (process running for 1.278)
2023-02-14T21:35:33.367+01:00 INFO 163817 --- [ main] t.microstream.MicrostreamApplication : root MicrostreamApplication.DemoStorage(sessions={acc=MicrostreamApplication.DemoSession(account=acc, numValue=0)})
2023-02-14T21:35:33.367+01:00 INFO 163817 --- [ main] t.microstream.MicrostreamApplication : storage MicrostreamApplication.DemoStorage(sessions={acc=MicrostreamApplication.DemoSession(account=acc, numValue=0)})
2023-02-14T21:35:33.367+01:00 INFO 163817 --- [ main] t.microstream.MicrostreamApplication : loaded session MicrostreamApplication.DemoSession(account=acc, numValue=0)
2023-02-14T21:35:33.367+01:00 INFO 163817 --- [ main] t.microstream.MicrostreamApplication : update session MicrostreamApplication.DemoSession(account=acc, numValue=1)
2023-02-14T21:35:33.367+01:00 INFO 163817 --- [ main] .t.PersistenceTypeHandlerManager$Default : Initializing type handler manager
2023-02-14T21:35:33.372+01:00 INFO 163817 --- [ main] t.microstream.MicrostreamApplication : after all
2023-02-14T21:35:33.372+01:00 INFO 163817 --- [ main] t.microstream.MicrostreamApplication : root MicrostreamApplication.DemoStorage(sessions={acc=MicrostreamApplication.DemoSession(account=acc, numValue=1)})
2023-02-14T21:35:33.372+01:00 INFO 163817 --- [ main] t.microstream.MicrostreamApplication : storage MicrostreamApplication.DemoStorage(sessions={acc=MicrostreamApplication.DemoSession(account=acc, numValue=1)})
2023-02-14T21:45:18.225+01:00 INFO 163817 --- [ionShutdownHook] o.m.storage.types.StorageSystem$Default : Stopping storage system
2023-02-14T21:45:18.227+01:00 INFO 163817 --- [ionShutdownHook] o.m.storage.types.StorageSystem$Default : Storage system stopped

There are dependencies from the file

<dependencies>
       

<details>
<summary>英文:</summary>

I&#39;m trying to use microstream (spring boot). I wrote a test class as demo. If I start the demo first time, it creates the session object and stores it in embedded storage. The second time session object is loaded again and &quot;numValue&quot; attribute is incremented. But all following runs, the numValue of stream is always 0. What do I missing? 

There is the demo class


package test.microstream;

import jakarta.annotation.PostConstruct;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import one.microstream.integrations.spring.boot.types.Storage;
import one.microstream.storage.types.StorageManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
@Slf4j
public class MicrostreamApplication {

public static void main(String[] args) {
SpringApplication.run(MicrostreamApplication.class, args);
}
@AllArgsConstructor
@Data
@ToString
public static class DemoSession {
String account;
int numValue;
}
@Data
@ToString
@Storage
public static class DemoStorage {
Map&lt;String, DemoSession&gt; sessions;
@PostConstruct
public void init() {
if(sessions == null) {
sessions = new HashMap&lt;&gt;();
}
}
public void setSession(DemoSession session) {
sessions.put(session.getAccount(), session);
}
public DemoSession getSession(String account) {
return sessions.get(account);
}
}
@Autowired
DemoStorage storage;
@Autowired
StorageManager storageManager;
@Bean
public ApplicationRunner runner() {
return new ApplicationRunner() {
@Override
public void run(ApplicationArguments args) throws Exception {
log.info(&quot;root {}&quot;, storageManager.root());
log.info(&quot;storage {}&quot;, storage);
var session = storage.getSession(&quot;acc&quot;);
if(session == null) {
session = new DemoSession(&quot;acc&quot;, 0);
log.info(&quot;create session {}&quot;, session);
storage.setSession(session);
storageManager.storeRoot();
} else {
log.info(&quot;loaded session {}&quot;, session);
session.setNumValue(session.getNumValue()+1);
log.info(&quot;update session {}&quot;, session);
storageManager.storeRoot();
}
log.info(&quot;after all&quot;);
log.info(&quot;root {}&quot;, storageManager.root());
log.info(&quot;storage {}&quot;, storage);
}
};
}

}


There are outputs of the programm

2023-02-14T21:35:32.947+01:00 INFO 163817 --- [ main] one.microstream.util.logging.Logging : MicroStream Version 08.00.00-MS-EA2
2023-02-14T21:35:33.089+01:00 INFO 163817 --- [ main] .s.e.t.EmbeddedStorageFoundation$Default : Creating embedded storage manager
2023-02-14T21:35:33.206+01:00 INFO 163817 --- [ main] .t.PersistenceTypeHandlerManager$Default : Initializing type handler manager
2023-02-14T21:35:33.266+01:00 INFO 163817 --- [ main] o.m.s.e.t.EmbeddedStorageManager$Default : Starting embedded storage manager
2023-02-14T21:35:33.268+01:00 INFO 163817 --- [ main] o.m.storage.types.StorageSystem$Default : Starting storage system
2023-02-14T21:35:33.269+01:00 INFO 163817 --- [ main] .m.s.t.StorageStructureValidator$Default : Storage structure validated successfully.
2023-02-14T21:35:33.295+01:00 INFO 163817 --- [ main] .t.PersistenceTypeHandlerManager$Default : Initializing type handler manager
2023-02-14T21:35:33.308+01:00 INFO 163817 --- [ main] o.m.s.e.t.EmbeddedStorageManager$Default : Embedded storage manager initialized
2023-02-14T21:35:33.366+01:00 INFO 163817 --- [ main] t.microstream.MicrostreamApplication : Started MicrostreamApplication in 0.963 seconds (process running for 1.278)
2023-02-14T21:35:33.367+01:00 INFO 163817 --- [ main] t.microstream.MicrostreamApplication : root MicrostreamApplication.DemoStorage(sessions={acc=MicrostreamApplication.DemoSession(account=acc, numValue=0)})
2023-02-14T21:35:33.367+01:00 INFO 163817 --- [ main] t.microstream.MicrostreamApplication : storage MicrostreamApplication.DemoStorage(sessions={acc=MicrostreamApplication.DemoSession(account=acc, numValue=0)})
2023-02-14T21:35:33.367+01:00 INFO 163817 --- [ main] t.microstream.MicrostreamApplication : loaded session MicrostreamApplication.DemoSession(account=acc, numValue=0)
2023-02-14T21:35:33.367+01:00 INFO 163817 --- [ main] t.microstream.MicrostreamApplication : update session MicrostreamApplication.DemoSession(account=acc, numValue=1)
2023-02-14T21:35:33.367+01:00 INFO 163817 --- [ main] .t.PersistenceTypeHandlerManager$Default : Initializing type handler manager
2023-02-14T21:35:33.372+01:00 INFO 163817 --- [ main] t.microstream.MicrostreamApplication : after all
2023-02-14T21:35:33.372+01:00 INFO 163817 --- [ main] t.microstream.MicrostreamApplication : root MicrostreamApplication.DemoStorage(sessions={acc=MicrostreamApplication.DemoSession(account=acc, numValue=1)})
2023-02-14T21:35:33.372+01:00 INFO 163817 --- [ main] t.microstream.MicrostreamApplication : storage MicrostreamApplication.DemoStorage(sessions={acc=MicrostreamApplication.DemoSession(account=acc, numValue=1)})
2023-02-14T21:45:18.225+01:00 INFO 163817 --- [ionShutdownHook] o.m.storage.types.StorageSystem$Default : Stopping storage system
2023-02-14T21:45:18.227+01:00 INFO 163817 --- [ionShutdownHook] o.m.storage.types.StorageSystem$Default : Storage system stopped


There are dependencies from the file

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

    &lt;dependency&gt;
&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
&lt;artifactId&gt;lombok&lt;/artifactId&gt;
&lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;one.microstream&lt;/groupId&gt;
&lt;artifactId&gt;microstream-storage-embedded&lt;/artifactId&gt;
&lt;version&gt;08.00.00-MS-EA2&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;one.microstream&lt;/groupId&gt;
&lt;artifactId&gt;microstream-integrations-spring-boot3&lt;/artifactId&gt;
&lt;version&gt;08.00.00-MS-EA2&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;one.microstream&lt;/groupId&gt;
&lt;artifactId&gt;microstream-storage-embedded-configuration&lt;/artifactId&gt;
&lt;version&gt;08.00.00-MS-EA2&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;

The file transaction*slt is growing every run. It looks like the new values are loaded, but every run the loaded root object is the same, in intial state. 
</details>
# 答案1
**得分**: 3
在更新会话对象后,您应该在更新后的会话对象上调用存储(store)方法,而不是再次存储根实例:
```java
} else {
log.info("加载会话 {}", session);
session.setNumValue(session.getNumValue() + 1);
log.info("更新会话 {}", session);
storageManager.store(session);
}

默认情况下,storeRoot() 方法不会更新已持久化的对象,这是因为默认的“懒惰”存储策略。

英文:

After updating the session object, you should call a store on the updated session object instead of storing the root instance again:

} else {
log.info(&quot;loaded session {}&quot;, session);
session.setNumValue(session.getNumValue()+1);
log.info(&quot;update session {}&quot;, session);
storageManager.store(session);
}

The storeRoot() method will not update already persisted objects by default due to the default “lazy” storing strategy.

huangapple
  • 本文由 发表于 2023年2月18日 07:08:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定