This file is indexed.

/usr/lib/perl5/Net/HandlerSocket/Pool.pm is in libnet-handlersocket-perl 1.1.0-7-g1044a28-1.1build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/perl

package Net::HandlerSocket::HSPool;

use strict;
use warnings;
use Net::HandlerSocket;
use Socket;

sub new {
	my $self = {
		config => $_[1],
		reopen_interval => 60,
		hostmap => { },
	};
	return bless $self, $_[0];
}

sub clear_pool {
	my ($self) = @_;
	$self->{hostmap} = { };
}

sub on_error {
	my ($self, $obj) = @_;
	my $error_func = $self->{config}->{error};
	if (defined($error_func)) {
		return &{$error_func}($obj);
	}
	die $obj;
}

sub on_warning {
	my ($self, $obj) = @_;
	my $warning_func = $self->{config}->{warning};
	if (defined($warning_func)) {
		return &{$warning_func}($obj);
	}
}

sub get_conf {
	my ($self, $dbtbl) = @_;
	my $hcent = $self->{config}->{hostmap}->{$dbtbl};
	if (!defined($hcent)) {
		$self->on_error("get_conf: $dbtbl not found");
		return undef;
	}
	my %cpy = %$hcent;
	$cpy{port} ||= 9998;
	$cpy{timeout} ||= 2;
	return \%cpy;
}

sub resolve_hostname {
	my ($self, $hcent, $host_ip_list) = @_;
	if (defined($host_ip_list)) {
		if (scalar(@$host_ip_list) > 0) {
			$hcent->{host} = shift(@$host_ip_list);
			return $host_ip_list;
		}
		return undef; # no more ip
	}
	my $host = $hcent->{host}; # unresolved name
	$hcent->{hostname} = $host;
	my $resolve_list_func = $self->{config}->{resolve_list};
	if (defined($resolve_list_func)) {
		$host_ip_list = &{$resolve_list_func}($host);
		if (scalar(@$host_ip_list) > 0) {
			$hcent->{host} = shift(@$host_ip_list);
			return $host_ip_list;
		}
		return undef; # no more ip
	}
	my $resolve_func = $self->{config}->{resolve};
	if (defined($resolve_func)) {
		$hcent->{host} = &{$resolve_func}($host);
		return [];
	}
	my $packed = gethostbyname($host);
	if (!defined($packed)) {
		return undef;
	}
	$hcent->{host} = inet_ntoa($packed);
	return [];
}

sub get_handle_exec {
	my ($self, $db, $tbl, $idx, $cols, $exec_multi, $exec_args) = @_;
	my $now = time();
	my $dbtbl = join('.', $db, $tbl);
	my $hcent = $self->get_conf($dbtbl); # copy
	if (!defined($hcent)) {
		return undef;
	}
	my $hmkey = join(':', $hcent->{host}, $hcent->{port});
	my $hment = $self->{hostmap}->{$hmkey};
		# [ open_time, handle, index_map, host, next_index_id ]
	my $host_ip_list;
	TRY_OTHER_IP:
	if (!defined($hment) ||
		$hment->[0] + $self->{reopen_interval} < $now ||
		!$hment->[1]->stable_point()) {
		$host_ip_list = $self->resolve_hostname($hcent, $host_ip_list);
		if (!defined($host_ip_list)) {
			my $hostport = $hmkey . '(' . $hcent->{host} . ')';
			$self->on_error("HSPool::get_handle" .
				"($db, $tbl, $idx, $cols): host=$hmkey: " .
				"no more active ip");
			return undef;
		}
		my $hnd = new Net::HandlerSocket($hcent);
		my %m = ();
		$hment = [ $now, $hnd, \%m, $hcent->{host}, 1 ];
		$self->{hostmap}->{$hmkey} = $hment;
	}
	my $hnd = $hment->[1];
	my $idxmap = $hment->[2];
	my $imkey = join(':', $idx, $cols);
	my $idx_id = $idxmap->{$imkey};
	if (!defined($idx_id)) {
		$idx_id = $hment->[4];
		my $e = $hnd->open_index($idx_id, $db, $tbl, $idx, $cols);
		if ($e != 0) {
			my $estr = $hnd->get_error();
			my $hostport = $hmkey . '(' . $hcent->{host} . ')';
			my $errmess = "HSPool::get_handle open_index" .
				"($db, $tbl, $idx, $cols): host=$hostport " .
				"err=$e($estr)";
			$self->on_warning($errmess);
			$hnd->close();
			$hment = undef;
			goto TRY_OTHER_IP;
		}
		$hment->[4]++;
		$idxmap->{$imkey} = $idx_id;
	}
	if ($exec_multi) {
		my $resarr;
		for my $cmdent (@$exec_args) {
			$cmdent->[0] = $idx_id;
		}
		if (scalar(@$exec_args) == 0) {
			$resarr = [];
		} else {
			$resarr = $hnd->execute_multi($exec_args);
		}
		my $i = 0;
		for my $res (@$resarr) {
			if ($res->[0] != 0) {
				my $cmdent = $exec_args->[$i];
				my $ec = $res->[0];
				my $estr = $res->[1];
				my $op = $cmdent->[1];
				my $kfvs = $cmdent->[2];
				my $kvstr = defined($kfvs)
					? join(',', @$kfvs) : '';
				my $limit = $cmdent->[3] || 0;
				my $skip = $cmdent->[4] || 0;
				my $hostport = $hmkey . '(' . $hcent->{host}
					. ')';
				my $errmess = "HSPool::get_handle execm" .
					"($db, $tbl, $idx, [$cols], " .
					"($idx_id), $op, [$kvstr] " .
					"$limit, $skip): " . 
					"host=$hostport err=$ec($estr)";
				if ($res->[0] < 0 || $res->[0] == 2) {
					$self->on_warning($errmess);
					$hnd->close();
					$hment = undef;
					goto TRY_OTHER_IP;
				} else {
					$self->on_error($errmess);
				}
			}
			shift(@$res);
			++$i;
		}
		return $resarr;
	} else {
		my $res = $hnd->execute_find($idx_id, @$exec_args);
		if ($res->[0] != 0) {
			my ($op, $kfvals, $limit, $skip) = @$exec_args;
			my $ec = $res->[0];
			my $estr = $res->[1];
			my $kvstr = join(',', @$kfvals);
			my $hostport = $hmkey . '(' . $hcent->{host} . ')';
			my $errmess = "HSPool::get_handle exec" .
				"($db, $tbl, $idx, [$cols], ($idx_id), " .
				"$op, [$kvstr], $limit, $skip): " .
				"host=$hostport err=$ec($estr)";
			if ($res->[0] < 0 || $res->[0] == 2) {
				$self->on_warning($errmess);
				$hnd->close();
				$hment = undef;
				goto TRY_OTHER_IP;
			} else {
				$self->on_error($errmess);
			}
		}
		shift(@$res);
		return $res;
	}
}

sub index_find {
	my ($self, $db, $tbl, $idx, $cols, $op, $kfvals, $limit, $skip) = @_;
	# cols: comma separated list
	# kfvals: arrayref
	$limit ||= 0;
	$skip ||= 0;
	my $res = $self->get_handle_exec($db, $tbl, $idx, $cols,
		0, [ $op, $kfvals, $limit, $skip ]);
	return $res;
}

sub index_find_multi {
	my ($self, $db, $tbl, $idx, $cols, $cmdlist) = @_;
	# cols : comma separated list
	# cmdlist : [ dummy, op, kfvals, limit, skip ]
	# kfvals : arrayref
	my $resarr = $self->get_handle_exec($db, $tbl, $idx, $cols,
		1, $cmdlist);
	return $resarr;
}

sub result_single_to_arrarr {
	my ($numcols, $hsres, $ret) = @_;
	my $hsreslen = scalar(@$hsres);
	my $rlen = int($hsreslen / $numcols);
	$ret = [ ] if !defined($ret);
	my @r = ();
	my $p = 0;
	for (my $i = 0; $i < $rlen; ++$i) {
		my @a = splice(@$hsres, $p, $numcols);
		$p += $numcols;
		push(@$ret, \@a);
	}
	return $ret; # arrayref of arrayrefs
}

sub result_multi_to_arrarr {
	my ($numcols, $mhsres, $ret) = @_;
	$ret = [ ] if !defined($ret);
	for my $hsres (@$mhsres) {
		my $hsreslen = scalar(@$hsres);
		my $rlen = int($hsreslen / $numcols);
		my $p = 0;
		for (my $i = 0; $i < $rlen; ++$i) {
			my @a = splice(@$hsres, $p, $numcols);
			$p += $numcols;
			push(@$ret, \@a);
		}
	}
	return $ret; # arrayref of arrayrefs
}

sub result_single_to_hasharr {
	my ($names, $hsres, $ret) = @_;
	my $nameslen = scalar(@$names);
	my $hsreslen = scalar(@$hsres);
	my $rlen = int($hsreslen / $nameslen);
	$ret = [ ] if !defined($ret);
	my $p = 0;
	for (my $i = 0; $i < $rlen; ++$i) {
		my %h = ();
		for (my $j = 0; $j < $nameslen; ++$j, ++$p) {
			$h{$names->[$j]} = $hsres->[$p];
		}
		push(@$ret, \%h);
	}
	return $ret; # arrayref of hashrefs
}

sub result_multi_to_hasharr {
	my ($names, $mhsres, $ret) = @_;
	my $nameslen = scalar(@$names);
	$ret = [ ] if !defined($ret);
	for my $hsres (@$mhsres) {
		my $hsreslen = scalar(@$hsres);
		my $rlen = int($hsreslen / $nameslen);
		my $p = 0;
		for (my $i = 0; $i < $rlen; ++$i) {
			my %h = ();
			for (my $j = 0; $j < $nameslen; ++$j, ++$p) {
				$h{$names->[$j]} = $hsres->[$p];
			}
			push(@$ret, \%h);
		}
	}
	return $ret; # arrayref of hashrefs
}

sub result_single_to_hashhash {
	my ($names, $key, $hsres, $ret) = @_;
	my $nameslen = scalar(@$names);
	my $hsreslen = scalar(@$hsres);
	my $rlen = int($hsreslen / $nameslen);
	$ret = { } if !defined($ret);
	my $p = 0;
	for (my $i = 0; $i < $rlen; ++$i) {
		my %h = ();
		for (my $j = 0; $j < $nameslen; ++$j, ++$p) {
			$h{$names->[$j]} = $hsres->[$p];
		}
		my $k = $h{$key};
		$ret->{$k} = \%h if defined($k);
	}
	return $ret; # hashref of hashrefs
}

sub result_multi_to_hashhash {
	my ($names, $key, $mhsres, $ret) = @_;
	my $nameslen = scalar(@$names);
	$ret = { } if !defined($ret);
	for my $hsres (@$mhsres) {
		my $hsreslen = scalar(@$hsres);
		my $rlen = int($hsreslen / $nameslen);
		my $p = 0;
		for (my $i = 0; $i < $rlen; ++$i) {
			my %h = ();
			for (my $j = 0; $j < $nameslen; ++$j, ++$p) {
				$h{$names->[$j]} = $hsres->[$p];
			}
			my $k = $h{$key};
			$ret->{$k} = \%h if defined($k);
		}
	}
	return $ret; # hashref of hashrefs
}

sub select_cols_where_eq_aa {
	# SELECT $cols FROM $db.$tbl WHERE $idx_key = $kv LIMIT 1
	my ($self, $db, $tbl, $idx, $cols_aref, $kv_aref) = @_;
	my $cols_str = join(',', @$cols_aref);
	my $res = $self->index_find($db, $tbl, $idx, $cols_str, '=', $kv_aref);
	return result_single_to_arrarr(scalar(@$cols_aref), $res);
}

sub select_cols_where_eq_hh {
	# SELECT $cols FROM $db.$tbl WHERE $idx_key = $kv LIMIT 1
	my ($self, $db, $tbl, $idx, $cols_aref, $kv_aref, $retkey) = @_;
	my $cols_str = join(',', @$cols_aref);
	my $res = $self->index_find($db, $tbl, $idx, $cols_str, '=', $kv_aref);
	my $r = result_single_to_hashhash($cols_aref, $retkey, $res);
	return $r;
}

sub select_cols_where_in_hh {
	# SELECT $cols FROM $db.$tbl WHERE $idx_key in ($vals)
	my ($self, $db, $tbl, $idx, $cols_aref, $vals_aref, $retkey) = @_;
	my $cols_str = join(',', @$cols_aref);
	my @cmdlist = ();
	for my $v (@$vals_aref) {
		push(@cmdlist, [ -1, '=', [ $v ] ]);
	}
	my $res = $self->index_find_multi($db, $tbl, $idx, $cols_str,
		\@cmdlist);
	return result_multi_to_hashhash($cols_aref, $retkey, $res);
}

1;