CQL3 Collections #23

Merged
merged 8 commits into from Mar 28, 2014

Projects

None yet

2 participants

@DamianCaruso
Collaborator

This pull adds support for CQL 3 collections (sets, lists and maps) introduced in Cassandra 1.2.

I think I covered most of the use cases including updates/deletes based on keys. There is a new file under the examples directory showing how to use them.

@jnunemaker jnunemaker and 1 other commented on an outdated diff Feb 7, 2014
examples/collections.rb
+ emails: :"set<text>",
+ top_places: :"list<text>",
+ todo: :"map<timestamp,text>",
+ },
+ },
+})
+users.create
+
+
+## Sets
+
+# insert a row
+users.insert(data: {id: '1', emails: Set.new(['f@baggings.com','baggins@gmail.com'])})
+
+# add an element to the set
+users.update(set: {emails: Cassanity.inc(Set.new(['a@b.com']))}, where: {id: '1'})
@jnunemaker
jnunemaker Feb 7, 2014

Why the use of inc here? Feels odd. Is it just to get the formatting the same as inc? Makes me wonder if we should just add like Cassanity.add/remove or something for set operations?

@DamianCaruso
DamianCaruso Mar 14, 2014

Yes, is because of the formatting (+ operator). One option is to make Cassanity.add an alias of Cassanity.inc.
With a custom add method, doing Cassanity.add('a@b.com') or Cassanity.add(['a@b.com', 'b@c.com']) looks good, but what do we do with lists? Lists have a different formatting.

What about sadd/sremove for sets, and add/remove for lists?

@jnunemaker
jnunemaker Mar 14, 2014

What about sadd/sremove for sets, and add/remove for lists?

I like the separation of set_add, set_remove, add and remove. Good call.

@jnunemaker jnunemaker and 1 other commented on an outdated diff Feb 7, 2014
examples/collections.rb
+# delete all elements from the set
+users.delete(columns: :emails, where: {id: '1'})
+# or
+users.update(set: {emails: Set.new}, where: {id: '1'})
+
+
+## Lists
+
+# insert a row
+users.insert(data: {id: '2', top_places: ['mordor','rivendell','rohan']})
+
+# add an element to the list
+users.update(set: {top_places: Cassanity.inc(['the shire'])}, where: {id: '2'})
+
+# update an element by its index
+users.update(set: {top_places: Cassanity.CollectionItem(0,'riddermark')}, where: {id: '2'})
@jnunemaker
jnunemaker Feb 7, 2014

I don't think I like Cassanity.CollectionItem. I would definitely prefer Cassanity.item or Cassanity.collection_item.

@DamianCaruso
DamianCaruso Mar 14, 2014

You're right.. looks better. I will update it.

@jnunemaker
Owner

Thanks for kicking this off! I really appreciate it.

My main concern right now is inconsistency of API. CollectionItem is capitalized, but inc is not (which is currently in cassanity). Set is used sometimes. Hash is used sometimes. But CollectionItem is needed in others. I'd love to somehow make these a bit more consistent. Does that make sense? Any ideas? I can think about it soon, but I don't have time right now. I should have time to react to suggestions/comments here though.

@DamianCaruso
Collaborator

Agree. here some ideas:

Inserts
users.insert(data: {id: '1', emails: Set[]})  # Sets
users.insert(data: {id: '2', top_places: []}) # Lists
users.insert(data: {id: '3', todo: {}})       # Maps

We have to set the correct type here, there is no much to change.

Updates
users.update(set: {top_places: Cassanity.add('the shire')}, where: {id: '2'}) # add to list
users.update(set: {emails: Cassanity.sadd('a@b.com')}, where: {id: '1'})  # add to set
users.update(set: {top_places: Cassanity.item(0,'riddermark')}, where: {id: '2'}) # update a list element by index (same for maps)

For removing from lists and sets, the same as above with remove/sremove.

Deletes
users.delete(columns: Cassanity.item(0, :top_places), where: {id: '2'}) # delete an element by its index in lists and maps.

Here we can use the same class (CollectionItem), the difference is the ColumnFamilyDelete arg generator will treat the value as the column name. Another option could be another method like Cassanity.column(column_name, column_index).

@jnunemaker
Owner

Love all the stuff above. With those changes I would be happy to pull. Thanks for thinking it through!

@DamianCaruso
Collaborator

Well, all the changes discussed above have been applied. Now the API is more consistent. All tests pass, so you can safely pull.

@jnunemaker jnunemaker merged commit 4fc3816 into jnunemaker:master Mar 28, 2014

1 check failed

Details default The Travis CI build could not complete due to an error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment