fix for a count issue in snapshot api

This commit is contained in:
Michael Peter Christen 2014-12-16 11:33:30 +01:00
parent 3354cd63be
commit 2362ad7c34
3 changed files with 52 additions and 13 deletions

View File

@ -139,22 +139,24 @@ public class snapshot {
// return a status of the transaction archive // return a status of the transaction archive
String host = post.get("host"); String host = post.get("host");
String depth = post.get("depth"); String depth = post.get("depth");
int depthi = depth == null ? -1 : Integer.parseInt(depth);
for (Transactions.State state: statename == null ? for (Transactions.State state: statename == null ?
new Transactions.State[]{Transactions.State.INVENTORY, Transactions.State.ARCHIVE} : new Transactions.State[]{Transactions.State.INVENTORY, Transactions.State.ARCHIVE} :
new Transactions.State[]{Transactions.State.valueOf(statename)}) { new Transactions.State[]{Transactions.State.valueOf(statename)}) {
if (host == null) { if (host == null) {
JSONObject hostCountInventory = new JSONObject(); JSONObject hostCountInventory = new JSONObject();
for (String h: Transactions.listHosts(state)) { for (String h: Transactions.listHosts(state)) {
hostCountInventory.put(h, Transactions.listIDs(state, h).size()); int size = Transactions.listIDsSize(h, depthi, state);
if (size > 0) hostCountInventory.put(h, size);
} }
result.put("count." + state.name(), hostCountInventory); result.put("count." + state.name(), hostCountInventory);
} else { } else {
TreeMap<Integer, Collection<Revisions>> ids = Transactions.listIDs(state, host); TreeMap<Integer, Collection<Revisions>> ids = Transactions.listIDs(host, depthi, state);
if (ids == null) { if (ids == null) {
result.put("error", "no host " + host + " found"); result.put("result", "fail");
result.put("comment", "no entries for host " + host + " found");
} else { } else {
for (Map.Entry<Integer, Collection<Revisions>> entry: ids.entrySet()) { for (Map.Entry<Integer, Collection<Revisions>> entry: ids.entrySet()) {
if (depth != null && Integer.parseInt(depth) != entry.getKey()) continue;
for (Revisions r: entry.getValue()) { for (Revisions r: entry.getValue()) {
try { try {
JSONObject metadata = new JSONObject(); JSONObject metadata = new JSONObject();

View File

@ -124,7 +124,7 @@ public class Snapshots {
} }
/** /**
* get a list of host names in the snapshot directory * get a list of <host>.<port> names in the snapshot directory
* @return * @return
*/ */
public Set<String> listHosts() { public Set<String> listHosts() {
@ -183,14 +183,16 @@ public class Snapshots {
/** /**
* list the snapshots for a given host name * list the snapshots for a given host name
* @param host the host for the domain * @param hostport the <host>.<port> identifier for the domain
* @param depth restrict the result to the given depth or if depth == -1 do not restrict to a depth
* @return a map with a set for each depth in the domain of the host name * @return a map with a set for each depth in the domain of the host name
*/ */
public TreeMap<Integer, Collection<Revisions>> listIDs(final String hostport) { public TreeMap<Integer, Collection<Revisions>> listIDs(final String hostport, final int depth) {
TreeMap<Integer, Collection<Revisions>> result = new TreeMap<>(); TreeMap<Integer, Collection<Revisions>> result = new TreeMap<>();
TreeMap<Integer, TreeSet<String>> list = directory.get(hostport); TreeMap<Integer, TreeSet<String>> list = directory.get(hostport);
if (list != null) { if (list != null) {
for (Map.Entry<Integer, TreeSet<String>> entry: list.entrySet()) { for (Map.Entry<Integer, TreeSet<String>> entry: list.entrySet()) {
if (depth != -1 && entry.getKey() != depth) continue;
Collection<Revisions> r = new ArrayList<>(entry.getValue().size()); Collection<Revisions> r = new ArrayList<>(entry.getValue().size());
for (String datehash: entry.getValue()) { for (String datehash: entry.getValue()) {
r.add(new Revisions(hostport, entry.getKey(), datehash)); r.add(new Revisions(hostport, entry.getKey(), datehash));
@ -201,6 +203,24 @@ public class Snapshots {
return result; return result;
} }
/**
* get the number of snapshots for the given host name
* @param hostport the <host>.<port> identifier for the domain
* @param depth restrict the result to the given depth or if depth == -1 do not restrict to a depth
* @return a count, the total number of documents for the domain and depth
*/
public int listIDsSize(final String hostport, final int depth) {
int count = 0;
TreeMap<Integer, TreeSet<String>> list = directory.get(hostport);
if (list != null) {
for (Map.Entry<Integer, TreeSet<String>> entry: list.entrySet()) {
if (depth != -1 && entry.getKey() != depth) continue;
count += entry.getValue().size();
}
}
return count;
}
/** /**
* Compute the path of a snapshot. This does not create the snapshot, only gives a path. * Compute the path of a snapshot. This does not create the snapshot, only gives a path.
* Also, the path to the storage location is not created. * Also, the path to the storage location is not created.

View File

@ -105,7 +105,7 @@ public class Transactions {
} }
/** /**
* get a list of host names in the snapshot directory * get a list of <host>.<port> names in the snapshot directory
* @return the list of the given state. if the state is ALL or unknown, all lists are combined * @return the list of the given state. if the state is ALL or unknown, all lists are combined
*/ */
public static Set<String> listHosts(final State state) { public static Set<String> listHosts(final State state) {
@ -118,14 +118,31 @@ public class Transactions {
/** /**
* list the snapshots for a given host name * list the snapshots for a given host name
* @param host the host for the domain * @param hostport the <host>.<port> identifier for the domain
* @param depth restrict the result to the given depth or if depth == -1 do not restrict to a depth
* @param state the wanted transaction state, State.INVENTORY, State.ARCHIVE or State.ANY
* @return a map with a set for each depth in the domain of the host name * @return a map with a set for each depth in the domain of the host name
*/ */
public static TreeMap<Integer, Collection<Revisions>> listIDs(final State state, final String host) { public static TreeMap<Integer, Collection<Revisions>> listIDs(final String hostport, final int depth, final State state) {
switch (state) { switch (state) {
case INVENTORY : return inventory.listIDs(host); case INVENTORY : return inventory.listIDs(hostport, depth);
case ARCHIVE : return archive.listIDs(host); case ARCHIVE : return archive.listIDs(hostport, depth);
default : TreeMap<Integer, Collection<Revisions>> a = inventory.listIDs(host); a.putAll(archive.listIDs(host)); return a; default : TreeMap<Integer, Collection<Revisions>> a = inventory.listIDs(hostport, depth); a.putAll(archive.listIDs(hostport, depth)); return a;
}
}
/**
* get the number of snapshots for the given host name
* @param hostport the <host>.<port> identifier for the domain
* @param depth restrict the result to the given depth or if depth == -1 do not restrict to a depth
* @param state the wanted transaction state, State.INVENTORY, State.ARCHIVE or State.ANY
* @return a count, the total number of documents for the domain and depth
*/
public static int listIDsSize(final String hostport, final int depth, final State state) {
switch (state) {
case INVENTORY : return inventory.listIDsSize(hostport, depth);
case ARCHIVE : return archive.listIDsSize(hostport, depth);
default : return inventory.listIDsSize(hostport, depth) + archive.listIDsSize(hostport, depth);
} }
} }