Skip to content

Instantly share code, notes, and snippets.

View advancedxy's full-sized avatar
:octocat:

advancedxy

:octocat:
View GitHub Profile
@advancedxy
advancedxy / TestDistribution.java
Created July 19, 2023 03:36
Bucket distribution
/*
*
* * Licensed to the Apache Software Foundation (ASF) under one
* * or more contributor license agreements. See the NOTICE file
* * distributed with this work for additional information
* * regarding copyright ownership. The ASF licenses this file
* * to you under the Apache License, Version 2.0 (the
* * "License"); you may not use this file except in compliance
* * with the License. You may obtain a copy of the License at
* *
@advancedxy
advancedxy / vpnc-script
Last active October 12, 2017 13:59
VPNC script which process no-route from ocserv. There is still a lot of problem. Needs reconsideration.
#!/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 22:38
Stackable Trait simulate
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
// ----------------------------------------------------------------------------
/**
* 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 January 29, 2015 07:45
simple BitMap impl.
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 January 20, 2015 02:00
This gist reproduce a bug in IntelliJ IDEA Scala plugin's type system
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.
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 August 29, 2015 14:13
advancedxy.com's blog related code
#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 August 29, 2015 14:13
advancedxy.com's post related code
#!/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 July 9, 2014 05:41
hadoop rack awareness script
#!/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(".")))