示例#1
0
            internal BulkWriting(StreamingClientDatabase <TKey, TValue> client)
            {
                if (client.m_writer != null)
                {
                    throw new Exception("Duplicate call to StartBulkWriting");
                }

                m_client          = client;
                m_client.m_writer = this;
                m_stream          = m_client.m_stream;
                m_encodingMode    = m_client.m_encodingMode;

                m_stream.Write((byte)ServerCommand.Write);
                m_encodingMode.ResetEncoder();
            }
        /// <summary>
        /// Accesses <see cref="StreamingClientDatabase{TKey,TValue}"/> for given <paramref name="databaseName"/>.
        /// </summary>
        /// <param name="databaseName">Name of database instance to access.</param>
        /// <param name="encodingMethod"></param>
        /// <returns><see cref="StreamingClientDatabase{TKey,TValue}"/> for given <paramref name="databaseName"/>.</returns>
        StreamingClientDatabase <TKey, TValue> GetDatabase <TKey, TValue>(string databaseName, EncodingDefinition encodingMethod = null)
            where TKey : SnapTypeBase <TKey>, new()
            where TValue : SnapTypeBase <TValue>, new()
        {
            DatabaseInfo dbInfo = m_databaseInfos[databaseName.ToUpper()];

            if ((object)encodingMethod == null)
            {
                encodingMethod = dbInfo.SupportedStreamingModes.First();
            }
            if (m_sortedTreeEngine != null)
            {
                throw new Exception("Can only connect to one database at a time. Please disconnect from database" + m_historianDatabaseString);
            }

            if (dbInfo.KeyType != typeof(TKey))
            {
                throw new InvalidCastException("Key types do not match");
            }
            if (dbInfo.ValueType != typeof(TValue))
            {
                throw new InvalidCastException("Value types do not match");
            }

            m_stream.Write((byte)ServerCommand.ConnectToDatabase);
            m_stream.Write(databaseName);
            m_stream.Write(new TKey().GenericTypeGuid);
            m_stream.Write(new TValue().GenericTypeGuid);
            m_stream.Flush();

            var command = (ServerResponse)m_stream.ReadUInt8();

            switch (command)
            {
            case ServerResponse.UnhandledException:
                string exception = m_stream.ReadString();
                throw new Exception("Server UnhandledExcetion: \n" + exception);

            case ServerResponse.DatabaseDoesNotExist:
                throw new Exception("Database does not exist on the server: " + databaseName);

            case ServerResponse.DatabaseKeyUnknown:
                throw new Exception("Database key does not match that passed to this function");

            case ServerResponse.DatabaseValueUnknown:
                throw new Exception("Database value does not match that passed to this function");

            case ServerResponse.SuccessfullyConnectedToDatabase:
                break;

            default:
                throw new Exception("Unknown server response: " + command.ToString());
            }

            var db = new StreamingClientDatabase <TKey, TValue>(m_stream, () => m_sortedTreeEngine = null, dbInfo);

            m_sortedTreeEngine        = db;
            m_historianDatabaseString = databaseName;

            db.SetEncodingMode(encodingMethod);

            return(db);
        }