@@ -272,53 +272,8 @@ public void savePolicy(Model model) {
272272
273273 try (Statement statement = conn .createStatement (); PreparedStatement ps = conn .prepareStatement (addSql )) {
274274 statement .execute (cleanSql );
275- for (Map .Entry <String , Assertion > entry : model .model .get ("p" ).entrySet ()) {
276- String ptype = entry .getKey ();
277- Assertion ast = entry .getValue ();
278-
279- for (List <String > rule : ast .policy ) {
280- CasbinRule line = savePolicyLine (ptype , rule );
281-
282- ps .setString (1 , line .ptype );
283- ps .setString (2 , line .v0 );
284- ps .setString (3 , line .v1 );
285- ps .setString (4 , line .v2 );
286- ps .setString (5 , line .v3 );
287- ps .setString (6 , line .v4 );
288- ps .setString (7 , line .v5 );
289-
290- ps .addBatch ();
291- if (++count == batchSize ) {
292- count = 0 ;
293- ps .executeBatch ();
294- ps .clearBatch ();
295- }
296- }
297- }
298-
299- for (Map .Entry <String , Assertion > entry : model .model .get ("g" ).entrySet ()) {
300- String ptype = entry .getKey ();
301- Assertion ast = entry .getValue ();
302-
303- for (List <String > rule : ast .policy ) {
304- CasbinRule line = savePolicyLine (ptype , rule );
305-
306- ps .setString (1 , line .ptype );
307- ps .setString (2 , line .v0 );
308- ps .setString (3 , line .v1 );
309- ps .setString (4 , line .v2 );
310- ps .setString (5 , line .v3 );
311- ps .setString (6 , line .v4 );
312- ps .setString (7 , line .v5 );
313-
314- ps .addBatch ();
315- if (++count == batchSize ) {
316- count = 0 ;
317- ps .executeBatch ();
318- ps .clearBatch ();
319- }
320- }
321- }
275+ count = saveSectionPolicyWithBatch (model , "p" , ps , count );
276+ count = saveSectionPolicyWithBatch (model , "g" , ps , count );
322277
323278 if (count != 0 ) {
324279 ps .executeBatch ();
@@ -336,6 +291,39 @@ public void savePolicy(Model model) {
336291 });
337292 }
338293
294+ /**
295+ * saveSectionPolicyWithBatch saves section policy rules to the storage.
296+ * as a helper function for savePolicy
297+ * only when the batchCount exceeds the batchSize will the actual save operation be performed
298+ */
299+ private int saveSectionPolicyWithBatch (Model model , String section , PreparedStatement ps , int batchCount ) throws SQLException {
300+ if (!model .model .containsKey (section )) return batchCount ;
301+ for (Map .Entry <String , Assertion > entry : model .model .get (section ).entrySet ()) {
302+ String ptype = entry .getKey ();
303+ Assertion ast = entry .getValue ();
304+
305+ for (List <String > rule : ast .policy ) {
306+ CasbinRule line = savePolicyLine (ptype , rule );
307+
308+ ps .setString (1 , line .ptype );
309+ ps .setString (2 , line .v0 );
310+ ps .setString (3 , line .v1 );
311+ ps .setString (4 , line .v2 );
312+ ps .setString (5 , line .v3 );
313+ ps .setString (6 , line .v4 );
314+ ps .setString (7 , line .v5 );
315+
316+ ps .addBatch ();
317+ if (++batchCount == batchSize ) {
318+ batchCount = 0 ;
319+ ps .executeBatch ();
320+ ps .clearBatch ();
321+ }
322+ }
323+ }
324+ return batchCount ;
325+ }
326+
339327 /**
340328 * addPolicy adds a policy rule to the storage.
341329 */
0 commit comments