public ListDomain(IVector list, DATA_TYPE type, DATA_CATEGORY cat) { this.type = type; this.cat = cat; if (list.getDataType() != DATA_TYPE.DT_ANY) { throw new Exception("The input list must be a tuple."); } dict = new Dictionary <IScalar, int>(); BasicAnyVector values = (BasicAnyVector)list; int partitions = values.rows(); for (int i = 0; i < partitions; ++i) { IEntity cur = values.getEntity(i); if (cur.isScalar()) { dict.Add((IScalar)cur, i); } else { IVector vec = (IVector)cur; for (int j = 0; j < vec.rows(); ++j) { dict.Add(vec.get(j), i); } } } }
private void initialize(IList <long?> longs, 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 (isSorted(longs) == false) { throw new Exception("ranges " + longs.ToString() + " not sorted!"); } if (longs.Count != locations.rows() + 1) { throw new Exception("expect # locations == # range values - 1"); } ranges = longs.ToArray(); this.locations = new List <>(); 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); this.locations.Add(location.String); } } else { for (int i = 0; i < locations.rows(); ++i) { BasicStringVector locationVector = (BasicStringVector)locations.getEntity(i); this.locations.Add(new List <>()); for (int j = 0; j < locationVector.rows(); ++j) { this.locations[this.locations.Count - 1].Add(((BasicString)locationVector.get(j)).String); } } } }
public void Test_run_return_anyvector() { DBConnection db = new DBConnection(); db.connect(SERVER, PORT); BasicAnyVector v = (BasicAnyVector)db.run("[1 2 3,3.4 3.5 3.6]"); Assert.AreEqual(2, v.rows()); Assert.AreEqual(1, v.columns()); Assert.AreEqual(3.4, ((BasicDouble)((BasicDoubleVector)v.getEntity(1)).get(0)).getValue()); }
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)); } } } }
public void run() { Socket socket = this.socket; try { long offset = -1; if (bis == null) { bis = new BufferedStream(new NetworkStream(socket)); } ExtendedDataInput @in = null; while (true) { 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(); if (offset == -1) { offset = msgid; } topic = @in.readString(); short flag = @in.readShort(); IEntityFactory factory = new BasicEntityFactory(); int form = flag >> 8; int type = flag & 0xff; 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); } catch { throw; } if (body.isTable()) { if (body.rows() != 0) { throw new Exception("When message is table, it should be empty."); } } else if (body.isVector()) { dispatcher.setMsgId(topic, msgid); 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); msgid++; } dispatcher.batchDispatch(messages); } offset += rowSize; } else { throw new Exception("message body has an invalid format. Vector or table is expected"); } } } catch (Exception) { if (dispatcher.isClosed(topic)) { return; } else { dispatcher.tryReconnect(topic); } } finally { try { socket.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.Write(ex.StackTrace); } } }
public virtual void run() { Socket socket = this.socket; try { if (bis == null) { bis = new BufferedInputStream(socket.InputStream); } long offset = -1; ExtendedDataInput @in = null; //isRemoteLittleEndian ? new LittleEndianDataInputStream(bis) : new BigEndianDataInputStream(bis); while (true) { if (@in == null) { bool?b = bis.read() != 0; //true/false : little/big if (b == true) { @in = new LittleEndianDataInputStream(bis); } else { @in = new BigEndianDataInputStream(bis); } } else { @in.readBoolean(); } long msgid = @in.readLong(); if (offset == -1) { offset = msgid; } else { assert(offset == msgid); } string topic = @in.readString(); short flag = @in.readShort(); EntityFactory factory = new BasicEntityFactory(); int form = flag >> 8; int type = flag & 0xff; 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); } Entity_DATA_FORM df = Enum.GetValues(typeof(Entity_DATA_FORM))[form]; Entity_DATA_TYPE dt = Enum.GetValues(typeof(Entity_DATA_TYPE))[type]; Entity body; try { body = factory.createEntity(df, dt, @in); } catch (Exception exception) { throw exception; } if (body.Vector) { BasicAnyVector dTable = (BasicAnyVector)body; int colSize = dTable.rows(); int rowSize = dTable.getEntity(0).rows(); if (rowSize >= 1) { if (rowSize == 1) { BasicMessage rec = new BasicMessage(msgid, topic, dTable); dispatcher.dispatch(rec); } else { IList <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); Entity entity = vector.get(i); row.setEntity(j, entity); } BasicMessage rec = new BasicMessage(msgid, topic, row); messages.Add(rec); msgid++; } dispatcher.batchDispatch(messages); } } offset += rowSize; } else { throw new Exception("message body has an invalid format.vector is expected"); } } } catch (Exception e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } finally { try { socket.close(); } catch (IOException e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } } }
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."); }
private void initialize(AbstractVector values, BasicAnyVector locations) { if (values.DataType != Entity_DATA_TYPE.DT_ANY || values.DataForm != Entity_DATA_FORM.DF_VECTOR) { throw new Exception("expect a vector of partitioning lists"); } if (values.rows() <= 0) { throw new Exception("requires at least one partitioning list"); } 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 (values.rows() != locations.rows()) { throw new Exception("expect # locations == # partitioning lists"); } this.locationMap = new Dictionary <>(); IList <string>[] locationListArray = new IList[locations.rows()]; 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); locationListArray[i] = location.String; } } else { for (int i = 0; i < locations.rows(); ++i) { BasicStringVector locationVector = (BasicStringVector)locations.getEntity(i); locationListArray[i] = new List <>(); for (int j = 0; j < locationVector.rows(); ++j) { BasicString location = (BasicString)locationVector.get(j); locationListArray[i].Add(location.String); } } } for (int i = 0; i < values.rows(); ++i) { AbstractVector partitioningList = (AbstractVector)((BasicAnyVector)values).getEntity(i); if (partitioningList.rows() <= 0) { throw new Exception("expect partitioning list to be nonempty"); } if (partitioningList.DataCategory != Entity_DATA_CATEGORY.INTEGRAL) { throw new Exception("expect partitioning column values to be integral-typed."); } for (int j = 0; j < partitioningList.rows(); ++j) { try { long val = partitioningList.get(j).Number.longValue(); locationMap[val] = locationListArray[i]; } catch (Exception e) { throw new Exception(e); } } } }