test_class.file: oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexWriterTest.java
test_class.identifier: BinaryReferencesIndexWriterTest
test_case.body: @Test
      public void testWrite() throws Exception {
          UUID s1 = new UUID(1, 1);
          UUID s2 = new UUID(1, 2);
          UUID s3 = new UUID(2, 1);
          UUID s4 = new UUID(2, 2);
  
          BinaryReferencesIndexWriter writer = newBinaryReferencesIndexWriter();
          writer.addEntry(1, 2, false, s1, "1.1.1");
          writer.addEntry(1, 2, false, s1, "1.1.2");
          writer.addEntry(1, 2, false, s2, "1.2.1");
          writer.addEntry(1, 2, false, s2, "1.2.2");
          writer.addEntry(3, 4, true, s3, "2.1.1");
          writer.addEntry(3, 4, true, s3, "2.1.2");
          writer.addEntry(3, 4, true, s4, "2.2.1");
          writer.addEntry(3, 4, true, s4, "2.2.2");
  
          byte[] data = writer.write();
  
          Buffer buffer = loadBinaryReferencesIndex((whence, length) -> Buffer.wrap(data, data.length - whence, length));
          BinaryReferencesIndex index = parseBinaryReferencesIndex(buffer);
  
          Generation g1 = new Generation(1, 2, false);
          Generation g2 = new Generation(3, 4, true);
  
          Map<Generation, Map<UUID, Set<String>>> expected = new HashMap<>();
          expected.put(g1, new HashMap<>());
          expected.put(g2, new HashMap<>());
          expected.get(g1).put(s1, new HashSet<>());
          expected.get(g1).put(s2, new HashSet<>());
          expected.get(g2).put(s3, new HashSet<>());
          expected.get(g2).put(s4, new HashSet<>());
          expected.get(g1).get(s1).addAll(asList("1.1.1", "1.1.2"));
          expected.get(g1).get(s2).addAll(asList("1.2.1", "1.2.2"));
          expected.get(g2).get(s3).addAll(asList("2.1.1", "2.1.2"));
          expected.get(g2).get(s4).addAll(asList("2.2.1", "2.2.2"));
  
          Map<Generation, Map<UUID, Set<String>>> actual = new HashMap<>();
          index.forEach((generation, full, compacted, id, reference) -> {
              actual
                  .computeIfAbsent(new Generation(generation, full, compacted), k -> new HashMap<>())
                  .computeIfAbsent(id, k -> new HashSet<>())
                  .add(reference);
          });
  
          assertEquals(expected, actual);
      }
test_case.class_method_signature: BinaryReferencesIndexWriterTest.testWrite()
test_case.full_signature: @Test public void testWrite()
test_case.identifier: testWrite
test_case.invocations: newBinaryReferencesIndexWriter addEntry addEntry addEntry addEntry addEntry addEntry addEntry addEntry write loadBinaryReferencesIndex wrap parseBinaryReferencesIndex put put put get put get put get put get addAll get get asList addAll get get asList addAll get get asList addAll get get asList forEach add computeIfAbsent computeIfAbsent assertEquals
test_case.modifiers: @Test public
test_case.parameters: ()
test_case.return: void
test_case.signature: void testWrite()
focal_class.declarator: entries
focal_class.modifier: private final
focal_class.original_string: private final Map<Generation, Map<UUID, Set<String>>> entries;
focal_class.type: Map<Generation, Map<UUID, Set<String>>>
focal_class.var_name: entries
focal_class.file: oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexWriter.java
focal_class.identifier: BinaryReferencesIndexWriter
focal_class.class_method_signature: BinaryReferencesIndexWriter.newBinaryReferencesIndexWriter()
focal_class.full_signature: public static BinaryReferencesIndexWriter newBinaryReferencesIndexWriter()
focal_class.identifier: newBinaryReferencesIndexWriter
focal_class.modifiers: public static
focal_class.parameters: ()
focal_class.return: BinaryReferencesIndexWriter
focal_class.signature: BinaryReferencesIndexWriter newBinaryReferencesIndexWriter()
focal_class.class_method_signature: BinaryReferencesIndexWriter.BinaryReferencesIndexWriter()
focal_class.full_signature: private  BinaryReferencesIndexWriter()
focal_class.identifier: BinaryReferencesIndexWriter
focal_class.modifiers: private
focal_class.parameters: ()
focal_class.signature:  BinaryReferencesIndexWriter()
focal_class.class_method_signature: BinaryReferencesIndexWriter.addEntry(int generation, int full, boolean compacted, UUID segment, String reference)
focal_class.full_signature: public void addEntry(int generation, int full, boolean compacted, UUID segment, String reference)
focal_class.identifier: addEntry
focal_class.modifiers: public
focal_class.parameters: (int generation, int full, boolean compacted, UUID segment, String reference)
focal_class.return: void
focal_class.signature: void addEntry(int generation, int full, boolean compacted, UUID segment, String reference)
focal_class.class_method_signature: BinaryReferencesIndexWriter.write()
focal_class.full_signature: public byte[] write()
focal_class.identifier: write
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: byte[]
focal_class.signature: byte[] write()
focal_method.body: public byte[] write() {
          int binaryReferenceSize = 0;
  
          // The following information are stored in the footer as meta-
          // information about the entry.
  
          // 4 bytes to store a magic number identifying this entry as containing
          // references to binary values.
          binaryReferenceSize += 4;
  
          // 4 bytes to store the CRC32 checksum of the data in this entry.
          binaryReferenceSize += 4;
  
          // 4 bytes to store the length of this entry, without including the
          // optional padding.
          binaryReferenceSize += 4;
  
          // 4 bytes to store the number of generations pairs in the binary
          // references map.
          binaryReferenceSize += 4;
  
          // The following information are stored as part of the main content of
          // this entry, after the optional padding.
  
          for (Map<UUID, Set<String>> segmentToReferences : entries.values()) {
              // 4 bytes per generation to store the generation number.
              binaryReferenceSize += 4;
  
              // 4 bytes per generation to store the full generation number.
              binaryReferenceSize += 4;
  
              // 1 byte per generation to store the "compacted" flag.
              binaryReferenceSize += 1;
  
              // 4 bytes per generation to store the number of segments.
              binaryReferenceSize += 4;
  
              for (Set<String> references : segmentToReferences.values()) {
                  // 16 bytes per segment identifier.
                  binaryReferenceSize += 16;
  
                  // 4 bytes to store the number of references for this segment.
                  binaryReferenceSize += 4;
  
                  for (String reference : references) {
                      // 4 bytes for each reference to store the length of the reference.
                      binaryReferenceSize += 4;
  
                      // A variable amount of bytes, depending on the reference itself.
                      binaryReferenceSize += reference.getBytes(Charsets.UTF_8).length;
                  }
              }
          }
  
          Buffer buffer = Buffer.allocate(binaryReferenceSize);
  
          for (Entry<Generation, Map<UUID, Set<String>>> be : entries.entrySet()) {
              Generation generation = be.getKey();
              Map<UUID, Set<String>> segmentToReferences = be.getValue();
  
              buffer.putInt(generation.generation);
              buffer.putInt(generation.full);
              buffer.put((byte) (generation.compacted ? 1 : 0));
              buffer.putInt(segmentToReferences.size());
  
              for (Entry<UUID, Set<String>> se : segmentToReferences.entrySet()) {
                  UUID segmentId = se.getKey();
                  Set<String> references = se.getValue();
  
                  buffer.putLong(segmentId.getMostSignificantBits());
                  buffer.putLong(segmentId.getLeastSignificantBits());
                  buffer.putInt(references.size());
  
                  for (String reference : references) {
                      byte[] bytes = reference.getBytes(Charsets.UTF_8);
  
                      buffer.putInt(bytes.length);
                      buffer.put(bytes);
                  }
              }
          }
  
          CRC32 checksum = new CRC32();
          checksum.update(buffer.array(), 0, buffer.position());
  
          buffer.putInt((int) checksum.getValue());
          buffer.putInt(entries.size());
          buffer.putInt(binaryReferenceSize);
          buffer.putInt(BinaryReferencesIndexLoaderV2.MAGIC);
  
          return buffer.array();
      }
focal_method.class_method_signature: BinaryReferencesIndexWriter.write()
focal_method.full_signature: public byte[] write()
focal_method.identifier: write
focal_method.invocations: values values getBytes allocate entrySet getKey getValue putInt putInt put putInt size entrySet getKey getValue putLong getMostSignificantBits putLong getLeastSignificantBits putInt size getBytes putInt put update array position putInt getValue putInt size putInt putInt array
focal_method.modifiers: public
focal_method.parameters: ()
focal_method.return: byte[]
focal_method.signature: byte[] write()