1212import org .apache .lucene .search .TotalHits ;
1313import org .opensearch .common .document .DocumentField ;
1414import org .opensearch .core .common .bytes .BytesArray ;
15+ import org .opensearch .core .common .bytes .BytesReference ;
1516import org .opensearch .core .common .bytes .CompositeBytesReference ;
1617import org .opensearch .core .common .text .Text ;
1718import org .opensearch .core .index .shard .ShardId ;
2223import org .opensearch .search .SearchShardTarget ;
2324import org .opensearch .search .fetch .subphase .highlight .HighlightField ;
2425import org .opensearch .test .OpenSearchTestCase ;
26+ import com .google .protobuf .UnsafeByteOperations ;
27+ import com .google .protobuf .ByteString ;
2528
2629import java .io .IOException ;
2730import java .nio .charset .StandardCharsets ;
@@ -306,16 +309,16 @@ public void testToProtoWithBytesArrayZeroCopyOptimization() throws IOException {
306309 searchHit .sourceRef (bytesArray );
307310
308311 // Call the method under test
309- Hit hit = SearchHitProtoUtils .toProto (searchHit );
312+ HitsMetadataHitsInner hit = SearchHitProtoUtils .toProto (searchHit );
310313
311314 // Verify the result
312315 assertNotNull ("Hit should not be null" , hit );
313- assertTrue ("Source should be set" , hit .hasSource ());
314- assertArrayEquals ("Source bytes should match exactly" , sourceBytes , hit .getSource ().toByteArray ());
316+ assertTrue ("Source should be set" , hit .hasXSource ());
317+ assertArrayEquals ("Source bytes should match exactly" , sourceBytes , hit .getXSource ().toByteArray ());
315318
316319 // Verify that the ByteString was created using UnsafeByteOperations.unsafeWrap
317320 // This is an indirect test - we verify the content is correct and assume the optimization was used
318- assertEquals ("Source size should match" , sourceBytes .length , hit .getSource ().size ());
321+ assertEquals ("Source size should match" , sourceBytes .length , hit .getXSource ().size ());
319322 }
320323
321324 public void testToProtoWithBytesArrayWithOffsetZeroCopyOptimization () throws IOException {
@@ -329,13 +332,13 @@ public void testToProtoWithBytesArrayWithOffsetZeroCopyOptimization() throws IOE
329332 searchHit .sourceRef (bytesArray );
330333
331334 // Call the method under test
332- Hit hit = SearchHitProtoUtils .toProto (searchHit );
335+ HitsMetadataHitsInner hit = SearchHitProtoUtils .toProto (searchHit );
333336
334337 // Verify the result
335338 assertNotNull ("Hit should not be null" , hit );
336- assertTrue ("Source should be set" , hit .hasSource ());
337- assertArrayEquals ("Source bytes should match the sliced portion" , expectedBytes , hit .getSource ().toByteArray ());
338- assertEquals ("Source size should match expected length" , length , hit .getSource ().size ());
339+ assertTrue ("Source should be set" , hit .hasXSource ());
340+ assertArrayEquals ("Source bytes should match the sliced portion" , expectedBytes , hit .getXSource ().toByteArray ());
341+ assertEquals ("Source size should match expected length" , length , hit .getXSource ().size ());
339342 }
340343
341344 public void testToProtoWithCompositeBytesReferenceUsesDeepCopy () throws IOException {
@@ -349,17 +352,17 @@ public void testToProtoWithCompositeBytesReferenceUsesDeepCopy() throws IOExcept
349352 searchHit .sourceRef (compositeBytesRef );
350353
351354 // Call the method under test
352- Hit hit = SearchHitProtoUtils .toProto (searchHit );
355+ HitsMetadataHitsInner hit = SearchHitProtoUtils .toProto (searchHit );
353356
354357 // Verify the result
355358 assertNotNull ("Hit should not be null" , hit );
356- assertTrue ("Source should be set" , hit .hasSource ());
359+ assertTrue ("Source should be set" , hit .hasXSource ());
357360
358361 // Verify the combined content is correct
359362 String expectedJson = "{\" field1\" :\" value1\" }" ;
360363 byte [] expectedBytes = expectedJson .getBytes (StandardCharsets .UTF_8 );
361- assertArrayEquals ("Source bytes should match the combined content" , expectedBytes , hit .getSource ().toByteArray ());
362- assertEquals ("Source size should match combined length" , expectedBytes .length , hit .getSource ().size ());
364+ assertArrayEquals ("Source bytes should match the combined content" , expectedBytes , hit .getXSource ().toByteArray ());
365+ assertEquals ("Source size should match combined length" , expectedBytes .length , hit .getXSource ().size ());
363366 }
364367
365368 public void testToProtoWithEmptyBytesArraySource () throws IOException {
@@ -370,13 +373,13 @@ public void testToProtoWithEmptyBytesArraySource() throws IOException {
370373 searchHit .sourceRef (emptyJsonBytesArray );
371374
372375 // Call the method under test
373- Hit hit = SearchHitProtoUtils .toProto (searchHit );
376+ HitsMetadataHitsInner hit = SearchHitProtoUtils .toProto (searchHit );
374377
375378 // Verify the result
376379 assertNotNull ("Hit should not be null" , hit );
377- assertTrue ("Source should be set" , hit .hasSource ());
378- assertEquals ("Source should contain empty JSON object" , emptyJsonBytes .length , hit .getSource ().size ());
379- assertArrayEquals ("Source bytes should match empty JSON object" , emptyJsonBytes , hit .getSource ().toByteArray ());
380+ assertTrue ("Source should be set" , hit .hasXSource ());
381+ assertEquals ("Source should contain empty JSON object" , emptyJsonBytes .length , hit .getXSource ().size ());
382+ assertArrayEquals ("Source bytes should match empty JSON object" , emptyJsonBytes , hit .getXSource ().toByteArray ());
380383 }
381384
382385 public void testToProtoWithLargeBytesArrayZeroCopyOptimization () throws IOException {
@@ -394,13 +397,13 @@ public void testToProtoWithLargeBytesArrayZeroCopyOptimization() throws IOExcept
394397 searchHit .sourceRef (largeBytesArray );
395398
396399 // Call the method under test
397- Hit hit = SearchHitProtoUtils .toProto (searchHit );
400+ HitsMetadataHitsInner hit = SearchHitProtoUtils .toProto (searchHit );
398401
399402 // Verify the result
400403 assertNotNull ("Hit should not be null" , hit );
401- assertTrue ("Source should be set" , hit .hasSource ());
402- assertEquals ("Source size should match large content" , largeSourceBytes .length , hit .getSource ().size ());
403- assertArrayEquals ("Source bytes should match exactly" , largeSourceBytes , hit .getSource ().toByteArray ());
404+ assertTrue ("Source should be set" , hit .hasXSource ());
405+ assertEquals ("Source size should match large content" , largeSourceBytes .length , hit .getXSource ().size ());
406+ assertArrayEquals ("Source bytes should match exactly" , largeSourceBytes , hit .getXSource ().toByteArray ());
404407 }
405408
406409 public void testToProtoWithBytesArraySliceZeroCopyOptimization () throws IOException {
@@ -419,15 +422,15 @@ public void testToProtoWithBytesArraySliceZeroCopyOptimization() throws IOExcept
419422 searchHit .sourceRef (slicedBytesArray );
420423
421424 // Call the method under test
422- Hit hit = SearchHitProtoUtils .toProto (searchHit );
425+ HitsMetadataHitsInner hit = SearchHitProtoUtils .toProto (searchHit );
423426
424427 // Verify the result
425428 assertNotNull ("Hit should not be null" , hit );
426- assertTrue ("Source should be set" , hit .hasSource ());
427- assertEquals ("Source size should match JSON length" , jsonLength , hit .getSource ().size ());
429+ assertTrue ("Source should be set" , hit .hasXSource ());
430+ assertEquals ("Source size should match JSON length" , jsonLength , hit .getXSource ().size ());
428431
429432 byte [] expectedJsonBytes = jsonContent .getBytes (StandardCharsets .UTF_8 );
430- assertArrayEquals ("Source bytes should match only the JSON portion" , expectedJsonBytes , hit .getSource ().toByteArray ());
433+ assertArrayEquals ("Source bytes should match only the JSON portion" , expectedJsonBytes , hit .getXSource ().toByteArray ());
431434 }
432435
433436 public void testToProtoSourceOptimizationBehaviorComparison () throws IOException {
@@ -439,23 +442,23 @@ public void testToProtoSourceOptimizationBehaviorComparison() throws IOException
439442 SearchHit searchHitWithBytesArray = new SearchHit (1 );
440443 BytesArray bytesArray = new BytesArray (jsonBytes );
441444 searchHitWithBytesArray .sourceRef (bytesArray );
442- Hit hitWithBytesArray = SearchHitProtoUtils .toProto (searchHitWithBytesArray );
445+ HitsMetadataHitsInner hitWithBytesArray = SearchHitProtoUtils .toProto (searchHitWithBytesArray );
443446
444447 // Test with CompositeBytesReference (should use deep copy)
445448 SearchHit searchHitWithComposite = new SearchHit (2 );
446449 BytesArray part1 = new BytesArray (jsonBytes , 0 , jsonBytes .length / 2 );
447450 BytesArray part2 = new BytesArray (jsonBytes , jsonBytes .length / 2 , jsonBytes .length - jsonBytes .length / 2 );
448451 CompositeBytesReference composite = (CompositeBytesReference ) CompositeBytesReference .of (part1 , part2 );
449452 searchHitWithComposite .sourceRef (composite );
450- Hit hitWithComposite = SearchHitProtoUtils .toProto (searchHitWithComposite );
453+ HitsMetadataHitsInner hitWithComposite = SearchHitProtoUtils .toProto (searchHitWithComposite );
451454
452455 // Both should produce the same result
453456 assertArrayEquals (
454457 "Both approaches should produce identical byte content" ,
455- hitWithBytesArray .getSource ().toByteArray (),
456- hitWithComposite .getSource ().toByteArray ()
458+ hitWithBytesArray .getXSource ().toByteArray (),
459+ hitWithComposite .getXSource ().toByteArray ()
457460 );
458- assertEquals ("Both approaches should produce same size" , hitWithBytesArray .getSource ().size (), hitWithComposite .getSource ().size ());
461+ assertEquals ("Both approaches should produce same size" , hitWithBytesArray .getXSource ().size (), hitWithComposite .getXSource ().size ());
459462 }
460463
461464 public void testToProtoWithNullSourceRef () throws IOException {
@@ -464,11 +467,11 @@ public void testToProtoWithNullSourceRef() throws IOException {
464467 // Don't set any source reference (sourceRef remains null)
465468
466469 // Call the method under test
467- Hit hit = SearchHitProtoUtils .toProto (searchHit );
470+ HitsMetadataHitsInner hit = SearchHitProtoUtils .toProto (searchHit );
468471
469472 // Verify the result
470473 assertNotNull ("Hit should not be null" , hit );
471- assertFalse ("Source should not be set when sourceRef is null" , hit .hasSource ());
474+ assertFalse ("Source should not be set when sourceRef is null" , hit .hasXSource ());
472475 }
473476
474477 public void testToProtoWithActuallyEmptyBytesArray () throws IOException {
@@ -479,11 +482,11 @@ public void testToProtoWithActuallyEmptyBytesArray() throws IOException {
479482 searchHit .sourceRef (null );
480483
481484 // Call the method under test
482- Hit hit = SearchHitProtoUtils .toProto (searchHit );
485+ HitsMetadataHitsInner hit = SearchHitProtoUtils .toProto (searchHit );
483486
484487 // Verify the result
485488 assertNotNull ("Hit should not be null" , hit );
486- assertFalse ("Source should not be set when explicitly null" , hit .hasSource ());
489+ assertFalse ("Source should not be set when explicitly null" , hit .hasXSource ());
487490 }
488491
489492 public void testToProtoWithBytesArrayOffsetConditionCoverage () throws IOException {
@@ -506,14 +509,14 @@ public void testToProtoWithBytesArrayOffsetConditionCoverage() throws IOExceptio
506509 searchHit .sourceRef (slicedBytesArray );
507510
508511 // Call the method under test
509- Hit hit = SearchHitProtoUtils .toProto (searchHit );
512+ HitsMetadataHitsInner hit = SearchHitProtoUtils .toProto (searchHit );
510513
511514 // Verify the result - should use the offset/length version of unsafeWrap
512515 assertNotNull ("Hit should not be null" , hit );
513- assertTrue ("Source should be set" , hit .hasSource ());
514- assertEquals ("Source size should match JSON length" , length , hit .getSource ().size ());
516+ assertTrue ("Source should be set" , hit .hasXSource ());
517+ assertEquals ("Source size should match JSON length" , length , hit .getXSource ().size ());
515518
516519 byte [] expectedBytes = jsonContent .getBytes (StandardCharsets .UTF_8 );
517- assertArrayEquals ("Source bytes should match JSON content" , expectedBytes , hit .getSource ().toByteArray ());
520+ assertArrayEquals ("Source bytes should match JSON content" , expectedBytes , hit .getXSource ().toByteArray ());
518521 }
519522}
0 commit comments