public static bool BulkLoad(Stream input, NetworkStream bufInputStream, NetworkStream outputStream) { Message msg = new Message(); int bytes_read = -1; try { while (bytes_read != 0) { byte[] buffer = new byte[BulkLoadPortionSize]; bytes_read = input.Read(buffer, 0, BulkLoadPortionSize); if (bytes_read != 0) { // crop the array to the correct size msg.Body = new byte[bytes_read + IntSize + StringTypeSize]; Array.Copy(buffer, 0, msg.Body, IntSize + StringTypeSize, bytes_read); msg.Instruction = InstructionCode.BulkLoadPortion; msg.Body[0] = 0; NetworkOperations.WriteInt(bytes_read, msg.Body, 0 + StringTypeSize); NetworkOperations.WriteMessage(msg, outputStream); } } NetworkOperations.WriteMessage(InstructionCode.BulkLoadEnd, outputStream, bufInputStream, InstructionCode.BulkLoadSucceeded, InstructionCode.UpdateSucceeded); return(true); } catch (IOException e) { msg.Instruction = InstructionCode.BulkLoadError; try { NetworkOperations.WriteMessage(msg, outputStream); NetworkOperations.ReadMessage(msg, bufInputStream); } catch (IOException ex) { throw new SednaException(InstructionCode.BulkLoadFailed, "Unable to bulk load: IO error: " + ex.Message); } throw new SednaException(InstructionCode.BulkLoadFailed, NetworkOperations.GetErrorInfo(msg.Instruction, msg.Body) + " : " + e.Message); } }
public static StringItem ReadStringItem(NetworkStream inputStream) { Message msg = new Message(); StringItem sitem = new StringItem(); StringBuilder strBuf = new StringBuilder(); NetworkOperations.ReadMessage(msg, inputStream); if ((msg.Instruction == InstructionCode.ItemEnd) || (msg.Instruction == InstructionCode.ResultEnd)) { sitem.HasNextItem = false; sitem.Item = null; return(sitem); } while ((msg.Instruction != InstructionCode.ItemEnd) && (msg.Instruction != InstructionCode.ResultEnd)) { if (msg.Instruction == InstructionCode.GeneralError) { throw NetworkOperations.GetErrorInfo(msg.Instruction, msg.Body); } if (msg.Instruction == InstructionCode.ItemPart) { strBuf.Append(Encoding.ASCII.GetString(msg.Body, (IntSize + StringTypeSize), msg.Body.Length - (IntSize + StringTypeSize))); } NetworkOperations.ReadMessage(msg, inputStream); } if (msg.Instruction == InstructionCode.ResultEnd) { sitem.HasNextItem = false; } else if (msg.Instruction == InstructionCode.ItemEnd) { sitem.HasNextItem = true; } sitem.Item = strBuf.ToString(); return(sitem); }
/// <summary> /// Executes a query that is read from a stream. /// </summary> public QueryResult Execute(TextReader queryReader) { try { if (!inTransaction) { BeginTransaction(); } string query = queryReader.ReadToEnd(); NetworkOperations.Message msg = new NetworkOperations.Message(); msg.Instruction = InstructionCode.Execute; MemoryStream stream = new MemoryStream(); BinaryWriter writer = new BinaryWriter(stream); writer.Write((byte)0); // xml is 0, scheme xml is 1 NetworkOperations.WriteString(writer, query); writer.Close(); msg.Body = stream.ToArray(); NetworkOperations.WriteMessage(msg, outputStream); NetworkOperations.ReadMessage(msg, inputStream); if (msg.Instruction == InstructionCode.QuerySucceeded) { return(new QueryResult(NetworkOperations.ReadStringItem(inputStream), inputStream, outputStream)); } else if (msg.Instruction == InstructionCode.QueryFailed || msg.Instruction == InstructionCode.UpdateFailed || msg.Instruction == InstructionCode.GeneralError) { throw NetworkOperations.GetErrorInfo(msg.Instruction, msg.Body); } else if (msg.Instruction == InstructionCode.UpdateSucceeded) { haveUpdated = true; return(null); } else if (msg.Instruction == InstructionCode.BulkLoadFileRequest) { try { haveUpdated = true; string filename = NetworkOperations.ReadString(msg, 0); Stream fileStream = File.OpenRead(filename); if (!NetworkOperations.BulkLoad(fileStream, inputStream, outputStream)) { throw new SednaException(InstructionCode.BulkLoadFailed, "Bulk load failed"); } return(null); } finally { stream.Close(); } } else if (msg.Instruction == InstructionCode.BulkLoadStreamRequest) { if (!NetworkOperations.BulkLoad(Console.OpenStandardInput(), inputStream, outputStream)) { throw new SednaException("Bulk load failed"); } return(null); } else { throw new SednaException("Unexpected error"); } } catch (Exception ex) { inTransaction = false; haveUpdated = false; throw ex; } }