Search in sources :

Example 6 with SimpleKey

use of com.ms.silverking.cloud.dht.common.SimpleKey in project SilverKing by Morgan-Stanley.

the class DataSegmentWalker method next.

/**
 * Position at next entry. Initial call
 * @return if there is an additional entry
 */
@Override
public DataSegmentWalkEntry next() {
    long msl;
    long lsl;
    int storedLength;
    int uncompressedLength;
    int compressedLength;
    long nextMSL;
    long nextLSL;
    if (!hasNext) {
        return null;
    } else {
        DHTKey curKey;
        int curOffset;
        long version;
        long creationTime;
        // double  keyEntropy;
        byte storageState;
        ValueCreator creator;
        ByteBuffer entry;
        int nextEntryPostKeyPosition;
        if (debug) {
            System.out.println("position: " + position);
        }
        msl = dataSegment.getLong(position);
        lsl = dataSegment.getLong(position + NumConversion.BYTES_PER_LONG);
        curOffset = position;
        position += DHTKey.BYTES_PER_KEY;
        curKey = new SimpleKey(msl, lsl);
        // FUTURE - probably delete the below code, but could consider some
        // additional sanity checking
        /*
            keyEntropy = KeyUtil.keyEntropy(curKey);            
            if (keyEntropy < minValidKeyEntropy) {
                boolean sane;
                
                Log.warning("Invalid key: ", curKey +" "+ position);
                sane = false;
                // FUTURE - need more sophisticated validation of entry
                // FUTURE - we need to decode the entry
                while (!sane) {
                    if (position > dataSegment.limit() - DHTKey.BYTES_PER_KEY) { // FUTURE - crude limit; refine
                        Log.warning("Ran off the end of the segment searching for a valid key");
                        return null;
                    }
                    position++;
                    msl = dataSegment.getLong(position);
                    lsl = dataSegment.getLong(position + NumConversion.BYTES_PER_LONG);
                    curKey = new SimpleKey(msl, lsl);
                    keyEntropy = KeyUtil.keyEntropy(curKey);

                    if (keyEntropy >= minValidKeyEntropy) {
                        try {
                            storedLength = MetaDataUtil.getStoredLength(dataSegment, position);
                            if (storedLength > 0) {
                                uncompressedLength = MetaDataUtil.getUncompressedLength(dataSegment, position);
                                if (uncompressedLength >= 0) {
                                    compressedLength = MetaDataUtil.getCompressedLength(dataSegment, position);
                                    if (compressedLength >= 0) {
                                        if (position + storedLength < dataSegment.limit() 
                                                && uncompressedLength < compressedLength) {
                                            sane = true;
                                        }
                                    }
                                }
                            }
                        } catch (Exception e) {
                            // couldn't decode entry, move to next
                        }
                    }
                }
            }
            
            if (debug) {
                System.out.printf("%x:%x %3.2f\n", msl, lsl, keyEntropy);
            }
            */
        storedLength = MetaDataUtil.getStoredLength(dataSegment, position);
        uncompressedLength = MetaDataUtil.getUncompressedLength(dataSegment, position);
        compressedLength = MetaDataUtil.getCompressedLength(dataSegment, position);
        version = MetaDataUtil.getVersion(dataSegment, position);
        creationTime = MetaDataUtil.getCreationTime(dataSegment, position);
        storageState = MetaDataUtil.getStorageState(dataSegment, position);
        creator = MetaDataUtil.getCreator(dataSegment, position);
        if (debug) {
            System.out.println(storedLength);
        }
        nextEntryPostKeyPosition = position + storedLength + DHTKey.BYTES_PER_KEY;
        // Check to see if it's possible that there is another entry
        if (nextEntryPostKeyPosition + MetaDataUtil.getMinimumEntrySize() < dataSegment.limit()) {
            int nextEntryStoredLength;
            // Check to see if the potential next entry actually fits in this segment
            nextEntryStoredLength = MetaDataUtil.getStoredLength(dataSegment, nextEntryPostKeyPosition);
            if (debug) {
                System.out.printf("nextEntryPostKeyPosition %d nextEntryStoredLength %d dataSegment.limit() %d\n", nextEntryPostKeyPosition, nextEntryStoredLength, dataSegment.limit());
            }
            if (nextEntryPostKeyPosition + nextEntryStoredLength < dataSegment.limit()) {
                nextMSL = dataSegment.getLong(nextEntryPostKeyPosition);
                nextLSL = dataSegment.getLong(nextEntryPostKeyPosition + NumConversion.BYTES_PER_LONG);
                hasNext = !(nextMSL == 0 && nextLSL == 0);
                if (debug) {
                    System.out.printf("a: hastNext %s\n", hasNext);
                }
            } else {
                hasNext = false;
                if (debug) {
                    System.out.printf("b: hastNext %s\n", hasNext);
                }
            }
        } else {
            // No room for valid next entry
            hasNext = false;
            if (debug) {
                System.out.printf("c: hastNext %s\n", hasNext);
            }
        }
        entry = (ByteBuffer) ((ByteBuffer) dataSegment.duplicate().position(position)).slice().limit(storedLength);
        position += storedLength;
        return new DataSegmentWalkEntry(curKey, version, curOffset, storedLength, uncompressedLength, compressedLength, DHTKey.BYTES_PER_KEY, entry, creationTime, creator, storageState);
    }
}
Also used : DHTKey(com.ms.silverking.cloud.dht.common.DHTKey) ByteBuffer(java.nio.ByteBuffer) ValueCreator(com.ms.silverking.cloud.dht.ValueCreator) SimpleKey(com.ms.silverking.cloud.dht.common.SimpleKey)

Example 7 with SimpleKey

use of com.ms.silverking.cloud.dht.common.SimpleKey in project SilverKing by Morgan-Stanley.

the class MD5KeyDigest method computeKey.

@Override
public DHTKey computeKey(byte[] bytes) {
    MessageDigest md;
    md = MD5Digest.getLocalMessageDigest();
    md.update(bytes, 0, bytes.length);
    return new SimpleKey(md.digest());
}
Also used : MessageDigest(java.security.MessageDigest) SimpleKey(com.ms.silverking.cloud.dht.common.SimpleKey)

Example 8 with SimpleKey

use of com.ms.silverking.cloud.dht.common.SimpleKey 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

SimpleKey (com.ms.silverking.cloud.dht.common.SimpleKey)8 DHTKey (com.ms.silverking.cloud.dht.common.DHTKey)4 ByteBuffer (java.nio.ByteBuffer)3 MessageDigest (java.security.MessageDigest)2 ValueCreator (com.ms.silverking.cloud.dht.ValueCreator)1 Compression (com.ms.silverking.cloud.dht.client.Compression)1 SerializedKeyValueBuffers (com.ms.silverking.cloud.dht.client.serialization.internal.SerializedKeyValueBuffers)1 IntArrayCuckoo (com.ms.silverking.cloud.dht.collection.IntArrayCuckoo)1 TableFullException (com.ms.silverking.cloud.dht.collection.TableFullException)1 WritableCuckooConfig (com.ms.silverking.cloud.dht.collection.WritableCuckooConfig)1 OpResult (com.ms.silverking.cloud.dht.common.OpResult)1 MessageGroupKeyOrdinalEntry (com.ms.silverking.cloud.dht.net.MessageGroupKeyOrdinalEntry)1 Compressor (com.ms.silverking.compression.Compressor)1 SimpleStopwatch (com.ms.silverking.time.SimpleStopwatch)1 Stopwatch (com.ms.silverking.time.Stopwatch)1 IOException (java.io.IOException)1 WeakReference (java.lang.ref.WeakReference)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1