Skip to main content

Print E-Z

This project had a very unique requirement: They wanted to dynamically serve up their products without augmenting their application with a server-side technology like PHP or Java. More specifically, they wanted their products to be pulled from a datasource instead of being statically specified in HTML.


What if we used a data source that could be hosted elsewhere and served over Javascript?

GoogleBase to the rescue!

In this project, we used GoogleBase to store their products and then javascript to read them from GoogleBase and render them on the correct pages.

It was tricky, but we were successful.


There was just one problem that we found during implementation, and that was that Google Base just wasn't prepared to have many integer values assigned to a single attribute. We needed this in order to map each product to the pages that it ought to appear on.

To make a long story short, we tried countless strategies over the course of a couple of weeks until the following thought dawned on me:

What if we assigned a prime number to each value and created composite numbers for each product's unique set of values, which we could then extract back out using modular arithmetic?

I'm sure that the thought occurred to you right away. Of course, it took me a couple of weeks to see it.

So here is what I did in the end:

Each page number would be assigned a unique prime number. So "Page 1" would be assigned the number "2", "Page 2" would be assigned "3", "Page 3" would be assigned "5", etc. The set of pages that an individual product should appear on can then be expressed as a set of those prime numbers:

Product A's 'page_numbers'-> {17, 3, 93, 41}

"But wait," you say. "Google Base won't let us specify a list of integers as a value in the data feed!" You speak well. What we will do now is multiple those prime numbers together to get a composite number, which will become the value for the "page_numbers" attribute.

17*3*93*41 = 194463

Product A's 'page_numbers' -> 194463

Now, if you stretch back to high school math, you'll remember that ever composite number is a product of primes (remember what they called "prime factorization"). This concept ensures that there is only one prime factorization per integer and thus, will not be divisible by any other primes than those that make up its prime factorization.

This is important because now I can say with confidence that if product xyz's 'page_numbers' attribute is divisible by 43, then it must belong on the page that is assigned to the prime number '43'.

Modular arithmetic is perfect for such a calculation. The question "Does x mod y equal 0?" is the same as asking "Does y divide evenly into x?"

So, the math works. Now, will Google Base support it?

It turns out that their Ranking Language does support modular arithmetic and can act as a secondary filtering mechanism after its Query Language. Yes!

Upon reading the documentation, I use the following Ranking Language code:

orderby=[x = page_number(int) : if ( x % 43 == 0 ) then x else fail()]

Drum roll.....


...It fails. Expected an 'int' for the '%' operator, but found 'page_number(int)'.

What? **Heart sinks**

Well, I won't tell you about the resulting melancholy that weighed on my stricken heart for the next 48 hours while I stewed over what had gone wrong.

Instead, I'll just tell you that I found out that since each attribute is able to have multiple values (just not necessarily through a data feed), I needed to apply an aggregating function on the value before applying the mod.

So, then I tried:

orderby=[x = page_number(int) : if ( max(x) % 43 ) then max(x) else fail()]

And it worked!

It's always fun running into problems like that because there is an interesting solution hanging right around the corner.


Popular posts from this blog

How Many Teeth Does The Tooth Fairy Pick Up Each Night in Utah?

Somebody asked me a question about my Tooth Fairy post the other day that got me thinking. How many baby teeth are lost every day in Utah? I began with Googling. Surely someone else has thought of this and run some numbers, right? Lo, there is a tooth fairy site that claims that the Tooth Fairy collects 300,000 teeth per night . That's a lot; however, when I ran the numbers, it started to seem awfully low. Let's assume that the Tooth Fairy collects all baby teeth regardless of quality and we assume that all children lose all their baby teeth. The world population of children sits at 2.2 billion , with 74.2 million of them in the United States. Of those, approximately 896,961 of them are in Utah . This means that somewhere around .04077% of the world's children are in Utah. If we assume that kids in Utah lose teeth at the same rate as all other children in the world and that each day in the year is just as likely as the rest to lose a tooth, then we have that of

BYU and the Sunday Compromise?

I read an article by Brad Rock this morning where he quoted heavily from Dr. Thomas Forsthoefel who was giving his opinion on religious institutions being involved in sports . BYU , of course, came up. I think Forsthoefel came off sounding a bit misinformed about the culture, drive, mission, etc. of BYU . Below is the email that I sent to Brad Rock this morning after finishing the article: Brad - That was an interesting article. I tend to disagree with Forsthoefel, though, or at least disagree with what I may have read into his comments. A quote in your article says: "There may be a kind of growing pain. BYU is in the real world and the real world works on Sunday. Can we (BYU) live with the adjustment? I'm empathetic with that, whatever decision is made, people are going to be unhappy.… Some will say get with the program, we'll be OK at the next level, others will say we've sold out and we've made a deal with the world." This seems to suggest one o

Baby Names: What my daughter's name has to do with an ancient Persian Fairy Tales

If you read my previous post on my sons' names, you'll know that this post is about my daughters' names. When we found out that we were going to have twins, I vowed that there names were not going to rhyme or alliterate. We weren't going to do Jadyn and Jordan, or Kim and Tim, or Esther and Edgar (all likely candidates for other, less elitist parents, especially Esther and Edgar). I did want the names to have something to do with one another somehow. Felicity Mae Cummings Felicity's first name has little to do with its underlying Hebrew meaning or its tie to Biblical history and everything to do with the fact that this was a name that Kristi had always wanted one of her girls to have because she liked that it meant "happiness". So, to tell you the truth, I didn't do a lot of research on this name because its place in our family had already been decided. But, it was excellent material to work with. The initial spark that 'Felicity' pro