AerospikeTraining.Program.EventToString C# (CSharp) Method

EventToString() private method

private EventToString ( Aerospike.Client.Record eventRecord ) : string
eventRecord Aerospike.Client.Record
return string
        private string EventToString(Record eventRecord)
        {
            // Note: If we want to get a list of all the records to iterate through rather than just the count,
            // we get the maximum sequence from the count, iterate through the keys from 2->Max Sequence (as we
            // already have record 1), form a list of keys then do a batch read. This code is provided for clarity
            String user = eventRecord.GetString("user-id");
            String dateStr = eventRecord.GetString ("day");
            List<Object> events = (List<Object>) eventRecord.GetValue ("events");
            int count = eventRecord.GetInt ("total-records");
            if (count < 0) {
                // Another thread has this record locked. Either retry or throw an exception
                throw new AerospikeException ("Record locked");
            }

            // TODO: We really need to obtain a lock to the record by setting total-records to -1 whilst we read it.
            // This is similar to the code in AddEvent, and probably should be put into a common routine. Otherwise
            // we risk the collection changing whilst we're still using it.

            List<Key> keyList = new List<Key> ();
            for (int i = 2; i <= GetSequenceNumberFromRecordNumber (count); i++) {
                keyList.Add (new Key ("test", "user-events", FormKeyString (user, dateStr, i)));
            }
            Key[] keys = keyList.ToArray ();
            Record[] records = client.Get (null, keys);
            foreach (Record record in records) {
                if (record != null) {
                    List<Object> theseEvents = (List<Object>)record.GetValue ("events");
                    events.AddRange(theseEvents);
                }
            }

            String result = eventRecord.GetString ("user-id") + ":" + eventRecord.GetString ("day") + ":" + eventRecord.GetInt ("sequence") + ":" + events.Count;
            return result;
        }