2626import org .eclipse .serializer .persistence .types .Storer ;
2727import org .eclipse .serializer .reference .ObjectSwizzling ;
2828import org .eclipse .store .storage .embedded .types .EmbeddedStorageFoundation ;
29+ import org .eclipse .store .storage .embedded .types .EmbeddedStorageManager ;
2930import org .eclipse .store .storage .types .StorageManager ;
3031import org .slf4j .Logger ;
3132import org .slf4j .LoggerFactory ;
3233
3334import software .xdev .spring .data .eclipse .store .core .EntityListProvider ;
3435import software .xdev .spring .data .eclipse .store .core .EntityProvider ;
3536import software .xdev .spring .data .eclipse .store .exceptions .AlreadyRegisteredException ;
37+ import software .xdev .spring .data .eclipse .store .exceptions .InvalidRootException ;
3638import software .xdev .spring .data .eclipse .store .repository .config .EclipseStoreClientConfiguration ;
3739import software .xdev .spring .data .eclipse .store .repository .config .EclipseStoreStorageFoundationProvider ;
3840import software .xdev .spring .data .eclipse .store .repository .root .EntityData ;
39- import software .xdev .spring .data .eclipse .store .repository .root .Root ;
41+ import software .xdev .spring .data .eclipse .store .repository .root .VersionedRoot ;
4042import software .xdev .spring .data .eclipse .store .repository .support .SimpleEclipseStoreRepository ;
4143import software .xdev .spring .data .eclipse .store .repository .support .concurrency .ReadWriteLock ;
4244import software .xdev .spring .data .eclipse .store .repository .support .concurrency .ReentrantJavaReadWriteLock ;
@@ -68,8 +70,8 @@ public class EclipseStoreStorage
6870 private final EclipseStoreStorageFoundationProvider foundationProvider ;
6971 private EntitySetCollector entitySetCollector ;
7072 private PersistableChecker persistenceChecker ;
71- private StorageManager storageManager ;
72- private Root root ;
73+ private EmbeddedStorageManager storageManager ;
74+ private VersionedRoot root ;
7375
7476 private final WorkingCopyRegistry registry = new WorkingCopyRegistry ();
7577 private final ReadWriteLock readWriteLock = new ReentrantJavaReadWriteLock ();
@@ -95,23 +97,55 @@ private synchronized void ensureEntitiesInRoot()
9597 {
9698 if (this .storageManager == null )
9799 {
98- LOG .info ("Starting storage..." );
99- this .root = new Root ();
100100 final EmbeddedStorageFoundation <?> embeddedStorageFoundation =
101- this .foundationProvider .createEmbeddedStorageFoundation ();
102- embeddedStorageFoundation .registerTypeHandler (BinaryHandlerImmutableCollectionsSet12 .New ());
103- embeddedStorageFoundation .registerTypeHandler (BinaryHandlerImmutableCollectionsList12 .New ());
104- this .storageManager = embeddedStorageFoundation .start (this .root );
101+ this .startStorageManager ();
105102 this .persistenceChecker = new RelayedPersistenceChecker (embeddedStorageFoundation );
106103 this .initRoot ();
107104 LOG .info (
108105 "Storage started with {} entity lists and {} entities." ,
109- this .root .getEntityTypesCount (),
110- this .root .getEntityCount ()
106+ this .root .getCurrentRootData (). getEntityTypesCount (),
107+ this .root .getCurrentRootData (). getEntityCount ()
111108 );
109+ EclipseStoreMigrator .migrate (this .root , this .storageManager );
112110 }
113111 }
114112
113+ private EmbeddedStorageFoundation <?> startStorageManager ()
114+ {
115+ LOG .info ("Starting storage..." );
116+ final EmbeddedStorageFoundation <?> embeddedStorageFoundation =
117+ this .foundationProvider .createEmbeddedStorageFoundation ();
118+ embeddedStorageFoundation .registerTypeHandler (BinaryHandlerImmutableCollectionsSet12 .New ());
119+ embeddedStorageFoundation .registerTypeHandler (BinaryHandlerImmutableCollectionsList12 .New ());
120+ final EmbeddedStorageManager embeddedStorageManager = embeddedStorageFoundation .start ();
121+ if (embeddedStorageManager .root () != null )
122+ {
123+ if (embeddedStorageManager .root () instanceof final Root oldRoot )
124+ {
125+ embeddedStorageManager .setRoot (new VersionedRoot (oldRoot ));
126+ embeddedStorageManager .storeRoot ();
127+ }
128+ else if (!(embeddedStorageManager .root () instanceof VersionedRoot ))
129+ {
130+ throw new InvalidRootException (
131+ "Root object of type %s is invalid."
132+ .formatted (embeddedStorageManager .root ()
133+ .getClass ()
134+ .getName ()
135+ )
136+ );
137+ }
138+ }
139+ else
140+ {
141+ embeddedStorageManager .setRoot (new VersionedRoot ());
142+ embeddedStorageManager .storeRoot ();
143+ }
144+ this .root = (VersionedRoot )embeddedStorageManager .root ();
145+ this .storageManager = embeddedStorageManager ;
146+ return embeddedStorageFoundation ;
147+ }
148+
115149 public <T > SimpleEclipseStoreRepository <T , ?> getRepository (final Class <T > entityClass )
116150 {
117151 return (SimpleEclipseStoreRepository <T , ?>)this .entityClassToRepository .get (entityClass );
@@ -124,22 +158,24 @@ private void initRoot()
124158 LOG .debug ("Initializing entity lists..." );
125159 }
126160 this .repositorySynchronizer =
127- new SimpleRepositorySynchronizer (this .root );
161+ new SimpleRepositorySynchronizer (this .root . getCurrentRootData () );
128162 boolean entityListMustGetStored = false ;
129163 for (final Class <?> entityClass : this .entityClassToRepository .keySet ())
130164 {
131- if (this .root .getEntityData (entityClass ) == null )
165+ if (this .root .getCurrentRootData (). getEntityData (entityClass ) == null )
132166 {
133167 this .createNewEntityList (entityClass );
134168 entityListMustGetStored = true ;
135169 }
136170 }
137171 if (entityListMustGetStored )
138172 {
139- this .storageManager .store (this .root .getEntityLists ());
173+ this .storageManager .store (this .root .getCurrentRootData (). getEntityLists ());
140174 }
141175 this .entitySetCollector =
142- new EntitySetCollector (this .root ::getEntityData , this .entityClassToRepository .keySet ());
176+ new EntitySetCollector (
177+ this .root .getCurrentRootData ()::getEntityData ,
178+ this .entityClassToRepository .keySet ());
143179 if (LOG .isDebugEnabled ())
144180 {
145181 LOG .debug ("Done initializing entity lists." );
@@ -149,7 +185,7 @@ private void initRoot()
149185 private <T , ID > void createNewEntityList (final Class <T > entityClass )
150186 {
151187 final IdManager <T , ID > idManager = this .ensureIdManager (entityClass );
152- this .root .createNewEntityList (entityClass , e -> idManager .getId (e ));
188+ this .root .getCurrentRootData (). createNewEntityList (entityClass , e -> idManager .getId (e ));
153189 }
154190
155191 public synchronized <T > void registerEntity (
@@ -174,7 +210,7 @@ private <T, ID> EntityData<T, ID> getEntityData(final Class<T> clazz)
174210 {
175211 this .ensureEntitiesInRoot ();
176212 return this .readWriteLock .read (
177- () -> this .root .getEntityData (clazz )
213+ () -> this .root .getCurrentRootData (). getEntityData (clazz )
178214 );
179215 }
180216
@@ -193,7 +229,7 @@ public <T> long getEntityCount(final Class<T> clazz)
193229 return this .readWriteLock .read (
194230 () ->
195231 {
196- final EntityData <T , Object > entityData = this .root .getEntityData (clazz );
232+ final EntityData <T , Object > entityData = this .root .getCurrentRootData (). getEntityData (clazz );
197233 return entityData == null ? 0 : entityData .getEntityCount ();
198234 }
199235 );
@@ -286,7 +322,7 @@ public void clearData()
286322 this .readWriteLock .write (
287323 () ->
288324 {
289- this .root = new Root ();
325+ this .root = new VersionedRoot ();
290326 final StorageManager instanceOfstorageManager = this .getInstanceOfStorageManager ();
291327 this .initRoot ();
292328
@@ -370,22 +406,22 @@ public <T> VersionManager<T> ensureVersionManager(final Class<T> possiblyVersion
370406
371407 public Object getLastId (final Class <?> entityClass )
372408 {
373- return this .readWriteLock .read (() -> this .root .getLastId (entityClass ));
409+ return this .readWriteLock .read (() -> this .root .getCurrentRootData (). getLastId (entityClass ));
374410 }
375411
376412 public void setLastId (final Class <?> entityClass , final Object lastId )
377413 {
378414 this .readWriteLock .write (
379415 () ->
380416 {
381- final EntityData <?, Object > entityData = this .root .getEntityData (entityClass );
417+ final EntityData <?, Object > entityData = this .root .getCurrentRootData (). getEntityData (entityClass );
382418 if (entityData == null )
383419 {
384420 this .createNewEntityList (entityClass );
385- this .storageManager .store (this .root .getEntityLists ());
421+ this .storageManager .store (this .root .getCurrentRootData (). getEntityLists ());
386422 }
387- this .root .setLastId (entityClass , lastId );
388- this .storageManager .store (this .root .getObjectsToStoreAfterNewLastId (entityClass ));
423+ this .root .getCurrentRootData (). setLastId (entityClass , lastId );
424+ this .storageManager .store (this .root .getCurrentRootData (). getObjectsToStoreAfterNewLastId (entityClass ));
389425 }
390426 );
391427 }
0 commit comments