Example #1
0
  @Override
  public void load(Chunk chunk) throws IOException {
    File regionDirectory = getRegionDirectory(chunk.world, chunk.x, chunk.z);
    File f = new File(regionDirectory, chunk.x + "_" + chunk.z);

    // if no file exists, just initialize a small-capacity map
    if (!f.exists()) {
      log.debug("load on chunk {}, no file exists, initializing empty dataset", chunk);
      chunk.map = new IntShortOpenHashMap(100);
      return;
    }

    DataInputStream is = null;

    try {
      is = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));

      // first 32 bits represent the size of the map
      int available = is.readInt();

      // we round up by 30% to account for .75 load factor in hash.
      // this initializes the map at less than the load factor with
      // some extra room for growth before needing a clone & grow
      chunk.map = new IntShortOpenHashMap((int) (available * 1.3));

      while (is.available() > 0) {
        int key = is.readInt();
        short value = is.readShort();
        chunk.map.put(key, value);
        if (log.isDebugEnabled()) {
          Formatter format = new Formatter();
          format.format("loaded chunk{%d,%d} owner %d for key %x", chunk.x, chunk.z, value, key);
          log.debug(format.toString());
          format.close();
        }
      }
    } finally {
      if (is != null) is.close();
    }
  }