|
| 1 | +// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors |
| 2 | +// Licensed under the MIT License: |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in |
| 12 | +// all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +// THE SOFTWARE. |
| 21 | + |
| 22 | +package org.capnproto; |
| 23 | + |
| 24 | +import java.io.File; |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.RandomAccessFile; |
| 27 | +import java.lang.ref.Cleaner; |
| 28 | +import java.nio.MappedByteBuffer; |
| 29 | +import java.nio.channels.FileChannel; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.HashMap; |
| 32 | +import java.util.Map; |
| 33 | +import java.util.Random; |
| 34 | + |
| 35 | + |
| 36 | +public class MemoryMappedAllocator implements Allocator { |
| 37 | + // cleaner for file cleanup when this is GCed |
| 38 | + private static final Cleaner cleaner = Cleaner.create(); |
| 39 | + private final Cleaner.Cleanable cleanable; |
| 40 | + |
| 41 | + // the length of the random prefix part of the random filename string |
| 42 | + private final int PREFIX_LENGTH = 5; |
| 43 | + // the used charset for creating random filenames |
| 44 | + private static final String CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
| 45 | + |
| 46 | + // (minimum) number of bytes in the next allocation |
| 47 | + private int nextSize = BuilderArena.SUGGESTED_FIRST_SEGMENT_WORDS; |
| 48 | + // the maximum allocateable size |
| 49 | + public int maxSegmentBytes = Integer.MAX_VALUE - 2; |
| 50 | + |
| 51 | + // the memory mapped file buffer name prefix |
| 52 | + private final String rPrefix; |
| 53 | + |
| 54 | + // the allocation strategy with which the allocation size grows |
| 55 | + public AllocationStrategy allocationStrategy = |
| 56 | + AllocationStrategy.GROW_HEURISTICALLY; |
| 57 | + |
| 58 | + // hashmaps used for keeping track of files |
| 59 | + private final Map<Integer, RandomAccessFile> randomAccFiles = Collections.synchronizedMap(new HashMap<>()); |
| 60 | + private final Map<Integer, FileChannel> channelMap = Collections.synchronizedMap(new HashMap<>()); |
| 61 | + |
| 62 | + public MemoryMappedAllocator(String baseFileName) { |
| 63 | + // create random file name with baseFileNamePrefix: |
| 64 | + // ${baseFileName}_XXXXX_000001 |
| 65 | + Random random = new Random(); |
| 66 | + StringBuilder sb = new StringBuilder(PREFIX_LENGTH); |
| 67 | + for (int i = 0; i < PREFIX_LENGTH; i++) { |
| 68 | + int index = random.nextInt(CHARSET.length()); |
| 69 | + sb.append(CHARSET.charAt(index)); |
| 70 | + } |
| 71 | + rPrefix = baseFileName + "_" + sb.toString(); |
| 72 | + this.cleanable = cleaner.register(this, new State(this.randomAccFiles, rPrefix)); |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + public MemoryMappedAllocator(String baseFileName, AllocationStrategy allocationStrategy) { |
| 77 | + // create random file name with baseFileNamePrefix: |
| 78 | + // ${baseFileName}_XXXXX_000001 |
| 79 | + Random random = new Random(); |
| 80 | + StringBuilder sb = new StringBuilder(PREFIX_LENGTH); |
| 81 | + for (int i = 0; i < PREFIX_LENGTH; i++) { |
| 82 | + int index = random.nextInt(CHARSET.length()); |
| 83 | + sb.append(CHARSET.charAt(index)); |
| 84 | + } |
| 85 | + rPrefix = baseFileName + "_" + sb.toString(); |
| 86 | + this.cleanable = cleaner.register(this, new State(this.randomAccFiles, rPrefix)); |
| 87 | + this.allocationStrategy = allocationStrategy; |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + private static String nameForInt(String prefix, int key) { |
| 92 | + String ret = prefix + "_" + String.format("%05d", key); |
| 93 | + return ret; |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + private Integer generateFile() throws IOException { |
| 98 | + int fCount; |
| 99 | + synchronized (randomAccFiles) { |
| 100 | + fCount = randomAccFiles.size(); |
| 101 | + String newFileName = nameForInt(rPrefix, fCount); |
| 102 | + RandomAccessFile newFile = new RandomAccessFile(newFileName, "rw"); |
| 103 | + randomAccFiles.put(fCount, newFile); |
| 104 | + File test = new File(newFileName); |
| 105 | + test.deleteOnExit(); |
| 106 | + } |
| 107 | + return fCount; |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + /** |
| 112 | + * set the grow size of the memory mapped file |
| 113 | + */ |
| 114 | + @Override |
| 115 | + public void setNextAllocationSizeBytes(int nextSize) { |
| 116 | + this.nextSize = nextSize; |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + private FileChannel createSegment(int segmentSize) throws IOException { |
| 121 | + int fileKey = generateFile(); |
| 122 | + FileChannel channel = null; |
| 123 | + synchronized (channelMap) { |
| 124 | + if (!channelMap.containsKey(fileKey)) { |
| 125 | + synchronized (randomAccFiles) { |
| 126 | + RandomAccessFile file = randomAccFiles.get(fileKey); |
| 127 | + file.setLength(segmentSize); |
| 128 | + channel = file.getChannel(); |
| 129 | + channelMap.put(fileKey, channel); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + return channel; |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + @Override |
| 138 | + public java.nio.ByteBuffer allocateSegment(int minimumSize) { |
| 139 | + int size = Math.max(minimumSize, this.nextSize); |
| 140 | + MappedByteBuffer result = null; |
| 141 | + try { |
| 142 | + FileChannel channel = createSegment(size); |
| 143 | + result = channel.map(FileChannel.MapMode.READ_WRITE, 0, size); |
| 144 | + } |
| 145 | + catch (IOException e) |
| 146 | + { |
| 147 | + System.err.println("IOException: allocateSegment failed with:" + e); |
| 148 | + } |
| 149 | + |
| 150 | + |
| 151 | + switch (this.allocationStrategy) { |
| 152 | + case GROW_HEURISTICALLY: |
| 153 | + if (size < this.maxSegmentBytes - this.nextSize) { |
| 154 | + this.nextSize += size; |
| 155 | + } else { |
| 156 | + this.nextSize = maxSegmentBytes; |
| 157 | + } |
| 158 | + break; |
| 159 | + case FIXED_SIZE: |
| 160 | + break; |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | + // if (size < this.maxSegmentBytes - this.nextSize) { |
| 166 | + // this.nextSize += size; |
| 167 | + // } else { |
| 168 | + // this.nextSize = maxSegmentBytes; |
| 169 | + // } |
| 170 | + return result; |
| 171 | + } |
| 172 | + |
| 173 | + |
| 174 | + private static class State implements Runnable { |
| 175 | + private final Map<Integer, RandomAccessFile> randomAccFiles; |
| 176 | + private final String rPrefix; |
| 177 | + |
| 178 | + State(Map<Integer, RandomAccessFile> files, String rPrefix) { |
| 179 | + this.randomAccFiles = files; |
| 180 | + this.rPrefix = rPrefix; |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public void run() { |
| 185 | + // Cleanup logic: delete all files |
| 186 | + for (Map.Entry<Integer,RandomAccessFile> entry : randomAccFiles.entrySet()) { |
| 187 | + try { |
| 188 | + entry.getValue().close(); |
| 189 | + } |
| 190 | + catch (IOException e) |
| 191 | + { |
| 192 | + } |
| 193 | + String name = nameForInt(rPrefix, entry.getKey()); |
| 194 | + File file = new File(name); |
| 195 | + file.delete(); |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * Explicit cleanup: WARNING: this invalidates all alloceted buffers, |
| 202 | + * use close() after using the ByteBuffers. |
| 203 | + */ |
| 204 | + public void close() { |
| 205 | + cleanable.clean(); |
| 206 | + } |
| 207 | +} |
0 commit comments