protected override void PreStart()
        {
            base.PreStart();

            // Create session
            CassandraJournalSettings settings = _cassandraExtension.JournalSettings;

            _session = _cassandraExtension.SessionManager.ResolveSession(settings.SessionKey);

            // Create keyspace if necessary and always try to create table
            if (settings.KeyspaceAutocreate)
            {
                _session.Execute(string.Format(JournalStatements.CreateKeyspace, settings.Keyspace, settings.KeyspaceCreationOptions));
            }

            var fullyQualifiedTableName = string.Format("{0}.{1}", settings.Keyspace, settings.Table);

            string createTable = string.IsNullOrWhiteSpace(settings.TableCreationProperties)
                                     ? string.Format(JournalStatements.CreateTable, fullyQualifiedTableName, string.Empty, string.Empty)
                                     : string.Format(JournalStatements.CreateTable, fullyQualifiedTableName, " WITH ",
                                                     settings.TableCreationProperties);

            _session.Execute(createTable);

            // Prepare some statements against C*
            _writeMessage              = _session.PrepareFormat(JournalStatements.WriteMessage, fullyQualifiedTableName);
            _writeHeader               = _session.PrepareFormat(JournalStatements.WriteHeader, fullyQualifiedTableName);
            _selectHeaderSequence      = _session.PrepareFormat(JournalStatements.SelectHeaderSequence, fullyQualifiedTableName);
            _selectLastMessageSequence = _session.PrepareFormat(JournalStatements.SelectLastMessageSequence, fullyQualifiedTableName);
            _selectMessages            = _session.PrepareFormat(JournalStatements.SelectMessages, fullyQualifiedTableName);
            _writeDeleteMarker         = _session.PrepareFormat(JournalStatements.WriteDeleteMarker, fullyQualifiedTableName);
            _deleteMessagePermanent    = _session.PrepareFormat(JournalStatements.DeleteMessagePermanent, fullyQualifiedTableName);
            _selectDeletedToSequence   = _session.PrepareFormat(JournalStatements.SelectDeletedToSequence, fullyQualifiedTableName);
            _selectConfigurationValue  = _session.PrepareFormat(JournalStatements.SelectConfigurationValue, fullyQualifiedTableName);
            _writeConfigurationValue   = _session.PrepareFormat(JournalStatements.WriteConfigurationValue, fullyQualifiedTableName);

            // The partition size can only be set once (the first time the table is created) so see if it's already been set
            long partitionSize = GetConfigurationValueOrDefault("partition-size", -1L);

            if (partitionSize == -1L)
            {
                // Persist the partition size specified in the cluster settings
                WriteConfigurationValue("partition-size", settings.PartitionSize);
            }
            else if (partitionSize != settings.PartitionSize)
            {
                throw new ConfigurationException(string.Format(InvalidPartitionSizeException, partitionSize, settings.PartitionSize));
            }
        }
        public CassandraExtension(ExtendedActorSystem system)
        {
            if (system == null) throw new ArgumentNullException("system");
            
            // Initialize fallback configuration defaults
            system.Settings.InjectTopLevelFallback(CassandraPersistence.DefaultConfig());

            // Get or add the session manager
            SessionManager = CassandraSession.Instance.Apply(system);
            
            // Read config
            var journalConfig = system.Settings.Config.GetConfig("cassandra-journal");
            JournalSettings = new CassandraJournalSettings(journalConfig);

            var snapshotConfig = system.Settings.Config.GetConfig("cassandra-snapshot-store");
            SnapshotStoreSettings = new CassandraSnapshotStoreSettings(snapshotConfig);
        }