Skip to content

Instantly share code, notes, and snippets.

View schmohlio's full-sized avatar

Matthew Schmohl schmohlio

View GitHub Profile
@schmohlio
schmohlio / sse.go
Last active February 13, 2023 08:38 — forked from ismasan/sse.go
Example SSE server in Golang
// v2 of the great example of SSE in go by @ismasan.
// includes fixes:
// * infinite loop ending in panic
// * closing a client twice
// * potentially blocked listen() from closing a connection during multiplex step.
package main
import (
"fmt"
"log"
@schmohlio
schmohlio / sortByteSlice.go
Created September 7, 2016 21:37
sort byte slices in Golang without needing to fmt as string. useful for Set hashes
package main
import (
"bytes"
"log"
"sort"
)
// implement `Interface` in sort package.
type sortByteArrays [][]byte
@schmohlio
schmohlio / TextEditorBuffer.java
Created January 14, 2019 00:44
Text Editor Buffer with 2 Stacks (Sedgewick Algorithms) Example
public class GapBuffer {
private final Stack<Char> left, right;
private final int n;
public GapBuffer() {
left = new Stack<>();
right = new Stack<>();
n = 0;
package latch.common.utils;
import com.google.common.collect.Lists;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
@schmohlio
schmohlio / Json2JavaMap.scala
Created July 18, 2016 23:34
parse Json to Generic Java Map in Scala, similar to Jackson.
import com.google.gson.Gson
import java.util.{Map => JMap, LinkedHashMap}
type GenericDecoder = String => JMap[String, Object]
val decoder: GenericDecoder = {
// Gson instances are apparently thread-safe, so curry...
val gson: Gson = new Gson()
// LinkedHashMap preserves ordering. use HashMap if not required.
x => gson.fromJson(x, (new LinkedHashMap[String, Object]()).getClass)
@schmohlio
schmohlio / prepend.py
Last active August 10, 2017 23:42
Prepend text to files matching pattern in current directory
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
from os.path import join
TEXT = """syntax = "proto2"\n"""
def main():
@schmohlio
schmohlio / remove_empty_part_files.sh
Last active May 3, 2017 06:22
delete or warn on empty part files in hadoop, by extension.
#!/bin/bash -e
""" USAGE: ./remove_empty_part_files.sh <qualified hdfs dir path> """
HDFS=$1
echo "checking for empty files in $HDFS..."
IFS=$'\n'
for i in `hadoop fs -ls $HDFS/* | grep -e "$HDFS/.*" | awk '{print $0}'` ; do
file=$(echo $i | awk '{print $8}')
size=$(echo $i | awk '{print $5}')
if [ $size -eq 0 ]; then
@schmohlio
schmohlio / NestedMap.java
Created April 21, 2017 22:23
Variable Depth Java HashMap
class NestedMap<K, V> {
private final HashMap<K, NestedMap> child;
private V value;
public NestedMap() {
child = new HashMap<>();
value = null;
}
@schmohlio
schmohlio / gist:4157d2fa82f3680fd08f1b111ed61515
Created April 10, 2017 03:16 — forked from mikepfeiffer/gist:4d9386afdcceaf29493a
EC2 UserData script to install CodeDeploy agent
#!/bin/bash
yum install -y aws-cli
cd /home/ec2-user/
aws s3 cp 's3://aws-codedeploy-us-east-1/latest/codedeploy-agent.noarch.rpm' . --region us-east-1
yum -y install codedeploy-agent.noarch.rpm
@schmohlio
schmohlio / BlockingQueue.java
Created February 16, 2017 05:04 — forked from dougnukem/BlockingQueue.java
Example Threadsafe BlockingQueue implementation in Java
public class BlockingQueue implements Queue {
private java.util.Queue queue = new java.util.LinkedList();
/**
* Make a blocking Dequeue call so that we'll only return when the queue has
* something on it, otherwise we'll wait until something is put on it.
*
* @returns This will return null if the thread wait() call is interrupted.
*/