Skip to content

Instantly share code, notes, and snippets.

View tzachz's full-sized avatar

Tzach Zohar tzachz

View GitHub Profile
@tzachz
tzachz / SparkAppStats.scala
Created November 4, 2015 19:47
Spark REST API usage example: shuffle memory totals
import org.json4s._
import org.json4s.jackson.JsonMethods.parse
import scala.io.Source.fromURL
object SparkAppStats {
/**
* (partial) representation of a Spark Stage object
*/
case class SparkStage(name: String, shuffleWriteBytes: Long, memoryBytesSpilled: Long, diskBytesSpilled: Long)
@tzachz
tzachz / StaticClass.java
Created September 1, 2015 12:59
Java behavior when static constructor throws exception
public class StaticClass {
static {
throwsException();
}
private static void throwsException() {
throw new RuntimeException("bam!");
}
@tzachz
tzachz / UserValidator.java
Created June 6, 2015 14:43
Functions should not have side effects (from Clean Code, p.44)
public class UserValidator {
private Cryptographer cryptographer;
public boolean checkPassword(String userName, String password) {
User user = UserGateway.findByName(userName);
if (user != User.NULL) {
String codedPhrase = user.getPhraseEncodedByPassword();
String phrase = cryptographer.decrypt(codedPhrase, password);
if ("Valid Password".equals(phrase)) {
Session.initialize();
@tzachz
tzachz / H2JDBIRule.java
Last active March 20, 2020 23:01
Dropwizard JDBI JUnit Rule using H2 by default - based on https://gist.github.com/yunspace/9a50e11dbd8661271220
import com.codahale.metrics.MetricRegistry;
import io.dropwizard.db.DataSourceFactory;
import io.dropwizard.jackson.Jackson;
import io.dropwizard.jdbi.DBIFactory;
import io.dropwizard.setup.Environment;
import liquibase.Liquibase;
import liquibase.database.jvm.JdbcConnection;
import liquibase.exception.LiquibaseException;
import liquibase.resource.ClassLoaderResourceAccessor;
import org.junit.rules.ExternalResource;
@tzachz
tzachz / tzachz.zsh-theme
Created March 15, 2015 15:26
My oh-my-zshell theme
local ret_status="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ %s)"
PROMPT='⌚ %{$fg_bold[red]%}%*%{$reset_color%} ${ret_status}%{$fg_bold[green]%}%p %{$fg[cyan]%}%c %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%} % %{$reset_color%}'
ZSH_THEME_GIT_PROMPT_PREFIX="git:(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}✗%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"
@tzachz
tzachz / spiderman.java
Created December 18, 2013 17:39
Spiderman exersize
public class Spiderman {
public int spiderman(int n) {
// we're going to calculate these for each floor, starting from first floor:
int waysToGetToThisFloorByJumpingTwoFloors = 0; // there's no -1 floor, so there's no way to jump two floors and get to first floor
int waysToGetToThisFloorDirectlyFromPreviousFloor = 1; // there's one way to get from 0 to 1
int totalWaysToGetToThisFloor = 0;
for (int floor=1; floor <= n; floor++) {
// total ways = sum of ways ending with a big jump (2 floors) and ways ending with a small jump (1 floor):
@tzachz
tzachz / OddMockitoStuffTest.java
Created September 24, 2013 15:31
Confusing Mockito behavior with overriding interfaces
package com.kenshoo.rtb.tools;
import org.junit.Test;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Created by IntelliJ IDEA.
@tzachz
tzachz / MyTest.java
Created July 22, 2013 07:22
logback verifier usage
import ch.qos.logback.classic.Level;
import com.kenshoo.test.LogbackVerifier;
import org.junit.Rule;
import org.junit.Test;
public class MyTest {
@Rule
public LogbackVerifier logbackVerifier = new LogbackVerifier();
@tzachz
tzachz / LogbackVerifier.java
Last active September 16, 2018 11:43
logback verifier
package com.kenshoo.test;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.core.Appender;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
@tzachz
tzachz / init.pp
Last active December 14, 2015 07:18
Dropwizard service puppet code
# sets up a dropwizard-based service packaged as debian
# requires: puppetlabs-apt, puppetlabs-stdlib
class sample-dw-service (
$version='latest', # you can pass a specified version, default is latest
$db_url='' # this is how you pass environment-specific configuration - in this case, a DB URL
) {
# refresh apt
notify { "refreshing apt":
notify => Class['apt::update'],