Netflix / astyanax

Cassandra Java Client
Apache License 2.0
1.04k stars 355 forks source link

Empty Set save using EntityPersister #375

Open rotemfogel opened 11 years ago

rotemfogel commented 11 years ago

Hi astyanaxes, I am using astyanax with Apache Cassandra 1.2. I have stumbled upon a strange behavior and I want to ask if it's Cassandra issue or Astyanax issue: A user object has a set. the set is part of the user data. user is saved using entitypersister. when set is empty, record is not deleted in Cassandra.

@Entity public static class User { @Id final private String id = "1"; @Column protected Set likes;

public String getId()
{
    return id;
}

public void setId(final String value)
{
}

public Set<String> getLikes()
{
    if (likes == null)
    {
        likes = Sets.newHashSet();
    }
    return likes;
}

public void setLikes(final Set<String> value)
{
    likes = value;
}

public void like(final String value)
{
    getLikes().add(value);
}

public void unlike(final String value)
{
    getLikes().remove(value);
}

@Override
public String toString()
{
    return getGson().toJson(this);
}

}

// create user User user = new User(); user.like(COCA_COLA); System.out.println("initial user: " + user); // save user final EntityManager<User, String> userManager = new DefaultEntityManager.Builder<User, String>().withEntityType(User.class).withKeyspace(keyspace).withColumnFamily(CF_TEST).build(); userManager.put(user);

// fetch user user = userManager.get(ID); System.out.println("db user: " + user);

// modify && save user.unlike(COCA_COLA); userManager.put(user); System.out.println("modified user: " + user);

// fetch user - expected - empty set user = userManager.get(ID); System.out.println("final user: " + user);

rotemfogel commented 11 years ago

Mark as duplicate #367

sagarl commented 10 years ago

@rotemfogel are you seeing following ? If yes, then it's an expected behavior.

cqlsh:myks> select * from users;

user_name | emails -----------+------------------------ r3 | {'e@y.com', 'f@y.com'} r2 | {'c@y.com', 'd@y.com'}

cqlsh:myks> delete emails from users where user_name = 'r3'; cqlsh:myks> select * from users;

user_name | emails -----------+------------------------ r3 | null r2 | {'c@y.com', 'd@y.com'}

rotemfogel commented 10 years ago

This is not the case. What you are showing me here is a perfectly understandable behavior due to tombstone in Cassandra. I am talking about the case when removing the last value in a list object, the data still resides in the database, although list is saved as empty. I bypassed the bug by deleting the row, if list is empty. However it is not the correct behavior. I expect the client to cope with this change.