Example #1
0
      public virtual void SetField(String fld, Object value, FieldFlags fieldFlags=FieldFlags.OverWrite, String sep=null)
      {
         //Logs.DebugLog.Log("SetField ({0}, {1})", fld, value);
         if (String.IsNullOrEmpty(fld))
         {
            if (value == null) return;
            accumulator = (JObject)value;
            goto EXIT_RTN; 
         }
         if ((flags & Bitmanager.ImportPipeline.Endpoint.DebugFlags._LogField) != 0) addLogger.Log("-- setfield {0}: '{1}'", fld, value);
         //if (value == null) addLogger.Log("Field {0}==null", fld);

         //Test for empty fields
         if ((fieldFlags & FieldFlags.SkipEmpty) != 0)
         {
            if (value == null) goto EXIT_RTN; ;
            String tmp = value as String;
            if (tmp != null && tmp.Length == 0) goto EXIT_RTN; ;
         }

         switch (fieldFlags & (FieldFlags.Append | FieldFlags.OverWrite | FieldFlags.ToArray | FieldFlags.KeepFirst | FieldFlags.KeepSmallest | FieldFlags.KeepLargest))
         {
            case 0:
            case FieldFlags.OverWrite:
               goto WRITE_TOKEN;

            case FieldFlags.KeepFirst:
               Object obj = accumulator[fld];
               if (obj != null) goto EXIT_RTN;
               goto WRITE_TOKEN;

            case FieldFlags.KeepSmallest:
               if (compareToken (accumulator[fld], value, fieldFlags) <= 0) goto EXIT_RTN;
               goto WRITE_TOKEN;

            case FieldFlags.KeepLargest:
               if (compareToken (accumulator[fld], value, fieldFlags) >= 0) goto EXIT_RTN;
               goto WRITE_TOKEN;

            case FieldFlags.Append:
               String existing = accumulator.ReadStr (fld, null);
               if (existing == null) goto WRITE_TOKEN;
               accumulator.WriteToken(fld, existing + sep + value);
               goto EXIT_RTN;

            default:
               JToken token = accumulator.SelectToken(fld, false);
               JArray arr = token as JArray;
               if (arr != null)
               {
                  if (0 != (fieldFlags & FieldFlags.Unique))
                      addUnique(arr, value, fieldFlags); 
                  else
                     arr.Add(value); 
                  goto EXIT_RTN;
               }
               arr = accumulator.AddArray (fld);
               if (token != null) arr.Add(token);
               arr.Add(value);
               return;
         }

         WRITE_TOKEN:
         accumulator.WriteToken(fld, value);
         EXIT_RTN:;
      }
Example #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);
        }