akavache/Akavache — pull request #162 by paulcbetts (2014-07-21 11:36:34+00:00)
Title: SQLite3 Operation Coalescing

This PR implements operation coalescing, which combines unrelated operations into single SQL queries.

### Coalescing unrelated operations

For example, imagine the queue consists of:

```
Get ["Foo"]
Get ["Bar"]
Get ["Baz"]
```

We can simplify this to:

```
Get ["Foo", "Bar", "Baz"]
```

### Optimizing operations to the same key

Furthermore, we can also simplify / dedup operations to the same key. For example:

```
Get ["Foo"]
Get ["Foo"]
Get ["Foo"]
```

We can simply execute a single `Get ["Foo"]` and deliver the results to all three requests. Or, we can cancel complementary requests:

```
Insert ["Foo"]
Invalidate ["Foo"]
```

To:

```
Invalidate ["Foo"]
```

#### Coalescing Algorithm

1. Group by the key being requested, ensuring that the resulting groupings preserve the original order of the requests
2. Within each list of operations to the same key, optimize this list
3. Take all the *first* items from every group created in 1.
4. Coalesce all of the operations by operation type
5. Yield the ops out
6. Goto 3. until you're out of items