0

friends

I am design a message history table

CREATE TABLE message_history (
    user_name text PRIMARY KEY,
    time timestamp,
    message_details list<text>, 
);

so that I can query a user's message via primary key user_name at once.

but the item in message_details list may be very long so that I want to limit the list size of the message_details list.

cause I just want care the latest, say, 1000 messages of a user.

can I achieve this?

thx!

1
  • I don't think it is possible to limit to amount of items returned from a list in Cassandra. You should think about the queries you want to write, then design your model based around that.
    – scrineym
    Aug 13, 2015 at 15:44

2 Answers 2

0

I'm afraid collections are not designed for this use-case (read more). Is there any reason you can't use a clustering key instead of a list?

CREATE TABLE message_history (
    user_name text,
    time timestamp,
    message_details text, 
    PRIMARY KEY(user_name, time)
) WITH CLUSTERING ORDER BY (time DESC);

insert into message_history (user_name, time, message_details) values ('user1', dateOf(now()), 'message text');
insert into message_history (user_name, time, message_details) values ('user1', dateOf(now()), 'message text2');
insert into message_history (user_name, time, message_details) values ('user1', dateOf(now()), 'message text3');

select * from message_history where user_name = 'user1' limit 1;

 user_name | time                     | message_details
-----------+--------------------------+-----------------
     user1 | 2015-08-13 15:44:45+0000 |   message text3
1
  • thank you for reply. but this still cannot fullfill the function of AUTO rotate(remove) the oldest message,if the total size of the user's message reached a certain size(say 1000). if not, i'm afraid the table will be very large.
    – yuankui
    Aug 14, 2015 at 1:26
0

You could do that using a map instead of a list. Keep a message id number and increment it each time the user has a new message. Then do a modulo on the message id by 1000 and use that as the map key.

By doing a modulo, the value will wrap around every 1000 and overwrite the oldest message to replace it with the most recent one.

So your table could look like this:

CREATE TABLE message_history (
    user_name text PRIMARY KEY,
    time timestamp,
    last_msg_id int static,
    message_details map<int, text>
);

Before you save a new message, read the current value of last_msg_id and increment it, calculate the modulo 1000, and then update the map using the modulo result as the key and the new message as the text, and also update last_msg_id.

Or you could keep last_msg_id as a counter column in a separate table.

1
  • it's a tricky use of map,and i believe it will work. But I'm afraid the client side will suffer the complexity, and performance issue: before every insert, I have to get and process and set. I wonder if there is a database has the function of SIZED list. I think it's very useful, at least for me.
    – yuankui
    Aug 14, 2015 at 1:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.