Search in sources :

Example 1 with WritableCuckooConfig

use of com.ms.silverking.cloud.dht.collection.WritableCuckooConfig in project SilverKing by Morgan-Stanley.

the class FileSegment method open.

private static FileSegment open(File nsDir, int segmentNumber, MapMode dataMapMode, int dataSegmentSize, SyncMode syncMode, NamespaceOptions nsOptions, SegmentIndexLocation segmentIndexLocation, SegmentPrereadMode segmentPrereadMode) throws IOException {
    RandomAccessFile raFile;
    ByteBuffer dataBuf;
    ByteBuffer rawHTBuf;
    ByteBuffer htBuf;
    int htBufSize;
    int htTotalEntries;
    String fileOpenMode;
    WritableCuckooConfig segmentCuckooConfig;
    if (dataMapMode == MapMode.READ_ONLY) {
        fileOpenMode = roFileMode;
    } else if (dataMapMode == MapMode.READ_WRITE) {
        fileOpenMode = syncModeToFileOpenMode(syncMode);
    } else if (dataMapMode == MapMode.PRIVATE) {
        throw new RuntimeException("MapMode.PRIVATE currently unsupported");
    } else {
        throw new RuntimeException("Unexpected dataMapMode: " + dataMapMode);
    }
    raFile = new RandomAccessFile(fileForSegment(nsDir, segmentNumber), fileOpenMode);
    if (false) {
        // This would allow us to force each segment onto the heap
        // FUTURE: consider whether we want to keep this option
        byte[] _bufArray;
        _bufArray = new byte[dataSegmentSize];
        raFile.seek(0);
        raFile.read(_bufArray);
        dataBuf = ByteBuffer.wrap(_bufArray);
    } else {
        int b;
        b = 0;
        dataBuf = raFile.getChannel().map(dataMapMode, 0, dataSegmentSize);
        if (segmentPrereadMode == SegmentPrereadMode.Preread) {
            forcePreread(dataBuf, dataSegmentSize);
        }
    }
    if (segmentIndexLocation == SegmentIndexLocation.RAM) {
        byte[] _htBufArray;
        _htBufArray = new byte[(int) (raFile.length() - dataSegmentSize)];
        raFile.seek(dataSegmentSize);
        raFile.read(_htBufArray);
        rawHTBuf = ByteBuffer.wrap(_htBufArray);
    } else {
        rawHTBuf = raFile.getChannel().map(MapMode.READ_ONLY, dataSegmentSize, raFile.length() - dataSegmentSize);
        if (segmentPrereadMode == SegmentPrereadMode.Preread) {
            forcePreread(rawHTBuf, (int) (raFile.length() - dataSegmentSize));
        }
    }
    rawHTBuf = rawHTBuf.order(ByteOrder.nativeOrder());
    try {
        htBuf = ((ByteBuffer) rawHTBuf.duplicate().position(NumConversion.BYTES_PER_INT + CuckooConfig.BYTES)).slice();
    } catch (RuntimeException re) {
        System.out.println(nsDir);
        System.out.println(segmentNumber);
        System.out.println(dataMapMode);
        System.out.println();
        System.out.println(dataBuf);
        System.out.printf("%d\t%d\t%d\t%d\n", dataSegmentSize, raFile.length(), dataSegmentSize, raFile.length() - dataSegmentSize);
        System.out.println();
        System.out.println(rawHTBuf);
        throw re;
    }
    htBuf = htBuf.order(ByteOrder.nativeOrder());
    // FUTURE - cache the below number or does the segment cache do this well enough? (also cache the segmentCuckooConfig...)
    htBufSize = rawHTBuf.getInt(0);
    // FIXME - verify -1
    segmentCuckooConfig = new WritableCuckooConfig(CuckooConfig.read(rawHTBuf, NumConversion.BYTES_PER_INT), -1);
    htTotalEntries = htBufSize / (NumConversion.BYTES_PER_LONG * 2 + NumConversion.BYTES_PER_INT);
    return new FileSegment(nsDir, segmentNumber, raFile, dataBuf, htBuf, segmentCuckooConfig.newTotalEntries(htTotalEntries), new BufferOffsetListStore(rawHTBuf, nsOptions), dataSegmentSize);
}
Also used : RandomAccessFile(java.io.RandomAccessFile) ByteBuffer(java.nio.ByteBuffer) WritableCuckooConfig(com.ms.silverking.cloud.dht.collection.WritableCuckooConfig)

Example 2 with WritableCuckooConfig

use of com.ms.silverking.cloud.dht.collection.WritableCuckooConfig in project SilverKing by Morgan-Stanley.

the class IntRehashTest method rehashTest.

public static void rehashTest(int size) {
    IntArrayCuckoo map;
    map = new IntArrayCuckoo(new WritableCuckooConfig(totalEntries, numSubTables, entriesPerBucket, cuckooLimit));
    for (int i = 0; i < size; i++) {
        try {
            map.put(new SimpleKey(0, i), i);
        } catch (TableFullException tfe) {
            System.out.println("rehashing");
            map = IntArrayCuckoo.rehashAndAdd(map, new SimpleKey(0, i), i);
        }
    }
}
Also used : TableFullException(com.ms.silverking.cloud.dht.collection.TableFullException) IntArrayCuckoo(com.ms.silverking.cloud.dht.collection.IntArrayCuckoo) WritableCuckooConfig(com.ms.silverking.cloud.dht.collection.WritableCuckooConfig) SimpleKey(com.ms.silverking.cloud.dht.common.SimpleKey)

Aggregations

WritableCuckooConfig (com.ms.silverking.cloud.dht.collection.WritableCuckooConfig)2 IntArrayCuckoo (com.ms.silverking.cloud.dht.collection.IntArrayCuckoo)1 TableFullException (com.ms.silverking.cloud.dht.collection.TableFullException)1 SimpleKey (com.ms.silverking.cloud.dht.common.SimpleKey)1 RandomAccessFile (java.io.RandomAccessFile)1 ByteBuffer (java.nio.ByteBuffer)1