HDFS-12539. Ozone: refactor some functions in KSMMetadataManagerImpl to be more readable and reusable. Contributed by Yuanbo Liu.

This commit is contained in:
Anu Engineer 2017-10-03 15:06:51 -07:00 committed by Owen O'Malley
parent ceec14bff5
commit c52019c291
3 changed files with 19 additions and 17 deletions

View File

@ -114,7 +114,7 @@ public interface KSMMetadataManager {
* @param key - key name * @param key - key name
* @return bytes of DB key. * @return bytes of DB key.
*/ */
byte[] getDBKeyForKey(String volume, String bucket, String key); byte[] getDBKeyBytes(String volume, String bucket, String key);
/** /**
* Returns the DB key name of a deleted key in KSM metadata store. * Returns the DB key name of a deleted key in KSM metadata store.

View File

@ -137,7 +137,12 @@ public class KSMMetadataManagerImpl implements KSMMetadataManager {
return DFSUtil.string2Bytes(bucketKeyString); return DFSUtil.string2Bytes(bucketKeyString);
} }
private String getBucketKeyPrefix(String volume, String bucket) { /**
* @param volume
* @param bucket
* @return
*/
private String getBucketWithDBPrefix(String volume, String bucket) {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
sb.append(OzoneConsts.KSM_VOLUME_PREFIX) sb.append(OzoneConsts.KSM_VOLUME_PREFIX)
.append(volume) .append(volume)
@ -148,7 +153,7 @@ public class KSMMetadataManagerImpl implements KSMMetadataManager {
return sb.toString(); return sb.toString();
} }
private String getKeyKeyPrefix(String volume, String bucket, String key) { private String getKeyWithDBPrefix(String volume, String bucket, String key) {
String keyVB = OzoneConsts.KSM_KEY_PREFIX + volume String keyVB = OzoneConsts.KSM_KEY_PREFIX + volume
+ OzoneConsts.KSM_KEY_PREFIX + bucket + OzoneConsts.KSM_KEY_PREFIX + bucket
+ OzoneConsts.KSM_KEY_PREFIX; + OzoneConsts.KSM_KEY_PREFIX;
@ -156,11 +161,8 @@ public class KSMMetadataManagerImpl implements KSMMetadataManager {
} }
@Override @Override
public byte[] getDBKeyForKey(String volume, String bucket, String key) { public byte[] getDBKeyBytes(String volume, String bucket, String key) {
String keyKeyString = OzoneConsts.KSM_KEY_PREFIX + volume return DFSUtil.string2Bytes(getKeyWithDBPrefix(volume, bucket, key));
+ OzoneConsts.KSM_KEY_PREFIX + bucket + OzoneConsts.KSM_KEY_PREFIX
+ key;
return DFSUtil.string2Bytes(keyKeyString);
} }
@Override @Override
@ -247,8 +249,7 @@ public class KSMMetadataManagerImpl implements KSMMetadataManager {
*/ */
public boolean isBucketEmpty(String volume, String bucket) public boolean isBucketEmpty(String volume, String bucket)
throws IOException { throws IOException {
String keyRootName = OzoneConsts.KSM_KEY_PREFIX + volume String keyRootName = getKeyWithDBPrefix(volume, bucket, null);
+ OzoneConsts.KSM_KEY_PREFIX + bucket + OzoneConsts.KSM_KEY_PREFIX;
byte[] keyRoot = DFSUtil.string2Bytes(keyRootName); byte[] keyRoot = DFSUtil.string2Bytes(keyRootName);
ImmutablePair<byte[], byte[]> firstKey = store.peekAround(0, keyRoot); ImmutablePair<byte[], byte[]> firstKey = store.peekAround(0, keyRoot);
if (firstKey != null) { if (firstKey != null) {
@ -281,7 +282,8 @@ public class KSMMetadataManagerImpl implements KSMMetadataManager {
// A bucket starts with /#volume/#bucket_prefix // A bucket starts with /#volume/#bucket_prefix
MetadataKeyFilter filter = (preKey, currentKey, nextKey) -> { MetadataKeyFilter filter = (preKey, currentKey, nextKey) -> {
if (currentKey != null) { if (currentKey != null) {
String bucketNamePrefix = getBucketKeyPrefix(volumeName, bucketPrefix); String bucketNamePrefix =
getBucketWithDBPrefix(volumeName, bucketPrefix);
String bucket = DFSUtil.bytes2String(currentKey); String bucket = DFSUtil.bytes2String(currentKey);
return bucket.startsWith(bucketNamePrefix); return bucket.startsWith(bucketNamePrefix);
} }
@ -329,15 +331,15 @@ public class KSMMetadataManagerImpl implements KSMMetadataManager {
ResultCodes.FAILED_BUCKET_NOT_FOUND); ResultCodes.FAILED_BUCKET_NOT_FOUND);
} }
MetadataKeyFilter filter = MetadataKeyFilter filter = new KeyPrefixFilter(
new KeyPrefixFilter(getKeyKeyPrefix(volumeName, bucketName, keyPrefix)); getKeyWithDBPrefix(volumeName, bucketName, keyPrefix));
List<Map.Entry<byte[], byte[]>> rangeResult; List<Map.Entry<byte[], byte[]>> rangeResult;
if (!Strings.isNullOrEmpty(startKey)) { if (!Strings.isNullOrEmpty(startKey)) {
//Since we are excluding start key from the result, //Since we are excluding start key from the result,
// the maxNumOfBuckets is incremented. // the maxNumOfBuckets is incremented.
rangeResult = store.getRangeKVs( rangeResult = store.getRangeKVs(
getDBKeyForKey(volumeName, bucketName, startKey), getDBKeyBytes(volumeName, bucketName, startKey),
maxKeys + 1, filter); maxKeys + 1, filter);
//Remove start key from result. //Remove start key from result.
rangeResult.remove(0); rangeResult.remove(0);

View File

@ -100,7 +100,7 @@ public class KeyManagerImpl implements KeyManager {
byte[] volumeKey = metadataManager.getVolumeKey(volumeName); byte[] volumeKey = metadataManager.getVolumeKey(volumeName);
byte[] bucketKey = metadataManager.getBucketKey(volumeName, bucketName); byte[] bucketKey = metadataManager.getBucketKey(volumeName, bucketName);
byte[] keyKey = byte[] keyKey =
metadataManager.getDBKeyForKey(volumeName, bucketName, keyName); metadataManager.getDBKeyBytes(volumeName, bucketName, keyName);
//Check if the volume exists //Check if the volume exists
if (metadataManager.get(volumeKey) == null) { if (metadataManager.get(volumeKey) == null) {
@ -188,7 +188,7 @@ public class KeyManagerImpl implements KeyManager {
String bucketName = args.getBucketName(); String bucketName = args.getBucketName();
String keyName = args.getKeyName(); String keyName = args.getKeyName();
try { try {
byte[] keyKey = metadataManager.getDBKeyForKey( byte[] keyKey = metadataManager.getDBKeyBytes(
volumeName, bucketName, keyName); volumeName, bucketName, keyName);
byte[] value = metadataManager.get(keyKey); byte[] value = metadataManager.get(keyKey);
if (value == null) { if (value == null) {
@ -216,7 +216,7 @@ public class KeyManagerImpl implements KeyManager {
String bucketName = args.getBucketName(); String bucketName = args.getBucketName();
String keyName = args.getKeyName(); String keyName = args.getKeyName();
try { try {
byte[] objectKey = metadataManager.getDBKeyForKey( byte[] objectKey = metadataManager.getDBKeyBytes(
volumeName, bucketName, keyName); volumeName, bucketName, keyName);
byte[] objectValue = metadataManager.get(objectKey); byte[] objectValue = metadataManager.get(objectKey);
if (objectValue == null) { if (objectValue == null) {