23
23
import jakarta .annotation .Nonnull ;
24
24
import jakarta .annotation .Nullable ;
25
25
import java .sql .SQLException ;
26
+ import java .sql .Statement ;
26
27
import java .util .ArrayList ;
27
28
import java .util .Collections ;
28
29
import java .util .HashMap ;
@@ -86,44 +87,14 @@ public void writeEntity(
86
87
@ Nonnull PolarisBaseEntity entity ,
87
88
boolean nameOrParentChanged ,
88
89
PolarisBaseEntity originalEntity ) {
89
- ModelEntity modelEntity = ModelEntity .fromEntity (entity );
90
- String query ;
91
- if (originalEntity == null ) {
92
- try {
93
- query = generateInsertQuery (modelEntity , realmId );
94
- datasourceOperations .executeUpdate (query );
95
- } catch (SQLException e ) {
96
- if ((datasourceOperations .isConstraintViolation (e )
97
- || datasourceOperations .isAlreadyExistsException (e ))) {
98
- throw new EntityAlreadyExistsException (entity , e );
99
- } else {
100
- throw new RuntimeException (
101
- String .format ("Failed to write entity due to %s" , e .getMessage ()), e );
102
- }
103
- }
104
- } else {
105
- Map <String , Object > params =
106
- Map .of (
107
- "id" ,
108
- originalEntity .getId (),
109
- "catalog_id" ,
110
- originalEntity .getCatalogId (),
111
- "entity_version" ,
112
- originalEntity .getEntityVersion (),
113
- "realm_id" ,
114
- realmId );
115
- query = generateUpdateQuery (modelEntity , params );
116
- try {
117
- int rowsUpdated = datasourceOperations .executeUpdate (query );
118
- if (rowsUpdated == 0 ) {
119
- throw new RetryOnConcurrencyException (
120
- "Entity '%s' id '%s' concurrently modified; expected version %s" ,
121
- entity .getName (), entity .getId (), originalEntity .getEntityVersion ());
122
- }
123
- } catch (SQLException e ) {
124
- throw new RuntimeException (
125
- String .format ("Failed to write entity due to %s" , e .getMessage ()), e );
126
- }
90
+ try {
91
+ datasourceOperations .runWithinTransaction (
92
+ statement -> {
93
+ persistEntity (callCtx , entity , originalEntity , statement );
94
+ return true ;
95
+ });
96
+ } catch (SQLException e ) {
97
+ throw new RuntimeException ("Error persisting entity" , e );
127
98
}
128
99
}
129
100
@@ -137,70 +108,21 @@ public void writeEntities(
137
108
statement -> {
138
109
for (int i = 0 ; i < entities .size (); i ++) {
139
110
PolarisBaseEntity entity = entities .get (i );
140
- ModelEntity modelEntity = ModelEntity .fromEntity (entity );
111
+ PolarisBaseEntity originalEntity =
112
+ originalEntities != null ? originalEntities .get (i ) : null ;
141
113
142
114
// first, check if the entity has already been created, in which case we will simply
143
115
// return it.
144
116
PolarisBaseEntity entityFound =
145
117
lookupEntity (
146
118
callCtx , entity .getCatalogId (), entity .getId (), entity .getTypeCode ());
147
- if (entityFound != null ) {
119
+ if (entityFound != null && originalEntity == null ) {
148
120
// probably the client retried, simply return it
149
121
// TODO: Check correctness of returning entityFound vs entity here. It may have
150
122
// already been updated after the creation.
151
123
continue ;
152
124
}
153
- // lookup by name
154
- EntityNameLookupRecord exists =
155
- lookupEntityIdAndSubTypeByName (
156
- callCtx ,
157
- entity .getCatalogId (),
158
- entity .getParentId (),
159
- entity .getTypeCode (),
160
- entity .getName ());
161
- if (exists != null ) {
162
- throw new EntityAlreadyExistsException (entity );
163
- }
164
- String query ;
165
- if (originalEntities == null || originalEntities .get (i ) == null ) {
166
- try {
167
- query = generateInsertQuery (modelEntity , realmId );
168
- statement .executeUpdate (query );
169
- } catch (SQLException e ) {
170
- if ((datasourceOperations .isConstraintViolation (e )
171
- || datasourceOperations .isAlreadyExistsException (e ))) {
172
- throw new EntityAlreadyExistsException (entity , e );
173
- } else {
174
- throw new RuntimeException (
175
- String .format ("Failed to write entity due to %s" , e .getMessage ()), e );
176
- }
177
- }
178
- } else {
179
- Map <String , Object > params =
180
- Map .of (
181
- "id" ,
182
- originalEntities .get (i ).getId (),
183
- "catalog_id" ,
184
- originalEntities .get (i ).getCatalogId (),
185
- "entity_version" ,
186
- originalEntities .get (i ).getEntityVersion (),
187
- "realm_id" ,
188
- realmId );
189
- query = generateUpdateQuery (modelEntity , params );
190
- try {
191
- int rowsUpdated = statement .executeUpdate (query );
192
- if (rowsUpdated == 0 ) {
193
- throw new RetryOnConcurrencyException (
194
- "Entity '%s' id '%s' concurrently modified; expected version %s" ,
195
- entity .getName (),
196
- entity .getId (),
197
- originalEntities .get (i ).getEntityVersion ());
198
- }
199
- } catch (SQLException e ) {
200
- throw new RuntimeException (
201
- String .format ("Failed to write entity due to %s" , e .getMessage ()), e );
202
- }
203
- }
125
+ persistEntity (callCtx , entity , originalEntity , statement );
204
126
}
205
127
return true ;
206
128
});
@@ -212,6 +134,56 @@ public void writeEntities(
212
134
}
213
135
}
214
136
137
+ private void persistEntity (
138
+ @ Nonnull PolarisCallContext callCtx ,
139
+ @ Nonnull PolarisBaseEntity entity ,
140
+ PolarisBaseEntity originalEntity ,
141
+ Statement statement )
142
+ throws SQLException {
143
+ ModelEntity modelEntity = ModelEntity .fromEntity (entity );
144
+ if (originalEntity == null ) {
145
+ try {
146
+ statement .executeUpdate (generateInsertQuery (modelEntity , realmId ));
147
+ } catch (SQLException e ) {
148
+ if (datasourceOperations .isConstraintViolation (e )) {
149
+ PolarisBaseEntity existingEntity =
150
+ lookupEntityByName (
151
+ callCtx ,
152
+ entity .getCatalogId (),
153
+ entity .getParentId (),
154
+ entity .getTypeCode (),
155
+ entity .getName ());
156
+ throw new EntityAlreadyExistsException (existingEntity , e );
157
+ } else {
158
+ throw new RuntimeException (
159
+ String .format ("Failed to write entity due to %s" , e .getMessage ()), e );
160
+ }
161
+ }
162
+ } else {
163
+ Map <String , Object > params =
164
+ Map .of (
165
+ "id" ,
166
+ originalEntity .getId (),
167
+ "catalog_id" ,
168
+ originalEntity .getCatalogId (),
169
+ "entity_version" ,
170
+ originalEntity .getEntityVersion (),
171
+ "realm_id" ,
172
+ realmId );
173
+ try {
174
+ int rowsUpdated = statement .executeUpdate (generateUpdateQuery (modelEntity , params ));
175
+ if (rowsUpdated == 0 ) {
176
+ throw new RetryOnConcurrencyException (
177
+ "Entity '%s' id '%s' concurrently modified; expected version %s" ,
178
+ originalEntity .getName (), originalEntity .getId (), originalEntity .getEntityVersion ());
179
+ }
180
+ } catch (SQLException e ) {
181
+ throw new RuntimeException (
182
+ String .format ("Failed to write entity due to %s" , e .getMessage ()), e );
183
+ }
184
+ }
185
+ }
186
+
215
187
@ Override
216
188
public void writeToGrantRecords (
217
189
@ Nonnull PolarisCallContext callCtx , @ Nonnull PolarisGrantRecord grantRec ) {
@@ -492,6 +464,8 @@ public PolarisGrantRecord lookupGrantRecord(
492
464
throw new IllegalStateException (
493
465
String .format (
494
466
"More than one grant record %s for a given Grant record" , results .getFirst ()));
467
+ } else if (results .isEmpty ()) {
468
+ return null ;
495
469
}
496
470
return results .getFirst ();
497
471
} catch (SQLException e ) {
0 commit comments