Made dictionary prefixes a separated definition
Change-Id: I89f4a182ec49f7420c41f8483110b5faf474984b
diff --git a/lib/Krawfish/Index/Dictionary.pm b/lib/Krawfish/Index/Dictionary.pm
index c6e7d4e..26a9e93 100644
--- a/lib/Krawfish/Index/Dictionary.pm
+++ b/lib/Krawfish/Index/Dictionary.pm
@@ -1,5 +1,6 @@
package Krawfish::Index::Dictionary;
use Krawfish::Index::Dictionary::Collations;
+use Krawfish::Util::Constants qw/:PREFIX/;
use Krawfish::Util::String qw/normalize_nfkc/;
use strict;
use warnings;
@@ -254,7 +255,7 @@
};
# Check if term exists
- my $term_id = $self->term_id_by_term('!' . $term);
+ my $term_id = $self->term_id_by_term(KEY_PREF . $term);
# The term already exists
if ($term_id) {
@@ -282,7 +283,7 @@
};
# The term does not exist yet
- $term_id = $self->add_term('!' . $term);
+ $term_id = $self->add_term(KEY_PREF . $term);
if (DEBUG) {
print_log('dict', "Add new collation $locale to field $term_id");
diff --git a/lib/Krawfish/Koral/Corpus/Field.pm b/lib/Krawfish/Koral/Corpus/Field.pm
index 403f771..e9b8bab 100644
--- a/lib/Krawfish/Koral/Corpus/Field.pm
+++ b/lib/Krawfish/Koral/Corpus/Field.pm
@@ -1,6 +1,7 @@
package Krawfish::Koral::Corpus::Field;
use parent 'Krawfish::Koral::Corpus';
use Krawfish::Koral::Corpus::FieldID;
+use Krawfish::Util::Constants ':PREFIX';
use strict;
use warnings;
@@ -124,7 +125,7 @@
print_log('kq_term', "Translate term $term to term_id") if DEBUG;
- my $term_id = $dict->term_id_by_term('+' . $term);
+ my $term_id = $dict->term_id_by_term(FIELD_PREF . $term);
return $self->builder->nothing unless defined $term_id;
@@ -236,7 +237,7 @@
unless ($self->{value}) {
return $str unless $op eq 'excludes';
- return '!' . $str;
+ return KEY_PREF . $str;
};
if ($op eq 'eq') {
diff --git a/lib/Krawfish/Koral/Document/FieldInt.pm b/lib/Krawfish/Koral/Document/FieldInt.pm
index e15febc..e7ddef9 100644
--- a/lib/Krawfish/Koral/Document/FieldInt.pm
+++ b/lib/Krawfish/Koral/Document/FieldInt.pm
@@ -1,5 +1,6 @@
package Krawfish::Koral::Document::FieldInt;
use Krawfish::Util::String qw/squote/;
+use Krawfish::Util::Constants qw/:PREFIX/;
use Krawfish::Log;
use Role::Tiny::With;
with 'Krawfish::Koral::Document::FieldBase';
@@ -22,7 +23,7 @@
return if $self->{key_id} && $self->{key_value_id};
# Get or introduce new key term_id
- my $key = '!' . $self->{key};
+ my $key = KEY_PREF . $self->{key};
$self->{key_id} = $dict->add_term($key);
if (DEBUG) {
@@ -39,7 +40,7 @@
};
# Get or introduce new key term_id
- my $term = '+' . $self->{key} . ':' . $self->{value};
+ my $term = FIELD_PREF . $self->{key} . ':' . $self->{value};
$self->{key_value_id} = $dict->add_term($term);
return $self;
diff --git a/lib/Krawfish/Koral/Document/FieldStore.pm b/lib/Krawfish/Koral/Document/FieldStore.pm
index a8832d0..8f6c882 100644
--- a/lib/Krawfish/Koral/Document/FieldStore.pm
+++ b/lib/Krawfish/Koral/Document/FieldStore.pm
@@ -1,5 +1,6 @@
package Krawfish::Koral::Document::FieldStore;
use Krawfish::Util::String qw/squote/;
+use Krawfish::Util::Constants ':PREFIX';
use Role::Tiny::With;
with 'Krawfish::Koral::Document::FieldBase';
use warnings;
@@ -19,7 +20,7 @@
sub identify {
my ($self, $dict) = @_;
- my $key = '!' . $self->{key};
+ my $key = KEY_PREF . $self->{key};
# Get or introduce new key term_id
$self->{key_id} = $dict->add_term($key);
diff --git a/lib/Krawfish/Koral/Document/FieldString.pm b/lib/Krawfish/Koral/Document/FieldString.pm
index 68715a6..e19b874 100644
--- a/lib/Krawfish/Koral/Document/FieldString.pm
+++ b/lib/Krawfish/Koral/Document/FieldString.pm
@@ -1,5 +1,6 @@
package Krawfish::Koral::Document::FieldString;
use Krawfish::Util::String qw/squote/;
+use Krawfish::Util::Constants ':PREFIX';
use Role::Tiny::With;
with 'Krawfish::Koral::Document::FieldBase';
use warnings;
@@ -21,7 +22,7 @@
return if $self->{key_id} && $self->{key_value_id};
# Get or introduce new key term_id
- my $key = '!' . $self->{key};
+ my $key = KEY_PREF . $self->{key};
$self->{key_id} = $dict->add_term($key);
@@ -31,7 +32,7 @@
};
# Get or introduce new key term_id
- my $term = '+' . $self->{key} . ':' . $self->{value};
+ my $term = FIELD_PREF . $self->{key} . ':' . $self->{value};
$self->{key_value_id} = $dict->add_term($term);
return $self;
diff --git a/lib/Krawfish/Koral/Meta/Type/Key.pm b/lib/Krawfish/Koral/Meta/Type/Key.pm
index 0728d84..1d1b3fe 100644
--- a/lib/Krawfish/Koral/Meta/Type/Key.pm
+++ b/lib/Krawfish/Koral/Meta/Type/Key.pm
@@ -1,9 +1,13 @@
package Krawfish::Koral::Meta::Type::Key;
use Krawfish::Koral::Meta::Type::KeyID;
+use Krawfish::Util::Constants qw':PREFIX';
use strict;
use warnings;
use Krawfish::Util::String qw/squote/;
+# TODO:
+# Merge this with KeyID and use the inflatable interface
+
sub new {
my $class = shift;
my $term = shift;
@@ -16,7 +20,7 @@
my ($self, $dict) = @_;
# Get term from dictionary
- my $term_id = $dict->term_id_by_term('!' . $$self);
+ my $term_id = $dict->term_id_by_term(KEY_PREF . $$self);
# Term does not exist!
return unless $term_id;
diff --git a/lib/Krawfish/Posting/Aggregate/Fields.pm b/lib/Krawfish/Posting/Aggregate/Fields.pm
index 09bc29c..31f3f07 100644
--- a/lib/Krawfish/Posting/Aggregate/Fields.pm
+++ b/lib/Krawfish/Posting/Aggregate/Fields.pm
@@ -1,5 +1,6 @@
package Krawfish::Posting::Aggregate::Fields;
use Krawfish::Log;
+use Krawfish::Util::Constants qw/:PREFIX/;
use strict;
use warnings;
@@ -105,16 +106,20 @@
# Remove the term marker
# TODO:
# this may be a direct feature of the dictionary instead
- $field_term =~ s/^!//;
+ $field_term = substr($field_term,1); # ~ s/^!//;
my $aggr = ($fields{$field_term} //= {});
# Get facets for field
my $values = $fields->{$field_id};
foreach my $value (keys %$values) {
- # Get the
+ # Get the field term
my $field = $dict->term_by_term_id($value);
- $field =~ s/^\+$field_term://;
+
+ # Remove the first character
+ # TODO:
+ # This may be a direct feature of the dictionary instead
+ $field =~ s/^.$field_term://;
$aggr->{$field} = $values->{$value};
};
diff --git a/lib/Krawfish/Posting/Aggregate/Values.pm b/lib/Krawfish/Posting/Aggregate/Values.pm
index da6a243..fb65966 100644
--- a/lib/Krawfish/Posting/Aggregate/Values.pm
+++ b/lib/Krawfish/Posting/Aggregate/Values.pm
@@ -64,7 +64,8 @@
# Remove the term marker
# TODO:
# this may be a direct feature of the dictionary instead
- $field_term =~ s/^!//;
+ # $field_term =~ s/^!//;
+ $field_term = substr($field_term, 1); # ~ s/^!//;
$fields{$field_term} = $fields->{$field_id};
};
diff --git a/lib/Krawfish/Posting/Group/Fields.pm b/lib/Krawfish/Posting/Group/Fields.pm
index e279018..8a6b8dd 100644
--- a/lib/Krawfish/Posting/Group/Fields.pm
+++ b/lib/Krawfish/Posting/Group/Fields.pm
@@ -80,7 +80,10 @@
foreach my $field_id (@{$field_keys}) {
my $field_term = $dict->term_by_term_id($field_id);
- $field_term =~ s/^!//;
+ # $field_term =~ s/^!//;
+ # TODO:
+ # This may be a direct feature of the dictionary
+ $field_term = substr($field_term, 1);
push @{$self->{field_terms}}, $field_term;
};
@@ -97,7 +100,10 @@
# Retrieve term
my $term = $dict->term_by_term_id($term_id);
my $field_term = $self->{field_terms}->[$i];
- $term =~ s/^\+$field_term://;
+
+ # TODO:
+ # This may be a direct feature of the dictionary
+ $term =~ s/^.$field_term://;
push @group, $term;
}
diff --git a/lib/Krawfish/Util/Constants.pm b/lib/Krawfish/Util/Constants.pm
new file mode 100644
index 0000000..132d383
--- /dev/null
+++ b/lib/Krawfish/Util/Constants.pm
@@ -0,0 +1,17 @@
+package Krawfish::Util::Constants;
+use strict;
+use warnings;
+use Exporter 'import';
+
+use constant {
+ KEY_PREF => '=', # ! Field keys
+ FIELD_PREF => '/' # + Field values
+};
+
+our @EXPORT_OK = (qw/KEY_PREF FIELD_PREF/);
+
+our %EXPORT_TAGS = (
+ PREFIX => [qw/KEY_PREF FIELD_PREF/]
+);
+
+1;