TestBed: Use nested class to hold test code
public class TestBed {
public void f() {
System.out.println("f()");
}
public static class Tester {
public static void main(String[] args) {
TestBed t = new TestBed();
t.f();
}
}
}
public class TestBed {
public void f() {
System.out.println("f()");
}
public static class Tester {
public static void main(String[] args) {
TestBed t = new TestBed();
t.f();
}
}
}
#!/usr/bin/perl
#use warnings;
use strict;
my $v;
undef $v;
if (!defined($v)) {
print "Oops! \$v is not defined\n";
}
$v = 0;
if ($v == '') {
print "Oops! \$v == \'\'\n";
}
__END__
Result:
Oops! $v is not defined
Oops! $v == ''
$v == ''
Argument "" isn't numeric in numeric eq (==) at ...
SELECT SUM(A + B) FROM youtable;
SELECT SUM(A) + SUM(B) FROM youtable;
this.s = s;
label1:
outer-iteration {
inner-iteration {
//...
break; // (1)
//...
continue; // (2)
//...
continue label1; // (3)
//...
break label1; // (4)
}
}
int i = -1;
i >>>= 123; // For int, only low-order 5 bits of 123 will be used: 27
// So it is actually i >>>= 27
// Result is 31
System.out.println(i);
-e enable interpretation of backslash escapes
-E disable interpretation of backslash escapes (default)
echo -e "\b\c"
exec [-cl] [-a name] [command [arguments]]
If command is specified, it replaces the shell. No new process is created. The
arguments become the arguments to command. If the -l option is supplied, the
shell places a dash at the beginning of the zeroth arg passed to command.
This is what login(1) does. The -c option causes command to be executed with
an empty environment. If -a is supplied, the shell passes name as the zeroth
argument to the executed command. If command cannot be executed for some reason,
a non-interactive shell exits, unless the shell option execfail is en-
abled, in which case it returns failure. An interactive shell returns failure
if the file can not be executed. If command is not specified, any redirections
take effect in the current shell, and the return status is 0. If there is a
redirection error, the return status is 1.
exec 1>STDOUT_GOES_TO_HERE 2>STDERR_GOES_TO_THERE
man ascii
sed "s/$something/$whoknows/"
ca=`echo '\001'` # control-A character
...
sed "s${ca}$something${ca}$whoknows${ca}"
:%s/^V^M//g
:%s/^M//g
$dbh->do("COPY mytable FROM STDIN");
$dbh->pg_putline("123\tPepperoni\t3\n");
$dbh->pg_putline("314\tMushroom\t8\n");
$dbh->pg_putline("6\tAnchovies\t100\n");
$dbh->pg_endcopy;
__PACKAGE__->set_primary_keys (__PACKAGE__->columns);
Incremental backup is a special form of GNU tar
archive that
stores additional metadata so that exact state of the file system
can be restored when extracting the archive.
GNU tar
currently offers two options for handling incremental
backups: ‘--listed-incremental=snapshot-file’ (‘-g
snapshot-file’) and ‘--incremental’ (‘-G’).
The option ‘--listed-incremental’ instructs tar to operate on
an incremental archive with additional metadata stored in a standalone
file, called a snapshot file. The purpose of this file is to help
determine which files have been changed, added or deleted since the
last backup, so that the next incremental backup will contain only
modified files. The name of the snapshot file is given as an argument
to the option:
Handle incremental backups with snapshot data in file.
To create an incremental backup, you would use
‘--listed-incremental’ together with ‘--create’
(see section How to Create Archives). For example:
$ tar --create \ |
This will create in ‘archive.1.tar’ an incremental backup of
the ‘/usr’ file system, storing additional metadata in the file
‘/var/log/usr.snar’. If this file does not exist, it will be
created. The created archive will then be a level 0 backup;
please see the next section for more on backup levels.
Otherwise, if the file ‘/var/log/usr.snar’ exists, it
determines which files are modified. In this case only these files will be
stored in the archive. Suppose, for example, that after running the
above command, you delete file ‘/usr/doc/old’ and create
directory ‘/usr/local/db’ with the following contents:
$ ls /usr/local/db |
Some time later you create another incremental backup. You will
then see:
$ tar --create \ |
The created archive ‘archive.2.tar’ will contain only these
three members. This archive is called a level 1 backup. Notice
that ‘/var/log/usr.snar’ will be updated with the new data, so if
you plan to create more ‘level 1’ backups, it is necessary to
create a working copy of the snapshot file before runningtar
. The above example will then be modified as follows:
$ cp /var/log/usr.snar /var/log/usr.snar-1 |
You can force ‘level 0’ backups either by removing the snapshot
file before running tar
, or by supplying the
‘--level=0’ option, e.g.:
$ tar --create \ |
Incremental dumps depend crucially on time stamps, so the results are
unreliable if you modify a file's time stamps during dumping (e.g.,
with the ‘--atime-preserve=replace’ option), or if you set the clock
backwards.
Metadata stored in snapshot files include device numbers, which,
obviously are supposed to be non-volatile values. However, it turns
out that NFS devices have undependable values when an automounter
gets in the picture. This can lead to a great deal of spurious
redumping in incremental dumps, so it is somewhat useless to compare
two NFS devices numbers over time. The solution implemented
currently is to consider all NFS devices as being equal
when it comes to comparing directories; this is fairly gross, but
there does not seem to be a better way to go.
Apart from using NFS, there are a number of cases where
relying on device numbers can cause spurious redumping of unmodified
files. For example, this occurs when archiving LVM snapshot
volumes. To avoid this, use ‘--no-check-device’ option:
Do not rely on device numbers when preparing a list of changed files
for an incremental dump.
Use device numbers when preparing a list of changed files
for an incremental dump. This is the default behavior. The purpose
of this option is to undo the effect of the ‘--no-check-device’
if it was given in TAR_OPTIONS
environment variable
(see TAR_OPTIONS).
There is also another way to cope with changing device numbers. It is
described in detail in Fixing Snapshot Files.
Note that incremental archives use tar
extensions and may
not be readable by non-GNU versions of the tar
program.
To extract from the incremental dumps, use
‘--listed-incremental’ together with ‘--extract’
option (see section Extracting Specific Files). In this case, tar
does
not need to access snapshot file, since all the data necessary for
extraction are stored in the archive itself. So, when extracting, you
can give whatever argument to ‘--listed-incremental’, the usual
practice is to use ‘--listed-incremental=/dev/null’.
Alternatively, you can use ‘--incremental’, which needs no
arguments. In general, ‘--incremental’ (‘-G’) can be
used as a shortcut for ‘--listed-incremental’ when listing or
extracting incremental backups (for more information regarding this
option, see incremental-op).
When extracting from the incremental backup GNU tar
attempts to
restore the exact state the file system had when the archive was
created. In particular, it will delete those files in the file
system that did not exist in their directories when the archive was
created. If you have created several levels of incremental files,
then in order to restore the exact contents the file system had when
the last level was created, you will need to restore from all backups
in turn. Continuing our example, to restore the state of ‘/usr’
file system, one would do(12):
$ tar --extract \ |
To list the contents of an incremental archive, use ‘--list’
(see section How to List Archives), as usual. To obtain more information about the
archive, use ‘--listed-incremental’ or ‘--incremental’
combined with two ‘--verbose’ options(13):
tar --list --incremental --verbose --verbose archive.tar |
This command will print, for each directory in the archive, the list
of files in that directory at the time the archive was created. This
information is put out in a format which is both human-readable and
unambiguous for a program: each file name is printed as
x file |
where x is a letter describing the status of the file: ‘Y’
if the file is present in the archive, ‘N’ if the file is not
included in the archive, or a ‘D’ if the file is a directory (and
is included in the archive). See section Dumpdir, for the detailed
description of dumpdirs and status codes. Each such
line is terminated by a newline character. The last line is followed
by an additional newline to indicate the end of the data.
The option ‘--incremental’ (‘-G’)
gives the same behavior as ‘--listed-incremental’ when used
with ‘--list’ and ‘--extract’ options. When used with
‘--create’ option, it creates an incremental archive without
creating snapshot file. Thus, it is impossible to create several
levels of incremental backups with ‘--incremental’ option.
$ /usr/local/pgsql/bin/psql test
Welcome to psql 8.3.7, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit
test=# SELECT relname, relpages FROM pg_class ORDER BY relpages DESC;
relname | relpages
-----------------------------------+----------
pg_proc | 50
pg_proc_proname_args_nsp_index | 40
pg_depend | 37
pg_attribute | 30
If you want only the first biggest table in the postgres database then append the above query with limit as:
# SELECT relname, relpages FROM pg_class ORDER BY relpages DESC limit 1;
relname | relpages
---------+----------
pg_proc | 50
(1 row)
pg_database_size is the function which gives the size of mentioned database. It shows the size in bytes.
# SELECT pg_database_size('geekdb');
pg_database_size
------------------
63287944
(1 row)
If you want it to be shown pretty, then use pg_size_pretty function which converts the size in bytes to human understandable format.
# SELECT pg_size_pretty(pg_database_size('geekdb'));
pg_size_pretty
----------------
60 MB
(1 row)
This is the total disk space size used by the mentioned table including index and toasted data. You may be interested in knowing only the size of the table excluding the index then use the following command.
# SELECT pg_size_pretty(pg_total_relation_size('big_table'));
pg_size_pretty
----------------
55 MB
(1 row)
Use pg_relation_size instead of pg_total_relation_size as shown below.
# SELECT pg_size_pretty(pg_relation_size('big_table'));
pg_size_pretty
----------------
38 MB
(1 row)
Syntax: # \d table_name
As shown in the example below, at the end of the output you will have a section titled as indexes, if you have index in that table. In the example below, table pg_attribute has two btree indexes. By default postgres uses btree index as it good for most common situations.
test=# \d pg_attribute
Table "pg_catalog.pg_attribute"
Column | Type | Modifiers
---------------+----------+-----------
attrelid | oid | not null
attname | name | not null
atttypid | oid | not null
attstattarget | integer | not null
attlen | smallint | not null
attnum | smallint | not null
attndims | integer | not null
attcacheoff | integer | not null
atttypmod | integer | not null
attbyval | boolean | not null
attstorage | "char" | not null
attalign | "char" | not null
attnotnull | boolean | not null
atthasdef | boolean | not null
attisdropped | boolean | not null
attislocal | boolean | not null
attinhcount | integer | not null
Indexes:
"pg_attribute_relid_attnam_index" UNIQUE, btree (attrelid, attname)
"pg_attribute_relid_attnum_index" UNIQUE, btree (attrelid, attnum)
By default the indexes are created as btree. You can also specify the type of index during the create index statement as shown below.
Syntax: CREATE INDEX name ON table USING index_type (column);
# CREATE INDEX test_index ON numbers using hash (num);
# BEGIN -- start the transaction.
All the operations performed after the BEGIN command will be committed to the postgreSQL database only you execute the commit command. Use rollback command to undo all the transactions before it is committed.
# ROLLBACK -- rollbacks the transaction.
# COMMIT -- commits the transaction.
# EXPLAIN query;
This executes the query in the server side, thus does not shows the output to the user. But shows the plan in which it got executed.
# EXPLAIN ANALYZE query;
This inserts 1,2,3 to 1000 as thousand rows in the table numbers.
# INSERT INTO numbers (num) VALUES ( generate_series(1,1000));
This shows the total number of rows in the table.
# select count(*) from table;
Following example gives the total number of rows with a specific column value is not null.
# select count(col_name) from table;
Following example displays the distinct number of rows for the specified column value.
# select count(distinct col_name) from table;
# select max(col_name) from table;
# SELECT MAX(num) from number_table where num < ( select MAX(num) from number_table );
# select min(col_name) from table;
# SELECT MIN(num) from number_table where num > ( select MIN(num) from number_table );
Below is the partial output that displays available basic datatypes and it’s size.
test=# SELECT typname,typlen from pg_type where typtype='b';
typname | typlen
----------------+--------
bool | 1
bytea | -1
char | 1
name | 64
int8 | 8
int2 | 2
int2vector | -1
# \o output_file
# SELECT * FROM pg_class;
The output of the query will be redirected to the “output_file”. After the redirection is enabled, the select command will not display the output in the stdout. To enable the output to the stdout again, execute the \o without any argument as mentioned below.
# \o
As explained in our earlier article, you can also backup and restore postgreSQL database using pg_dump and psql.
PostgreSQL database can encrypt the data using the crypt command as shown below. This can be used to store your custom application username and password in a custom table.
# SELECT crypt ( 'sathiya', gen_salt('md5') );
The postgreSQL crypt command may not work on your environment and display the following error message.
ERROR: function gen_salt("unknown") does not exist
HINT: No function matches the given name and argument types.
You may need to add explicit type casts.
To solve this problem, installl the postgresql-contrib-your-version package and execute the following command in the postgreSQL prompt.
# \i /usr/share/postgresql/8.1/contrib/pgcrypto.sql
# tar xzvf perl-5.12.1.tar.gz
# cd perl-5.12.1
# sh Configure -de -Dprefix=/opt/nmetrics
# make
# make test
# make install
use 5.012;
no warnings 'deprecated';
temp_warning();
sub temp_warning {
# needs to be one level lower than the warnings setting
warn "Hey Evel Knievel! Deprecation warnings are disabled!" unless
warnings::enabled( 'deprecated' );
}
my $form = $c->request->parameters;
# ?a=b&b=c
# $form = { a => 'b', b => 'c' }
# ?a=b&a=c&b=c
# $form = { a => [ 'b', 'c' ], b => 'c' };
my $v = $c->request->parameters;
my $query = $v->{query};
my @names = @{$v->{name}};
my $v = $c->request->parameters;
my $query = ref $v->{query} eq 'ARRAY' ? $v->{query}->[0] : $v->{query};
my @names = ref $v->{name} eq 'ARRAY' ? @{$v->{name}} : ($v->{name});
my $_private_method = sub { my ($self, ...) = @_; ... };
...
$self->$_private_method($some, $arguments);
...
+----------+ fork +------+
|chart page| ----------------> |poller|
+----------+ +------+
| ^
| |
|request |Shared Memory
| |
| +--------+ |
+------> | charter| <------+
+--------+
...
...
defined (my $pid = fork) or die "Can't fork: $!";
if (!$pid) {
setsid;
open STDIN, '>/dev/null' or die "Can't read from /dev/null: $!";
open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!";
open STDERR, '>/dev/null' or die "Can't write to /dev/null: $!";
exec '/usr/local/NPMS/bin/if_poller.pl', $val->{ip}, $val->{commstring}, $val->{if};
}
...
...
print <<ENDOFFORM;
<script type="text/javascript">
function clean_up() {
document.getElementById('form1').submit();
window.close();
}
</script>
<form id="form1" method="post" action="clean_poller.cgi">
<input name="pid" value="$pid" type="hidden">
<input name="close" value="Close" onclick="clean_up();" type="button">
</form>
ENDOFFORM
...
...
kill 'INT' => $in{'pid'};
...
...
...
...
my $EXIT = 0;
$SIG{INT} = sub {
$EXIT = 1;
};
...
...
while (1) {
...
...
do polling stuff
do {
IPC::Shareable->clean_up_all;
exit;
} if $EXIT;
...
...
}
[root@buildbox55-64bit Perl]# snmpwalk -Os -c public -v2c 192.168.1.230 .1.3.6.1.2.1.2.2.1.10.2
ifInOctets.2 = Counter32: 77043026
[root@buildbox55-64bit Perl]# snmpwalk -Os -c public -v2c localhost 1.3.6.1.2.1.31.1.1.1.6.2
ifHCInOctets.2 = Counter64: 78219248
IPC::Shareable::SharedMem: shmget: Permission denied
(tied VARIABLE)->remove;
IPC::Shareable->clean_up;
IPC::Shareable->clean_up_all;
# find ~/.cpan/sources/authors/id -regex '.*\.tar\.gz$' -exec cp {} . \;
[root@buildbox55-64bit SPECS]# while [ 1 ]; do snmpwalk -Os -c MYCOMMUNITY -v2c localhost .1.3.6.1.2.1.2.2.1.10.2; sleep 1; done
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7925145
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
ifInOctets.2 = Counter32: 7927505
/usr/src/redhat/SOURCES/net-snmp-5.3.2.2/agent/mibgroup/if-mib/ifTable/ifTable_data_access.h
#define IFTABLE_CACHE_TIMEOUT 30
#define IFTABLE_CACHE_TIMEOUT 1
[root@buildbox55-64bit SPECS]# while [ 1 ]; do snmpwalk -Os -c MYCOMMUNITY -v2c localhost .1.3.6.1.2.1.2.2.1.10.2; sleep 1; done
ifInOctets.2 = Counter32: 8087029
ifInOctets.2 = Counter32: 8087421
ifInOctets.2 = Counter32: 8087519
ifInOctets.2 = Counter32: 8087911
ifInOctets.2 = Counter32: 8088219
ifInOctets.2 = Counter32: 8088475
ifInOctets.2 = Counter32: 8088633
ifInOctets.2 = Counter32: 8088731
ifInOctets.2 = Counter32: 8088889
ifInOctets.2 = Counter32: 8088987
ifInOctets.2 = Counter32: 8089145
ifInOctets.2 = Counter32: 8089243
ifInOctets.2 = Counter32: 8089401
ifInOctets.2 = Counter32: 8089499
ifInOctets.2 = Counter32: 8089657
ifInOctets.2 = Counter32: 8090085
ifInOctets.2 = Counter32: 8090670
ifInOctets.2 = Counter32: 8090768
ifInOctets.2 = Counter32: 8091268
ifInOctets.2 = Counter32: 8091426
ifInOctets.2 = Counter32: 8091524
ifInOctets.2 = Counter32: 8091682
ifInOctets.2 = Counter32: 8091780
ifInOctets.2 = Counter32: 8091938
ifInOctets.2 = Counter32: 8092036
ifInOctets.2 = Counter32: 8092194
ifInOctets.2 = Counter32: 8092382
ifInOctets.2 = Counter32: 8092604
ifInOctets.2 = Counter32: 8092702
ifInOctets.2 = Counter32: 8092924
ifInOctets.2 = Counter32: 8093022
ifInOctets.2 = Counter32: 8093180
ifInOctets.2 = Counter32: 8093278
ifInOctets.2 = Counter32: 8093436
ifInOctets.2 = Counter32: 8093692
ifInOctets.2 = Counter32: 8093790
ifInOctets.2 = Counter32: 8093948
ifInOctets.2 = Counter32: 8094046
ifInOctets.2 = Counter32: 8094452
ifInOctets.2 = Counter32: 8094550
ifInOctets.2 = Counter32: 8094708
ifInOctets.2 = Counter32: 8094806
ifInOctets.2 = Counter32: 8094964
ifInOctets.2 = Counter32: 8095062
ifInOctets.2 = Counter32: 8095220
ifInOctets.2 = Counter32: 8095318
ifInOctets.2 = Counter32: 8095476
ifInOctets.2 = Counter32: 8095818
ifInOctets.2 = Counter32: 8095976
;;; cperl-mode is preferred to perl-mode
;;; "Brevity is the soul of wit"
(defalias 'perl-mode 'cperl-mode)
(add-to-list 'auto-mode-alist '("\\.\\([pP][Llm]\\|al\\)\\'" . cperl-mode))
(add-to-list 'auto-mode-alist '("\\.\\([tT]\\)\\'" . cperl-mode))
(add-to-list 'interpreter-mode-alist '("perl" . cperl-mode))
(add-to-list 'interpreter-mode-alist '("perl5" . cperl-mode))
(add-to-list 'interpreter-mode-alist '("miniperl" . cperl-mode))
(mapc
(lambda (pair)
(if (eq (cdr pair) 'perl-mode)
(setcdr pair 'cperl-mode)))
(append auto-mode-alist interpreter-mode-alist))