Skip to content

Instantly share code, notes, and snippets.

Avatar

Xianjin YE advancedxy

View GitHub Profile
@advancedxy
advancedxy / vpnc-script
Last active Oct 12, 2017
VPNC script which process no-route from ocserv. There is still a lot of problem. Needs reconsideration.
View vpnc-script
#!/bin/sh
#
# Originally part of vpnc source code:
# © 2005-2012 Maurice Massar, Jörg Mayer, Antonio Borneo et al.
# © 2009-2012 David Woodhouse <dwmw2@infradead.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
@advancedxy
advancedxy / Simulate.scala
Created May 1, 2015
Stackable Trait simulate
View Simulate.scala
trait BeforeAndAfterEach { def beforeEach(): Unit = () }
trait ResetSystemProperties extends BeforeAndAfterEach { override def beforeEach(): Unit = println("reset") }
class A extends BeforeAndAfterEach with ResetSystemProperties { override def beforeEach() { println("test") } }
class B extends BeforeAndAfterEach with ResetSystemProperties { override def beforeEach() { super.beforeEach(); println("test") } }
scala> (new A()).beforeEach
test
scala> (new B()).beforeEach
reset
View Sizeof.java
// ----------------------------------------------------------------------------
/**
* A simple class to experiment with your JVM's garbage collector
* and memory sizes for various data types.
*
* @author <a href="mailto:vlad@trilogy.com">Vladimir Roubtsov</a>
*/
public class Sizeof
{
public static void main (String [] args) throws Exception
@advancedxy
advancedxy / BitMap.scala
Created Jan 29, 2015
simple BitMap impl.
View BitMap.scala
case class BitMap(val n: Int) {
private val bytes = Array.fill(n / 8 + 1)(0.toByte)
def setBit(i: Int) {
bytes(i / 8) = (bytes(i / 8) | (1 << (i & 7))).toByte
}
def unSetBit(i: Int) {
bytes(i / 8) = (bytes(i / 8) & ~(1 << (i & 7))).toByte
}
@advancedxy
advancedxy / ParallelCollectionRDD.sc
Created Jan 20, 2015
This gist reproduce a bug in IntelliJ IDEA Scala plugin's type system
View ParallelCollectionRDD.sc
import scala.collection.immutable.NumericRange
import scala.collection.mutable.ArrayBuffer
import scala.reflect.ClassTag
private object ParallelCollectionRDD {
/**
* Slice a collection into numSlices sub-collections. One extra thing we do here is to treat Range
* collections specially, encoding the slices as other Ranges to minimize memory cost. This makes
* it efficient to run Spark over RDDs representing large sets of numbers. And if the collection
* is an inclusive Range, we use inclusive range for the last slice.
View gist:bbf6eb58f0af94521cf2
public int whileLoop(int);
descriptor: (I)I
flags: ACC_PUBLIC
Code:
stack=2, locals=3, args_size=2
0: iconst_0
1: istore_2
2: iload_2
3: iload_1
4: if_icmpge 16
@advancedxy
advancedxy / convert_text_chunzhen_bin.py
Last active Aug 29, 2015
advancedxy.com's blog related code
View convert_text_chunzhen_bin.py
#coding:utf-8
import sys,struct,socket,array,datetime
def ip2str(ip):
return str(ip>>24)+'.'+str((ip>>16)&0xffL)+'.' \
+str((ip>>8)&0xffL)+'.'+str(ip&0xffL)
def str2ip(s):
(ip,) = struct.unpack('!I',socket.inet_aton(s))
return ((ip>>24)&0xffL)|((ip&0xffL)<<24) \
@advancedxy
advancedxy / downxml.py
Last active Aug 29, 2015
advancedxy.com's post related code
View downxml.py
#!/usr/bin/python2
#coding=utf-8
import os
import urllib2
import zipfile
import zlib
from urlparse import urlparse
from time import strftime,localtime
def initsites(filename):
@advancedxy
advancedxy / rack_info.py
Created Jul 9, 2014
hadoop rack awareness script
View rack_info.py
#!/usr/bin/env python
import sys
import socket
data = """
100.200.208.12-100.200.208.46 : /xxx/rack_1
"""
def ip_integer_from_string(s):
return reduce(lambda a,b: a<<8 | b, map(int, s.split(".")))
View route
2.0.0.0/7
4.0.0.0/6
8.0.0.0/7
11.0.0.0/8
12.0.0.0/7
15.0.0.0/8
16.0.0.0/5
24.0.0.0/7
26.0.0.0/8
28.0.0.0/6