public ExclusiveDBConnectionPool(string host, int port, string uid, string pwd, int count, bool loadBalance, bool highAvaliability)
 {
     conns = new List <DBConnection>(count);
     if (!loadBalance)
     {
         for (int i = 0; i < count; i++)
         {
             DBConnection conn = new DBConnection();
             if (!conn.connect(host, port, uid, pwd, "", highAvaliability))
             {
                 throw new Exception("Cant't connect to the specified host.");
             }
             conns.Add(conn);
         }
     }
     else
     {
         DBConnection entryPoint = new DBConnection();
         if (!entryPoint.connect(host, port, uid, pwd))
         {
             throw new Exception("Can't connect to the specified host.");
         }
         BasicStringVector nodes = (BasicStringVector)entryPoint.run("rpc(getControllerAlias(), getClusterLiveDataNodes{false})");
         int      nodeCount      = nodes.rows();
         string[] hosts          = new string[nodeCount];
         int[]    ports          = new int[nodeCount];
         for (int i = 0; i < nodeCount; i++)
         {
             string[] fields = nodes.getString(i).Split(':');
             if (fields.Length < 2)
             {
                 throw new Exception("Invalid datanode adress: " + nodes.getString(i));
             }
             hosts[i] = fields[0];
             ports[i] = int.Parse(fields[1]);
         }
         for (int i = 0; i < count; i++)
         {
             DBConnection conn = new DBConnection();
             if (!(conn.connect(hosts[i % nodeCount], ports[i % nodeCount], uid, pwd, "", highAvaliability)))
             {
                 throw new Exception("Can't connect to the host: " + nodes.getString(i % nodeCount));
             }
             conns.Add(conn);
         }
     }
     ThreadPool.SetMaxThreads(count, count);
 }
Ejemplo n.º 2
0
        private void initialize(IList <string> strings, BasicAnyVector locations)
        {
            if (locations.rows() <= 0)
            {
                throw new Exception("requires at least one location");
            }
            if (locations.getEntity(0).DataType != Entity_DATA_TYPE.DT_STRING)
            {
                throw new Exception("location must be a string");
            }
            if (strings.Count != locations.rows())
            {
                throw new Exception("expect # locations == # values");
            }
            map = new Dictionary <>();
            bool isScalar = locations.getEntity(0).DataForm == Entity_DATA_FORM.DF_SCALAR;

            if (isScalar)
            {
                for (int i = 0; i < locations.rows(); ++i)
                {
                    BasicString location = (BasicString)locations.get(i);
                    string      val      = strings[i];
                    map[val] = location.String;
                }
            }
            else
            {
                for (int i = 0; i < locations.rows(); ++i)
                {
                    BasicStringVector locationVector = (BasicStringVector)locations.getEntity(i);
                    string            val            = strings[i];
                    map[val] = new List <>();
                    for (int j = 0; j < locationVector.rows(); ++j)
                    {
                        map[val].Add(locationVector.getString(j));
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public autoFitTableAppender(string dbUrl, string tableName, bool asynTask, DBConnection conn)
        {
            this.dbUrl     = dbUrl;
            this.tableName = tableName;
            this.asynTask  = asynTask;
            this.conn      = conn;
            conn.setasynTask(false);
            BasicDictionary tableInfo = (BasicDictionary)conn.run("schema(loadTable(\"" + dbUrl + "\", \"" + tableName + "\"))");
            BasicTable      colDefs;

            colDefs = ((BasicTable)tableInfo.get(new BasicString("colDefs")));
            BasicStringVector names = (BasicStringVector)colDefs.getColumn("name");
            BasicIntVector    types = (BasicIntVector)colDefs.getColumn("typeInt");
            int rows = names.rows();


            this.schema = new Dictionary <string, DATA_TYPE>();
            for (int i = 0; i < rows; ++i)
            {
                schema.Add(names.getString(i), (DATA_TYPE)types.getInt(i));
            }
            conn.setasynTask(asynTask);
        }
        public PartitionedTableAppender(string dbUrl, string tableName, string partitionColName, string appendFunction, IDBConnectionPool pool)
        {
            this.pool    = pool;
            threadCount  = pool.getConnectionCount();
            chunkIndices = new List <List <int> >(threadCount);
            for (int i = 0; i < threadCount; ++i)
            {
                chunkIndices.Add(new List <int>());
            }
            DBConnection   conn = new DBConnection();
            IEntity        partitionSchema;
            BasicTable     colDefs;
            BasicIntVector typeInts;
            int            partitionType;
            DATA_TYPE      partitionColType;

            try
            {
                IDBTask task;
                if (dbUrl == null || dbUrl.Length == 0)
                {
                    task         = new BasicDBTask("schema(" + tableName + ")");
                    appendScript = "tableInsert{" + tableName + "}";
                }
                else
                {
                    task         = new BasicDBTask("schema(loadTable(\"" + dbUrl + "\", \"" + tableName + "\"))");
                    appendScript = "tableInsert{loadTable('" + dbUrl + "', '" + tableName + "')}";
                }
                if (appendFunction != null && appendFunction.Length != 0)
                {
                    appendScript = appendFunction;
                }
                pool.execute(task);
                if (!task.isSuccessful())
                {
                    throw new Exception(task.getErrorMsg());
                }
                tableInfo = (BasicDictionary)task.getResults();

                IEntity partColNames = tableInfo.get(new BasicString("partitionColumnName"));
                if (partColNames == null)
                {
                    throw new Exception("Can't find specified partition column name.");
                }
                if (partColNames.isScalar())
                {
                    if (!((BasicString)partColNames).getString().Equals(partitionColName, StringComparison.OrdinalIgnoreCase))
                    {
                        throw new Exception("Can't find specified partition column name.");
                    }
                    partitionColumnIdx = ((BasicInt)tableInfo.get(new BasicString("partitionColumnIndex"))).getValue();
                    partitionSchema    = tableInfo.get(new BasicString("partitionSchema"));
                    partitionType      = ((BasicInt)tableInfo.get(new BasicString("partitionType"))).getValue();
                    //
                    partitionColType = (DATA_TYPE)((BasicInt)tableInfo.get(new BasicString("partitionColumnType"))).getValue();
                }


                else
                {
                    BasicStringVector vec = (BasicStringVector)partColNames;
                    int dims  = vec.rows();
                    int index = -1;
                    for (int i = 0; i < dims; ++i)
                    {
                        if (!vec.getString(i).Equals(partitionColName, StringComparison.OrdinalIgnoreCase))
                        {
                            index = i;
                            break;
                        }
                    }
                    if (index < 0)
                    {
                        throw new Exception("Can't find specified partition column name.");
                    }
                    partitionColumnIdx = ((BasicIntVector)tableInfo.get(new BasicString("partitionColumnIndex"))).getInt(index);
                    partitionSchema    = ((BasicAnyVector)tableInfo.get(new BasicString("partitionSchema"))).getEntity(index);
                    partitionType      = ((BasicIntVector)tableInfo.get(new BasicString("partitionType"))).getInt(index);
                    //
                    partitionColType = (DATA_TYPE)((BasicIntVector)tableInfo.get(new BasicString("partitionColumnType"))).getInt(index);
                }
                colDefs               = ((BasicTable)tableInfo.get(new BasicString("colDefs")));
                this.cols             = colDefs.getColumn(0).rows();
                typeInts              = (BasicIntVector)colDefs.getColumn("typeInt");
                this.columnCategories = new DATA_CATEGORY[this.cols];
                this.columnTypes      = new DATA_TYPE[this.cols];
                for (int i = 0; i < cols; ++i)
                {
                    this.columnTypes[i]      = (DATA_TYPE)typeInts.getInt(i);
                    this.columnCategories[i] = Utils.typeToCategory(this.columnTypes[i]);
                }
                domain = DomainFactory.createDomain((PARTITION_TYPE)partitionType, partitionColType, partitionSchema);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                conn.close();
            }
        }
Ejemplo n.º 5
0
        /**
         * Add the name of the database and table that you want to insert data into before actually call insert.
         * The parameter partitioned indicates whether the added table is a partitioned table. If this function
         * is called to add an in-memory table, the parameter dbName indicates the name of the shared in-memory
         * table, and the parameter tableName should be a null string. If error is raised on the server, this
         * function throws an exception.
         */
        public void addTable(string dbName, string tableName = "", bool partitioned = true)
        {
            try
            {
                rwLock_.EnterReadLock();
                if (destTables_.ContainsKey(Tuple.Create <string, string>(dbName, tableName)))
                {
                    throw new Exception("Failed to add table, the specified table has not been removed yet.");
                }
            }
            finally
            {
                rwLock_.ExitReadLock();
            }
            DBConnection conn = new DBConnection(false, false);
            bool         ret  = conn.connect(hostName_, port_, userId_, password_);

            if (!ret)
            {
                throw new Exception("Failed to connect to server.");
            }
            string          tableInsert;
            string          saveTable = "";
            BasicDictionary schema;
            string          tmpDiskGlobal = "tmpDiskGlobal";

            if (tableName == "")
            {
                tableInsert = "tableInsert{" + dbName + "}";
                schema      = (BasicDictionary)conn.run("schema(" + dbName + ")");
            }
            else if (partitioned)
            {
                tableInsert = "tableInsert{loadTable(\"" + dbName + "\",\"" + tableName + "\")}";
                schema      = (BasicDictionary)conn.run("schema(loadTable(\"" + dbName + "\",\"" + tableName + "\"))");
            }
            else
            {
                throw new Exception("The target table must be an in-memory table or a table in a distributed database.");
            }

            BasicTable colDefs = (BasicTable)schema.get(new BasicString("colDefs"));

            DestTable destTable;

            if (destTables_.ContainsKey(Tuple.Create <string, string>(dbName, tableName)))
            {
                throw new Exception("Failed to add table, the specified table has not been removed yet.");
            }
            destTable = new DestTable();

            destTable.dbName         = dbName;
            destTable.tableName      = tableName;
            destTable.conn           = conn;
            destTable.tableInsert    = tableInsert;
            destTable.saveTable      = saveTable;
            destTable.colDefs        = colDefs;
            destTable.columnNum      = colDefs.rows();
            destTable.colDefsTypeInt = (BasicIntVector)colDefs.getColumn("typeInt");
            destTable.destroy        = false;
            destTable.writeQueue     = new ConcurrentQueue <List <IScalar> >();
            destTable.failQueue      = new ConcurrentQueue <List <IScalar> >();

            List <string>     colNames    = new List <string>();
            List <DATA_TYPE>  colTypes    = new List <DATA_TYPE>();
            BasicStringVector colDefsName = (BasicStringVector)colDefs.getColumn("name");

            for (int i = 0; i < destTable.columnNum; i++)
            {
                colNames.Add(colDefsName.getString(i));
                colTypes.Add((DATA_TYPE)destTable.colDefsTypeInt.getInt(i));
            }
            destTable.colNames = colNames;
            destTable.colTypes = colTypes;

            if (!partitioned)
            {
                string            colName           = "";
                string            colType           = "";
                BasicStringVector colDefsTypeString = (BasicStringVector)colDefs.getColumn("typeString");
                for (int i = 0; i < destTable.columnNum; i++)
                {
                    colName = colName + "`" + colDefsName.getString(i);
                    colType = colType + "`" + colDefsTypeString.getString(i);
                }
                destTable.createTmpSharedTable = "share table(" + "1000:0," + colName + "," + colType + ") as " + tmpDiskGlobal;
            }
            try
            {
                rwLock_.EnterWriteLock();
                if (destTables_.ContainsKey(Tuple.Create <string, string>(dbName, tableName)))
                {
                    throw new Exception("Failed to add table, the specified table has not been removed yet.");
                }
                destTables_[Tuple.Create <string, string>(dbName, tableName)] = destTable;
                WriteThread writeThreadFuc = new WriteThread(destTable, partitioned);
                destTable.writeThread = new Thread(writeThreadFuc.run);
                destTable.writeThread.Start();
            }

            finally
            {
                rwLock_.ExitWriteLock();
            }
        }
        /**
         * If fail to connect to the specified DolphinDB server, this function throw an exception.
         */
        public MultithreadedTableWriter(string hostName, int port, string userId, string password,
                                        string dbName, string tableName, bool useSSL, bool enableHighAvailability = false, string[] pHighAvailabilitySites = null,
                                        int batchSize = 1, float throttle = 0.01f, int threadCount = 5, string partitionCol = "", int[] pCompressMethods = null)
        {
            hostName_          = hostName;
            port_              = port;
            userId_            = userId;
            password_          = password;
            useSSL_            = useSSL;
            dbName_            = dbName;
            tableName_         = tableName;
            batchSize_         = batchSize;
            throttleMilsecond_ = (int)throttle * 1000;
            isExiting_         = false;
            if (threadCount < 1)
            {
                throw new Exception("The parameter threadCount must be greater than or equal to 1.");
            }
            if (batchSize < 1)
            {
                throw new Exception("The parameter batchSize must be greater than or equal to 1.");
            }
            if (throttle < 0)
            {
                throw new Exception("The parameter throttle must be positive.");
            }
            if (threadCount > 1 && partitionCol == String.Empty)
            {
                throw new Exception("The parameter partitionCol must be specified when threadCount is greater than 1.");
            }
            DBConnection pConn = new DBConnection(false, useSSL_, pCompressMethods != null);
            bool         ret   = pConn.connect(hostName_, port_, userId_, password_, "", enableHighAvailability, pHighAvailabilitySites);

            if (!ret)
            {
                throw new Exception(string.Format("Failed to connect to server {0}:{1}. ", hostName, port));
            }
            BasicDictionary schema;

            if (tableName == "")
            {
                schema = (BasicDictionary)pConn.run("schema(" + dbName + ")");
            }
            else
            {
                schema = (BasicDictionary)pConn.run("schema(loadTable(\"" + dbName + "\",\"" + tableName + "\"))");
            }
            IEntity partColNames = null;

            if (schema.ContainsKey("partitionColumnName"))
            {
                partColNames      = schema.get(new BasicString("partitionColumnName"));
                isPartionedTable_ = true;
            }
            else
            {
                isPartionedTable_ = false;
                if (tableName != "")
                {
                    if (threadCount > 1)
                    {//只有多线程的时候需要
                        throw new Exception("The parameter threadCount must be 1 for a dimension table.");
                    }
                }
            }
            BasicTable colDefs = (BasicTable)schema.get("colDefs");

            BasicIntVector colDefsTypeInt = (BasicIntVector)colDefs.getColumn("typeInt");

            BasicStringVector colDefsName       = (BasicStringVector)colDefs.getColumn("name");
            BasicStringVector colDefsTypeString = (BasicStringVector)colDefs.getColumn("typeString");

            colTypes_      = new List <DATA_TYPE>();
            colNames_      = new List <string>();
            colTypeString_ = new List <string>();
            int columnSize = colDefsName.rows();

            if (pCompressMethods != null)
            {
                if (columnSize != pCompressMethods.Length)
                {
                    throw new Exception(string.Format("The number of elements in parameter compressMethods does not match the column size {0}. ", columnSize));
                }
                this.compressTypes_ = new int[columnSize];
                Array.Copy(pCompressMethods, this.compressTypes_, columnSize);
            }
            for (int i = 0; i < columnSize; i++)
            {
                colNames_.Add(colDefsName.getString(i));
                colTypes_.Add((DATA_TYPE)colDefsTypeInt.getInt(i));
                colTypeString_.Add(colDefsTypeString.getString(i));
                if (compressTypes_ != null)
                {
                    AbstractVector.checkCompressedMethod(colTypes_[i], compressTypes_[i]);
                }
            }

            if (threadCount > 1)
            {
                if (isPartionedTable_)
                {
                    IEntity partitionSchema;
                    int     partitionType;
                    if (partColNames.isScalar())
                    {
                        if (partColNames.getString() != partitionCol)
                        {
                            throw new Exception(string.Format("The parameter partionCol must be the partitioning column \"{0}\" in the table. ", partitionCol));
                        }
                        partitionColumnIdx_ = ((BasicInt)schema.get("partitionColumnIndex")).getInt();
                        partitionSchema     = schema.get("partitionSchema");
                        partitionType       = ((BasicInt)schema.get("partitionType")).getInt();
                    }
                    else
                    {
                        int dims = ((BasicStringVector)partColNames).rows();
                        if (dims > 1 && partitionCol == "")
                        {
                            throw new Exception("The parameter partitionCol must be specified when threadCount is greater than 1.");
                        }
                        int index = -1;
                        for (int i = 0; i < dims; ++i)
                        {
                            if (((BasicStringVector)partColNames).getString(i) == partitionCol)
                            {
                                index = i;
                                break;
                            }
                        }
                        if (index < 0)
                        {
                            throw new Exception(string.Format("The parameter partionCol must be the partitioning column \"{0}\" in the table. ", partitionCol));
                        }
                        partitionColumnIdx_ = ((BasicIntVector)schema.get("partitionColumnIndex")).getInt(index);
                        partitionSchema     = ((BasicAnyVector)schema.get("partitionSchema")).get(index);
                        partitionType       = ((BasicIntVector)schema.get("partitionType")).getInt(index);
                    }
                    DATA_TYPE partitionColType = colTypes_[partitionColumnIdx_];
                    partitionDomain_ = DomainFactory.createDomain((PARTITION_TYPE)partitionType, partitionColType, partitionSchema);
                }
                else
                {//isPartionedTable_==false
                    if (partitionCol != "")
                    {
                        int threadcolindex = -1;
                        for (int i = 0; i < colNames_.Count; i++)
                        {
                            if (colNames_[i] == partitionCol)
                            {
                                threadcolindex = i;
                                break;
                            }
                        }
                        if (threadcolindex < 0)
                        {
                            throw new Exception(string.Format("No match found for {0}. ", partitionCol));
                        }
                        threadByColIndexForNonPartion_ = threadcolindex;
                    }
                }
            }

            // init done, start thread now.
            isExiting_ = false;
            threads_   = new List <WriterThread>(threadCount);
            for (int i = 0; i < threadCount; i++)
            {
                WriterThread writerThread = new WriterThread(this, pConn);
                if (i == 0)
                {
                    writerThread.conn_ = pConn;
                }
                else
                {
                    writerThread.conn_ = new DBConnection(useSSL_, false);
                    if (writerThread.conn_.connect(hostName_, port_, userId_, password_, "", enableHighAvailability, pHighAvailabilitySites) == false)
                    {
                        throw new Exception(string.Format("Failed to connect to server {0}:{1}. ", hostName, port));
                    }
                }
                threads_.Add(writerThread);
            }
        }