1515 */
1616package software .xdev .spring .data .eclipse .store .repository .support ;
1717
18- import java .lang .reflect .Field ;
1918import java .util .ArrayList ;
2019import java .util .Collection ;
2120import java .util .List ;
3433import org .springframework .data .domain .Sort ;
3534import org .springframework .data .repository .query .FluentQuery ;
3635
37- import software .xdev .spring .data .eclipse .store .exceptions .FieldAccessReflectionException ;
38- import software .xdev .spring .data .eclipse .store .exceptions .NoIdFieldFoundException ;
3936import software .xdev .spring .data .eclipse .store .repository .EclipseStoreStorage ;
40- import software .xdev .spring .data .eclipse .store .repository .access .modifier .FieldAccessModifier ;
4137import software .xdev .spring .data .eclipse .store .repository .interfaces .EclipseStoreCrudRepository ;
4238import software .xdev .spring .data .eclipse .store .repository .interfaces .EclipseStoreListCrudRepository ;
4339import software .xdev .spring .data .eclipse .store .repository .interfaces .EclipseStoreListPagingAndSortingRepositoryRepository ;
5248import software .xdev .spring .data .eclipse .store .repository .query .executors .ListQueryExecutor ;
5349import software .xdev .spring .data .eclipse .store .repository .query .executors .PageableQueryExecutor ;
5450import software .xdev .spring .data .eclipse .store .repository .query .executors .SingleOptionalQueryExecutor ;
51+ import software .xdev .spring .data .eclipse .store .repository .support .copier .id .IdManager ;
5552import software .xdev .spring .data .eclipse .store .repository .support .copier .working .WorkingCopier ;
5653import software .xdev .spring .data .eclipse .store .repository .support .copier .working .WorkingCopierResult ;
5754import software .xdev .spring .data .eclipse .store .transactions .EclipseStoreTransaction ;
@@ -72,37 +69,24 @@ public class SimpleEclipseStoreRepository<T, ID>
7269 private final Class <T > domainClass ;
7370 private final WorkingCopier <T > copier ;
7471 private final EclipseStoreTransactionManager transactionManager ;
75- private Field idField ;
72+ private final IdManager < T , ID > idManager ;
7673
7774 public SimpleEclipseStoreRepository (
7875 final EclipseStoreStorage storage ,
7976 final WorkingCopier <T > copier ,
8077 final Class <T > domainClass ,
81- final EclipseStoreTransactionManager transactionManager )
78+ final EclipseStoreTransactionManager transactionManager ,
79+ final IdManager <T , ID > idManager
80+ )
8281 {
8382 this .storage = storage ;
8483 this .domainClass = domainClass ;
84+ this .idManager = idManager ;
8585 this .storage .registerEntity (domainClass , this );
8686 this .copier = copier ;
8787 this .transactionManager = transactionManager ;
8888 }
8989
90- public Field getIdField ()
91- {
92- if (this .idField == null )
93- {
94- final Optional <Field > foundIdField = IdFieldFinder .findIdField (this .domainClass );
95- if (foundIdField .isEmpty ())
96- {
97- throw new NoIdFieldFoundException (String .format (
98- "Could not find id field in class %s" ,
99- this .domainClass .getSimpleName ()));
100- }
101- this .idField = foundIdField .get ();
102- }
103- return this .idField ;
104- }
105-
10690 @ SuppressWarnings ("unchecked" )
10791 public <S extends T > List <S > saveBulk (final Collection <S > entities )
10892 {
@@ -180,39 +164,17 @@ public <S extends T> List<S> saveAll(@Nonnull final Iterable<S> entities)
180164 public Optional <T > findById (@ Nonnull final ID id )
181165 {
182166 return this .storage .getReadWriteLock ().read (
183- () -> this .storage
184- .getEntityList (this .domainClass )
185- .parallelStream ()
186- .filter (
187- entity ->
188- {
189- try (final FieldAccessModifier <T > fam = FieldAccessModifier .prepareForField (
190- this .getIdField (),
191- entity ))
192- {
193- if (id .equals (fam .getValueOfField (entity )))
194- {
195- return true ;
196- }
197- }
198- catch (final Exception e )
199- {
200- throw new FieldAccessReflectionException (String .format (
201- FieldAccessReflectionException .COULD_NOT_READ_FIELD ,
202- this .getIdField ().getName ()), e );
203- }
204- return false ;
205- }
206- )
207- .findAny ()
167+ () -> this .idManager .findById (id )
208168 .map (foundEntity -> this .copier .copy (foundEntity ))
209169 );
210170 }
211171
212172 @ Override
213173 public boolean existsById (@ Nonnull final ID id )
214174 {
215- return this .findById (id ).isPresent ();
175+ return this .storage .getReadWriteLock ().read (
176+ () -> this .idManager .findById (id ).isPresent ()
177+ );
216178 }
217179
218180 @ Override
@@ -233,37 +195,7 @@ public List<T> findAllById(@Nonnull final Iterable<ID> idsToFind)
233195 // Must get copied as one list to keep same references objects the same.
234196 // (Example: If o1 and o2 (both part of the entity list) are referencing o3,
235197 // o3 should be the same no matter from where it is referenced.
236- () -> this .copier .copy (
237- this .storage
238- .getEntityList (this .domainClass )
239- .parallelStream ()
240- .filter (
241- entity ->
242- {
243- try (final FieldAccessModifier <T > fam = FieldAccessModifier .prepareForField (
244- this .getIdField (),
245- entity ))
246- {
247- final Object idOfEntity = fam .getValueOfField (entity );
248- for (final ID idToFind : idsToFind )
249- {
250- if (idToFind .equals (idOfEntity ))
251- {
252- return true ;
253- }
254- }
255- }
256- catch (final Exception e )
257- {
258- throw new FieldAccessReflectionException (String .format (
259- FieldAccessReflectionException .COULD_NOT_READ_FIELD ,
260- this .getIdField ().getName ()), e );
261- }
262- return false ;
263- }
264- )
265- .toList ()
266- )
198+ () -> this .copier .copy (this .idManager .findAllById (idsToFind ))
267199 );
268200 }
269201
@@ -276,11 +208,18 @@ public long count()
276208 @ Override
277209 public void deleteById (@ Nonnull final ID id )
278210 {
279- this .storage .getReadWriteLock ().write (
280- () -> {
281- final Optional <T > byId = this .findById (id );
282- byId .ifPresent (this ::delete );
283- }
211+ final EclipseStoreTransaction transaction = this .transactionManager .getTransaction ();
212+ transaction .addAction (() ->
213+ this .storage .getReadWriteLock ().write (
214+ () -> {
215+ this
216+ .idManager
217+ .findById (id )
218+ .ifPresent (
219+ foundEntity -> this .storage .delete (this .domainClass , foundEntity )
220+ );
221+ }
222+ )
284223 );
285224 }
286225
0 commit comments