예제 #1
0
        private void QueueSingleIndexUpdate(IndexInfo index, int id, int revision, XDoc doc)
        {
            Map(doc);

            // TODO (arnec): what to do when enqueue fails...
            _processingQueue.TryEnqueue(new WorkItem(index, id, revision, doc));
        }
예제 #2
0
 public WorkItem(IndexInfo index, int id, int revision, XDoc doc)
 {
     Index    = index;
     Id       = id;
     Revision = revision;
     Doc      = doc;
 }
예제 #3
0
 private void BuildIndex(IndexInfo info)
 {
     _catalog.NewQuery(string.Format("SELECT id, revision, doc FROM {0}", _name)).Execute(delegate(IDataReader reader) {
         while (reader.Read())
         {
             int id       = reader.GetInt32(0);
             int revision = reader.GetInt32(1);
             XDoc doc     = XDocFactory.From(reader.GetString(2), MimeType.TEXT_XML);
             QueueSingleIndexUpdate(info, id, revision, doc);
         }
     });
 }
예제 #4
0
        /// <summary>
        /// Rebuild all values in an index.
        /// </summary>
        /// <param name="keyName">Name of the index.</param>
        public void RebuildIndex(string keyName)
        {
            // make sure index exists
            IndexInfo info = GetIndexInfo(keyName);

            if (info == null)
            {
                throw new ArgumentException(string.Format("No index exists for key '{0}'", keyName));
            }
            _catalog.NewQuery(string.Format("TRUNCATE TABLE {0}", info.Table)).Execute();
            BuildIndex(info);
        }
예제 #5
0
        /// <summary>
        /// Drop an an index.
        /// </summary>
        /// <param name="keyName">Name of the index.</param>
        public void RemoveIndex(string keyName)
        {
            // make sure index exists
            IndexInfo info = GetIndexInfo(keyName);

            if (info == null)
            {
                return;
            }
            _catalog.NewQuery(string.Format("DELETE FROM {0} WHERE idx_name = ?KEY; DROP TABLE {1};", _indexLookupTable, info.Table))
            .With("KEY", info.Name)
            .Execute();
        }
예제 #6
0
        //--- Methods ---

        /// <summary>
        /// Manually add an index.
        /// </summary>
        /// <param name="keyName">Name of the index.</param>
        /// <param name="xpath">XPath expression to index.</param>
        public void AddIndex(string keyName, string xpath)
        {
            // make sure index doesn't already exist
            if (_indicies.ContainsKey(keyName))
            {
                RefreshIndicies();
                if (_indicies.ContainsKey(keyName))
                {
                    return;
                }
            }

            // TODO: need to sanity check keyName
            IndexInfo info = new IndexInfo();

            info.Name  = keyName;
            info.Table = _name + "_idx_" + keyName;
            info.XPath = xpath;
            try {
                _catalog.NewQuery(string.Format("INSERT INTO {0} VALUES (?KEY, ?XPATH)", _indexLookupTable))
                .With("KEY", keyName)
                .With("XPATH", xpath)
                .Execute();
                _catalog.NewQuery(string.Format(@"
CREATE TABLE {0} (
  ref_id int not null,
  ref_revision int not null,
  idx_value varchar(255),
  key(ref_id),
  key(idx_value(40)) );", info.Table))
                .Execute();
            } catch (Exception e) {
                // Note: need to do this by reflection magic, because Dream doesn't take DB dependencies at the
                // dll level
                if (StringUtil.EqualsInvariant(e.GetType().ToString(), "MySql.Data.MySqlClient.MySqlException"))
                {
                    try {
                        int errorNumber = (int)e.GetType().GetProperty("Number").GetValue(e, null);

                        // trap for duplicate key or existing table collisions
                        if (errorNumber == 1062 || errorNumber == 1050)
                        {
                            return;
                        }
                    } catch { }
                }
                throw;
            }
            _indicies[keyName] = info;
            BuildIndex(info);
        }
예제 #7
0
        /// <summary>
        /// Modify an existing index.
        /// </summary>
        /// <param name="keyName">Name of existing index.</param>
        /// <param name="xpath">New XPath expression.</param>
        public void ChangeIndex(string keyName, string xpath)
        {
            // make sure index exists
            IndexInfo info = GetIndexInfo(keyName);

            if (info == null)
            {
                AddIndex(keyName, xpath);
                return;
            }
            _catalog.NewQuery(string.Format("UPDATE {0} SET idx_xpath = ?XPATH where idx_name = ?KEY", _indexLookupTable))
            .With("KEY", keyName)
            .With("XPATH", xpath)
            .Execute();
            RefreshIndicies();
            RebuildIndex(keyName);
        }
예제 #8
0
 private void RefreshIndicies()
 {
     lock (_indicies) {
         Dictionary <string, IndexInfo> indicies = new Dictionary <string, IndexInfo>();
         _catalog.NewQuery(string.Format(@"SELECT idx_name, idx_xpath FROM {0}", _indexLookupTable))
         .Execute(delegate(IDataReader reader) {
             while (reader.Read())
             {
                 IndexInfo index = new IndexInfo();
                 index.Name      = reader.GetString(0);
                 index.Table     = _name + "_idx_" + index.Name;
                 index.XPath     = reader.GetString(1);
                 indicies.Add(index.Name, index);
             }
         });
         _indicies = indicies;
     }
 }
예제 #9
0
 public WorkItem(IndexInfo index, int id, int revision, XDoc doc)
 {
     Index = index;
     Id = id;
     Revision = revision;
     Doc = doc;
 }
예제 #10
0
 private void RefreshIndicies()
 {
     lock(_indicies) {
         Dictionary<string, IndexInfo> indicies = new Dictionary<string, IndexInfo>();
         _catalog.NewQuery(string.Format(@"SELECT idx_name, idx_xpath FROM {0}", _indexLookupTable))
             .Execute(delegate(IDataReader reader) {
             while(reader.Read()) {
                 IndexInfo index = new IndexInfo();
                 index.Name = reader.GetString(0);
                 index.Table = _name + "_idx_" + index.Name;
                 index.XPath = reader.GetString(1);
                 indicies.Add(index.Name, index);
             }
         });
         _indicies = indicies;
     }
 }
예제 #11
0
        private void QueueSingleIndexUpdate(IndexInfo index, int id, int revision, XDoc doc)
        {
            Map(doc);

            // TODO (arnec): what to do when enqueue fails...
            _processingQueue.TryEnqueue(new WorkItem(index, id, revision, doc));
        }
예제 #12
0
 private void BuildIndex(IndexInfo info)
 {
     _catalog.NewQuery(string.Format("SELECT id, revision, doc FROM {0}", _name)).Execute(delegate(IDataReader reader) {
         while(reader.Read()) {
             int id = reader.GetInt32(0);
             int revision = reader.GetInt32(1);
             XDoc doc = XDocFactory.From(reader.GetString(2), MimeType.TEXT_XML);
             QueueSingleIndexUpdate(info, id, revision, doc);
         }
     });
 }
예제 #13
0
        //--- Methods ---
        /// <summary>
        /// Manually add an index.
        /// </summary>
        /// <param name="keyName">Name of the index.</param>
        /// <param name="xpath">XPath expression to index.</param>
        public void AddIndex(string keyName, string xpath)
        {
            // make sure index doesn't already exist
            if(_indicies.ContainsKey(keyName)) {
                RefreshIndicies();
                if(_indicies.ContainsKey(keyName)) {
                    return;
                }
            }

            // TODO: need to sanity check keyName
            IndexInfo info = new IndexInfo();
            info.Name = keyName;
            info.Table = _name + "_idx_" + keyName;
            info.XPath = xpath;
            try {
                _catalog.NewQuery(string.Format("INSERT INTO {0} VALUES (?KEY, ?XPATH)", _indexLookupTable))
                    .With("KEY", keyName)
                    .With("XPATH", xpath)
                    .Execute();
                _catalog.NewQuery(string.Format(@"
            CREATE TABLE {0} (
              ref_id int not null,
              ref_revision int not null,
              idx_value varchar(255),
              key(ref_id),
              key(idx_value(40)) );", info.Table))
                .Execute();
            } catch(Exception e) {

                // Note: need to do this by reflection magic, because Dream doesn't take DB dependencies at the
                // dll level
                if(StringUtil.EqualsInvariant(e.GetType().ToString(), "MySql.Data.MySqlClient.MySqlException")) {
                    try {
                        int errorNumber = (int)e.GetType().GetProperty("Number").GetValue(e, null);

                        // trap for duplicate key or existing table collisions
                        if(errorNumber == 1062 || errorNumber == 1050) {
                            return;
                        }
                    } catch { }
                }
                throw;
            }
            _indicies[keyName] = info;
            BuildIndex(info);
        }