Generating Music

Recently I’ve undertaken a personal project (computoser) that is a bit strange. I tried to write software that generates music at random. Good music, that is, because “random” usually generates noise. The idea, of course, is not at all new – there has been research on the topic and there is software that attempts to generate music. I generally group this software in three categories: software for helping composers – it’s the composers who handle most of the things, but the software can suggest some motifs, variations, etc. to help the composer programs that take functions or other mathematical concept and try to transform it to frequencies, based on some arbitrary mapping programs that generate music adhering to composition rules Out of these three, the first one is very specialized and not suitable for a large audience, and the second is not producing results that a sane person would listen to (the mathematical/physics rules that these algorithms try to employ are already used at the lower level of music – e.g. note frequencies double each octave; additionally, the mapping between the functions/sequences to notes are often arbitrary, so even though the the Fibonacci numbers are “nice”, there is no definitive way of mapping them to music that is guaranteed to sound good). The third group can generate “listenable” music, but it needs to properly define the rules that composers adhere to when writing music. Some of these programs use fractals, other use pictures, text, or whatever to generate music. In fact, these are all different ways to seed the random. So, I obviously I went for the third option,...

Tip Of The Day: Make Your Email Column Unique

Skype screwed up and it got revealed today. Due to a series of stupid things, there is (was) a trivial way to hijack anyone’s account. One of the stupid things is that they allowed a 2nd user to register with an email that is already registered. I have seen this many times, with forums, groupon-like sites (there I debugged their system over the phone), and whatnot – too many systems lack a unique constraint on their email column. Even though the problems are not always that big as with Skype, you should always have a unique constraint on the email column in the users table. Because that’s what the logic says – one user – one email. If you allow a 2nd user with the same email, then “forgotten password” works for which account? What about emails about promotions – duplicate emails will be sent. Any many more issues coming from not paying attention to this simple thing. Have in mind that the unique constraint is best imposed at a database level. If you have it just in your code, some future code or change might not obey it. So, tip of the day – make your email column unique. Skype screwed up and it got revealed today. Due to a series of stupid things, there is (was) a trivial way to hijack anyone’s account. One of the stupid things is that they allowed a 2nd user to register with an email that is already registered. I have seen this many times, with forums, groupon-like sites (there I debugged their system over the phone), and whatnot – too many...

How To Provide Default Serialization Format With Spring MVC RESTful Services

Spring MVC allows you to serialize objects with @ResponseBody to any format – it has built-in converters for JSON and XML. However, even though your application may support both, you may need a default in case the Accept header is not set. Furthermore (this is a strange case, but we had it), you may want different defaults for different URLs. As far as I know you can’t choose which of the converters to be used – they are ordered and the first converter in the list gets picked up and serializes the response. My solution to the problem is trivial, but not the first thing you’d think about. Use a Filter, and in case the client hasn’t supplied an Accept header, wrap the request and make it return the default Accept header, as if the client has set it. That way, whenever spring-mvc invokes the getHeader() method, it will get the desired Accept header. /** * Filter that is needed to set the default Accept header per gateway. * According to previous API specifications, each gateway had a default serialization format. Unfortunately these formats differ, * and spring can't have a default-per-url. That's why this filter wraps the request in case there is no Accept header and returns * the default one depending on the url * * @author bozho * */ public class DefaultAcceptHeaderSettingFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; String acceptHeader = httpRequest.getHeader("Accept"); if (StringUtils.isBlank(acceptHeader)) { chain.doFilter(new DefaultAcceptHeaderHttpServletRequestWrapper(httpRequest), response); } else { chain.doFilter(httpRequest,...

Don’t Use JSON And XML As Internal Transfer Formats

You have a system that has multiple components and they have to communicate. They do that either via internal web services or using a message queue. Normally, you would want to send (data transfer) objects from one component to another. Three typical examples: a user has registered and you send a message to a message queue and whenever the message is consumed, an email is sent to the user. The message needs the user at least the email and names of the user. if your layers communicate via web services for some reason (rather than live within one JVM), on registration the web layer needs to invoke a back-end service and pass a User object. you store objects in some (distributed) in-memory cache in order to reduce redundant calls to the database (assuming you map your database results to objects in some way, either an ORM or some mapper, but this is done in the majority of cases). So when a request arrives asking for a user profile, you check if it’s present in the cache, and if it is – you get it from there, rather than hitting the database. In order to achieve these things you need to serialize the objects to some format that will then be deserialized on the other end. Many frameworks include XML and JSON serializers and they are used in many examples online. Therefore people are inclined to use JSON or XML for these purposes. And that’s not a good idea. Using these formats internally has no benefit – you don’t actually need the serialized objects to be human-readable, and if you...

A Tip For Properly Utilizing Hibernate 2nd Level Cache

Your entity is @Cacheable, your cache is configured properly, but still, loading an entity produces a lot of sql queries. I noticed that the other day, and realized something: hibernate does not cache collection mappings. So if you have @OneToMany, they are fetched from the database. Which is a bit counter-intuitive, because my assumption was that hibernate caches the whole entity. This article explains what happens. The solution is to use @Cache(..) (a hibernate-specific annotation) on all the collections. What about lazy collections? Laziness serves a similar purpose – not to fetch data unnecessarily. But if you are actually using the collection, then for each collection a new query is sent. That’s why you need to annotate it with @Cache. Oh, and remember that generally you should avoid lazy JPA collections, and this caching problem is one more side-effect. Eager collections, of course, won’t help in this case, but if you didn’t have collections in the first place (most of them were unnecessary in our case), then you could spare a lot of queries, and also realize that they are not cached before you hit production performance problems. Your entity is @Cacheable, your cache is configured properly, but still, loading an entity produces a lot of sql queries. I noticed that the other day, and realized something: hibernate does not cache collection mappings. So if you have @OneToMany, they are fetched from the database. Which is a bit counter-intuitive, because my assumption was that hibernate caches the whole entity. This article explains what happens. The solution is to use @Cache(..) (a hibernate-specific annotation) on all the collections. What about...