2013-08-30

Java: Running any type of Callable with any kind of constructors

Java 1.7
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.*;

public class ParallelCaller {

    public static final int NUM_CORES = 2 * Runtime.getRuntime().availableProcessors();
    private ExecutorService eservice = Executors.newFixedThreadPool(NUM_CORES);
    ;
    private CompletionService<Object> cservice = new ExecutorCompletionService<>(eservice);

    public void Parallel(int numtasks, int iterations, Class clazz, Object... args) throws NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        long begTest = new java.util.Date().getTime();

        for (int index = 1; index <= numtasks; index++) {
            cservice.submit((Callable<Object>) clazz.getConstructors()[0].newInstance(args));
        }

        eservice.shutdown();

        Object taskResult;
        for (int index = 0; index < numtasks; index++) {
            try {
                taskResult = cservice.take().get();
                System.out.println("result " + taskResult);
            } catch (ExecutionException e) {
                System.out.println(e.getMessage());
            } catch (InterruptedException e) {
                System.out.println(e.getMessage());
                // (Re-)Cancel if current thread also interrupted
                eservice.shutdownNow();
                // Preserve interrupt status
                Thread.currentThread().interrupt();
            }
        }

        Double secs = new Double((new java.util.Date().getTime() - begTest) * 0.001);
        System.out.println("run time " + secs + " secs");
    }
}

Usage:
public static void main(String[] args) throws NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        int parallel = args.length > 0 ? Integer.parseInt(args[0]) : 5;
        int iterations = args.length > 1 ? Integer.parseInt(args[1]) : 100;

        new ParallelCaller().Parallel(parallel, iterations, LoginTest.class, "http://somethingtotest.com", "testuser02", "testuser02", iterations, LoginTest.KnownDrivers.Firefox);
    }
The referred class should implement Callable.

Java: Parallel.For

This piece is from StackOverflow:

public class Parallel {
    private static final int NUM_CORES = Runtime.getRuntime().availableProcessors();

    private static final ExecutorService forPool = Executors.newFixedThreadPool(NUM_CORES  * 2);

    public static void For(final Iterable<T> elements, final Operation<T> operation) {
        try {
            // invokeAll blocks for us until all submitted tasks in the call complete
            forPool.invokeAll(createCallables(elements, operation));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static Collection<Callable<Void>> createCallables(final Iterable<T> elements, final Operation<T> operation) {
        List<Callable<Void>> callables = new LinkedList<Callable<Void>>();
        for (final T elem : elements) {
            callables.add(new Callable<Void>() {
                @Override
                public Void call() {
                    operation.perform(elem);
                    return null;
                }
            });
        }

        return callables;
    }

    public static interface Operation<T> {
        public void perform(T pParameter);
    }
}

Usage:
// Collection of items to process in parallel
Collection<Integer> elems = new LinkedList<Integer>();
for (int i = 0; i < 40; ++i) {
    elems.add(i);
}
Parallel.For(elems, 
 // The operation to perform with each item
 new Parallel.Operation<Integer>() {
    public void perform(Integer param) {
        System.out.println(param);
    };
});

Java: Simple multithreading a piece of code

import java.util.*;
import java.util.concurrent.*;

// ...

ExecutorService exec = Executors.newFixedThreadPool(2 * Runtime.getRuntime().availableProcessors());
try {
    for (int i = 0; i < num_of_runs; i++) {
        exec.submit(new Runnable() {
            @Override
            public void run() {
                // do stuff in num_of_runs times through a 2*#CPU threadpool
            }
        });
    }
} finally {
    exec.shutdown();
}

2013-08-29

C, MySQL: A minimal UDF

An example, how to start writing an UDF for MySQL.
#include <stdio.h>
#include <string.h>

#include <mysql/my_global.h>
#include <mysql/my_sys.h>
#include <mysql/mysql.h>
#include <mysql/m_ctype.h>
#include <mysql/m_string.h>

#define UUID_LEN 36
#define FMASK "%36s"
#define UUID "/proc/sys/kernel/random/uuid"

my_bool newid_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
void new_deinit(UDF_INIT *initid);
char *newid(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);

my_bool newid_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
    if (args->arg_count != 0 || args->arg_type[0] != STRING_RESULT)
    {
        strcpy(message, "Wrong arguments to newid; Use the source");
        return 1;
    }
    initid->max_length=UUID_LEN;
    return 0;
}

void newid_deinit(UDF_INIT *initid)
{
//
}

char *newid(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error)
{
FILE *fp;
char id[UUID_LEN];

    if ((fp = fopen(UUID, "r")) != NULL) {
        fscanf(fp, FMASK, id);
        memcpy(result, id, initid->max_length); // UUID_LEN
        *length = initid->max_length; // UUID_LEN
        *error = 0;
        *is_null = 0;
        return result;
    }
    *is_null = 1;
    *error = 1;
    free(result);
    return NULL;
}

Provides a newid function, which returns an UUID from the kernel's random pool.

C: conversion from int to string in a given base

char* baseconv(unsigned long long num, int base)
{
 char* retbuf = (char*)calloc(65, sizeof(char));
 char *p;

 if(base < 2 || base > 16)
  return NULL;

 p = &retbuf[64];
 *p = '';

 do {
  *--p = "0123456789abcdef"[num % base];
  num /= base;
 } while(num != 0);

 return p;
}

The base can be any integer between 2 and 16. If you expand the string on line 13, you can increment the limit in line 6.

Why is retbuf 65 char long? The largest parameter value we can get is 264-1. In hexadecimal is 0xFFFFFFFFFFFFFFFF, 16 pieces of "0xF", plus the '\0' at the end. Without the "0x", of course, which we do not prepend.
But in binary, 264-1 = 11111111111111111111111111111111111111111111111111111111111111112, 64 pieces of "1", plus the final '\0'.

C#: If you stuck with an Excel interop

Error message:
System.Runtime.InteropServices.COMException (0x80028018): Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))

Solution:
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
WB = XL.Workbooks.Open(newfilepath, ...);

Set the CurrentCulture to en-US just before the Workbooks.Open(). This is required because the one and only VBA language supported by Excel is en-US.
And no, the Office MUI does NOT exists (altough it is often referenced on msdn...)

SQL: why you cannot use regex to validate a select

Regular expressions can match languages only a finite state automaton can parse, which is very limited, whereas SQL is a syntax. It can be demonstrated you can't validate SQL with a regex. So, you can stop trying.
SQL is a type-2 grammar, it is too powerful to be described by regular expressions. It's the same as if you decided to generate C# code and then validate it without invoking a compiler. Database engine in general is too complex to be easily stubbed.

C#: Writing xlsx from C#

http://simpleooxml.codeplex.com/

C#: Crosscontext calls in WPF

public static class DispatcherObjectExtensions{
 public static TRet myInvoke(this T source, Func func)  where T:DispatcherObject {
  return (TRet)source.Dispatcher.Invoke(func);
 }
 public static void myInvoke(this T source, Action act) where T:DispatcherObject {
  source.Dispatcher.Invoke(act);
 }
}

This will put these two extension methods on all DispatcherObject descendants (like System.Windows.Window), and you can call a delegate put in the object's thread/context.

C#: Anonymous delegates asynchronously

this.BeginInvoke(new Action(delegate
{ 
 MessageBox.Show("Asynchronous :D"); 
}));

System.Action is a framework-defined Delegate successor, that does not return anything and doesn't take any arguments. You should use this, instead of fucking around with inheriting from System.Delegate.

C: Log a message through a macro with the current function name and line number

// macro magic :D:D
 #define STRINGIFY(x) #x
 #define TOSTRING(x) STRINGIFY(x)
 #ifdef DEBUG
     #define LOG(x, ...) logzor( "NIHLEDTEST " __FILE__  ":"   \
          TOSTRING(__LINE__) " : " __FUNCTION__  "() ==> " x "n", __VA_ARGS__)
 #else
     #define LOG(x, ...)
 #endif
 
 #ifdef DEBUG
 static __inline void logzor(char *msg, ...)
 {
     va_list argptr;
     va_start(argptr, msg);
     vfprintf(stderr, msg, argptr);
     va_end(argptr);
 }
 #endif

2013-08-07

Oracle: delete duplicate rows from a table, to have only one row for each unique expression

DELETE FROM EXTERNAL_ENTITIES
WHERE
   ROWID IN
   (
      SELECT "ROWID" -- ESCAPED, BECAUSE WE NEED THE INNER ROWID, NOT THIS TMPTABLE'S ROWID
      FROM
         (
            SELECT
               RANK() OVER(PARTITION BY NAME ORDER BY ROWID) RANK_N, -- ORDER IN THE SUBGROUP
               ROWID AS "ROWID"                                    , -- ESCAPE THE ROWID
            FROM EXTERNAL_ENTITIES
            WHERE
               NAME IN
               (
                  SELECT NAME
                  FROM EXTERNAL_ENTITIES
                  GROUP BY NAME
                  HAVING COUNT(*) > 1 -- ONLY DUPLICATES OR MORE
               )
         )
      WHERE RANK_N > 1
   );
or
DELETE FROM CUSTOMFIELDVALUE
WHERE ROWID IN
   (SELECT T."ROWID" FROM
      (SELECT RANK() OVER(PARTITION BY ISSUE, CUSTOMFIELD ORDER BY ROWID) RANK_N, ROWID AS "ROWID"
         FROM CUSTOMFIELDVALUE
         WHERE CUSTOMFIELD IN (10180, 10216, 10336, 10340, 10464, 10550, 10590, 10911, 14714) -- customfield ID-s come here, taken from the log.
      ) T
   WHERE T.RANK_N > 1);

Oracle: Get bind variables inline'd

This should be run as a script (F5), in SQL Developer. The serveroutput enabling syntax may differ in other clients. Sorry. Run it as SYS.
This query tries to put inline the bind values, if they were captured.
EXECUTE DBMS_MONITOR.DATABASE_TRACE_ENABLE( BINDS => TRUE, WAITS => TRUE );
ALTER SYSTEM SET "_cursor_bind_capture_interval" = 3999;
ALTER SYSTEM SET "_xpl_peeked_binds_log_size" = 8192;

DECLARE
  V_CHILD_ADDRESS VARCHAR2(64) ;
  V_SQLTEXT       VARCHAR2(4000) ;
  V_BINDNAME      VARCHAR2(30) ;
  V_VALUE         VARCHAR2(4000) ;
  V_WASCAPTURED   VARCHAR2(3) ;
  V_DATATYPE      VARCHAR2(32) ;
  CURSOR C_BIND IS
    SELECT
      NAME        ,
      VALUE_STRING,
      WAS_CAPTURED,
      DATATYPE_STRING
    FROM GV$SQL_BIND_CAPTURE
    WHERE CHILD_ADDRESS = V_CHILD_ADDRESS
    ORDER BY NAME DESC;
BEGIN
  DBMS_OUTPUT.ENABLE(BUFFER_SIZE => NULL) ;
  FOR ITEM IN
  (SELECT CHILD_ADDRESS, SQL_TEXT
    FROM
      (SELECT
          SQL_ID        ,
          CHILD_ADDRESS ,
          SQL_TEXT      ,
          LAST_LOAD_TIME,
          ROW_NUMBER() OVER(PARTITION BY SQL_ID ORDER BY LAST_LOAD_TIME DESC) AS RN
        FROM GV$SQL
          WHERE SERVICE = (SELECT DISTINCT LOWER(VALUE || '_user') FROM GV$PARAMETER WHERE NAME = 'db_unique_name')
          AND SQL_TEXT NOT LIKE '%I will be filtered out.%'
      ) SQ
    INNER JOIN GV$SESSION SS ON SS.PREV_SQL_ID = SQ.SQL_ID
    WHERE RN = 1
    ORDER BY PREV_EXEC_START DESC
  )
  LOOP
    V_CHILD_ADDRESS := ITEM.CHILD_ADDRESS;
    V_SQLTEXT       := ITEM.SQL_TEXT;
    OPEN C_BIND;
    LOOP
      FETCH C_BIND INTO V_BINDNAME, V_VALUE, V_WASCAPTURED, V_DATATYPE;
      EXIT WHEN C_BIND%NOTFOUND;
      IF V_WASCAPTURED = 'NO' THEN
        V_SQLTEXT := REPLACE(V_SQLTEXT, V_BINDNAME, '''(THE ' || V_DATATYPE || ' VALUE WAS NOT CAPTURED)''') ;
      ELSE
        V_SQLTEXT := REPLACE(V_SQLTEXT, V_BINDNAME, '''' || SUBSTR(V_VALUE, 0, 500) || '''') ;
      END IF;
    END LOOP;
    CLOSE C_BIND;
    DBMS_OUTPUT.PUT_LINE(V_SQLTEXT || ';') ;
  END LOOP;
END;

Oracle: get a lot of variables

Run this query as SYS user:
SELECT ksppinm AS param_name, ksppdesc AS descr, ksppstvl AS param_value, ksppstdvl AS default_value, ksppstcmnt AS comments
FROM x$ksppi
INNER JOIN x$ksppcv USING(indx)
WHERE ksppinm LIKE '%bind_capture%' -- This line is optional, of course
ORDER BY ksppinm;