List <IVector> createListVector()
        {
            int cols = colTypes_.Count;
            BasicEntityFactory basicEntityFactory = (BasicEntityFactory)BasicEntityFactory.instance();
            List <IVector>     tmp = new List <IVector>();

            for (int i = 0; i < cols; ++i)
            {
                tmp.Add(basicEntityFactory.createVectorWithDefaultValue(colTypes_[i], 0));
            }
            return(tmp);
        }
        public ErrorCodeInfo insertUnwrittenData(List <List <IEntity> > data)
        {
            if (hasError_)
            {
                throw new Exception("Thread is exiting. ");
            }
            BasicEntityFactory basicEntityFactory = (BasicEntityFactory)BasicEntityFactory.instance();
            ErrorCodeInfo      errorCodeInfo      = new ErrorCodeInfo();
            IVector            partitionCol       = basicEntityFactory.createVectorWithDefaultValue(colTypes_[partitionColumnIdx_], data.Count);
            int rows = data.Count;

            for (int i = 0; i < rows; ++i)
            {
                partitionCol.set(i, (IScalar)data[i][partitionColumnIdx_]);
            }

            if (threads_.Count() > 1)
            {
                List <int>         threadindexs = new List <int>();
                BasicEntityFactory factory      = (BasicEntityFactory)BasicEntityFactory.instance();
                if (isPartionedTable_)
                {
                    threadindexs = partitionDomain_.getPartitionKeys(partitionCol);
                }
                else
                {
                    for (int i = 0; i < rows; ++i)
                    {
                        threadindexs.Add(partitionCol.get(i).GetHashCode());
                    }
                }
                for (int i = 0; i < rows; ++i)
                {
                    insertThreadWrite(threadindexs[i], data[i]);
                }
            }
            else
            {
                for (int i = 0; i < rows; ++i)
                {
                    insertThreadWrite(0, data[i]);
                }
            }
            return(new ErrorCodeInfo());
        }
        ErrorCodeInfo insertRecursive(List <IEntity> args)
        {
            ErrorCodeInfo errorCodeInfo = new ErrorCodeInfo();

            if (!suitableType(errorCodeInfo, args))
            {
                return(errorCodeInfo);
            }
            int threadindex;

            if (threads_.Count() > 1)
            {
                BasicEntityFactory factory = (BasicEntityFactory)BasicEntityFactory.instance();
                if (isPartionedTable_)
                {
                    {
                        try
                        {
                            threadindex = partitionDomain_.getPartitionKey((IScalar)args[partitionColumnIdx_]);
                        }
                        catch (Exception e)
                        {
                            return(new ErrorCodeInfo(ErrorCodeInfo.Code.EC_InvalidObject, e.Message));
                        }
                    }
                }
                else
                {
                    threadindex = args[threadByColIndexForNonPartion_].GetHashCode();
                }
            }
            else
            {
                threadindex = 0;
            }
            insertThreadWrite(threadindex, args);
            return(errorCodeInfo);
        }
示例#4
0
        public void run()
        {
            ConcurrentDictionary <string, SubscribeInfo> subscribeInfos = dispatcher_.getSubscribeInfos();
            Socket socket = this.socket_;

            try
            {
                if (bis_ == null)
                {
                    bis_ = new BufferedStream(new NetworkStream(socket));
                }
                ExtendedDataInput @in = null;

                while (!dispatcher_.isClose())
                {
                    if (@in == null)
                    {
                        bool isLittle = bis_.ReadByte() != 0;
                        if (isLittle)
                        {
                            @in = new LittleEndianDataInputStream(bis_);
                        }
                        else
                        {
                            @in = new BigEndianDataInputStream(bis_);
                        }
                    }
                    else
                    {
                        @in.readBoolean();
                    }

                    @in.readLong();
                    long msgid = @in.readLong();

                    topics = @in.readString();
                    short          flag     = @in.readShort();
                    IEntityFactory factory  = new BasicEntityFactory();
                    int            form     = flag >> 8;
                    int            type     = flag & 0xff;
                    bool           extended = type >= 128;
                    if (type >= 128)
                    {
                        type -= 128;
                    }

                    if (form < 0 || form > MAX_FORM_VALUE)
                    {
                        throw new IOException("Invalid form value: " + form);
                    }
                    if (type < 0 || type > MAX_TYPE_VALUE)
                    {
                        throw new IOException("Invalid type value: " + type);
                    }

                    DATA_FORM df = (DATA_FORM)form;
                    DATA_TYPE dt = (DATA_TYPE)type;

                    IEntity body;
                    try
                    {
                        body = factory.createEntity(df, dt, @in, extended);
                    }
                    catch
                    {
                        throw;
                    }
                    if (body.isTable())
                    {
                        foreach (string HATopic in topics.Split(','))
                        {
                            string topic = dispatcher_.getTopicForHATopic(HATopic);
                            if (topic == null)
                            {
                                throw new Exception("Subscription with topic " + HATopic + " does not exist. ");
                            }
                            if (!successTopics.Contains(topic))
                            {
                                SubscribeInfo subscribeInfo = null;
                                //Prevents a situation where streaming data arrives earlier than the subscription succeeds.
                                lock (subscribeInfos)
                                {
                                    if (!subscribeInfos.TryGetValue(topic, out subscribeInfo))
                                    {
                                        throw new Exception("Subscription with topic " + topic + " does not exist. ");
                                    }
                                }
                                lock (subscribeInfo)
                                {
                                    if (subscribeInfo.getConnectState() != ConnectState.RECEIVED_SCHEMA)
                                    {
                                        subscribeInfo.setConnectState(ConnectState.RECEIVED_SCHEMA);
                                    }
                                    else
                                    {
                                        throw new Exception("Subscription with topic " + topic + " already has a thread parsing the stream data. ");
                                    }
                                }
                                successTopics.Add(topic);
                            }
                        }
                    }
                    else if (body.isVector())
                    {
                        foreach (string HATopic in topics.Split(','))
                        {
                            string topic = dispatcher_.getTopicForHATopic(HATopic);
                            if (topic == null)
                            {
                                throw new Exception("Subscription with topic " + HATopic + " does not exist. ");
                            }
                            SubscribeInfo subscribeInfo = null;
                            if (!subscribeInfos.TryGetValue(topic, out subscribeInfo))
                            {
                                throw new Exception("Subscription with topic " + topic + " does not exist. ");
                            }
                            BasicAnyVector dTable = (BasicAnyVector)body;

                            int colSize = dTable.rows();
                            int rowSize = dTable.getEntity(0).rows();
                            if (rowSize == 1)
                            {
                                BasicMessage rec = new BasicMessage(msgid, topic, dTable);
                                dispatcher_.dispatch(rec);
                            }
                            else if (rowSize > 1)
                            {
                                List <IMessage> messages = new List <IMessage>(rowSize);
                                for (int i = 0; i < rowSize; i++)
                                {
                                    BasicAnyVector row = new BasicAnyVector(colSize);

                                    for (int j = 0; j < colSize; j++)
                                    {
                                        AbstractVector vector = (AbstractVector)dTable.getEntity(j);
                                        IEntity        entity = vector.get(i);
                                        row.setEntity(j, entity);
                                    }
                                    BasicMessage rec = new BasicMessage(msgid, topic, row);
                                    messages.Add(rec);
                                }
                                dispatcher_.batchDispatch(messages);
                            }
                            lock (subscribeInfo)
                            {
                                subscribeInfo.setMsgId(msgid);
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("message body has an invalid format. Vector or table is expected");
                    }
                }
            }
            catch (Exception e)
            {
                System.Console.Out.WriteLine(e.StackTrace);
                foreach (string topic in successTopics)
                {
                    SubscribeInfo subscribeInfo = null;
                    if (!subscribeInfos.TryGetValue(topic, out subscribeInfo))
                    {
                        System.Console.Out.WriteLine("Subscription with topic " + topic + " doesn't exist. ");
                    }
                    else
                    {
                        lock (subscribeInfo)
                        {
                            subscribeInfo.setConnectState(ConnectState.NO_CONNECT);
                        }
                    }
                }
            }
            finally
            {
                try
                {
                    socket.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    Console.Write(ex.StackTrace);
                }
            }
            Console.WriteLine("MessageParser thread stopped.");
        }