Blog Archive 18 entries
Welcome to the archives of my rumblings. New content will appear.rDBLP version 0.4.5 released
DBLP version 0.4.5 is more or less a maintenance release. It fixes several connection problems and as an improvement removes the cross-reference from the BibTeX entry. The book title in the main entry is now replaced by the title of the cross-reference. This should be more convenient with regards to the limited space in a research paper.
To update your gem execute
sudo gem install dblp
For more detail see my original post.
Emacs Movement Shortcuts Wallpaper
Stumbling across the Vim Movements Shortcuts Wallpaper, I decided to make one for Emacs. Basically I really like the idea of having a real quick reference to the most important shortcuts on your desktop.
If you have any additional shortcuts that you would love to have, please leave a comment. If you want to modify the wallpaper, I uploaded the original PDF version of the wallpaper that you can easily modify with any tool of your choice.
Switch, case, typelists and type_switch
Whenever you are building a system that has it’s own type system you will come to a point where you perform type dependent operations. If your types seamlessly map to standard integral types most of the mapping code and extraction can be handled by simple template methods, but from time to time you will find the following code fragments:
switch(type)
{
case IntegerType: /**/
do_something_important<int>(value);
break;
case DoubleType: /**/
do_something_important<double>(value);
break;
}
Interestingly the only difference in the above code line is only the requested type. A concrete example is e.g. hashing of a certain value. The type of the value is stored in a variable and depending on the actual type different hash functions have to be called. When you find something like this the first time, you will feel ok, the second time a little more nervous and the third time…
The question is now: How can I rewrite my code so that it will be less explicit and most important easier to extend. The biggest problem with the above solution is that once you extend your type system the whole code will be changed and there must be an easier way to solve this.
One of my first approaches was macro magic to iterate over a sequence and than generate the right code by text expansion. However this will not work out due to the fact that macros in C++ are not recursive and will not be called a second time. Reading in “Modern C++ Design” by Alexandrescu – a must read – I stumbled upon typelists (well I had some support by @bastih01). After one evening screwing my had around them finally I made some progress.
The solution I found is based on static recursive template generation plus dynamic type switching at runtime. The reason for this rather complicated approach is the following: while the general type information is available at compile-time, the explicit instance related type information can only be mapped using an enum at run-time.
Enough words lost, whats the solution?
Consider the following setup: First we define the type list and the enum for storing the type information.
#include <boost/mpl/vector.hpp>
typedef enum
{
IntegerType,
FloatType,
StringType
} DataType
typedef boost::mpl::vector<int, float, std::string> basic_types;
Now we need to implement our type_switch operator, basically it is based on TinyTL but used in a Boost environment, because I did not find anything alike in Boost directly.
template <typename L, int N=0, bool Stop=(N==boost::mpl::size<hyrise_basic_types>::value)> struct type_switch;
template <typename L, int N, bool Stop>
struct type_switch
{
template<class F>
typename F::value_type operator()(size_t i, F& f)
{
if (i == N)
{
return f.operator()<typename boost::mpl::at_c< hyrise_basic_types, N>::type>();
} else {
type_switch<L, N+1> next;
return next(i,f);
}
}
};
template <typename L, int N>
struct type_switch<L, N, true>
{
template<class F>
typename F::value_type operator()(size_t i, F& f)
{
throw std::runtime_error("Type does not exist");
}
};
If you look at the above code for the first time it is kind of weird to understand what is going on. But once you understand template recursion it’s totally clear. But easy things first: boost::mpl::size defines a template that contains the size of the typelist hyrise_basic_types. The boost::mpl::at_c template defines an random accessor to the typlist based on a constant index.
The template type_switch is a special construct with 3 parameters, the first is the type list, the second is the current position in this list defaulting to 0, and the third is a boolean parameter determining if the recursion should stop. The default implementation of this struct with the operator() method checks if the current value i is equal to N and if this is the case calls the operator() method on the function object submitted as a parameter. If this is not the case it instantiates a new template and increases N by 1. Tnhis is possible because all int values for the complete list are known in advance at compile time and so they can be used as template parameters. To avoid infinite recursion a dedicated template specialization with Stop=true provides an implementation that should never be called and does not further invoke any template recursion.
But back to the functor used in this setting. We have one requirement on the functor and this is that we have to specify the value_type of the operator() method directly in the functor. A sample implementation based on boost hash could look like the following.
template<typename T>
struct hash_functor
{
typedef T value_type;
AbstractTable* table;
size_t f;
ValueId vid;
hash_functor(): table(0) {}
hash_functor(AbstractTable * t, size_t f, ValueId v): table(t), f(f), vid(v) {}
template<typename R>
T operator()()
{
return boost::hash<R>()(table->getValueForValueId<R>(f, vid));
}
};
For this functor, T defines the return type of the functor with an valye_type typedef and R is the type of the actual type used for the type_swtich. Instead of the clustered switch case statement the code for my type depended hash value method looks a lot better.
size_t hash_value(AbstractTable * source, size_t f, ValueId vid)
{
hash_functor<size_t> fun(source, f, vid);
type_switch<hyrise_basic_types> ts;
return ts(source->typeOfColumn(f), fun);
}
The last sentence goes to the cost of this access: Each level of hierarchy generates at least one method call plus an evaluation of an if statement. The generic switch/case only generates the comparison, but I think this overhead is neglectable compared to the huge amount of time saved when it comes to extending the usable data types.
rDBLP -- Easy BibTeX Management for your Research Paper
Maintain your LaTex bibliography files using DBLP
It’s always the same, you write a paper / thesis and you are searching for source, than you modify them until they fit into the format you are currently using. But this should not be. A better way is to extract the right key from DBLP and automagically create the correct bib file. You think this is magic? No it’s not, it’s so easy.
There are only two prerequisites for using rDBLP on your computer:
- Make sure Ruby and RubyGems are installed
- Make sure you have a working internet connection
To install the gem execute this from you command line:
sudo gem install dblp
Once this is done you can use it directly for any LaTex file. Imagine you have LaTex file containing somewhere the following citation.
The entity shaping used in web services as discussed in \cite{DBLP:conf/IEEEscc/GrundKZ08}...
\bibliographystyle{abbrv}
\bibliography{dblp}
When you run now the dblp command in your terminal, the program will read the auxiliary files from the compilation and extract the requested DBLP citation sources. Than it will download them and store them directly in a file called “dblp.bib” which you can use in your LaTex document.
dblp my_file
To make it really easy, here is a screencast I did for my small little tool:
If you have any questions, feel free to ask via mail or contact me using Twitter.
Relational DBMS vs Document Oriented Key-Value Stores?!?!
In the recent days document databases get big buzz from all different areas: programmers who search for freedom or VCs who search for the next big buck. But there is a big disadvantage: the bigger the noise is the less the people using buzzwords like cloud or key-value store, understand whats behind. So let me start with a short disclaimer: I am not against key-value stores or any other document oriented database!
But nonetheless I think it is required to clarify what a standard relational database can do, especially how powerful a language like SQL is. So lets clarify this by some examples and let me give you my take on the buzz. To make it more entertaining consider this a small Q&A session with a key-value store guy and me as the defender of relational DBMS.
"When I'm working with documents I just dump everything into the object and can map-reduce everything! SQL cant do this."Sure you can dump anything in an object, but SQL has a different heritage: SQL is a declarative language that is designed to express the What instead of the How. If you want to express algorithms directly on the database use stored procedures!
"But stored procedures are bad, because they are not portable!"Ah get that, but how about your custom map-reduce code? Writing custom code for querying data is nothing more than using the data store API directly and is a stored procedure.
"In SQL I can't create my own functions, I'm limited to MIN,MAX,AVG. That's so bad!"Whew, I hope that you did not stop reading about SQL in ‘89 or ’92. The SQL standard provides everything you need to define user-defined functions and custom functions. Any major DBMS allows you to even write them in any programming language you can program.
"You know, I want to be flexible, stream content of different types. SQL cant do this!"Maybe you are missing a point here that is called normalization. Yes, SQL is restricted to flat tables but you can use normalization and queries to create any list of your content.
"But SQL is so slow, every time I create a join I'm lost. Look, XXX-DB is so fast using static indices once I created a map function!"Dude, using and index to increase query performance is as old as I am. You want indices, go create them! If you don’t know what queries you use, you wrote a bad application. For any other case you might go for index optimization techniques well researched in the last decades.
"But it runs in the cloud! This makes it even more faster and scalable."Gnargh, I’m pretty sure you know about all this parallel database research going on since — decades. Just because it is a key-value store does not make it scalable and faster. Please dig deeper.
To conclude, please understand me I’m not against key-value stores or document databases, they have their very specific terrain — as do standard relational databases. So what might be a good example for a key-value store document oriented application? When looking at the characteristics of such databases one thing that comes to my mind immediately is the following: Since every object is stored by a unique key all those applications benefit where the workload is almost only single object lookup and no aggregations take place. Since hash functions that are used to build the indices for the keys are ideal for partitioning scale-out can be implemented easier. Another important fact is the ability for semi-structured data, here document oriented storage system clearly win over relational systems.
The only thing I wish from people propagating document oriented storage is that they do not only talk about it because its cool but because they can sketch a valid use case. If you feel like you don’t know enough about the possibilities of standard relational databases talk to people who do or read something about it.
Did I offend you? Challenge me, let’s discuss! - Martin