Beispiel #1
0
        private static DBProvider InitTable(TableConfig tableCfg)
        {
            DBProvider dbProvider = new DBProvider();

            dbProvider.Open(tableCfg.Directory);

            return(dbProvider);
        }
Beispiel #2
0
        public static void NewDBProvider(string tableName, DBProvider dbProvider)
        {
            tableName = Setting.GetTableFullName(tableName);

            lock (_sLockObj)
            {
                _DBProviderTable.Add(tableName.ToLower().Trim(), dbProvider);
            }
        }
Beispiel #3
0
        public Analysis.IAnalyzer GetAnalyzer()
        {
            if (string.IsNullOrEmpty(AnalyzerName))
            {
                _Analyzer = new Analysis.SimpleAnalyzer();
            }
            else
            {
                _Analyzer = DBProvider.GetAnalyzer(AnalyzerName);

                if (_Analyzer == null)
                {
                    throw new DataException(string.Format("Can't find analyzer name : {0}", AnalyzerName));
                }
            }

            return(_Analyzer);
        }
Beispiel #4
0
        unsafe private string GetInSql(Data.DBProvider dbProvider, Query.DocumentResultForSort[] result, int begin, int count)
        {
            if (begin + count > result.Length)
            {
                return(null);
            }

            StringBuilder sql = new StringBuilder();

            if (dbProvider.DocIdReplaceField == null)
            {
                sql.Append("docId in (");
            }
            else
            {
                sql.AppendFormat("{0} in (", dbProvider.DocIdReplaceField);
            }

            Dictionary <long, int> replaceFieldValueToDocId = null;

            if (dbProvider.DocIdReplaceField != null)
            {
                replaceFieldValueToDocId = new Dictionary <long, int>();
            }

            int i = 0;

            for (int j = begin; j < begin + count; j++)
            {
                Query.DocumentResultForSort docResult = result[j];
                int docId = docResult.DocId;

                if (dbProvider.DocIdReplaceField == null)
                {
                    if (i++ == 0)
                    {
                        sql.AppendFormat("{0}", docId);
                    }
                    else
                    {
                        sql.AppendFormat(",{0}", docId);
                    }
                }
                else
                {
                    long replaceFieldValue = dbProvider.GetDocIdReplaceFieldValue(docId);

                    if (i++ == 0)
                    {
                        sql.AppendFormat("{0}", replaceFieldValue);
                    }
                    else
                    {
                        sql.AppendFormat(",{0}", replaceFieldValue);
                    }
                }
            }

            sql.Append(")");

            return(sql.ToString());
        }
Beispiel #5
0
        public static void Init()
        {
#if HubblePro
            System.Reflection.Assembly bigTableAsm;
            bigTableAsm       = System.Reflection.Assembly.LoadFrom("HubblePro.dll");
            BigTableParseType = bigTableAsm.GetType("HubblePro.BigTable.BigTableParse");
#else
            BigTableParseType = typeof(BigTable.BigTableParse);
#endif


            //Init XML Cache File Serialization
            Hubble.Framework.Serialization.XmlSerialization <Cache.CacheFile> .Serialize(
                new Cache.CacheFile("",
                                    new Hubble.Core.Cache.QueryCacheDocuments(),
                                    new Hubble.Core.Cache.QueryCacheInformation()),
                Encoding.UTF8, new System.IO.MemoryStream());


            //Load user right from right.db
            try
            {
                Global.UserRightProvider.Load(Global.Setting.UserRigthFilePath);
            }
            catch (Exception e)
            {
                Global.Report.WriteErrorLog("Load user right db fail!", e);
            }

            //Set Ram Index Cache Max Memory
            try
            {
                Hubble.Framework.IO.CachedFileBufferManager.Init(Global.Setting.Config.RamIndexMemoryLimited,
                                                                 ErrorMessageForCachedFileBufferManager);
            }
            catch (Exception e)
            {
                Global.Report.WriteErrorLog("Set RamIndexMemoryLimited fail!", e);
            }


            //Build QueryTable

            System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();

            foreach (Type type in asm.GetTypes())
            {
                //Init _QueryTable
                if (type.GetInterface("Hubble.Core.Query.IQuery") != null)
                {
                    Hubble.Core.Query.IQuery iQuery = asm.CreateInstance(type.FullName) as
                                                      Hubble.Core.Query.IQuery;
                    string key = iQuery.Command.ToLower().Trim();
                    if (!_QueryTable.ContainsKey(key))
                    {
                        _QueryTable.Add(key, type);
                    }
                    else
                    {
                        Global.Report.WriteErrorLog(string.Format("Reduplicate query command name = {0}",
                                                                  iQuery.Command));
                    }
                }

                //Init _AnalyzerTable
                if (type.GetInterface("Hubble.Core.Analysis.IAnalyzer") != null)
                {
                    INamedExternalReference refer = asm.CreateInstance(type.FullName) as
                                                    INamedExternalReference;

                    if (refer == null)
                    {
                        Report.WriteErrorLog(string.Format("External reference {0} does not inherit from INamedExternalReference",
                                                           type.FullName));
                    }
                    else
                    {
                        string key = refer.Name.ToLower().Trim();


                        if (!_AnalyzerTable.ContainsKey(key))
                        {
                            Analysis.IAnalyzer analyzer = refer as Analysis.IAnalyzer;
                            analyzer.Init();
                            _AnalyzerTable.Add(key, type);
                        }
                        else
                        {
                            Global.Report.WriteErrorLog(string.Format("Reduplicate name = {0} in External reference {1}!",
                                                                      refer.Name, type.FullName));
                        }
                    }
                }

                //Init _DBAdapterTable
                if (type.GetInterface("Hubble.Core.DBAdapter.IDBAdapter") != null)
                {
                    INamedExternalReference refer = asm.CreateInstance(type.FullName) as
                                                    INamedExternalReference;

                    if (refer == null)
                    {
                        Report.WriteErrorLog(string.Format("External reference {0} does not inherit from INamedExternalReference",
                                                           type.FullName));
                    }
                    else
                    {
                        string key = refer.Name.ToLower().Trim();

                        if (!_DBAdapterTable.ContainsKey(key))
                        {
                            _DBAdapterTable.Add(key, type);
                        }
                        else
                        {
                            Global.Report.WriteErrorLog(string.Format("Reduplicate name = {0} in External reference {1}!",
                                                                      refer.Name, type.FullName));
                        }
                    }
                }

                if (type.GetInterface("Hubble.Core.StoredProcedure.IStoredProc") != null)
                {
                    Hubble.Core.StoredProcedure.IStoredProc iSP = asm.CreateInstance(type.FullName) as
                                                                  Hubble.Core.StoredProcedure.IStoredProc;
                    string key = iSP.Name.ToLower().Trim();
                    if (!_StoredProcTable.ContainsKey(key))
                    {
                        _StoredProcTable.Add(key, type);
                    }
                    else
                    {
                        Global.Report.WriteErrorLog(string.Format("Reduplicate StoredProcedure name = {0}",
                                                                  iSP.Name));
                    }
                }
            }


            //Load from external reference
            //Load IQuery external reference
            foreach (IQueryConfig iquery in Setting.Config.IQuerys)
            {
                try
                {
                    if (iquery != null)
                    {
                        iquery.Load(_QueryTable);
                    }
                }
                catch (Exception e)
                {
                    Global.Report.WriteErrorLog(string.Format("Load IQuery fail. IQuery asm file:{0}", iquery.AssemblyFile),
                                                e);
                }
            }

            //Load IAnalyzer external reference
            foreach (IAnalyzerConfig ianalyzer in Setting.Config.IAnalyzers)
            {
                try
                {
                    if (ianalyzer != null)
                    {
                        ianalyzer.Load(_AnalyzerTable);
                    }
                }
                catch (Exception e)
                {
                    Global.Report.WriteErrorLog(string.Format("Load IAnalyzer fail. IAnalyzer asm file:{0}", ianalyzer.AssemblyFile),
                                                e);
                }
            }

            //Load IDBAdapter external reference
            foreach (IDBAdapterConfig idbadapter in Setting.Config.IDBAdapters)
            {
                try
                {
                    if (idbadapter != null)
                    {
                        idbadapter.Load(_DBAdapterTable);
                    }
                }
                catch (Exception e)
                {
                    Global.Report.WriteErrorLog(string.Format("Load IDBAdapter fail. IDBAdapter asm file:{0}", idbadapter.AssemblyFile),
                                                e);
                }
            }


            //Load IDistinct external reference
            foreach (IDistinctConfig idistinct in Setting.Config.IDistincts)
            {
                try
                {
                    if (idistinct != null)
                    {
                        idistinct.Load(Core.SFQL.Parse.DistinctInterfaceLoader.ExternalDistincts);
                    }
                }
                catch (Exception e)
                {
                    Global.Report.WriteErrorLog(string.Format("Load IDistinct fail. IDistinct asm file:{0}", idistinct.AssemblyFile),
                                                e);
                }
            }
            //List<string> removeDirs = new List<string>();

            //Init table
            foreach (TableConfig tc in Setting.Config.Tables)
            {
                try
                {
                    if (tc.Directory == null)
                    {
                        continue;
                    }

                    DBProvider dbProvider = InitTable(tc);

                    if (_DBAdapterTable.ContainsKey(dbProvider.TableName.ToLower().Trim()))
                    {
                        Global.Report.WriteErrorLog(string.Format("Init Table {0} fail, table exists!",
                                                                  dbProvider.TableName));
                    }
                    else
                    {
                        _DBProviderTable.Add(dbProvider.TableName.ToLower().Trim(), dbProvider);
                    }
                }
                catch (Exception e)
                {
                    //removeDirs.Add(tc.Directory);

                    Global.Report.WriteErrorLog(string.Format("Init Table at {0} fail, errmsg:{1} stack {2}",
                                                              tc.Directory, e.Message, e.StackTrace));
                }
            }

            Cache.QueryCacheManager.Manager.MaxMemorySize = Global.Setting.Config.QueryCacheMemoryLimited;

            //foreach (string removeDir in removeDirs)
            //{
            //    Setting.RemoveTableConfig(removeDir);
            //}
        }
Beispiel #6
0
        public static void Truncate(string tableName)
        {
            lock (_sLockObj)
            {
                if (_sNeedClose)
                {
                    return;
                }

                _sCanClose = false;
            }

            try
            {
                string fullTableName = Setting.GetTableFullName(tableName);

                DBProvider dbProvider = GetDBProvider(fullTableName, false);

                if (dbProvider != null)
                {
                    Table  table         = dbProvider.Table;
                    string dir           = dbProvider.Directory;
                    bool   indexReadOnly = table.IndexOnly;

                    dbProvider.SetIndexOnly(true);

                    //Don't need truncate table. because it will do drop table. 2012-04-26

                    //if (!indexReadOnly)
                    //{
                    //    dbProvider.DBAdapter.Truncate();
                    //}
                    //else
                    //{
                    //    if (dbProvider.MirrorDBAdapter != null)
                    //    {
                    //        try
                    //        {
                    //            dbProvider.MirrorDBAdapter.Truncate();
                    //        }
                    //        catch (Exception e)
                    //        {
                    //            Global.Report.WriteErrorLog(string.Format("Truncate mirror table fail! tablename={0}, dbadapter={1} connectstring={2}",
                    //                dbProvider.Table.MirrorDBTableName, dbProvider.Table.MirrorDBAdapterTypeName,
                    //                dbProvider.Table.MirrorConnectionString), e);
                    //        }
                    //    }
                    //}

                    Drop(tableName);

                    CreateTable(table, dir);

                    fullTableName = Setting.GetTableFullName(tableName);
                    dbProvider    = GetDBProvider(fullTableName);
                    dbProvider.SetIndexOnly(indexReadOnly);
                }
                else
                {
                    throw new DataException(string.Format("Table name {0} does not exist!",
                                                          tableName));
                }
            }
            finally
            {
                lock (_sLockObj)
                {
                    _sCanClose = true;
                }
            }
        }
Beispiel #7
0
        public static void Drop(string tableName)
        {
            lock (_sLockObj)
            {
                if (_sNeedClose)
                {
                    return;
                }

                _sCanClose = false;
            }

            try
            {
                tableName = Setting.GetTableFullName(tableName);

                DBProvider dbProvider = GetDBProvider(tableName, false);


                if (dbProvider != null)
                {
                    if (dbProvider.Table != null)
                    {
                        if (!dbProvider.Table.IsBigTable)
                        {
                            if (!dbProvider.Table.IndexOnly)
                            {
                                dbProvider.DBAdapter.Drop();
                            }
                            else
                            {
                                if (dbProvider.MirrorDBAdapter != null)
                                {
                                    dbProvider.MirrorDBAdapter.Drop();
                                }
                            }
                        }
                    }

                    if (!dbProvider._TableLock.Enter(Lock.Mode.Mutex, 30000))
                    {
                        throw new TimeoutException();
                    }

                    try
                    {
                        string dir = dbProvider.Directory;

                        if (dir == null)
                        {
                            DeleteTableName(tableName);
                            return;
                        }

                        dbProvider.Drop();
                        DeleteTableName(tableName);

                        Global.Setting.RemoveTableConfig(dir, tableName);
                        Global.Setting.Save();
                    }
                    finally
                    {
                        dbProvider._TableLock.Leave(Lock.Mode.Mutex);
                    }
                }
            }
            finally
            {
                lock (_sLockObj)
                {
                    _sCanClose = true;
                }
            }
        }
Beispiel #8
0
        public static void CreateTable(Table table, string directory)
        {
            lock (_sLockObj)
            {
                if (_sNeedClose)
                {
                    return;
                }

                _sCanClose = false;
            }

            try
            {
                if (table.Name == null)
                {
                    throw new System.ArgumentNullException("Null table name");
                }

                if (table.Name.Trim() == "")
                {
                    throw new System.ArgumentException("Empty table name");
                }

                table.Name = Setting.GetTableFullName(table.Name.Trim());

                if (DBProvider.DBProviderExists(table.Name))
                {
                    throw new DataException(string.Format("Table {0} exists already!", table.Name));
                }

                directory = Hubble.Framework.IO.Path.AppendDivision(directory, '\\');

                if (!System.IO.Directory.Exists(directory))
                {
                    System.IO.Directory.CreateDirectory(directory);
                }

                DBProvider.NewDBProvider(table.Name, new DBProvider());

                try
                {
                    DBProvider.GetDBProvider(table.Name, false).Create(table, directory);
                }
                catch (Exception e)
                {
                    Global.Report.WriteErrorLog("Create table fail!", e);

                    DBProvider.Drop(table.Name);

                    throw e;
                }
            }
            finally
            {
                lock (_sLockObj)
                {
                    _sCanClose = true;
                }
            }
        }