Created
July 9, 2020 16:51
-
-
Save ken-tune/77e4297e54d296c49fb3a3140dfc3460 to your computer and use it in GitHub Desktop.
How to store logical objects inside physical Aerospike objects to reduce key space size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static final int BIT_MASK_SIZE = 25; | |
public static final BigInteger BIT_MASK = (new BigInteger("2")).pow(BIT_MASK_SIZE).subtract(new BigInteger("1")); | |
public static final String METADATA_BIN_NAME = "deviceMetaData"; | |
public static final AerospikeClient aeroClient = new AerospikeClient(hostName,aerospikeServicePort); | |
/** | |
* Store device meta data in Aerospike using 'blocking' technique | |
* @param deviceIDasUUID - device id | |
* @param deviceMetaData - metadat | |
*/ | |
public void storeDeviceMetaData(UUID deviceIDasUUID ,Map<String,Object> deviceMetaData){ | |
// Turn UUID into a BigInteger to help with bit masking | |
BigInteger deviceID = new BigInteger(deviceIDasUUID.toString().replace("-",""),32); | |
// Do bit masking | |
BigInteger physicalKey = deviceID.and(BIT_MASK); | |
// Construct Aerospike key using key built using bit masking | |
Key aerospikeKey= new Key(deviceDataNamespace,deviceDataSetName,physicalKey.toString()); | |
// Store using Map API - stores device metadata in an Aerospike object using the bit masked key | |
// but within that object inserts to a map using the device id as a key | |
aeroClient.operate(new WritePolicy(),aerospikeKey, | |
MapOperation.put(new MapPolicy(),METADATA_BIN_NAME,Value.get(deviceIDasUUID.toString()), Value.get(deviceMetaData))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment