Thursday, July 13, 2017

In Java, two Integer objects with the same value are not == to each other, most of the time

Today I've discovered, to my dismay, that two Integer objects with the same value compared with the == operator may return false, because they are different objects. So you need to use .equals (before you check for null, of course). I was about to write a scathing blog entry on how much Java sucks, but then I discovered this amazing link: Java gotchas: Immutable Objects / Wrapper Class Caching that explains that the Integer class creates a cache of 256 values so that everything between -128 and 127 is actual equal as an instance as well.

Yes, folks, you've heard that right. I didn't believe it, either, so I wrote a little demo code:
Integer i1 = Integer.valueOf(1);
Integer i2 = Integer.valueOf(1);
boolean b1 = i1 == i2; // true

i1 = Integer.valueOf(1000);
i2 = Integer.valueOf(1000);
boolean b2 = i1 == i2; // false

i1=1;
i2=1;
boolean b3 = i1 == i2; // true

i1=1000;
i2=1000;
boolean b4 = i1 == i2; // false

i1=126;
i2=126;
boolean b5 = i1 == i2; // true

i1++;
i2++;
boolean b6 = i1 == i2; // true

i1++;
i2++;
boolean b7 = i1 == i2; // false

i1 = 2000;
i2 = i1;
boolean b8 = i1 == i2; // true

i1++;
i1--;
boolean b9 = i1 == i2; // false


I now submit to you that "sucks" applies to many things, but not to Java. A new term needs to be defined for it, so that it captures the horror above in a single word.

Wednesday, July 12, 2017

Microsoft SQL Server table partitioning

Tonight I went to an ADCES presentation about SQL table partitioning, a concept that allows for a lot of flexibility while preserving the same basic interface for a table one would use for a simpler and less scalable application. The talk was very professionally held by Bogdan Sahlean and you should have been there to see it :)

He talked about how one can create filegroups on which a table can be split into as many partitions as needed. He then demonstrated the concept of partition switching, which means swapping two tables without overhead, just via metadata, and, used in the context of partitions, the possibility to create a staging table, do stuff on it, then just swap it with a partition with no downtime. The SQL scripts used in the demo can be found on Sahlean's blog. This technology exists since SQL Server 2005, it's not something terribly new, and features with similar but limited functionality existed since SQL Server 2000. Basically the data in a table can be organized in separate buckets and one can even put each partition on a different drive for extra speed.

Things I've found interesting, in no particular order:
  • Best practice: create custom filegroups for databases and put objects in them, rather than in the primary (default) filegroup. Reason: each filegroup is restored separately,
    with the primary being the first and the one the database restore waits for to call a database as online. That means one can quickly restore the important data and see the db online, while the less accessed or less important data, like archive info, loaded afterwards.
  • Using constraints with CHECK on tables is useful in so many ways. For example, even since SQL Server 2000, one could create tables on different databases, even different servers, and if they are marked with not overlapping checks, one can not only create a view that combines all data with UNION ALL, but also insert into the view. The server will know which tables, databases and servers to connect to. Also, useful in the partition presentation.
  • CREATE INDEX with a DROP_EXISTING hint to quickly recreate or alter clustered indexes. With DROP_EXISTING, you save one complete cycle of dropping and recreating nonclustered indexes. Also, if specifying a different filegroup, you are effectively moving the data in a table from a filegroup to another.
  • Finally, the SWITCH TO partition switching can be used to quickly swap two tables, since from Sql Server 2005 all tables are considered partitioned, with regular ones just having one partition. So one creates a table identical in structure with another, does whatever with it, then just uses something like this: ALTER TABLE Orders SWITCH PARTITION 1 TO OrdersHistory PARTITION 1; to swap them out, with minimal overhang.

Monday, July 10, 2017

Memory Mapped Files in .NET 4.0

Just a heads up on a technology than I had no idea existed. To get the details read this 2009 (!!! :( ) article.

Basically you define a MemoryMappedFile instance from a path or a file reader, then create one or more MemoryMappedViewAccessors, then read or write binary data. The data can be structures, by using the generic Read/Write<[type]> methods.

Drawbacks: The size of the file has to be fixed, it cannot be increased or decreased. Also the path of the file needs to be on a local drive, it can't be on a network path.
Advantages: Fast access, built in persistency, the most efficient method to share data between processes.

Friday, July 07, 2017

NullPointerException in Java when using null values in a switch statement

Fuck Java! Just fuck it! I have been trying for half an hour to understand why a NullPointerException is returned in a Java code that I can't debug. It was a simple String object that was null inside a switch statement. According to this link states that The prohibition against using null as a switch label prevents one from writing code that can never be executed. If the switch expression is of a reference type, that is, String or a boxed primitive type or an enum type, then a run-time error will occur if the expression evaluates to null at run time.

Friday, June 30, 2017

Central Station, by Lavie Tidhar

Book cover Central Station describes a very interesting cyberpunk style of future, where amazing yet commonplace technology mixes with the traditional and with the local culture in a world in which the Solar System has been colonized. And then the book ends. Lavie Tidhar manages to imagine all of this creative world, but doesn't succeed on making the book more than the collection of short stories it actually is. That doesn't mean the book is not worth reading or that other works from the author will not benefit from the world building in it, but it feels like a missed opportunity. The book is short, it describes interactions between a surprisingly small and somehow related people and then it just ends with none of the threads in it being resolved in any way. The main character remains as the background city of Central Station, former Tel Aviv.

The writing style is also a bit heavy. It is descriptive, a little pretentious, but it might have felt like that because I was reading in the subway or when going to sleep and I wasn't in the mood for intellectual work. Even so I believe that a lighter style with more attention to story development would have benefited this book.

Bottom line: I liked the story immediately and felt betrayed after it abandoned me right when I was intrigued enough to seek closure. It is worth a read and I hope Tidhar expands the world in other stories beyond the insular location of the book. I am also looking forward to reading other things from the same author.

Thursday, June 29, 2017

Blood Drive, the perfect combination of Mad Max and Wacky Races, via Z-Nation

Oh, SyFy does it again with a show that is wild, totally over the top and really really fun. Post-apocalypse, hot rod cars that use human blood for fuel, cannibals, wild sex, murder sprees, wild people, Colin Cunningham, corporate overlords, awakened psychotic robots, making fun of corporate overlords, ridiculously attractive people surrounded by ridiculously ugly people... Blood Drive is just too silly to care and too wild to not enjoy. I am watching the third episode already and I am laughing my ass off: "Praise synergy for it provides us with low hanging fruit!", you gotta love that.

Monday, June 26, 2017

Ninefox Gambit (The Machineries of Empire #1), by Yoon Ha Lee

book cover Finally, a fresh, unapologetically sci-fi story with so many interesting ideas and cultural innovation that I can barely wait for the second book in the series to come out.

Yoon Ha Lee creates this far into the future universe in which everything from social structure to space travel and military technology is run by rigid doctrine that uses a particular calendar. Certain battle formations, using certain weapons, doing specific things leads to "exotic effects", carefully manipulated through higher mathematics, that power society and military expansion. Of course, there are multiple possible calendaristical configurations, but they interfere with each other, so after choosing one, any deviation is considered heretical. Add to this an Asian view of hierarchy and politics and you get the most delicious book I've read in a long, long time.

Ninefox Gambit is, unfortunately, merely the beginning of the story. While one could consider the entire thing a standalone book that leaves the rest of the story to the imagination of the reader, the rich universe that it creates makes followups inevitable. In this case, I can barely wait for them. There isn't much else to say about the book other than urge you to read it. As with any good writing, the plot is simple, but the individual scenes give its flavor. It is an almost unspoilable story, since it doesn't rely much on twists, but on bringing value in every chapter, through rich characterization and original scenecraft.