Skip to content

Commit bdc0205

Browse files
authored
HHH-19005 Reduce memory usage for JSON string literals in BasicFormatterImpl
1 parent 067a2e8 commit bdc0205

File tree

2 files changed

+48
-26
lines changed

2 files changed

+48
-26
lines changed

hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/BasicFormatterImpl.java

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -70,39 +70,26 @@ public String perform() {
7070
while ( tokens.hasMoreTokens() ) {
7171
token = tokens.nextToken();
7272

73-
if ( "-".equals(token) && result.toString().endsWith("-") ) {
73+
if ( "-".equals( token ) && result.toString().endsWith( "-" ) ) {
7474
do {
7575
result.append( token );
7676
token = tokens.nextToken();
7777
}
7878
while ( !"\n".equals( token ) && tokens.hasMoreTokens() );
7979
}
8080

81-
lcToken = token.toLowerCase(Locale.ROOT);
82-
switch (lcToken) {
83-
81+
lcToken = token.toLowerCase( Locale.ROOT );
82+
switch ( lcToken ) {
8483
case "'":
8584
case "`":
8685
case "\"":
87-
String t;
88-
do {
89-
t = tokens.nextToken();
90-
token += t;
91-
}
92-
while ( !lcToken.equals( t ) && tokens.hasMoreTokens() );
93-
lcToken = token;
86+
appendUntilToken(lcToken);
9487
misc();
9588
break;
9689
// SQL Server uses "[" and "]" to escape reserved words
9790
// see SQLServerDialect.openQuote and SQLServerDialect.closeQuote
9891
case "[":
99-
String tt;
100-
do {
101-
tt = tokens.nextToken();
102-
token += tt;
103-
}
104-
while ( !"]".equals( tt ) && tokens.hasMoreTokens() );
105-
lcToken = token;
92+
appendUntilToken("]");
10693
misc();
10794
break;
10895

@@ -238,7 +225,7 @@ private void from() {
238225
}
239226

240227
private void comma() {
241-
if ( afterByOrSetOrFromOrSelect && inFunction==0 ) {
228+
if ( afterByOrSetOrFromOrSelect && inFunction == 0 ) {
242229
commaAfterByOrFromOrSelect();
243230
}
244231
// else if ( afterOn && inFunction==0 ) {
@@ -313,7 +300,7 @@ private void beginCase() {
313300

314301
private void misc() {
315302
out();
316-
if ( afterInsert && inFunction==0 ) {
303+
if ( afterInsert && inFunction == 0 ) {
317304
newline();
318305
afterInsert = false;
319306
}
@@ -329,7 +316,7 @@ private void white() {
329316
}
330317

331318
private void updateOrInsertOrDelete() {
332-
if ( indent>1 ) {
319+
if ( indent > 1 ) {
333320
//probably just the insert SQL function
334321
out();
335322
}
@@ -367,7 +354,7 @@ private void select() {
367354

368355
private void out() {
369356
if ( result.charAt( result.length() - 1 ) == ',' ) {
370-
result.append(" ");
357+
result.append( " " );
371358
}
372359
result.append( token );
373360
}
@@ -390,8 +377,8 @@ private void endNewClause() {
390377
newline();
391378
afterBeginBeforeEnd = false;
392379
afterByOrSetOrFromOrSelect = "by".equals( lcToken )
393-
|| "set".equals( lcToken )
394-
|| "from".equals( lcToken );
380+
|| "set".equals( lcToken )
381+
|| "from".equals( lcToken );
395382
}
396383

397384
private void beginNewClause() {
@@ -507,9 +494,21 @@ private static boolean isWhitespace(String token) {
507494

508495
private void newline() {
509496
result.append( System.lineSeparator() )
510-
.append( INDENT_STRING.repeat(indent) );
497+
.append( INDENT_STRING.repeat( indent ) );
511498
beginLine = true;
512499
}
500+
501+
private void appendUntilToken(String stopToken) {
502+
final StringBuilder sb = new StringBuilder( this.token );
503+
String t;
504+
do {
505+
t = tokens.nextToken();
506+
sb.append( t );
507+
}
508+
while ( !stopToken.equals( t ) && tokens.hasMoreTokens() );
509+
this.token = sb.toString();
510+
lcToken = token;
511+
}
513512
}
514513

515514
}

hibernate-core/src/test/java/org/hibernate/orm/test/jdbc/util/BasicFormatterTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
* @author Steve Ebersole
2222
*/
2323
public class BasicFormatterTest extends BaseUnitTestCase {
24-
2524
@Test
2625
public void testNoLoss() {
2726
assertNoLoss( "insert into Address (city, state, zip, \"from\") values (?, ?, ?, 'insert value')" );
@@ -47,6 +46,30 @@ public void testNoLoss() {
4746
"(select p.pid from Address where city = 'Boston') union (select p.pid from Address where city = 'Taipei')"
4847
);
4948
assertNoLoss( "select group0.[order] as order0 from [Group] group0 where group0.[order]=?1" );
49+
assertNoLoss( """
50+
INSERT INTO TEST_TABLE (JSON) VALUES
51+
('{
52+
"apiVersion": "2.0",
53+
"data": {
54+
"updated": "2010-01-07T19:58:42.949Z",
55+
"totalItems": 800,
56+
"startIndex": 1,
57+
"itemsPerPage": 1,
58+
"items": [
59+
{
60+
"id": "hYB0mn5zh2c",
61+
"uploaded": "2007-06-05T22:07:03.000Z",
62+
"updated": "2010-01-07T13:26:50.000Z",
63+
"uploader": "GoogleDeveloperDay",
64+
"category": "News",
65+
"title": "Google Developers Day US - Maps API Introduction",
66+
"description": "Google Maps API Introduction ..."
67+
}
68+
]
69+
}
70+
}')
71+
"""
72+
);
5073
}
5174

5275
@Test

0 commit comments

Comments
 (0)