Ejemplo n.º 1
0
  public synchronized void flush(List<Update> updates) {
    Assert.state(storedFile != null, "The FileStorage is not initialized");
    try {
      for (Update update : updates) {
        ensureCapacity(update.index * BYTES_IN_LONG + BYTES_IN_LONG);
        if (activateMemoryMappedBuffers) {
          buffer.putLong(update.index * BYTES_IN_LONG, update.value);
        } else {
          storedFile.seek(update.index * BYTES_IN_LONG);
          storedFile.writeLong(update.value);
        }
      }
      if (activateMemoryMappedBuffers) {
        buffer.force();
      }
      storedFile.getFD().sync();

    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
Ejemplo n.º 2
0
  private long writeContainerHeader(
      long date,
      int version,
      short tilePixel,
      String comment,
      boolean debugStrings,
      boolean waynodeCompression,
      boolean polygonClipping,
      boolean pixelCompression,
      GeoCoordinate mapStartPosition)
      throws IOException {

    // get metadata for the map file
    int numberOfZoomIntervals = dataStore.getZoomIntervalConfiguration().getNumberOfZoomIntervals();

    logger.fine("writing header");

    MappedByteBuffer containerHeaderBuffer =
        randomAccessFile.getChannel().map(MapMode.READ_WRITE, 0, HEADER_BUFFER_SIZE);

    // write file header
    // magic byte
    byte[] magicBytes = MAGIC_BYTE.getBytes();
    containerHeaderBuffer.put(magicBytes);

    // write container header size
    int headerSizePosition = containerHeaderBuffer.position();
    containerHeaderBuffer.position(headerSizePosition + 4);

    // version number of the binary file format
    containerHeaderBuffer.putInt(version);

    // meta info byte
    containerHeaderBuffer.put(
        buildMetaInfoByte(
            debugStrings,
            mapStartPosition != null,
            pixelCompression,
            polygonClipping,
            waynodeCompression));

    // amount of map files inside this file
    containerHeaderBuffer.put((byte) numberOfZoomIntervals);

    // projection type
    writeUTF8(PROJECTION, containerHeaderBuffer);

    // width and height of a tile in pixel
    containerHeaderBuffer.putShort(tilePixel);

    logger.fine(
        "Bounding box for file: "
            + dataStore.getBoundingBox().maxLatitudeE6
            + ", "
            + dataStore.getBoundingBox().minLongitudeE6
            + ", "
            + dataStore.getBoundingBox().minLatitudeE6
            + ", "
            + dataStore.getBoundingBox().maxLongitudeE6);
    // upper left corner of the bounding box
    containerHeaderBuffer.putInt(dataStore.getBoundingBox().maxLatitudeE6);
    containerHeaderBuffer.putInt(dataStore.getBoundingBox().minLongitudeE6);
    containerHeaderBuffer.putInt(dataStore.getBoundingBox().minLatitudeE6);
    containerHeaderBuffer.putInt(dataStore.getBoundingBox().maxLongitudeE6);

    if (mapStartPosition != null) {
      containerHeaderBuffer.putInt(mapStartPosition.getLatitudeE6());
      containerHeaderBuffer.putInt(mapStartPosition.getLongitudeE6());
    }

    // date of the map data
    containerHeaderBuffer.putLong(date);

    // store the mapping of tags to tag ids
    containerHeaderBuffer.putShort((short) PoiEnum.values().length);
    for (PoiEnum poiEnum : PoiEnum.values()) {
      writeUTF8(poiEnum.toString(), containerHeaderBuffer);
      containerHeaderBuffer.putShort((short) poiEnum.ordinal());
    }
    containerHeaderBuffer.putShort((short) WayEnum.values().length);
    for (WayEnum wayEnum : WayEnum.values()) {
      writeUTF8(wayEnum.toString(), containerHeaderBuffer);
      containerHeaderBuffer.putShort((short) wayEnum.ordinal());
    }

    // comment
    if (comment != null && !comment.equals("")) {
      writeUTF8(comment, containerHeaderBuffer);
    } else {
      writeUTF8("", containerHeaderBuffer);
    }

    // initialize buffer for writing zoom interval configurations
    bufferZoomIntervalConfig =
        randomAccessFile
            .getChannel()
            .map(
                MapMode.READ_WRITE,
                containerHeaderBuffer.position(),
                SIZE_ZOOMINTERVAL_CONFIGURATION * numberOfZoomIntervals);

    containerHeaderBuffer.position(
        containerHeaderBuffer.position() + SIZE_ZOOMINTERVAL_CONFIGURATION * numberOfZoomIntervals);

    // -4 bytes of header size variable itself
    int headerSize = containerHeaderBuffer.position() - headerSizePosition - 4;
    containerHeaderBuffer.putInt(headerSizePosition, headerSize);

    return containerHeaderBuffer.position();
  }
Ejemplo n.º 3
0
  @Test
  public void testFile() throws Exception {

    String datadir = ".tmp";
    String longdata = "";
    for (int i = 0; i < 30; i++) longdata += "1234567890";
    String path = datadir + "/new_topic/00000000000000000000.dat";
    RandomAccessFile raf = new RandomAccessFile(path, "rw");
    FileChannel ch = raf.getChannel();
    MappedByteBuffer buf = ch.map(MapMode.READ_WRITE, 0, 1024);
    buf.put((byte) 0);
    buf.put((byte) 5);
    buf.put("12345".getBytes());
    buf.put((byte) 1); // more
    buf.put((byte) 11);
    buf.put("67890abcdef".getBytes());
    buf.put((byte) 2); // long
    buf.putLong(300);
    buf.put(longdata.getBytes());

    raf.close();
    ch.close();

    Context ctx = ZMQ.context(1);
    Socket router = ctx.socket(ZMQ.ROUTER);
    router.bind("tcp://127.0.0.1:6001");
    router.setEncoder(Persistence.PersistEncoder.class);

    Socket dealer = ctx.socket(ZMQ.DEALER);
    dealer.setIdentity("A");
    dealer.connect("tcp://127.0.0.1:6001");

    Thread.sleep(1000);
    router.sendMore("A");
    router.sendMore(new byte[] {Persistence.MESSAGE_FILE});
    router.sendMore(new byte[] {STATUS_OK});
    router.sendMore(path);
    router.sendMore(ByteBuffer.wrap(new byte[8]).putLong(0).array());
    router.send(ByteBuffer.wrap(new byte[8]).putLong(329).array());

    assertEquals(dealer.recv()[0], STATUS_OK);
    ByteBuffer content = ByteBuffer.wrap(dealer.recv());

    assertEquals(content.limit(), 329);
    assertEquals(0, content.get());
    int length = content.get();
    assertEquals(5, length);
    byte[] data = new byte[length];
    content.get(data);
    assertEquals("12345", new String(data));

    assertEquals(1, content.get());
    length = content.get();
    assertEquals(11, length);
    data = new byte[length];
    content.get(data);
    assertEquals("67890abcdef", new String(data));

    assertEquals(2, content.get());
    length = (int) content.getLong();
    assertEquals(300, length);
    data = new byte[length];
    content.get(data);
    assertEquals(longdata, new String(data));

    assertEquals(false, content.hasRemaining());

    dealer.close();
    router.close();
    ctx.term();
  }