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);
        }
Exemple #2
0
        public override void Add(PipelineContext ctx)
        {
            if ((ctx.ImportFlags & _ImportFlags.TraceValues) != 0)
            {
                ctx.DebugLog.Log("Add: accumulator.Count={0}", accumulator.Count);
            }
            if (accumulator.Count == 0)
            {
                return;
            }

            OptLogAdd();

            if (DocType.AutoTimestampFieldName != null)
            {
                accumulator[DocType.AutoTimestampFieldName] = DateTime.UtcNow;
            }

            if (cache == null)
            {
                if (asyncQ == null)
                {
                    Connection.Post(DocType.GetUrlForAdd(accumulator), accumulator, _recordSerializer).ThrowIfError();
                }
                else
                {
                    asyncQ.PushAndOptionalPop(new AsyncRequestElement(accumulator, asyncAdd));
                }
            }
            else
            {
                cache.Add(new ESBulkEntry(accumulator));
                if (cache.Count >= cacheSize)
                {
                    FlushCache();
                }
            }
            Clear();
        }
        public bool DeleteByKey(ESConnection conn, String key)
        {
            if (!IndexExists)
            {
                goto NOT_EXIST;
            }

            ESMainResponse resp;

            if (KeyFieldName == null)
            {
                goto NOT_EXIST;
            }
            ;
            Logs.DebugLog.Log("DeleteByKey ({0})", key);
            if (KeyFieldName.Equals("_id", StringComparison.InvariantCultureIgnoreCase))
            {
                return(deleteById(conn, key));
            }
            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;
            }

            String id = (String)((JObject)hits[0])["_id"];

            if (id == null)
            {
                goto NOT_EXIST;
            }

            return(deleteById(conn, id));

NOT_EXIST:
            return(false);
        }
        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);
        }