Beispiel #1
0
        /// <summary>
        /// Returns a list of statuses
        /// </summary>
        /// <returns></returns>
        public List <string> Status()
        {
            SphinxBinaryWriter req_bw = new SphinxBinaryWriter(new MemoryStream(), Encoding);

            req_bw.WriteShort((short)SearchdCommand.SEARCHD_COMMAND_STATUS);
            req_bw.WriteShort((short)VerCommand.VER_COMMAND_STATUS);
            req_bw.WriteInt(4);
            req_bw.WriteInt(1);
            req_bw.Flush();

            using (SphinxConnection sc = GetConnection())
            {
                SphinxBinaryWriter client_bw = new SphinxBinaryWriter(sc.Stream, Encoding);
                client_bw.WriteStream(req_bw.Stream);

                SphinxBinaryReader res_br = new SphinxBinaryReader(ReadResponse(sc.Stream), Encoding);
                int           num_rows    = res_br.ReadInt();
                int           num_cols    = res_br.ReadInt();
                List <string> results     = new List <string>();
                for (int i = 0; i < num_rows; i++)
                {
                    for (int j = 0; j < num_cols; j++)
                    {
                        results.Add(res_br.ReadStr());
                    }
                }
                return(results);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Builds keywords.
        /// </summary>
        /// <param name="_query">The _query.</param>
        /// <param name="_index">The _index.</param>
        /// <param name="_hits">if set to <c>true</c> [_hits].</param>
        /// <returns></returns>
        public List <Keyword> BuildKeywords(string _query, string _index = "", bool _hits = true)
        {
            SphinxBinaryWriter req_bw = new SphinxBinaryWriter(new MemoryStream(), Encoding);

            req_bw.WriteShort((short)SearchdCommand.SEARCHD_COMMAND_KEYWORDS);
            req_bw.WriteShort((short)VerCommand.VER_COMMAND_KEYWORDS);
            req_bw.WriteInt(12 + _query.Length + _index.Length);
            req_bw.WriteStr(_query);
            req_bw.WriteStr(_index);
            req_bw.WriteInt(_hits ? 1 : 0);
            req_bw.Flush();

            using (SphinxConnection sc = GetConnection())
            {
                SphinxBinaryWriter client_bw = new SphinxBinaryWriter(sc.Stream, Encoding);
                client_bw.WriteStream(req_bw.Stream);

                SphinxBinaryReader res_br   = new SphinxBinaryReader(ReadResponse(sc.Stream), Encoding);
                int            num_keywords = res_br.ReadInt();
                List <Keyword> results      = new List <Keyword>();
                while (num_keywords-- > 0)
                {
                    Keyword kw = new Keyword();
                    kw.Tokenized  = res_br.ReadStr();
                    kw.Normalized = res_br.ReadStr();
                    if (_hits)
                    {
                        kw.NumDocs = res_br.ReadInt();
                        kw.NumHits = res_br.ReadInt();
                    }
                    results.Add(kw);
                }
                return(results);
            }
        }
Beispiel #3
0
 internal void ReadFrom(SphinxBinaryReader _br, IEnumerable <Attribute> _attributes)
 {
     Weight = _br.ReadInt();
     foreach (Attribute attr in _attributes)
     {
         AttributeValue attr_val = new AttributeValue(attr);
         attr_val.ReadFrom(_br);
         Attributes.Add(attr.Name, attr_val);
     }
 }
        public SphinxConnection(String _host, int _port)
        {
            tcpclient.Connect(_host, _port);
            SphinxBinaryWriter bw = new SphinxBinaryWriter(tcpclient.GetStream());

            bw.WriteInt(1); // dummy write (Nagle)
            SphinxBinaryReader br = new SphinxBinaryReader(tcpclient.GetStream());

            if (br.ReadInt() < 1)
            {
                throw new SphinxClientException("Server version < 1");
            }
        }
Beispiel #5
0
        /// <summary>
        /// Forces searchd to flush pending attribute updates to disk, and blocks until completion.
        /// </summary>
        /// <returns>Returns a non-negative internal "flush tag"</returns>
        public int FlushAttributes()
        {
            SphinxBinaryWriter req_bw = new SphinxBinaryWriter(new MemoryStream(), Encoding);

            req_bw.WriteShort((short)SearchdCommand.SEARCHD_COMMAND_FLUSHATTRS);
            req_bw.WriteShort((short)VerCommand.VER_COMMAND_FLUSHATTRS);
            req_bw.WriteInt(0);
            req_bw.Flush();

            using (SphinxConnection sc = GetConnection())
            {
                SphinxBinaryWriter client_bw = new SphinxBinaryWriter(sc.Stream, Encoding);
                client_bw.WriteStream(req_bw.Stream);

                SphinxBinaryReader res_br = new SphinxBinaryReader(ReadResponse(sc.Stream), Encoding);
                return(res_br.ReadInt());
            }
        }
Beispiel #6
0
        /// <summary>
        /// Reads the response from a _tcpclient
        /// This method can set LastError or LastWarning
        /// </summary>
        /// <param name="_tcpclient">A connected TcpClient.</param>
        /// <returns></returns>
        private Stream ReadResponse(NetworkStream _stream)
        {
            LastError   = "";
            LastWarning = "";
            Stream ms = null;

            // read response header (8 bytes)
            SphinxBinaryReader client_br  = new SphinxBinaryReader(_stream, Encoding);
            SphinxBinaryReader res_br     = new SphinxBinaryReader(new MemoryStream(client_br.ReadBytes(8)), Encoding);
            SeachdStatusCodes  res_status = (SeachdStatusCodes)res_br.ReadShort();

            res_br.ReadShort();
            int res_len = res_br.ReadInt();

            if ((res_len < 0) || (res_len > MAX_PACKET_LEN))
            {
                throw new SphinxClientException(string.Format("response length out of bounds (len={0})", res_len));
            }

            // read response (res_len)
            res_br = new SphinxBinaryReader(new MemoryStream(client_br.ReadBytes(res_len)), Encoding);
            switch (res_status)
            {
            case SeachdStatusCodes.SEARCHD_OK:
                ms = res_br.Stream;
                break;

            case SeachdStatusCodes.SEARCHD_ERROR:
            case SeachdStatusCodes.SEARCHD_RETRY:
                LastError = res_br.ReadStr();
                throw new SphinxClientException(LastError);

            case SeachdStatusCodes.SEARCHD_WARNING:
                LastWarning = res_br.ReadStr();
                ms          = res_br.Stream;
                break;

            default:
                throw new SphinxClientException(string.Format("unknown status (status={0})", res_status));
            }

            return(ms);
        }
Beispiel #7
0
        public int UpdateAttributes(string _index, List <Attribute> _attrs, Dictionary <long, List <AttributeValue> > _values)
        {
            SphinxBinaryWriter req_bw = new SphinxBinaryWriter(new MemoryStream(), Encoding);

            req_bw.WriteShort((short)SearchdCommand.SEARCHD_COMMAND_UPDATE);
            req_bw.WriteShort((short)VerCommand.VER_COMMAND_UPDATE);
            req_bw.WriteInt(0); // request length
            req_bw.WriteStr(_index);
            req_bw.WriteInt(_attrs.Count);
            foreach (Attribute a in _attrs)
            {
                req_bw.WriteStr(a.Name);
                req_bw.WriteInt(a.isMVA ? 1 : 0);
            }
            req_bw.WriteInt(_values.Count);
            foreach (KeyValuePair <long, List <AttributeValue> > kv in _values)
            {
                req_bw.WriteLong(kv.Key);
                foreach (AttributeValue av in kv.Value)
                {
                    av.WriteTo(req_bw);
                }
            }
            req_bw.Flush();

            req_bw.Seek(4, SeekOrigin.Begin);               // move to the request length position
            req_bw.WriteInt((int)req_bw.Stream.Length - 8); // request length - 8 (fixed header)
            req_bw.Flush();

            using (SphinxConnection sc = GetConnection())
            {
                SphinxBinaryWriter client_bw = new SphinxBinaryWriter(sc.Stream, Encoding);
                client_bw.WriteStream(req_bw.Stream);

                SphinxBinaryReader res_br = new SphinxBinaryReader(ReadResponse(sc.Stream), Encoding);
                return(res_br.ReadInt());
            }
        }
Beispiel #8
0
 internal void ReadFrom(SphinxBinaryReader _br)
 {
     Word = _br.ReadStr();
     Docs = _br.ReadInt();
     Hits = _br.ReadInt();
 }
Beispiel #9
0
        internal void ReadFrom(SphinxBinaryReader _br)
        {
            Status = (SeachdStatusCodes)_br.ReadInt();

            if (Status != SeachdStatusCodes.SEARCHD_OK)
            {
                if (Status == SeachdStatusCodes.SEARCHD_WARNING)
                {
                    Warning = _br.ReadStr();
                }
                else
                {
                    Error = _br.ReadStr();
                    return;
                }
            }

            int count = _br.ReadInt(); // num_fields

            while (count-- > 0)
            {
                FieldList.Add(_br.ReadStr());
            }

            count = _br.ReadInt(); // num_attr
            while (count-- > 0)
            {
                AttributeList.Add(new Attribute(_br.ReadStr(), (AttributeTypes)_br.ReadInt()));
            }

            count = _br.ReadInt(); // num_matches
            bool id64 = _br.ReadInt() == 1;

            while (count-- > 0)
            {
                Match m = new Match((id64) ? _br.ReadLong() : _br.ReadInt());
                m.ReadFrom(_br, Attributes);
                MatcheList.Add(m);
            }

            Total      = _br.ReadInt();
            TotalFound = _br.ReadInt();
            TimeMsec   = _br.ReadInt();

            count = _br.ReadInt(); // num_words
            while (count-- > 0)
            {
                WordInfo wi = new WordInfo();
                wi.ReadFrom(_br);
                WordList.Add(wi);
            }
        }