Exemplo n.º 1
0
  @Override
  public void flush(Directory directory, String segmentName) throws IOException {
    Set<CartesianGeoRecord> treeToFlush;
    Set<String> fieldNamesToFlush;
    synchronized (treeLock) {
      fieldNamesToFlush = fieldNames;
      fieldNames = new HashSet<String>();

      treeToFlush = fieldTree;
      fieldTree = geoUtil.getBinaryTreeOrderedByBitMag();
    }

    GeoSegmentWriter<CartesianGeoRecord> geoRecordBTree = null;

    GeoSegmentInfo geoSegmentInfo = buildGeoSegmentInfo(fieldNamesToFlush, segmentName);

    boolean success = false;
    try {
      String fileName = config.getGeoFileName(segmentName);
      geoRecordBTree =
          new GeoSegmentWriter<CartesianGeoRecord>(
              treeToFlush, directory, fileName, geoSegmentInfo, geoRecordSerializer);

      success = true;
    } finally {
      // see https://issues.apache.org/jira/browse/LUCENE-3405
      if (success) {
        IOUtils.close(geoRecordBTree);
      } else {
        IOUtils.closeWhileHandlingException(geoRecordBTree);
      }
    }
  }
Exemplo n.º 2
0
  private void doCreateAndTest(final int docsToAdd, final int version) throws IOException {
    final byte[] byteBuf = new byte[10];
    final Class<?> clazz = byteBuf.getClass();

    context.assertIsSatisfied(); // we should have no calls to mock Objects before we flush
    context.checking(
        new Expectations() {
          {
            ignoring(mockOutput).getFilePointer();
            will(returnValue(1L));

            // get output
            one(directory).createOutput(info.getSegmentName() + "." + config.getGeoFileExtension());
            will(returnValue(mockOutput));
            inSequence(outputSequence);

            // write file header
            one(mockOutput).writeVInt(version);
            inSequence(outputSequence);
            one(mockOutput).writeInt(0);
            inSequence(outputSequence);
            one(mockOutput).writeVInt(docsToAdd);
            inSequence(outputSequence);
            if (version > GeoVersion.VERSION_0) {
              one(mockOutput).writeVInt(GeoSegmentInfo.BYTES_PER_RECORD_V1);
              inSequence(outputSequence);
            }
            one(fieldNameFilterConverter).writeToOutput(mockOutput);
            inSequence(outputSequence);

            one(mockOutput).seek(1);
            inSequence(outputSequence);
            one(mockOutput).writeInt(1);
            inSequence(outputSequence);

            // fill zeroes
            one(mockOutput).length();
            will(returnValue(7L));
            inSequence(outputSequence);
            one(mockOutput).seek(with(any(Long.class)));
            inSequence(outputSequence);
            one(mockOutput)
                .writeBytes(
                    with(any(byteBuf.getClass())),
                    with(any(Integer.TYPE)),
                    with(any(Integer.TYPE)));
            inSequence(outputSequence);
            one(mockOutput).seek(with(any(Long.class)));
            inSequence(outputSequence);
            one(mockOutput).length();
            will(returnValue((long) (7 + 13 * docsToAdd)));
            inSequence(outputSequence);

            // write actual tree
            for (int i = 0; i < docsToAdd; i++) {

              one(mockOutput).seek(with(any(Long.class)));
              inSequence(outputSequence);
              one(geoRecordSerializer)
                  .writeGeoRecord(
                      with(mockOutput), with(any(GeoRecord.class)), with(any(Integer.class)));
              inSequence(outputSequence);
            }

            // close
            one(mockOutput).close();
          }
        });

    info.setGeoVersion(version);
    String fileName = config.getGeoFileName(info.getSegmentName());
    GeoSegmentWriter<GeoRecord> bTree =
        new GeoSegmentWriter<GeoRecord>(treeSet, directory, fileName, info, geoRecordSerializer);
    bTree.close();
    context.assertIsSatisfied();
  }