Clean up criterion code

Change-Id: I74a645f6fd45e93a0fa9aab9c046934c8f1d3ed0
diff --git a/lib/Krawfish/Compile/Segment/Enrich/SortCriterion.pm b/lib/Krawfish/Compile/Segment/Enrich/SortCriterion.pm
deleted file mode 100644
index 7f2e08d..0000000
--- a/lib/Krawfish/Compile/Segment/Enrich/SortCriterion.pm
+++ /dev/null
@@ -1,75 +0,0 @@
-package Krawfish::Compile::Segment::Enrich::SortCriterion;
-use warnings;
-use strict;
-use Role::Tiny;
-
-with 'Krawfish::Compile';
-
-# Enrich an item with sort criteria.
-# This is necessary to sort items beyond the segment.
-# The problem with this enrichment is,
-# that it needs to augment the sorted items after sorting,
-# so they are not in a proper order to go through
-# the fields lists (for example) to collect the field values
-# or through the forward index to collect term_ids (though
-# this may be a different API).
-#
-# A proper way to do this would be to go through the sorted
-# lists and create a new sorted list in doc order (or to somehow
-# keep match order) to make it possible to enrich with all
-# sorting criteria.
-#
-# 1. For Fields: Create a list of all docs to enrich in doc_id order
-#    (Ignore duplicates)
-# 2. Prepare all requested fields in field order
-# 3. Go through all fields and collect values or term_ids
-# 4. Create criterion vectors per match based on these information
-#
-# But:
-# It may very well be possible to only enrich if required
-# on the node level.
-#
-# On the node level, the relevant criteria (top_k) will be inflated,
-# taken the ordering into account (which means following matches may
-# have a lot of criteria in common.
-
-
-sub new {
-  my $class = shift;
-  bless {
-    query => shift,
-
-    # Store all criteria in sorted order,
-    # which may include terms and fields.
-    # This will also keep the direction
-    # and possibly the collation.
-    criteria => shift
-  }, $class
-};
-
-sub _init {
-  my $self = shift;
-
-  return if $self->{init}++;
-
-  # TODO:
-  #   Go through all criteria and collect required field IDs.
-  #   Bring required field IDs in order.
-  #   Create an array for field_id => criterion_position to
-  #   map the surface term to the criterion after fetching.
-  #   Remember the criterion position for optional term sorting.
-};
-
-
-sub current_match {
-  # TODO:
-  #   Create an empty list for sorting criteria.
-  #   a) Retrieve for the document id all the relevant fields
-  #      if there are fields to retrieve.
-  #      Add in the position of the criteria list.
-  #   b) The surface term is already retrieved and enriched.
-  #      Add in the position of the criteria list.
-};
-
-
-1;
diff --git a/lib/Krawfish/Compile/Segment/Sort/Criterion.pm b/lib/Krawfish/Compile/Segment/Sort/Criterion.pm
index 33efdc5..52f3f04 100644
--- a/lib/Krawfish/Compile/Segment/Sort/Criterion.pm
+++ b/lib/Krawfish/Compile/Segment/Sort/Criterion.pm
@@ -7,16 +7,19 @@
 
 use constant DEBUG => 0;
 
-# TODO:
-#   On the segment level, it's enough to compare on the ranks,
-#   but it's also necessary to enrich with the fields
-#   to have the necessary enrichment when moving to the cluster
-#   (at least having the collation comparation key).
-#   To make this work in multivalued fields, the fields
-#   would
+# Enrich an item with sort criteria.
+# This is necessary to sort items beyond the segment.
+# The problem with this enrichment is,
+# that it needs to augment the sorted items after sorting,
+# so they are not in a proper order to go through
+# the fields lists (for example) to collect the field values
+# or through the forward index to collect term_ids (though
+# this may be a different API).
 #
-#     a) need to be sorted in alphabetic or numeric order
-#     b) the ranking sorted field is indexed
+# The current solution expects an indexed sorting list
+# for the ranks, so the rank of the level is retrieved
+# and based on that, the sorted field list returns
+# the key.
 
 # TODO:
 #   This may very well be in Krawfish::Compile::Enrich::SortCriterion;
@@ -37,18 +40,27 @@
     );
   };
 
+  # Current match is already defined
   return $self->{match} if $self->{match};
 
+  # There is no current posting defined
   unless ($self->{current}) {
     warn 'No current defined!';
     return;
   };
 
-  my $match = $self->match_from_posting($self->{current});
+  # Get match from current posting
+  my $match = $self->match_from_posting(
+    $self->{current}
+  );
 
+  # No match constructible by posting
   unless ($match) {
     if (DEBUG) {
-      print_log('c_s_crit', 'No match found requested by ' . ref($self));
+      print_log(
+        'c_s_crit',
+        'No match found requested by ' . ref($self)
+      );
     };
     return;
   };
@@ -61,16 +73,18 @@
       );
   };
 
-  # Add criteria
+  # Add criteria to match
   $self->add_criteria_to($match);
 
+  # Set current match
   $self->{match} = $match;
 
   return $match;
 };
 
 
-# Add criteria
+# Add criteria to match
+# This will go down all levels of the query
 sub add_criteria_to {
   my ($self, $match) = @_;
 
@@ -175,7 +189,9 @@
   };
 
   # Add criteria for deeper levels
-  if (Role::Tiny::does_role($self->{query}, 'Krawfish::Compile::Segment::Sort::Criterion')) {
+  if (Role::Tiny::does_role(
+    $self->{query},
+    'Krawfish::Compile::Segment::Sort::Criterion')) {
     $self->{query}->add_criteria_to($match);
   };
 };