Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -804,13 +804,16 @@ public List<RepeatedOmKeyInfo> listTrash(String volumeName, String bucketName,
return deletedKeys;
}

/**
* @param userName volume owner, null for listing all volumes.
*/
@Override
public List<OmVolumeArgs> listVolumes(String userName,
String prefix, String startKey, int maxKeys) throws IOException {

if (StringUtil.isBlank(userName)) {
throw new OMException("User name is required to list Volumes.",
ResultCodes.USER_NOT_FOUND);
// null userName represents listing all volumes in cluster.
return listAllVolumes(prefix, startKey, maxKeys);
}

final List<OmVolumeArgs> result = Lists.newArrayList();
Expand Down Expand Up @@ -850,6 +853,44 @@ public List<OmVolumeArgs> listVolumes(String userName,
return result;
}

/**
* @return list of all volumes.
*/
private List<OmVolumeArgs> listAllVolumes(String prefix, String startKey,
int maxKeys) {
List<OmVolumeArgs> result = Lists.newArrayList();

/* volumeTable is full-cache, so we use cacheIterator. */
Iterator<Map.Entry<CacheKey<String>, CacheValue<OmVolumeArgs>>>
cacheIterator = getVolumeTable().cacheIterator();

String volumeName;
OmVolumeArgs omVolumeArgs;
boolean prefixIsEmpty = Strings.isNullOrEmpty(prefix);
boolean startKeyIsEmpty = Strings.isNullOrEmpty(startKey);
while (cacheIterator.hasNext() && result.size() < maxKeys) {
Map.Entry<CacheKey<String>, CacheValue<OmVolumeArgs>> entry =
cacheIterator.next();
omVolumeArgs = entry.getValue().getCacheValue();
volumeName = omVolumeArgs.getVolume();

if (!prefixIsEmpty && !volumeName.startsWith(prefix)) {
continue;
}

if (!startKeyIsEmpty) {
if (volumeName.equals(startKey)) {
startKeyIsEmpty = true;
}
continue;
}

result.add(omVolumeArgs);
}

return result;
}

private UserVolumeInfo getVolumesByUser(String userNameKey)
throws OMException {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,54 @@ public void testListVolumes() throws Exception {
Assert.assertEquals(volumeList.size(), totalVol - startOrder - 1);
}

@Test
public void testListAllVolumes() throws Exception {
OmVolumeArgs.Builder argsBuilder =
OmVolumeArgs.newBuilder().setAdminName("admin");
String volName;
String ownerName;
for (int i = 0; i < 50; i++) {
ownerName = "owner" + i;
volName = "vola" + i;
OmVolumeArgs omVolumeArgs = argsBuilder.
setOwnerName(ownerName).setVolume(volName).build();
TestOMRequestUtils.addVolumeToOM(omMetadataManager, omVolumeArgs);
TestOMRequestUtils.addUserToDB(volName, ownerName, omMetadataManager);
}
for (int i = 0; i < 50; i++) {
ownerName = "owner" + i;
volName = "volb" + i;
OmVolumeArgs omVolumeArgs = argsBuilder.
setOwnerName(ownerName).setVolume(volName).build();
TestOMRequestUtils.addVolumeToOM(omMetadataManager, omVolumeArgs);
TestOMRequestUtils.addUserToDB(volName, ownerName, omMetadataManager);
}

String prefix = "";
String startKey = "";

// Test list all volumes
List<OmVolumeArgs> volListA = omMetadataManager.listVolumes(null,
prefix, startKey, 1000);
Assert.assertEquals(volListA.size(), 100);

// Test list all volumes with prefix
prefix = "volb";
List<OmVolumeArgs> volListB = omMetadataManager.listVolumes(null,
prefix, startKey, 1000);
Assert.assertEquals(volListB.size(), 50);

// Test list all volumes with setting startVolume
// that was not part of result.
prefix = "";
int totalVol = volListB.size();
int startOrder = 0;
startKey = "volb" + startOrder;
List<OmVolumeArgs> volListC = omMetadataManager.listVolumes(null,
prefix, startKey, 1000);
Assert.assertEquals(volListC.size(), totalVol - startOrder - 1);
}

@Test
public void testListBuckets() throws Exception {

Expand Down