McMMOSimpleRegionFile.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * This file is part of SpoutPlugin.
  3. *
  4. * Copyright (c) 2011-2012, SpoutDev <http://www.spout.org/>
  5. * SpoutPlugin is licensed under the GNU Lesser General Public License.
  6. *
  7. * SpoutPlugin is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SpoutPlugin is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.gmail.nossr50.util.blockmeta;
  21. import org.jetbrains.annotations.NotNull;
  22. import org.jetbrains.annotations.Nullable;
  23. import java.io.*;
  24. import java.util.BitSet;
  25. import java.util.zip.DeflaterOutputStream;
  26. import java.util.zip.InflaterInputStream;
  27. /**
  28. * File format:
  29. * bytes 0-4096 contain 1024 integer values representing the segment index of each chunk
  30. * bytes 4096-8192 contain 1024 integer values representing the byte length of each chunk
  31. * bytes 8192-8196 is the integer value of the segment exponent
  32. * bytes 8196-12288 are reserved for future use
  33. * bytes 12288+ contain the data segments, by default 1024 byte segments.
  34. * Chunk data is compressed and stored in 1 or more segments as needed.
  35. */
  36. public class McMMOSimpleRegionFile {
  37. private static final int DEFAULT_SEGMENT_EXPONENT = 10; // TODO, analyze real world usage and determine if a smaller segment(512) is worth it or not. (need to know average chunkstore bytesize)
  38. private static final int DEFAULT_SEGMENT_SIZE = (int)Math.pow(2, DEFAULT_SEGMENT_EXPONENT); // 1024
  39. private static final int RESERVED_HEADER_BYTES = 12288; // This needs to be divisible by segment size
  40. private static final int NUM_CHUNKS = 1024; // 32x32
  41. private static final int SEEK_CHUNK_SEGMENT_INDICES = 0;
  42. private static final int SEEK_CHUNK_BYTE_LENGTHS = 4096;
  43. private static final int SEEK_FILE_INFO = 8192;
  44. // Chunk info
  45. private final int[] chunkSegmentIndex = new int[NUM_CHUNKS];
  46. private final int[] chunkNumBytes = new int[NUM_CHUNKS];
  47. private final int[] chunkNumSegments = new int[NUM_CHUNKS];
  48. // Segments
  49. private final BitSet segments = new BitSet(); // Used to denote which segments are in use or not
  50. // Segment size/mask
  51. private final int segmentExponent;
  52. private final int segmentMask;
  53. // File location
  54. private final @NotNull File parent;
  55. // File access
  56. private final RandomAccessFile file;
  57. // Region index
  58. private final int rx;
  59. private final int rz;
  60. public McMMOSimpleRegionFile(@NotNull File f, int rx, int rz) {
  61. this.rx = rx;
  62. this.rz = rz;
  63. this.parent = f;
  64. try {
  65. this.file = new RandomAccessFile(parent, "rw");
  66. // New file, write out header bytes
  67. if (file.length() < RESERVED_HEADER_BYTES) {
  68. file.write(new byte[RESERVED_HEADER_BYTES]);
  69. file.seek(SEEK_FILE_INFO);
  70. file.writeInt(DEFAULT_SEGMENT_EXPONENT);
  71. }
  72. file.seek(SEEK_FILE_INFO);
  73. this.segmentExponent = file.readInt();
  74. this.segmentMask = (1 << segmentExponent) - 1;
  75. // Mark reserved segments reserved
  76. int reservedSegments = this.bytesToSegments(RESERVED_HEADER_BYTES);
  77. segments.set(0, reservedSegments, true);
  78. // Read chunk header data
  79. file.seek(SEEK_CHUNK_SEGMENT_INDICES);
  80. for (int i = 0; i < NUM_CHUNKS; i++)
  81. chunkSegmentIndex[i] = file.readInt();
  82. file.seek(SEEK_CHUNK_BYTE_LENGTHS);
  83. for (int i = 0; i < NUM_CHUNKS; i++) {
  84. chunkNumBytes[i] = file.readInt();
  85. chunkNumSegments[i] = bytesToSegments(chunkNumBytes[i]);
  86. markChunkSegments(i, true);
  87. }
  88. fixFileLength();
  89. }
  90. catch (IOException fnfe) {
  91. throw new RuntimeException(fnfe);
  92. }
  93. }
  94. public synchronized @NotNull DataOutputStream getOutputStream(int x, int z) {
  95. int index = getChunkIndex(x, z); // Get chunk index
  96. return new DataOutputStream(new DeflaterOutputStream(new McMMOSimpleChunkBuffer(this, index)));
  97. }
  98. private static class McMMOSimpleChunkBuffer extends ByteArrayOutputStream {
  99. final McMMOSimpleRegionFile rf;
  100. final int index;
  101. McMMOSimpleChunkBuffer(McMMOSimpleRegionFile rf, int index) {
  102. super(DEFAULT_SEGMENT_SIZE);
  103. this.rf = rf;
  104. this.index = index;
  105. }
  106. @Override
  107. public void close() throws IOException {
  108. rf.write(index, buf, count);
  109. }
  110. }
  111. private synchronized void write(int index, byte[] buffer, int size) throws IOException {
  112. int oldSegmentIndex = chunkSegmentIndex[index]; // Get current segment index
  113. markChunkSegments(index, false); // Clear our old segments
  114. int newSegmentIndex = findContiguousSegments(oldSegmentIndex, size); // Find contiguous segments to save to
  115. file.seek(newSegmentIndex << segmentExponent); // Seek to file location
  116. file.write(buffer, 0, size); // Write data
  117. // update in memory info
  118. chunkSegmentIndex[index] = newSegmentIndex;
  119. chunkNumBytes[index] = size;
  120. chunkNumSegments[index] = bytesToSegments(size);
  121. // Mark segments in use
  122. markChunkSegments(index, true);
  123. // Update header info
  124. file.seek(SEEK_CHUNK_SEGMENT_INDICES + (4 * index));
  125. file.writeInt(chunkSegmentIndex[index]);
  126. file.seek(SEEK_CHUNK_BYTE_LENGTHS + (4 * index));
  127. file.writeInt(chunkNumBytes[index]);
  128. }
  129. public synchronized @Nullable DataInputStream getInputStream(int x, int z) throws IOException {
  130. int index = getChunkIndex(x, z); // Get chunk index
  131. int byteLength = chunkNumBytes[index]; // Get byte length of data
  132. // No bytes
  133. if (byteLength == 0)
  134. return null;
  135. byte[] data = new byte[byteLength];
  136. file.seek(chunkSegmentIndex[index] << segmentExponent); // Seek to file location
  137. file.readFully(data); // Read in the data
  138. return new DataInputStream(new InflaterInputStream(new ByteArrayInputStream(data)));
  139. }
  140. public synchronized void close() {
  141. try {
  142. file.close();
  143. segments.clear();
  144. }
  145. catch (IOException ioe) {
  146. throw new RuntimeException("Unable to close file", ioe);
  147. }
  148. }
  149. private synchronized void markChunkSegments(int index, boolean inUse) {
  150. // No bytes used
  151. if (chunkNumBytes[index] == 0)
  152. return;
  153. int start = chunkSegmentIndex[index];
  154. int end = start + chunkNumSegments[index];
  155. // If we are writing, assert we don't write over any in-use segments
  156. if (inUse)
  157. {
  158. int nextSetBit = segments.nextSetBit(start);
  159. if (nextSetBit != -1 && nextSetBit < end)
  160. throw new IllegalStateException("Attempting to overwrite an in-use segment");
  161. }
  162. segments.set(start, end, inUse);
  163. }
  164. private synchronized void fixFileLength() throws IOException {
  165. int fileLength = (int)file.length();
  166. int extend = -fileLength & segmentMask; // how many bytes do we need to be divisible by segment size
  167. // Go to end of file
  168. file.seek(fileLength);
  169. // Append bytes
  170. file.write(new byte[extend], 0, extend);
  171. }
  172. private synchronized int findContiguousSegments(int hint, int size) {
  173. if (size == 0)
  174. return 0; // Zero byte data will not claim any chunks anyways
  175. int segments = bytesToSegments(size); // Number of segments we need
  176. // Check the hinted location (previous location of chunk) most of the time we can fit where we were.
  177. boolean oldFree = true;
  178. for (int i = hint; i < this.segments.size() && i < hint + segments; i++) {
  179. if (this.segments.get(i)) {
  180. oldFree = false;
  181. break;
  182. }
  183. }
  184. // We fit!
  185. if (oldFree)
  186. return hint;
  187. // Find somewhere to put us
  188. int start = 0;
  189. int current = 0;
  190. while (current < this.segments.size()) {
  191. boolean segmentInUse = this.segments.get(current); // check if segment is in use
  192. current++; // Move up a segment
  193. // Move up start if the segment was in use
  194. if (segmentInUse)
  195. start = current;
  196. // If we have enough segments now, return
  197. if (current - start >= segments)
  198. return start;
  199. }
  200. // Return the end of the segments (will expand to fit them)
  201. return start;
  202. }
  203. private synchronized int bytesToSegments(int bytes) {
  204. if (bytes <= 0)
  205. return 1;
  206. return ((bytes - 1) >> segmentExponent) + 1; // ((bytes - 1) / segmentSize) + 1
  207. }
  208. private synchronized int getChunkIndex(int x, int z) {
  209. if (rx != (x >> 5) || rz != (z >> 5))
  210. throw new IndexOutOfBoundsException();
  211. x = x & 0x1F; // 5 bits (mod 32)
  212. z = z & 0x1F; // 5 bits (mod 32)
  213. return (x << 5) + z; // x in the upper 5 bits, z in the lower 5 bits
  214. }
  215. }