Skip to content

Instantly share code, notes, and snippets.

@suresk
suresk / AdminAuthorizeAttribute.cs
Created December 15, 2011 23:41
Unity component locator, and sample
public class AdminAuthorizeAttribute : AuthorizeAttribute
{
[Dependency]
public AccountHelper AccountHelper { get; set; }
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext == null) throw new ArgumentNullException("httpContext");
@suresk
suresk / gist:1811157
Created February 12, 2012 22:22
Caeser Cipher Scala
def printChar(i: Int) = print(i.toChar)
def decipherChar(c: Char, n: Int) = c match {
case x if x < 97 || x > 122 => printChar(x)
case x if x - n < 97 => printChar(122 - (97 - (x -n)) + 1)
case _ => printChar(c - n)
}
def decipher(str: String, n: Int) = str.toLowerCase.toArray.foreach(decipherChar(_, n))
@suresk
suresk / gist:3223267
Created August 1, 2012 03:07
FF Draft Order
import random
import sys
if (len(sys.argv) > 1):
list = open(sys.argv[1]).readlines()
random.shuffle(list)
for team in list:
print team.strip()
else:
print("You need to specify the file name.")
@suresk
suresk / parser.js
Created October 3, 2012 06:13
Part of my Java Class file parser
function Typedef(bytes) {
this.bytes = bytes;
};
Typedef.prototype.getData = function(data, start){
var values = data.slice(start, start + this.bytes);
var c = 0;
for(i = 0; i < values.length; i++) {
var offset = this.bytes - (i + 1);
c += values[i] * Math.pow(2, offset);
@suresk
suresk / gist:19f16576ed7cecec6576db261f4659cc
Last active April 10, 2016 05:55
Elasticsearch get distributed lock
private boolean getLock(String aliasName)
{
synchronized (aliasName)
{
IndexRequestBuilder indexRequest = elasticClient.prepareIndex(LOCK_DIRECTORY, LOCK_TYPE, aliasName).setSource("{}").setOpType(OpType.CREATE);
boolean successful = false;
try
{
IndexResponse response = indexRequest.get();
@suresk
suresk / gist:fe07f27c22463dbbff0454e40f1e4e53
Created April 10, 2016 05:59
Elasticsearch determine new index
private int findCurrentIndex(String aliasName, int indexCount)
{
Map<String, AliasOrIndex> allAliases = elasticClient
.admin()
.cluster()
.state(Requests.clusterStateRequest())
.actionGet()
.getState()
.getMetaData()
.getAliasAndIndexLookup();
@suresk
suresk / gist:ffc05117c36cd3cabc66be69f95f58c0
Created April 10, 2016 06:02
Elasticsearch delete index if it exists
// Delete it, if it already exists
if(elasticClient.admin().indices().exists(new IndicesExistsRequest(indexName)).actionGet().isExists())
{
elasticClient.admin().indices().delete(new DeleteIndexRequest(indexName));
}
// Create the index
Settings.Builder settingsBuilder = Settings.builder();
for(Map.Entry<String, Integer> setting : config.getIndexSettings().entrySet())
{
settingsBuilder.put(setting.getKey(), setting.getValue());
}
elasticClient.admin().indices().create(new CreateIndexRequest(indexName, settingsBuilder.build()));
@suresk
suresk / gist:38012ca8845019a9a8ef09e2659627d8
Created April 10, 2016 06:07
Elasticsearch put mapping
String mapping = getMapFile(aliasName);
if(mapping != null)
{
elasticClient.admin().indices().putMapping(new PutMappingRequest(indexName).source(getMapFile(aliasName)));
}
...
private String getMapFile(String aliasName)
@suresk
suresk / gist:18f6db58b52fb770381328bfca9d3341
Created April 10, 2016 06:09
Elasticsearch rebuild job
// Do the indexing
T res = job.rebuildIndex(indexName);
....
public interface IndexRebuildJob<T>
{
public T rebuildIndex(String indexName);
}