MongoDB Notes

When using MongoDB to store files, we have 2 collections:
1. The place where MongoDB store the file metadata: store.files
2. Ans place where MongoDB store the file content: store.chunks

Depending on the size of the file, one entry in store.files can points to many entries in store.chunks. The bigger the file, the more entry you’ll encounter.


// Show all/ list all entries from store.files
db.getCollection('store.files').find({});

// Show only a particular entry from store.files
db.getCollection('store.files').find({ _id : ObjectId("5b02d232cbce1d07e08401c7")});

// The same can be used for store chunks.
db.getCollection('store.chunks').find({});

The metadata/fields in the store.files can be expanded during query runtime (not saved into the MongoDB, only displayed during runtime)

db.getCollection('store.files').aggregate([
{ $match: { _id : ObjectId("5b02d232cbce1d07e08401c7")} },
{ $addFields:{'key_reference':'1234'}}
]);

Or we do an update into store.files which will be saved into the MongoDB

db.getCollection('store.files').updateMany({ _id : ObjectId("5b02d232cbce1d07e08401c7")},{$set:{'key_reference':'1234'}});

Assorted postgres queries

Get table sizes:


SELECT
relname AS objectname,
relkind AS objecttype,
reltuples AS "#entries",
pg_size_pretty(relpages::bigint*8*1024) AS size
FROM pg_class
WHERE relpages >= 8

ORDER BY relpages DESC;

TCL programming


#!/usr/bin/expect -f
# TCL script. Beware whitespace matter!

# To set multi-line comments do the following
# set comment {
# your multi-line comment
# }

# ###################################################
# ALL functions Start
# ###################################################

if {[llength $argv] == 0} {
send_user "Usage: scriptname FUNCTION_NAME\n"
send_user "Example script.exp updateSomething\n"
exit 1
}
set functionName [lindex $argv 0]

proc simpleTest {} {
set aProductionServers {your_server_4 your_server_5 your_server_6 your_server_1 your_server_2 your_server_3}
for {set i 0} {$i < [llength $aProductionServers]} {incr i} { send_user "Processing ... '[lindex $aProductionServers $i]'\n" set timeout 60 spawn ssh root@[lindex $aProductionServers $i] expect "*assword: " send "\r" send "hostname\r" expect "[lindex $aProductionServers $i]" send "date\r" expect "2015" send "exit\r" } } # simpleTest proc printPass {} { send_user "\n\[PASS\] \n" } proc printFail {} { send_user "\n\[FAIL\] \n" exit 1 } #Untar Lucene indexes proc UntarLuceneindexes {} { set aLuceneServers {your_server_1 your_server_2 your_server_3} set sCurrentLuceneTar "index2_20151009.tar.bz2" for {set i 0} {$i < [llength $aLuceneServers]} {incr i} { send_user "\n\n" send_user "================================================\n" send_user "Processing ... '[lindex $aLuceneServers $i]'\n" send_user "================================================\n" set timeout 60 spawn ssh root@[lindex $aLuceneServers $i] expect "*assword: " send "YOUR_SERVER_PASSWORD\r" send "hostname\r" expect "[lindex $aLuceneServers $i]" send "su - path\r" send "pwd\r" expect "/home/path" send "id\r" expect "path" send "ls $sCurrentLuceneTar|wc -l\r" expect "1" send "tar -xjvf $sCurrentLuceneTar &\r" send "sleep 2\r" send "ps aux|grep index2|grep tar|wc -l\r" send "disown %1" expect "1" send_user "Done processing ... '[lindex $aLuceneServers $i]'" } } proc updateDBRef {} { set apathServers {your_server_6} set newDB "some_db_20150925" for {set i 0} {$i < [llength $apathServers]} {incr i} { send_user "\n\n" send_user "================================================\n" send_user "Processing ... '[lindex $apathServers $i]'\n" send_user "================================================\n" set timeout 60 spawn ssh root@[lindex $apathServers $i] expect "*assword: " send "YOUR_SERVER_PASSWORD\r" send "hostname\r" expect "[lindex $apathServers $i]" send "su - path\r" send "pwd\r" expect "/home/path" send "id\r" expect "path" send "cat /home/path/path-current/SomeConfig.properties |grep -v 'com.somecompany.setting.server.db.pathdb2.url=' > /home/path/path-current/SomeConfig.properties.new"
send "echo 'com.somecompany.setting.server.db.pathdb2.url=jdbc:postgresql://127.0.0.1:5432/$newDB' >> /home/path/path-current/SomeConfig.properties.new"

# ps aux | grep jar | grep 8806
# kill xxxxxx

send_user "Done processing ... '[lindex $apathServers $i]'"
}
}

set aLuceneServers {your_server_1 your_server_2 your_server_3}
proc stopLuceneServices {} {
global aLuceneServers
for {set i 0} {$i < [llength $aLuceneServers]} {incr i} { send_user "\n\n" send_user "================================================\n" send_user "Processing ... '[lindex $aLuceneServers $i]'\n" send_user "================================================\n" set timeout 60 spawn ssh root@[lindex $aLuceneServers $i] expect "*assword: " send "YOUR_SERVER_PASSWORD\r" send "hostname\r" expect "[lindex $aLuceneServers $i]" send "service lucene stop\r" expect { "stopped PID" { printPass } "lucene is not running" { printFail } "lucene is not running x" { printPass } timeout { exit 1 } } send_user "Done processing ... '[lindex $aLuceneServers $i]' \n" } } proc startLuceneServices {} { global aLuceneServers for {set i 0} {$i < [llength $aLuceneServers]} {incr i} { send_user "\n\n" send_user "================================================\n" send_user "Processing ... '[lindex $aLuceneServers $i]'\n" send_user "================================================\n" set timeout 60 spawn ssh root@[lindex $aLuceneServers $i] expect "*assword: " send "YOUR_SERVER_PASSWORD\r" send "hostname\r" expect "[lindex $aLuceneServers $i]" send "service lucene start\r" expect "started PID" send_user "Done processing ... '[lindex $aLuceneServers $i]'" } } # ################################################### # ALL functions End # ################################################### set timeout 60 log_file -noappend expect.log ;# Default to append to a file. To disable append use: log_file -noappend # ################################################### # Calling functionName from, which supplied from command line # ################################################### $functionName

Multithreading in Java using ThreadPoolExecutor

ThreadWorker is your custom class.

try {
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(MAX_THREAD_SIZE);
for (int i = 1; i Random randomGenerator = new Random();
executor.submit(new ThreadWorker("worker" + i, randomGenerator.nextInt(10)));
LOG.info(i);
}
} catch (Exception e) {
LOG.error("Hmm something is not right.", e);
}

Compare File Permission Recussively on Linux Directories

Scan

#!/usr/bin/perl

use File::Find;

my $directory1 = '/root/rpmbuild/RPMSX';
my $directory2 = '/root/rpmbuild/RPMSX.bak';

find(\&hashfiles, $directory1);

sub hashfiles {
my $file1 = $File::Find::name;
(my $file2 = $file1) =~ s/^$directory1/$directory2/;
return(0) if (! -f $file2) ;
my $mode1 = (stat($file1))[2] ;
my $mode2 = (stat($file2))[2] ;

my $uid1 = (stat($file1))[4] ;
my $uid2 = (stat($file2))[4] ;

print "Permissions for $file1 and $file2 are not the same\n" if ( $mode1 != $mode2 );
print "Ownership for $file1 and $file2 are not the same\n" if ( $uid1 != $uid2 );
}