예제 #1
0
        public JObject LoadByKey(ESConnection conn, String key)
        {
            if (!IndexExists)
            {
                goto NOT_EXIST;
            }

            JObject        record = null;
            ESMainResponse resp;

            if (KeyFieldName == null)
            {
                goto NOT_EXIST;
            }
            ;
            if (KeyFieldName.Equals("_id", StringComparison.InvariantCultureIgnoreCase))
            {
                String url = UrlPart + "/" + HttpUtility.UrlEncode(key);
                resp = conn.Get(url);
                if (resp.StatusCode == HttpStatusCode.NotFound)
                {
                    goto NOT_EXIST;
                }
                resp.ThrowIfError();
                if (!resp.JObject.ReadBool("found"))
                {
                    goto NOT_EXIST;
                }
                record = resp.JObject;
            }
            else
            {
                var req = createMatchQueryRequest(KeyFieldName, key);
                resp = conn.Post(UrlPart + "/_search", req);
                if (resp.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    goto NOT_EXIST;
                }
                resp.ThrowIfError();
                JArray hits = (JArray)resp.JObject.SelectToken("hits.hits", false);
                if (hits == null || hits.Count == 0)
                {
                    goto NOT_EXIST;
                }

                record = (JObject)hits[0];
            }
            Logs.DebugLog.Log("Key={0}: Record={1}", key, record);
            return((JObject)record.SelectToken("_source", false));

NOT_EXIST:
            return(null);
        }
예제 #2
0
        public ExistState Exists(ESConnection conn, String key, DateTime?timeStamp = null)
        {
            JObject        record = null;
            ESMainResponse resp;
            bool           dateNeeded = DateFieldName != null && timeStamp != null;

            if (KeyFieldName == null)
            {
                goto NOT_EXIST;
            }
            ;
            if (KeyFieldName.Equals("_id", StringComparison.InvariantCultureIgnoreCase))
            {
                String url = UrlPart + "/" + HttpUtility.UrlEncode(key);
                if (dateNeeded)
                {
                    url += "?fields=" + HttpUtility.UrlEncode(DateFieldName);
                }
                resp = conn.Get(url);
                if (resp.StatusCode == HttpStatusCode.NotFound)
                {
                    goto NOT_EXIST;
                }
                resp.ThrowIfError();
                if (!resp.JObject.ReadBool("found"))
                {
                    goto NOT_EXIST;
                }
                if (!dateNeeded)
                {
                    return(ExistState.Exist);
                }
                record = resp.JObject;
            }
            else
            {
                JObject req = createMatchQueryRequest(KeyFieldName, key);
                req.AddArray("_source").Add(DateFieldName);
                resp = conn.Post(UrlPart + "/_search", req);
                if (resp.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    goto NOT_EXIST;
                }
                resp.ThrowIfError();
                JArray hits = (JArray)resp.JObject.SelectToken("hits.hits", false);
                if (hits == null || hits.Count == 0)
                {
                    goto NOT_EXIST;
                }

                if (!dateNeeded)
                {
                    return(ExistState.Exist);
                }
                record = (JObject)hits[0];
            }
            DateTime dt = record.ReadDate("_source." + DateFieldName, DateTime.MinValue);

            if (dt == DateTime.MinValue)
            {
                if (conn.Logger != null)
                {
                    conn.Logger.Log(_LogType.ltWarning, "Exists: Record without field [" + DateFieldName + "] returned.");
                }
                return(ExistState.Exist);
            }
            //Logs.DebugLog.Log("Record=" + record);
            //Logs.DebugLog.Log("RecDate={0}, Timestamp={1}, cmp={2}", date, timeStamp, Comparer<DateTime>.Default.Compare((DateTime)date, (DateTime)timeStamp));
            if (dt < timeStamp)
            {
                return(ExistState.ExistOlder);
            }
            if (dt > timeStamp)
            {
                return(ExistState.ExistNewer);
            }
            return(ExistState.ExistSame);

NOT_EXIST:
            return(ExistState.NotExist);
        }