2010-06-20 08:15:10 +02:00
|
|
|
# File: DualIndexHashObject.pm
|
|
|
|
# Author: pragma_
|
|
|
|
#
|
|
|
|
# Purpose: Provides a hash-table object with an abstracted API that includes
|
|
|
|
# setting and deleting values, saving to and loading from files, etc.
|
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2010-06-20 08:15:10 +02:00
|
|
|
package PBot::DualIndexHashObject;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
use Text::Levenshtein qw(fastdistance);
|
|
|
|
use Carp ();
|
|
|
|
|
|
|
|
sub new {
|
|
|
|
if(ref($_[1]) eq 'HASH') {
|
|
|
|
Carp::croak("Options to DualIndexHashObject should be key/value pairs, not hash reference");
|
|
|
|
}
|
|
|
|
|
|
|
|
my ($class, %conf) = @_;
|
|
|
|
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
$self->initialize(%conf);
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
$self->{name} = delete $conf{name} // 'Dual Index hash object';
|
|
|
|
$self->{filename} = delete $conf{filename} // Carp::carp("Missing filename to DualIndexHashObject, will not be able to save to or load from file.");
|
|
|
|
$self->{ignore_duplicates} = delete $conf{ignore_duplicates} // 0;
|
2010-06-20 08:15:10 +02:00
|
|
|
$self->{hash} = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub load_hash_add {
|
|
|
|
my ($self, $primary_index_key, $secondary_index_key, $hash, $i, $filename) = @_;
|
|
|
|
|
|
|
|
if(defined $hash) {
|
2014-05-17 22:08:19 +02:00
|
|
|
if(not $self->{ignore_duplicates} and exists $self->hash->{$primary_index_key}->{$secondary_index_key}) {
|
2010-06-20 08:15:10 +02:00
|
|
|
if($i) {
|
|
|
|
Carp::croak "Duplicate secondary_index_key '$secondary_index_key' found in $filename around line $i\n";
|
|
|
|
} else {
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach my $key (keys %$hash) {
|
|
|
|
$self->hash->{$primary_index_key}->{$secondary_index_key}->{$key} = $hash->{$key};
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub load {
|
2014-05-17 22:08:19 +02:00
|
|
|
my ($self, $filename) = @_;
|
2010-06-20 08:15:10 +02:00
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
$filename = $self->filename if not defined $filename;
|
2010-06-20 08:15:10 +02:00
|
|
|
|
|
|
|
if(not defined $filename) {
|
|
|
|
Carp::carp "No $self->{name} filename specified -- skipping loading from file";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(not open(FILE, "< $filename")) {
|
|
|
|
Carp::carp "Skipping loading from file: Couldn't open $filename: $!\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
my ($primary_index_key, $secondary_index_key, $i, $hash);
|
|
|
|
$hash = {};
|
|
|
|
|
|
|
|
foreach my $line (<FILE>) {
|
|
|
|
$i++;
|
|
|
|
|
|
|
|
$line =~ s/^\s+//;
|
|
|
|
$line =~ s/\s+$//;
|
|
|
|
|
|
|
|
if($line =~ /^\[(.*)\]$/) {
|
|
|
|
$primary_index_key = $1;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($line =~ /^<(.*)>$/) {
|
|
|
|
$secondary_index_key = $1;
|
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
if(not $self->{ignore_duplicates} and exists $self->hash->{$primary_index_key}->{$secondary_index_key}) {
|
2010-06-20 08:15:10 +02:00
|
|
|
Carp::croak "Duplicate secondary_index_key '$secondary_index_key' at line $i of $filename\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($line eq '') {
|
|
|
|
# store the old hash
|
|
|
|
$self->load_hash_add($primary_index_key, $secondary_index_key, $hash, $i, $filename);
|
|
|
|
|
|
|
|
# start a new hash
|
|
|
|
$hash = {};
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
my ($key, $value) = split /:/, $line, 2;
|
|
|
|
|
|
|
|
$key =~ s/^\s+//;
|
|
|
|
$key =~ s/\s+$//;
|
|
|
|
$value =~ s/^\s+//;
|
|
|
|
$value =~ s/\s+$//;
|
|
|
|
|
|
|
|
if(not length $key or not length $value) {
|
|
|
|
Carp::croak "Missing key or value at line $i of $filename\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$hash->{$key} = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(FILE);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub save {
|
|
|
|
my $self = shift;
|
|
|
|
my $filename;
|
|
|
|
|
|
|
|
if(@_) { $filename = shift; } else { $filename = $self->filename; }
|
|
|
|
|
|
|
|
if(not defined $filename) {
|
|
|
|
Carp::carp "No $self->{name} filename specified -- skipping saving to file.\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
open(FILE, "> $filename") or die "Couldn't open $filename: $!\n";
|
|
|
|
|
|
|
|
foreach my $primary_index_key (sort keys %{ $self->hash }) {
|
|
|
|
print FILE "[$primary_index_key]\n";
|
|
|
|
|
|
|
|
foreach my $secondary_index_key (sort keys %{ $self->hash->{$primary_index_key} }) {
|
|
|
|
print FILE "<$secondary_index_key>\n";
|
|
|
|
|
|
|
|
foreach my $key (sort keys %{ $self->hash->{$primary_index_key}->{$secondary_index_key} }) {
|
|
|
|
print FILE "$key: " . $self->hash->{$primary_index_key}->{$secondary_index_key}{$key} . "\n";
|
|
|
|
}
|
|
|
|
print FILE "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close FILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub find_index {
|
2011-01-29 02:21:17 +01:00
|
|
|
my $self = shift;
|
|
|
|
my ($primary_index_key, $secondary_index_key) = map {lc} @_;
|
2010-06-20 08:15:10 +02:00
|
|
|
|
2011-01-29 02:21:17 +01:00
|
|
|
return undef if not defined $primary_index_key;
|
2010-06-20 08:15:10 +02:00
|
|
|
|
2011-01-29 02:21:17 +01:00
|
|
|
return undef if not exists $self->hash->{$primary_index_key};
|
2010-06-20 08:15:10 +02:00
|
|
|
|
2015-12-14 07:08:17 +01:00
|
|
|
return $primary_index_key if not defined $secondary_index_key;
|
|
|
|
|
2011-01-29 02:21:17 +01:00
|
|
|
foreach my $index (keys %{ $self->hash->{$primary_index_key} }) {
|
|
|
|
return $index if $secondary_index_key eq lc $index;
|
2010-06-20 08:15:10 +02:00
|
|
|
}
|
|
|
|
|
2011-01-29 02:21:17 +01:00
|
|
|
return undef;
|
2010-06-20 08:15:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub levenshtein_matches {
|
2011-01-29 02:21:17 +01:00
|
|
|
my ($self, $primary_index_key, $secondary_index_key, $distance) = @_;
|
2010-06-20 08:15:10 +02:00
|
|
|
my $comma = '';
|
|
|
|
my $result = "";
|
|
|
|
|
2011-01-29 02:21:17 +01:00
|
|
|
$distance = 0.60 if not defined $distance;
|
|
|
|
|
2010-06-20 08:15:10 +02:00
|
|
|
$primary_index_key = '.*' if not defined $primary_index_key;
|
|
|
|
|
|
|
|
if(not $secondary_index_key) {
|
|
|
|
foreach my $index (sort keys %{ $self->hash }) {
|
2011-01-29 02:21:17 +01:00
|
|
|
my $distance_result = fastdistance($primary_index_key, $index);
|
2010-06-20 08:15:10 +02:00
|
|
|
my $length = (length($primary_index_key) > length($index)) ? length $primary_index_key : length $index;
|
|
|
|
|
2011-01-29 02:21:17 +01:00
|
|
|
if($distance_result / $length < $distance) {
|
2010-06-20 08:15:10 +02:00
|
|
|
$result .= $comma . $index;
|
|
|
|
$comma = ", ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
my $primary = $self->find_index($primary_index_key);
|
|
|
|
|
|
|
|
if(not $primary) {
|
|
|
|
return 'none';
|
|
|
|
}
|
|
|
|
|
2011-01-29 02:21:17 +01:00
|
|
|
my $last_header = "";
|
|
|
|
my $header = "";
|
2010-06-20 08:15:10 +02:00
|
|
|
|
2011-01-29 02:21:17 +01:00
|
|
|
foreach my $index1 (sort keys %{ $self->hash }) {
|
|
|
|
$header = "[$index1] ";
|
|
|
|
$header = "[global channel] " if $header eq "[.*] ";
|
|
|
|
|
|
|
|
foreach my $index2 (sort keys %{ $self->hash->{$index1} }) {
|
|
|
|
my $distance_result = fastdistance($secondary_index_key, $index2);
|
|
|
|
my $length = (length($secondary_index_key) > length($index2)) ? length $secondary_index_key : length $index2;
|
|
|
|
|
|
|
|
if($distance_result / $length < $distance) {
|
|
|
|
$header = "" if $last_header eq $header;
|
|
|
|
$last_header = $header;
|
|
|
|
$result .= $comma . $header . $index2;
|
|
|
|
$comma = ", ";
|
|
|
|
}
|
2010-06-20 08:15:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$result =~ s/(.*), /$1 or /;
|
|
|
|
$result = 'none' if $comma eq '';
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub set {
|
2014-05-17 22:08:19 +02:00
|
|
|
my ($self, $primary_index_key, $secondary_index_key, $key, $value, $dont_save) = @_;
|
2010-06-20 08:15:10 +02:00
|
|
|
|
|
|
|
my $primary = $self->find_index($primary_index_key);
|
|
|
|
|
|
|
|
if(not $primary) {
|
2014-05-17 22:08:19 +02:00
|
|
|
my $result = "No such $self->{name} object [$primary_index_key]; similiar matches: ";
|
2010-06-20 08:15:10 +02:00
|
|
|
$result .= $self->levenshtein_matches($primary_index_key);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $secondary = $self->find_index($primary, $secondary_index_key);
|
|
|
|
|
|
|
|
if(not $secondary) {
|
2014-05-17 22:08:19 +02:00
|
|
|
my $result = "No such $self->{name} object [$primary_index_key] $secondary_index_key; similiar matches: ";
|
2010-06-20 08:15:10 +02:00
|
|
|
$result .= $self->levenshtein_matches($primary, $secondary_index_key);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(not defined $key) {
|
2014-05-24 14:01:59 +02:00
|
|
|
my $result = "[" . ($primary eq '.*' ? 'global' : $primary) . "] $secondary keys:\n";
|
2010-06-20 08:15:10 +02:00
|
|
|
my $comma = '';
|
|
|
|
foreach my $key (sort keys %{ $self->hash->{$primary}->{$secondary} }) {
|
|
|
|
$result .= $comma . "$key => " . $self->hash->{$primary}->{$secondary}->{$key};
|
2014-05-24 14:01:59 +02:00
|
|
|
$comma = ";\n";
|
2010-06-20 08:15:10 +02:00
|
|
|
}
|
|
|
|
$result .= "none" if($comma eq '');
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(not defined $value) {
|
|
|
|
$value = $self->hash->{$primary}->{$secondary}->{$key};
|
|
|
|
} else {
|
|
|
|
$self->hash->{$primary}->{$secondary}->{$key} = $value;
|
2014-05-17 22:08:19 +02:00
|
|
|
$self->save unless $dont_save;
|
2010-06-20 08:15:10 +02:00
|
|
|
}
|
|
|
|
|
2010-06-29 09:31:27 +02:00
|
|
|
$primary = 'global' if $primary eq '.*';
|
2014-05-17 22:08:19 +02:00
|
|
|
return "[$primary] $secondary: '$key' " . (defined $value ? "set to '$value'" : "is not set.");
|
2010-06-20 08:15:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub unset {
|
|
|
|
my ($self, $primary_index_key, $secondary_index_key, $key) = @_;
|
|
|
|
|
|
|
|
my $primary = $self->find_index($primary_index_key);
|
|
|
|
|
|
|
|
if(not $primary) {
|
|
|
|
my $result = "No such $self->{name} object group '$primary_index_key'; similiar matches: ";
|
|
|
|
$result .= $self->levenshtein_matches($primary_index_key);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $secondary = $self->find_index($primary, $secondary_index_key);
|
|
|
|
|
|
|
|
if(not $secondary) {
|
|
|
|
my $result = "No such $self->{name} object '$secondary_index_key'; similiar matches: ";
|
|
|
|
$result .= $self->levenshtein_matches($primary, $secondary_index_key);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete $self->hash->{$primary}->{$secondary}->{$key};
|
|
|
|
$self->save();
|
|
|
|
|
2010-06-29 09:31:27 +02:00
|
|
|
$primary = 'global' if $primary eq '.*';
|
2010-06-20 08:15:10 +02:00
|
|
|
return "[$self->{name}] ($primary) $secondary: '$key' unset.";
|
|
|
|
}
|
|
|
|
|
|
|
|
sub add {
|
|
|
|
my ($self, $primary_index_key, $secondary_index_key, $hash) = @_;
|
|
|
|
|
|
|
|
if($self->load_hash_add($primary_index_key, $secondary_index_key, $hash, 0)) {
|
|
|
|
$self->save();
|
|
|
|
} else {
|
|
|
|
return "Error occurred adding new $self->{name} object.";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "'$secondary_index_key' added to $primary_index_key [$self->{name}].";
|
|
|
|
}
|
|
|
|
|
|
|
|
sub remove {
|
|
|
|
my ($self, $primary_index_key, $secondary_index_key) = @_;
|
|
|
|
|
|
|
|
my $primary = $self->find_index($primary_index_key);
|
|
|
|
|
|
|
|
if(not $primary) {
|
|
|
|
my $result = "No such $self->{name} object group '$primary_index_key'; similiar matches: ";
|
|
|
|
$result .= $self->levenshtein_matches($primary_index_key);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(not $secondary_index_key) {
|
|
|
|
delete $self->hash->{$primary};
|
|
|
|
$self->save;
|
|
|
|
return "'$primary' group removed from $self->{name}.";
|
|
|
|
}
|
|
|
|
|
|
|
|
my $secondary = $self->find_index($primary, $secondary_index_key);
|
|
|
|
|
|
|
|
if(not $secondary) {
|
|
|
|
my $result = "No such $self->{name} object '$secondary_index_key'; similiar matches: ";
|
|
|
|
$result .= $self->levenshtein_matches($primary, $secondary_index_key);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete $self->hash->{$primary}->{$secondary};
|
2013-07-24 14:35:40 +02:00
|
|
|
|
|
|
|
# remove primary group if no more secondaries
|
|
|
|
if(scalar keys $self->hash->{$primary} == 0) {
|
|
|
|
delete $self->hash->{$primary};
|
|
|
|
}
|
|
|
|
|
2010-06-20 08:15:10 +02:00
|
|
|
$self->save();
|
|
|
|
return "'$secondary' removed from $primary group [$self->{name}].";
|
|
|
|
}
|
|
|
|
|
|
|
|
# Getters and setters
|
|
|
|
|
|
|
|
sub hash {
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{hash};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub filename {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
if(@_) { $self->{filename} = shift; }
|
|
|
|
return $self->{filename};
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|