diff --git a/htroot/api/snapshot.java b/htroot/api/snapshot.java index f347aa4ad..c86ab9e7b 100644 --- a/htroot/api/snapshot.java +++ b/htroot/api/snapshot.java @@ -139,22 +139,24 @@ public class snapshot { // return a status of the transaction archive String host = post.get("host"); String depth = post.get("depth"); + int depthi = depth == null ? -1 : Integer.parseInt(depth); for (Transactions.State state: statename == null ? new Transactions.State[]{Transactions.State.INVENTORY, Transactions.State.ARCHIVE} : new Transactions.State[]{Transactions.State.valueOf(statename)}) { if (host == null) { JSONObject hostCountInventory = new JSONObject(); 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); } else { - TreeMap> ids = Transactions.listIDs(state, host); + TreeMap> ids = Transactions.listIDs(host, depthi, state); if (ids == null) { - result.put("error", "no host " + host + " found"); + result.put("result", "fail"); + result.put("comment", "no entries for host " + host + " found"); } else { for (Map.Entry> entry: ids.entrySet()) { - if (depth != null && Integer.parseInt(depth) != entry.getKey()) continue; for (Revisions r: entry.getValue()) { try { JSONObject metadata = new JSONObject(); diff --git a/source/net/yacy/crawler/data/Snapshots.java b/source/net/yacy/crawler/data/Snapshots.java index f2ba55c0a..40e5fce30 100644 --- a/source/net/yacy/crawler/data/Snapshots.java +++ b/source/net/yacy/crawler/data/Snapshots.java @@ -124,7 +124,7 @@ public class Snapshots { } /** - * get a list of host names in the snapshot directory + * get a list of . names in the snapshot directory * @return */ public Set listHosts() { @@ -183,14 +183,16 @@ public class Snapshots { /** * list the snapshots for a given host name - * @param host the host for the domain + * @param hostport the . 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 */ - public TreeMap> listIDs(final String hostport) { + public TreeMap> listIDs(final String hostport, final int depth) { TreeMap> result = new TreeMap<>(); TreeMap> list = directory.get(hostport); if (list != null) { for (Map.Entry> entry: list.entrySet()) { + if (depth != -1 && entry.getKey() != depth) continue; Collection r = new ArrayList<>(entry.getValue().size()); for (String datehash: entry.getValue()) { r.add(new Revisions(hostport, entry.getKey(), datehash)); @@ -200,6 +202,24 @@ public class Snapshots { } return result; } + + /** + * get the number of snapshots for the given host name + * @param hostport the . 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> list = directory.get(hostport); + if (list != null) { + for (Map.Entry> 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. diff --git a/source/net/yacy/crawler/data/Transactions.java b/source/net/yacy/crawler/data/Transactions.java index 8ec57f4c2..2ebcd77d4 100644 --- a/source/net/yacy/crawler/data/Transactions.java +++ b/source/net/yacy/crawler/data/Transactions.java @@ -105,7 +105,7 @@ public class Transactions { } /** - * get a list of host names in the snapshot directory + * get a list of . names in the snapshot directory * @return the list of the given state. if the state is ALL or unknown, all lists are combined */ public static Set listHosts(final State state) { @@ -118,14 +118,31 @@ public class Transactions { /** * list the snapshots for a given host name - * @param host the host for the domain + * @param hostport the . 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 */ - public static TreeMap> listIDs(final State state, final String host) { + public static TreeMap> listIDs(final String hostport, final int depth, final State state) { + switch (state) { + case INVENTORY : return inventory.listIDs(hostport, depth); + case ARCHIVE : return archive.listIDs(hostport, depth); + default : TreeMap> 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 . 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.listIDs(host); - case ARCHIVE : return archive.listIDs(host); - default : TreeMap> a = inventory.listIDs(host); a.putAll(archive.listIDs(host)); return a; + case INVENTORY : return inventory.listIDsSize(hostport, depth); + case ARCHIVE : return archive.listIDsSize(hostport, depth); + default : return inventory.listIDsSize(hostport, depth) + archive.listIDsSize(hostport, depth); } }