Incrementing counters

196 views
Skip to first unread message

Collin Sauve

unread,
Jun 13, 2016, 10:50:45 PM6/13/16
to DataStax C# Driver for Apache Cassandra User Mailing List
Hi,

I'm having some trouble incrementing counters properly with the C# driver.  Say I have the following entity:

 
 
[Table("test_counter_v1")]
 
public class TestCounterEntity1
 
{
     
[Column("pk")]
     
public string PartitionKey { get; set; }

     
[Column("counter")]
     
[Counter]
     
public long Counter { get; set; }
 
}



I originally expected I should have to do something like this:

 
 
[Test]
 
public async Task TestUpdateCounterConstant()
 
{
     
var paritionKey = Guid.NewGuid().ToString();
     
var table = new Table<TestCounterEntity1>(Session);

     await table
         
.Where(_ => _.PartitionKey == paritionKey)
         
.Select(_ => new TestCounterEntity1
         
{
             
Counter = _.Counter + 4                 // We will be working with this line...
         
})
         
.Update()
         
.ExecuteAsync();
 
}



However I receive this exception:

"System.InvalidOperationException : variable '_' of type 'Nudge.Tests.Core.Cassandra.TestCounterEntity1' referenced from scope '', but it is not defined

Digging through some examples, including the LinqGeneratedUpdateStatementForCounterTest that was created in PR#43, I see I need to change that line to:
   
             Counter = 1

And that works!  However, what if I want to increment by more than 1? ie:

             Counter = 4

This actually only increments it by 1.  Even going back and trying to change the original LinqGeneratedUpdateStatementForCounterTest to have Value = 2 it writes CQL that would only increment by 1.

 Additionally, if I want to increment it by a non-constant, ie

             Counter = x.SomeProperty

I get this exception:

Cassandra.Data.Linq.CqlLinqNotSupportedException : The expression Convert = [Convert(value(Cassandra.Tests.Mapping.Linq.LinqToCqlUnitTests+<>c__DisplayClass12_0).x.Value)] is not supported in SelectBinding parse phase.


So... am I missing some other way of doing this? ie with the Mapper instead of Linq component perhaps?   Should I file a bug / attempt to fix it myself?
    

Jorge Bay Gondra

unread,
Jun 14, 2016, 4:58:31 AM6/14/16
to csharp-dr...@lists.datastax.com
Hi Collin,
It is indeed a bug in the Linq component, I've created a ticket for it: https://datastax-oss.atlassian.net/browse/CSHARP-467

Thanks,
Jorge

--
You received this message because you are subscribed to the Google Groups "DataStax C# Driver for Apache Cassandra User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to csharp-driver-u...@lists.datastax.com.

David Buckle

unread,
Aug 10, 2017, 9:55:42 AM8/10/17
to DataStax C# Driver for Apache Cassandra User Mailing List
I have a similar issue but if the counter record doesn't already exist in the table is doesn't get created using Update().  I thought Update actually did an Upsert() ?

I can't use Insert() since it will throw an exception as it is a counter type.  Really stuck now, and I'm not sure if this is related to the original bug.

I also tried this direct:

"UPDATE test_table set count_value = count_value + 1 WHERE somekey = ?"

But still the record isn't created.  


Any ideas?

David Buckle

unread,
Aug 10, 2017, 10:01:43 AM8/10/17
to DataStax C# Driver for Apache Cassandra User Mailing List
I just found out seconds after posting this that the original record had been deleted.  If I used another key, it works.  That's pretty confusing.

David Buckle

unread,
Aug 10, 2017, 11:19:03 AM8/10/17
to DataStax C# Driver for Apache Cassandra User Mailing List
Reply all
Reply to author
Forward
0 new messages