Introducing Yuca: A light-weight, in-memory, fast and simple to use search engine library.

https://github.com/gubatron/yuca

If your app can’t handle or doesn’t really need installing a full featured and heavy search engine like Lucene, nor you want to depend on a SQL database for indexing and doing simple search based strings you can use Yuca to index documents under any number of arbitrary keys which can be grouped under tags.

The shared library currently weighs ~170kb without any packing optimizations, we hope to reduce the size further in the near future.

Today, Wed May 9th the library is only available as a C++ shared or static library, the goal is to have bindings for popular programming languages, the first being Java since I need to use it on Android apps I’m developing.

If you feel like you need something like this and you’re not coding in C++, please create an issue on the github repository asking for the language bindings that you need and I’ll try to prioritize your request for the next set of language bindings.

How to run your Kotlin gradle built app from the command line

So you build your Kotlin app, you went through the trouble of creating a build.gradle script that you build with

gradle assemble

this outputs a a “build/libs/kotlin.jar” .jar file, but you have no clue how to run your Kotlin code from the command line.

Doing it by hand with “java -cp ” is too much work, and there is a way to do

gradle run

or even

gradle -b /home/myuser/mykotlinapp/build.gradle run

in case you need to run your Kotlin script from a cronjob.

Make sure you have the following inside your build.gradle script in order to make the “run” task available


apply plugin: 'application'

// DO notice the "Kt" suffix on the class name below, if you don't use the Kt generated class you will get errors
mainClassName = 'com.myapp.MyKotlinAppKt'

// optional: add one string per argument you want as the default JVM args
applicationDefaultJvmArgs = ["-Xms512m", "-Xmx1g"]

What if I don’t want to use gradle, and just java

ok… ok…
java -cp $KOTLIN_LIB/kotlin-runtime.jar:build/libs/kotlin.jar: com.myapp.MyKotlinAppKt

Fix high CPU usage by WordPress and MySQL

Today one of our wordpress sites had very high server load and it was being caused by MySQL

So I went to the mysql console, and looked up the process list:

So this guy is appearing a lot
SELECT option_name, option_value FROM wp_options WHERE autoload = 'yes';

Let’s see how it’s behaving with explain
explain SELECT option_name, option_value FROM wp_options WHERE autoload = 'yes';

It’s scanning 226k rows to get its search results!

Probably some moronic plugin is doing this and wordpress does not add an index on that table. The solution is simple, let’s add an index!

ALTER TABLE wp_options ADD INDEX (`autoload`);

Now let’s run explain again

From scanning 226k it went down to 408!, 3 orders of magnitude drop.

And now the CPU load went below 4%, crisis averted.

[Monero-Development] Installing dependencies on Mac

March 12th 2017.

In order to build Monero on MacOSX with cmake, I had to install the following dependencies via homebrew

brew install libunwind-headers --force
brew link libunwind-headers --force
brew install miniupnpc
brew install ldns
brew link ldns --force
brew install expat
brew link expat --force
brew install doxygen

[CODE] Detecting if a VPN connection is active programmatically (Jan/2017)

This method of programatic detection works as of January 24, 2017 with the latest versions of Express VPN and PIA (Private Internet Access)

On Mac OSX/
This works for Mac OSX 10.2.2.

The trick is to request your ip routing table and examine through which network interface your default traffic is going through.
(To do this programmatically you will have to parse the output with your favorite programming language)

This is how it looks for both ExpressVPN and PIA when the VPN is active:

To request your routing table you can do this on the command line:
netstat -nr

Notice the line starting with “0/1”, it’s going through that tunnel interface. (In Linux it would show 0.0.0.0 instead of 0/1)

VPN ON output in Mac

Internet:
Destination        Gateway            Flags        Refs      Use   Netif Expire
0/1                10.81.10.5         UGSc            5        0   utun1
...

VPN ON output in Linux

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         10.31.10.5      128.0.0.0       UG        0 0          0 tun0
...

When you turn VPN off this is how it looks:

VPN OFF in Mac

Destination        Gateway            Flags        Refs      Use   Netif Expire
default            192.168.1.1        UGSc           66        0     en0
127                127.0.0.1          UCS             2        4     lo0

VPN OFF in Linux

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         172.16.245.2    0.0.0.0         UG        0 0          0 eth0

So a quick way to determine if the VPN is on or off in Mac or Linux, is to filter-out what you care for using grep.
If you have any output it’s on, if not it’s off

netstat -nr | egrep "^0" | grep "tun"

(we filter for “tun” and not utun1, as in linux vpn network interfaces start with “tun”)

Parse the output of that command and you will have your VPN status. No output means VPN is disconnected. Some output means the VPN is connected.

On Windows
Do a nestat -nr and look for 128.0.0.1, if you find it, VPN is on.

[CODE/JAVA] Scanner – read a full line

I was solving problems at HackerRank to teach a friend how to code in Java and to process the input data it’s convenient to use Java’s built in Scanner class.

However, in one of the problems we wanted to read a whole line at once and we noticed that the Scanner’s API is geared towards reading the next token, and it doesn’t have a method that reads the entire line. It has one method that skips a line and reads from the last point which is very weird to use, so we created a static method to just read the next entire line, keeping it simple.

public static String readLine(Scanner scanner) {
            Pattern oldDelimiter = scanner.delimiter();
            scanner.useDelimiter("\\r\\n|[\\n\\x0B\\x0C\\r\\u0085\\u2028\\u2029]");
            String r = scanner.next();
            scanner.useDelimiter(oldDelimiter);
            return r;
    }

The Scanner uses as its default token delimiter a whitespace pattern, so what we do in this method is to temporarily replace the token delimiter for a newline delimiter. Once set to newline all we need to do is to read the next token, which will be the entirety of the line, and then we put the previous delimiter set back in place.

In Java 8 there’s a new Linebreak matcher expression \R, however, if we want our method to be backwards compatible (as we don’t know on which JRE it could be running) we set the equivalent delimiter manually in our function

\R  Any Unicode linebreak sequence, is equivalent to \u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029]

If you don’t like the static function approach you can always repurpose this code and extend Scanner into a custom MyScanner class , and add a method nextLine that does the same thing

[CODE] Arduino 4 BIT LED binary counter

Here’s my “Hello World” on Arduino.
A 4-bit binary counter for arduino.
If you add another LED on the 5th pin, then it can count from 0 to 31 (32 values)

Circuit wired by @KataySantos

int counter = -1;
int MAX_VALUE = 16;

void updateLED(int pin, int bit, int ctr) {
  digitalWrite(pin, (ctr & (1<<bit)) == (1<<bit) ? HIGH : LOW);
}
void setup() {
  // this runs once
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop() {
  // this runs repeatedly
  counter = (counter + 1) % MAX_VALUE;
  updateLED(1, 0, counter);
  updateLED(2, 1, counter);
  updateLED(3, 2, counter);
  updateLED(4, 3, counter);
  delay(500);
}