Is FluentCassandra / Cassandra losing my data?

345 Views Asked by At
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentCassandra;

namespace ConsoleApplication2
{
        class Program
        {
                static void Main(string[] args)
                {
                        var session = new CassandraSession("Tester", "localhost");

                        CreateData(session);
                        SelectData(session);
                        Console.WriteLine("Done");
                        Console.ReadLine();
                }

                private static void SelectData(CassandraSession session)
                {
                        using (var db = new CassandraContext(session))
                        {
                                var users = db.GetColumnFamily("Users2");
                                var count = users.AsObjectQueryable<User>().Count();
                                Console.WriteLine("Counted: " + count.ToString());
                        }
                }

                private static void CreateData(CassandraSession session)
                {
                        var startTime = DateTime.UtcNow;
                        for (int index = 0; index < 11 * 1000; index++)
                        {
                                using (var db = new CassandraContext(session))
                                {
                                        var users = db.GetColumnFamily("Users2");
                                        dynamic user = users.CreateRecord(index);
                                        user.Name = "UserX" + index.ToString();
                                        user.Password = "PasswordX" + index.ToString();
                                        db.Attach(user);
                                        db.SaveChanges();
                                        if (index % 1000 == 0)
                                        {
                                                var taken = DateTime.UtcNow - startTime;
                                                Console.WriteLine(string.Format("{0} thousand in {1} seconds", (index / 1000) + 1, taken.TotalSeconds));
                                        }
                                }
                        }
                }
        }

        class User
        {
                public string Name { get; set; }
                public string Text { get; set; }
        }
}

The output of this app is

1 thousand in 0.2760158 seconds
2 thousand in 0.8180468 seconds
3 thousand in 1.3700784 seconds
4 thousand in 1.8971085 seconds
5 thousand in 2.4091378 seconds
6 thousand in 3.0791761 seconds
7 thousand in 3.6002059 seconds
8 thousand in 4.126236 seconds
9 thousand in 4.8292762 seconds
10 thousand in 5.3513061 seconds
11 thousand in 5.8543349 seconds
Counted: 9018
Done
1

There are 1 best solutions below

1
On

There is no way this code works, and I should know, I am the developer of FluentCassandra.

         using (var db = new CassandraContext(session))
         {
              var users = db.GetColumnFamily("Users2");
              var count = users.AsObjectQueryable<User>().Count();
              Console.WriteLine("Counted: " + count.ToString());
         }

You are using LINQ to do a count, but Count hasn't been implemented in the LINQ support yet. Right now there isn't a great way to count records in Cassandra. The best way is to launch a CQL statement and run

SELECT count(*) FROM <COLUMN FAMILY>

Hope this helps. I don't know what value you are getting from that Count method, but it sure isn't anything intended.