Пример #1
0
 /// <summary>
 /// Dispose of this Table instance.
 /// </summary>
 public virtual void Dispose()
 {
     if (!_initialize)
     {
         _keyspace.RemoveTable(this);
         _keyspace = null;
         Columns.Dispose();
     }
 }
Пример #2
0
 /// <summary>
 /// Add a Keyspace to this DataCluster.
 /// </summary>
 internal void AddKeyspace(Keyspace keyspace)
 {
     _lock.Take();
     // if the default keyspace hasn't been set
     if (_defaultKeyspace == null)
     {
         _defaultKeyspace = keyspace;
     }
     Keyspaces.Add(keyspace.Metadata.Name, keyspace);
     _lock.Release();
 }
Пример #3
0
        /// <summary>
        /// Initialize a new query for the specified table and keyspace.
        /// </summary>
        public Query(Table table, Keyspace keyspace, int pageSize = -1)
        {
            Table     = table;
            Keyspace  = keyspace;
            _pageSize = pageSize == -1 ? Table.DefaultPageSize : pageSize;

            // initialize the collections of value sets
            Keywords = new ArrayRig <Cql>(3);
            Sets     = new ArrayRig <int>(3);
            Values   = new ArrayRig <object>(Table.Columns.Count);
            Columns  = new ArrayRig <Column>(Table.Columns.Count);
        }
Пример #4
0
        /// <summary>
        /// Remove a Keyspace from this DataCluster.
        /// </summary>
        internal void RemoveKeyspace(Keyspace keyspace)
        {
            _lock.Take();

            // if the keyspace removed is the default keyspace
            Keyspaces.Remove(keyspace.Metadata.Name);
            if (_defaultKeyspace == keyspace)
            {
                if (Keyspaces.Count == 0)
                {
                    _defaultKeyspace = null;
                }
                else
                {
                    foreach (var entry in Keyspaces)
                    {
                        _defaultKeyspace = entry.Value;
                        break;
                    }
                }
            }
            _lock.Release();
        }
Пример #5
0
        //-------------------------------------------//

        /// <summary>
        /// Initialize this Table instance.
        /// </summary>
        private void Initialize()
        {
            // take the initialize lock
            _lock.Take();
            // was the table initialized by another thread? yes, skip
            if (!_initialize)
            {
                _lock.Release();
                return;
            }

            // if the keyspace name wasn't set - get the current default
            _keyspace = _keyspaceName == null ?
                        ManagerCql.DefaultCluster.DefaultKeyspace :
                        ManagerCql.DefaultCluster.GetKeyspace(_keyspaceName);

            // get the table metadata
            if (_keyspace.Metadata == null)
            {
                // create an inline timer to wait for keyspace initialization
                TimeInline timer = new TimeInline(4000, () => _keyspace.Metadata != null);

                // wait until timeout or keyspace metadata being populated
                while (timer.Wait)
                {
                }

                // was the metadata retrieved?
                if (!timer.Success)
                {
                    // no, error log
                    Log.Error("Table '" + Name + "' isn't available. Keyspace '" + _keyspaceName + "' couldn't initialize.");
                    _lock.Release();
                    return;
                }
            }

            // get the metadata for the table - does the table exist?
            if (!_keyspace.TableMetadata.TakeItem().TryGetValue(Name, out Metadata))
            {
                // no, create the table
                _keyspace.TableMetadata.Release();

                bool primaryKey = false;

                // add this Table to the Keyspace
                Query          builder     = new Query(this, _keyspace);
                CqlCreateTable createQuery = new CqlCreateTable(builder);
                // iterate the columns to create
                foreach (Column column in Columns)
                {
                    createQuery.With(column);
                    primaryKey |= column.Class == Column.ColumnClass.Primary;
                }

                // if there were no primary keys
                if (!primaryKey)
                {
                    Log.Error("Cannot create a Table with no primary keys.");
                    _lock.Release();
                    return;
                }

                // run the query and get the metadata
                Metadata = createQuery;

                // add the metadata to the keyspace
                _keyspace.TableMetadata.TakeItem().Add(Name, Metadata);
            }
            _keyspace.TableMetadata.Release();

            // add this table to the keyspace
            _keyspace.AddTable(this);

            ArrayRig <Column>      newColumns = new ArrayRig <Column>(Columns);
            ArrayRig <TableColumn> oldColumns = new ArrayRig <TableColumn>();

            // iterate table column in the metadata
            foreach (TableColumn col in Metadata.TableColumns)
            {
                // flag to indicate the column reference exists
                bool found = false;

                // iterate the columns
                for (int i = Columns.Count - 1; i >= 0; --i)
                {
                    // if the name matches
                    if (col.Name == Columns[i].Name)
                    {
                        // set the index
                        Columns[i].Index = col.Index;
                        // remove from add collection
                        newColumns.RemoveQuick(Columns[i]);
                        found = true;
                        break;
                    }
                }

                // was the column found in the metadata? no, add to be removed
                if (!found)
                {
                    oldColumns.Add(col);
                }
            }

            // add each new column
            foreach (var newColumn in newColumns)
            {
                // log the removal
                Log.CriticalChange("A COLUMN IS ABOUT TO BE Added : " + Keyspace.Metadata.Name + Chars.Stop, 2);

                // get the builder
                var builder = StringBuilderCache.Get();

                // append a query that will add the new column
                builder.Append("ALTER TABLE ");
                builder.Append(Keyspace.Metadata.Name);
                builder.Append(Chars.Stop);
                builder.Append(Name);
                builder.Append(" ADD ");
                builder.Append(newColumn.Name);
                builder.Append(Chars.Space);
                builder.Append(newColumn.DataType);
                builder.Append(Chars.SemiColon);

                // get a simple statement
                var addStatement = new SimpleStatement(builder.ToString());

                // execute the statement
                Keyspace.Session.Execute(addStatement);
            }

            // iterate the old columns
            foreach (var oldColumn in oldColumns)
            {
                // log the removal
                Log.CriticalChange("A COLUMN IS ABOUT TO BE DROPPED : " + Keyspace.Metadata.Name + Chars.Stop, 10);

                // get the builder
                var builder = StringBuilderCache.Get();

                // append a query that will remove the old column
                builder.Append("ALTER TABLE ");
                builder.Append(Keyspace.Metadata.Name);
                builder.Append(Chars.Stop);
                builder.Append(Name);
                builder.Append(" DROP ");
                builder.Append(oldColumn.Name);
                builder.Append(Chars.SemiColon);

                // get a simple statement
                var removeStatement = new SimpleStatement(builder.ToString());

                // execute the statement
                Keyspace.Session.Execute(removeStatement);
            }

            // the table has been initialized
            _initialize = false;

            // release the lock
            _lock.Release();
        }