Data structures and Domain Driven Design

After listening to a presentation about Domain Driven Security, I got some additional insights about what a data structure (as opposed to OOP object) should be able to do. Data structures (I call them struct) should have a subset of OOP features which I think is useful and harmless.

A struct should allow constructors. Such constructors can have two purposes, to convert one type of data into another and to validate the input. Such constructors should take parameters and be able to throw an exception if the input is invalid, to make it impossible to create an invalid instance of a struct. Each constructor should be required to set every field of the struct, and if no constructors are defined, a default one should be generated which simply takes all fields as parameters.

It can also be useful to have derived fields, which would be parameter-less function which return a value computed from one or more fields in the struct. They should be pure functions from the current state of the struct.

There should be encapsulation so that fields can be marked as public or private. Which one should be default?

structs should be immutable by default, with an option to mark individual fields of them mutable. However, it should be required to mark the whole struct as mutable to be able to mark any field of it mutable. A mutable field cannot be private and it’s value can be set from outside (typically with assignment syntax).

structs should not have any inheritance or polymorphism, and should not have any methods.

structs should be value types without identity, like a struct in C# or a primitive type in Java.

Posted in programming | Leave a comment

Objects vs. data structures

Several popular statically typed programming languages, including C++, Java and C#, have a serious design flaw. They make no useful distinction between objects in the OOP sense and plain data structures.

A plain data structure should only be a dumb container for data. It should not have any behaviour, only simple accessors for data items in it. It should not be possible to override data accessors. It should be clear and transparent what it does.

Good old C has a working construct for plain data structures, the struct.

C++, even though trying to be compatible with C, managed to mess it up. C++ added class for OOP objects, but also extended struct to be more or less equivalent to class. Even though there seems to be a convention in C++ to use struct for plain data structures and class for OOP objects, there is no guarantee that a struct in fact is just a plain data structure without surprising behaviour.

Java made it worse by skipping struct completely and providing no other construct for plain data structures than class which is primarily made for OOP objects. In the Java community, there is no general convention for making any distinction. To make things even worse, the horribly verbose Java Beans convention is quite popular and has support in the standard library.

C# actually have a meaningful difference between struct and class, making struct more suitable for plain data structures. However, it seems like struct have too much OOP like functionality built in and that it is possible to provide surprising behaviour.

Scala has class for OOP objects, but does also provide case classes which are suitable for plain data structures. Case classes also have some bonus features like patten matching and auto generated methods for equality, hash code and string representation. Together with Scala language features like named parameters and immutability by default, they work reasonably well as plain data structures. However, I wish they could be more restrictive and not allow OOP features like general inheritance (which does not work well for case classes anyway) and overriding accessors.

It is possible to use associative arrays as data structures, and then you get no surprising behaviour. This is popular in Javascript which have nice syntax for this. However, most other languages do not have a convenient syntax for this, and in particular it does not work well in a statically typed language. I like to declare my data structures, and it should not be possible to add arbitrary values to instances of them at runtime. Associative arrays usually also have a runtime performance overhead.

It is also possible to use tuples (which some languages have) as data structures, but I want the items in them to be named, not ordered.

Is my only choice to use C?

Updated: mentioned that it is about statically typed languages

Posted in Java, programming | 2 Comments

Typesafe’s Reactive Straw man

In their quest to promote Reactive, Typesafe is beating up a straw man by portraying blocking I/O in a particularly stupid way which is rarely (if ever) done in practice.

In a recent webinar, I found this slide which suggests that a blocking I/O operation will waste CPU time while waiting for the I/O to complete.

If I understand it correctly, it does not actually work like this in any reasonable runtime environment / operating system. As an example, consider doing java.io.InputStream.read() on a socket in Java (or any other language on the JVM) on Linux. If there is not data available in the buffer, this call will block until some packet is received by the network interface, which may take several seconds. During that time, the JVM Thread is blocked, but the JVM and/or Linux kernel will reschedule the CPU core to another thread or process and will wait for the network interface to issue an interrupt when some packet is received. You will waste some time on thread scheduling overhead and user/kernel mode switching, but that’s typically much less than the I/O waiting time.

I am quite sure it will work the same way on other runtime environments (such as .Net) and operating systems (such as Microsoft Windows, Solaris, Mac OS X).

There are other reasons why blocking I/O can be problematic, and the Reactive principles are useful in many cases. But please be honest and don’t portray blocking I/O worse than it actually is.

Posted in Java, Linux, reactive | 2 Comments

Leafpad > gedit

I want a simple, fast and lightweight text editor for my Linux desktop. I don’t want to learn a lot of new arcane key bindings, so not Vim or Emacs. I want a real GUI desktop application, not a console based one, so not nano (even though nano is nice when you only have a text console available). I don’t want a full-blown IDE for programming (I already have that) so I don’t need syntax highlighting and similar features.

So far I have been using gedit which is bundled as default text editor with Ubuntu. But it is not lightweight and sometimes really slow. It is particularly bad at handling large files and slow at searching, searching for a word in a 5 MB text file was painfully slow on my quite fast computer.

I got fed up with gedit and went out to look for alternatives. I found Leafpad and really like it. It is available in the standard Ubuntu APT repository, so it is trivial to install. It is GTK+ based (just like gedit), so it integrates nicely with the Unity desktop, it even supports the Unity global menu bar.

Leafpad is really lightweight and much faster than gedit. Searching through a 5 MB text file is not a problem at all.

Leafpad lacks some of the features that gedit have. It doesn’t support multiple files in tabs, but since it’s lightweight you can start multiple instances of it in separate windows, and the tab support in gedit is not very good anyway. It does not support syntax highlighting, but for me gedit is not good enough for serious programming anyway, I want a real IDE with more than just syntax highlighting for that.

Now gedit is uninstalled from my machine and I don’t miss it.

Posted in Linux, Ubuntu | 4 Comments

How to fix keyboard layout in Ubuntu 14.04

I regularly use Swedish keyboard layout, but I keep the English layout around in case I would like to temporary switch to it.

Ubuntu 14.04 sometimes mess this up and I suddenly get English layout when I log in. I fix this by installing dconf-editor, go to desktop/ibus/general, and make sure that the values engines-order and preload-engines are the same and in the desired order with the default layout first.

Posted in Linux, Ubuntu | 1 Comment

Custom Preference for Android

I need a setting with a numeric value in my Android app. There is no obvious fit among the standard Preferences in Android, so I decided to implement my own Preference subclass.

This was a bit more involved than what I hoped for. I ended up with this. An improvement would be to be able to configure min and max values through XML attributes.

public class NumericPreference extends DialogPreference {
    private static final int DEFAULT_VALUE = 0;

    private NumberPicker mWidget;

    private int value = DEFAULT_VALUE;

    public NumericPreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        setDialogLayoutResource(R.layout.numeric_dialog);
    }

    @Override
    protected void onBindDialogView(View view) {
        mWidget = (NumberPicker)view.findViewById(R.id.picker);
        mWidget.setMinValue(0);
        mWidget.setMaxValue(10);
        mWidget.setValue(value);
        super.onBindDialogView(view);
    }

    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
        return a.getInteger(index, DEFAULT_VALUE);
    }

    @Override
    protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
        if (restorePersistedValue) {
            value = getPersistedInt(DEFAULT_VALUE);
        } else {
            value = (Integer)defaultValue;
            persistInt(value);
        }
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        if (positiveResult) {
            value = mWidget.getValue();
            persistInt(value);
        }
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:gravity="center_horizontal">

    <NumberPicker
            android:id="@+id/picker"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

</LinearLayout>

The code is covered by Apache License 2.0.

Posted in Android | 1 Comment

Running Adobe Lightroom 4.4 in Ubuntu 14.04

I use Adobe Lightroom 4.4 for photo editing. There is one very annoying aspect of this program, it is not available for Linux (only for Windows and Max OS X).

In order to run Lightroom on my computer, I had to use VirtualBox and install Windows 7 in it. This works, but is quite clumsy and annoying. And since Lightroom is the only reason for me to run Windows, I would like to get rid of it.

So I decided to try out Wine. The current version of Wine does not support Lightroom out of the box, but I found some patches and tricks which makes it work. This is how I did it, for Ubuntu 14.04 32-bit (might work in older versions as well).

  1. Uninstall any Wine installation you might already have
  2. Install stuff necessary to build Wine:
    sudo apt-get build-dep wine1.6
  3. Download and unpack Wine 1.7.33 sources:
    tar -xjf ~/Downloads/wine-1.7.33.tar.bz2
  4. Apply this patch:
    patch -p0 <Menu-wine-1.7.33.patch
  5. Build and install the patched Wine 1.7.33:
    cd wine-1.7.33
    ./configure
    make
    sudo make install
  6. Install winetricks:
    sudo apt-get install cabextract
    cd /usr/local/bin
    sudo wget http://winetricks.org/winetricks
    sudo chmod +x winetricks
  7. Prepare and install Lightroom 4.4.1 32-bit:
    winetricks win7
    wine ~/Downloads/Lightroom_4_LS11_win_4_4_1.exe
    winetricks winxp
    winetricks gdiplus corefonts ie7
  8. Download unpack and install sRGB color profile:
    tar -xzf ~/Downloads/lr-wine-1.5.17.tar.gz
    cp "sRGB Color Space Profile.icm" ~/.wine/drive_c/windows/system32/spool/drivers/color

Now you can start Lightroom with this command:
wine "C:\\Program Files\\Adobe\\Adobe Photoshop Lightroom 4.4.1\\lightroom.exe"

I found most of the information here, thanks to Roland Baudin who did most of the job.

Posted in Linux, Ubuntu, wine | 4 Comments

Create a self-contained .jar file with Maven

If you want to create a self-contained .jar file, with all library dependencies included, you can do it with Maven. Include this in your pom.xml:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.yourcompany.youapp.Main</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-jar-with-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
	</plugins>
    </build>

Then when you run mvn package, you will get a target/*-jar-with-dependencies.jar file which you can run with java -jar.

Posted in Java | 1 Comment

Running Jetty as a service in Ubuntu Linix

Jetty is a popular open source Java application server. In order to run it as a service on a Linux server, they recommend using a horribly overcomplicated and quite fragile script.

In Ubuntu server, there is a better way. Leverage Upstart and its declarative job definitions.

First install Jetty into /opt/jetty and create a jetty user:

useradd –user-group –shell /bin/false –home-dir /opt/jetty/temp jetty

Then create a file /etc/init/jetty.conf with content like this:

start on runlevel [2345]
stop on starting rc RUNLEVEL=[016]

chdir /opt/jetty

setuid jetty

reload signal SIGQUIT

exec /usr/local/lib/jvm/bin/java \
-Xmx512m \
-Djava.awt.headless=true \
-Djetty.state=/opt/jetty/jetty.state \
-Djetty.port=8080 \
-Djetty.home=/opt/jetty \
-Djava.io.tmpdir=/tmp \
-jar /opt/jetty/start.jar \
jetty.port=8080 \
etc/jetty-logging.xml \
etc/jetty-started.xml \
etc/jetty-requestlog.xml \
OPTIONS=plus etc/jetty-plus.xml \
–daemon

This will replace both /etc/init.d/jetty and /etc/default/jetty.

If you run service jetty reload, it will create a thread dump (like jstack) in /var/log/upstart/jetty.log.

You can even add respwan to have the JVM automatically restarted if it unexpectedly quits for some reason.

A similar approach can be used for other JVM based servers, like Apache Tomcat.

Posted in Java, Linux, Ubuntu | 1 Comment

Don’t use large BLOBs in MySQL from Java

The MySQL database can store large chunks of binary data (up to 1 GB) using the BLOB data types.

However, this does not work well if you access the MySQL database from Java (or any other JVM based language) using the MySQL JDBC driver.

The JDBC API supports an efficient stream based way to handle BLOBs, but the MySQL JDBC driver does not implement this properly. It works, but it will buffer all data in memory in a way which is very inefficient and can make your JVM run out of memory if the BLOBs are large.

A good rule of thumb is to not store any objects larger than 64 KB in MySQL. Store such objects as files outside of the database instead. So never use the MEDIUMBLOB, LONGBLOB, MEDIUMTEXT or LONGTEXT data types.

(I am not sure whether this advise is only valid for JVM, or for usage of MySQL from other environments as well.)

Posted in database, Java | 2 Comments