Skip to content

Instantly share code, notes, and snippets.

View khan5v's full-sized avatar

Vladimir Orekhov khan5v

  • Zurich, Switzerland
View GitHub Profile
@khan5v
khan5v / MongoQueryObjectID
Last active August 29, 2015 14:22
MongoDB query based on ObjectID, getting MongoDB ObjectID from String
# getting an objectId object from a string
getObjectID = function(str) {
return(mongo.oid.from.string(str))
}
database = ""
mongo = NULL
namespace = ""
connectToMongoDB = function(host, db, collection) {
@khan5v
khan5v / filterout.py
Created August 29, 2015 16:49
Shows how to filter entries out based on some logic in Spark
#reading data from a file
logData = sc.textFile(logFile)
X = 10
#for each key finding entries that occur more than X times
outliers = logData.map(lambda (k, v): (k, 1)).reduceByKey(lambda a, b: a + b).filter(lambda (k, v): v > X).cache()
#filtering these entries out
@khan5v
khan5v / elementary_forking.pl
Created November 3, 2016 10:52
Basic perl fork() example
use strict;
use warnings;
my $child_pid = fork();
if(defined $child_pid && $child_pid > 0) {
## Parent
wait();
print "p($$) c($child_pid) child finished, exiting\n";
} else {
## Child
@khan5v
khan5v / n_child_forking.pl
Last active November 3, 2016 13:27
Perl fork() example with multiple child processes involved
use strict;
use warnings;
my $spawn_processes = 5;
my $forked = 0;
my $err = 0;
print "($$) parent has started\n";
foreach(1..$spawn_processes) {
my $child_pid = fork();
@khan5v
khan5v / nohang_forking.pl
Created November 3, 2016 14:36
Perl fork() example with multiple child processes and a way to stop the process in case child processes are stuck
use strict;
use warnings;
use POSIX qw( WNOHANG );
my $spawn_processes = 5;
my $forked = 0;
my ($err, $success) = (0, 0);
my $local_id = 0;
print "($$) parent has started\n";
use strict;
use warnings;
use Data::Dumper;
use AnyEvent::Handle;
use AnyEvent;
my($pipe_w, $pipe_r);
pipe $pipe_r, $pipe_w;
my $cv = AnyEvent->condvar;
use strict;
use warnings;
use Parallel::SubFork;
use Data::Dumper;
my $manager = Parallel::SubFork->new();
for(1..10) {
$manager->start(\&cb, [$_]);
@khan5v
khan5v / PairCompare.java
Created December 15, 2016 14:46
Java comparator usage
Collections.sort(list, new Comparator<Pair>() {
public int compare(Pair p1, Pair p2) {
//Reverse order of i (integers) and lexicographic order of s (strings)
return p1.i < p2.i ? 1 : p1.i > p2.i ? -1 : p1.s.compareTo(p2.s);
}
});
@khan5v
khan5v / pipe_ipc.pl
Created February 6, 2017 15:41
Parent - child IPC through pipes
#/usr/bin/perl
use strict;
use warnings;
use AnyEvent;
use AnyEvent::Handle;
use Parallel::SubFork;
my $manager = Parallel::SubFork->new();
@khan5v
khan5v / ascii
Created August 11, 2017 19:08
Java conversion from ASCII to int and back
int ascii_int = (int) 'a';
char ascii_char = (char) 97;