Keep Time Explicit

May 2, 2020

In a lot of software is operations are depending on time. For example creating invoices, statistics, showing the latest news so on so forth. In this blog post, I use an example summing up bought items in the last week. We might have multiple layers in our system, like database access, places to do extra processing, and a web interface. I’m using C# here, but it applies to most mainstream languages. As you can see, to get the right database records the cure uses the current time.

Example Database Access:
public class Purchases
{
    public IList<Purchase> LastWeeksPurchases()
    {
        // Assume more complex queries, let's say joins with users, applied coupons, discounts at the time and god knows what
        return Database.InTransaction(conn => conn.Query<Purchase>(@"select * from purchases
            where dateTime < current_timestamp
            and (current_timestamp + INTERVAL '-7 day') < dateTime
            order by dateTime").ToList());
    }
}
Continue reading →

C# Loves Code Patterns

December 11, 2019

This post is part of the third annual C# Advent. Check out the home page for up to 50 C# blog posts in December 2019! Thanks, Matthew D. Groves for organizing it.

You might recall that LINQ has two forms. The first way is to call LINQ methods and the second is the special LINQ syntax. Here’s an example of the LINQ syntax.

Continue reading →

Synchronizing File Writes

November 27, 2019

Assuming we have a simple task to write events to a file, one event after another. Events can be written from multiple threads.

public interface EventLog {
    public final class Event{
        public final UUID id;
        public final ZonedDateTime created;
        public final String message;

        public Event(UUID id, ZonedDateTime created, String message) {
            this.id = id;
            this.created = created;
            this.message = message;
        }
    }

    void recordEvent(Event event);

    // Null Logger doing nothing to have a base line
    EventLog NULL_LOGGER = new NullLogger();

    class NullLogger implements EventLog {
        @Override
        public void recordEvent(Event event) {
            // do nothing;
        }

        @Override
        public void close() throws Exception {
        }
    }
}
Continue reading →

FileChannel Closes When Interrupted

November 12, 2019

Recently I worked on some threaded IO where a control thread interrupts the IO threads once in a while. Periodically the app started to fail with a java.nio.channels.ClosedChannelException.

FileChannel Closes When Interrupted
Figure 1. FileChannel Closes When Interrupted
Continue reading →

MVStore: Accessing Old Versions

November 6, 2019

A while back I blogged about MVStore basics. Let’s look at the way MVStore keeps old revisions around. First, MVStore maps have a .getVersion() method and a .openVersion() method pair. With that, you can access old version previous values. A new version of the map is created on (auto-) commit.

MVMap restores old versions of itself
Figure 1. MVMap restores old versions of itself
Continue reading →