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
* @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.

View File

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

View File

@ -100,7 +100,7 @@ public KsmKeyInfo allocateKey(KsmKeyArgs args) throws IOException {
byte[] volumeKey = metadataManager.getVolumeKey(volumeName);
byte[] bucketKey = metadataManager.getBucketKey(volumeName, bucketName);
byte[] keyKey =
metadataManager.getDBKeyForKey(volumeName, bucketName, keyName);
metadataManager.getDBKeyBytes(volumeName, bucketName, keyName);
//Check if the volume exists
if (metadataManager.get(volumeKey) == null) {
@ -188,7 +188,7 @@ public KsmKeyInfo lookupKey(KsmKeyArgs args) throws IOException {
String bucketName = args.getBucketName();
String keyName = args.getKeyName();
try {
byte[] keyKey = metadataManager.getDBKeyForKey(
byte[] keyKey = metadataManager.getDBKeyBytes(
volumeName, bucketName, keyName);
byte[] value = metadataManager.get(keyKey);
if (value == null) {
@ -216,7 +216,7 @@ public void deleteKey(KsmKeyArgs args) throws IOException {
String bucketName = args.getBucketName();
String keyName = args.getKeyName();
try {
byte[] objectKey = metadataManager.getDBKeyForKey(
byte[] objectKey = metadataManager.getDBKeyBytes(
volumeName, bucketName, keyName);
byte[] objectValue = metadataManager.get(objectKey);
if (objectValue == null) {