Exemplo n.º 1
0
        private bool TestPutItem(int _TestStepNo, BDatabaseAttributeCondition _ConditionExpression = null)
        {
            //Test put item
            PrintAction?.Invoke("Step " + _TestStepNo + "->" + "TestPutItem->Log-> Testing PutItem...");
            bool bLocalFailure = !SelectedDBService.PutItem(
                TableName,
                Key, new BPrimitiveType("test_1"),
                new JObject(
                    new JProperty(Key, "test_1"),
                    new JProperty("Test_Attr_Key_1", "Test_Attr_Val_1"),
                    new JProperty("Test_Attr_Key_2", 2),
                    new JProperty("Test_Attr_Key_3", 3.0f),
                    new JProperty("Test_Attr_Key_4",
                                  new JArray(
                                      new JValue("Array_Elem_1"),
                                      new JValue(2),
                                      new JValue(3.0f)
                                      ))),
                out JObject Result_1,
                EBReturnItemBehaviour.ReturnAllOld,
                _ConditionExpression,
                (string Message) =>
            {
                Console.WriteLine("Step " + _TestStepNo + "->" + "TestPutItem->Error-> " + Message);
                bLocalFailure = true;
            });

            if (bLocalFailure)
            {
                PrintAction?.Invoke("Step " + _TestStepNo + "->" + "TestPutItem->Error-> PutItem failed.");
                return(false);
            }
            PrintAction?.Invoke("Step " + _TestStepNo + "->" + "TestPutItem->Log-> PutItem succeed. Returned: " + Result_1?.ToString());
            return(true);
        }
Exemplo n.º 2
0
        private bool TestUpdateItem(int _TestStepNo, BDatabaseAttributeCondition _ConditionExpression = null, bool bUpdateOnlyTwoAttributes = false)
        {
            JObject NewItem = null;

            if (bUpdateOnlyTwoAttributes)
            {
                NewItem = new JObject(
                    new JProperty(Key, "test_1"),
                    new JProperty("Test_Attr_Key_2", 11115.0f),
                    new JProperty("Test_Attr_Key_4",
                                  new JArray(
                                      new JValue(123123123.0f)
                                      )));
            }
            else
            {
                NewItem = new JObject(
                    new JProperty(Key, "test_1"),
                    new JProperty("Test_Attr_Key_1", "Test_Attr_Val_New_1"),
                    new JProperty("Test_Attr_Key_2", 22),
                    new JProperty("Test_Attr_Key_3", 33.0f),
                    new JProperty("Test_Attr_Key_4",
                                  new JArray(
                                      new JValue("Array_Elem_New_1"),
                                      new JValue(22),
                                      new JValue(33.0f)
                                      )));
            }

            //Test update item
            PrintAction?.Invoke("Step " + _TestStepNo + "->" + "TestUpdateItem->Log-> Testing UpdateItem...");
            bool bLocalFailure = !SelectedDBService.UpdateItem(
                TableName,
                Key, new BPrimitiveType("test_1"),
                NewItem,
                out JObject Result_1,
                EBReturnItemBehaviour.ReturnAllOld,
                _ConditionExpression,
                (string Message) =>
            {
                Console.WriteLine("Step " + _TestStepNo + "->" + "TestUpdateItem->Error-> " + Message);
                bLocalFailure = true;
            });

            if (bLocalFailure)
            {
                PrintAction?.Invoke("Step " + _TestStepNo + "->" + "TestUpdateItem->Error-> UpdateItem failed.");
                return(false);
            }
            PrintAction?.Invoke("Step " + _TestStepNo + "->" + "TestUpdateItem->Log-> UpdateItem succeed. Returned: " + Result_1?.ToString());
            return(true);
        }
Exemplo n.º 3
0
        /// <summary>
        ///
        /// <para>AddElementsToArrayItem</para>
        ///
        /// <para>Adds element to the array item</para>
        ///
        /// <para>Check <seealso cref="IBDatabaseServiceInterface.AddElementsToArrayItem"/> for detailed documentation</para>
        ///
        /// </summary>
        public bool AddElementsToArrayItem(string _Table, string _KeyName, BPrimitiveType _KeyValue, string _ElementName, BPrimitiveType[] _ElementValueEntries, out JObject _ReturnItem, EBReturnItemBehaviour _ReturnItemBehaviour, BDatabaseAttributeCondition _ConditionExpression, Action <string> _ErrorMessageAction)
        {
            _ReturnItem = null;

            if (_ElementValueEntries == null || _ElementValueEntries.Length == 0)
            {
                _ErrorMessageAction?.Invoke("BDatabaseServiceMongoDB->AddElementsToArrayItem: ElementValueEntries must contain values.");
                return(false);
            }
            var ExpectedType = _ElementValueEntries[0].Type;

            foreach (var _ElementValueEntry in _ElementValueEntries)
            {
                if (_ElementValueEntry.Type != ExpectedType)
                {
                    _ErrorMessageAction?.Invoke("BDatabaseServiceMongoDB->AddElementsToArrayItem: ElementValueEntries must contain elements with the same type.");
                    return(false);
                }
            }

            if (_KeyValue == null)
            {
                _ErrorMessageAction?.Invoke("BDatabaseServiceMongoDB->AddElementsToArrayItem: Key is null.");
                return(false);
            }

            var Table = GetTable(_Table);

            if (Table == null)
            {
                return(false);
            }

            var Filter = Builders <BsonDocument>
                         .Filter.Eq(_KeyName, _KeyValue.ToString());

            if (_ConditionExpression != null)
            {
                if (_ConditionExpression.AttributeConditionType == EBDatabaseAttributeConditionType.ArrayElementNotExist)
                {
                    Filter = Builders <BsonDocument> .Filter.And(Filter, (_ConditionExpression as BAttributeArrayElementNotExistConditionMongoDb).GetArrayElementFilter(_ElementName));
                }
                else
                {
                    Filter = Builders <BsonDocument> .Filter.And(Filter, (_ConditionExpression as BDatabaseAttributeConditionMongo).Filter);
                }
            }

            List <object> TempList = new List <object>();

            foreach (var Element in _ElementValueEntries)
            {
                switch (Element.Type)
                {
                case EBPrimitiveTypeEnum.String:
                    TempList.Add(Element.AsString);
                    break;

                case EBPrimitiveTypeEnum.Integer:
                    TempList.Add(Element.AsInteger);
                    break;

                case EBPrimitiveTypeEnum.Double:
                    TempList.Add(Element.AsDouble);
                    break;

                case EBPrimitiveTypeEnum.ByteArray:
                    TempList.Add(Element.AsByteArray);
                    break;
                }
            }

            UpdateDefinition <BsonDocument> Update = Builders <BsonDocument> .Update.PushEach(_ElementName, TempList);

            try
            {
                if (_ReturnItemBehaviour == EBReturnItemBehaviour.DoNotReturn)
                {
                    Table.UpdateOne(Filter, Update);
                    return(true);
                }
                else
                {
                    if (_ReturnItemBehaviour == EBReturnItemBehaviour.ReturnAllOld)
                    {
                        BsonDocument Document = Table.FindOneAndUpdate(Filter, Update, new FindOneAndUpdateOptions <BsonDocument, BsonDocument>()
                        {
                            ReturnDocument = ReturnDocument.Before
                        });
                        _ReturnItem = BsonToJObject(Document);
                        return(true);
                    }
                    else
                    {
                        BsonDocument Document = Table.FindOneAndUpdate(Filter, Update, new FindOneAndUpdateOptions <BsonDocument, BsonDocument>()
                        {
                            ReturnDocument = ReturnDocument.After
                        });
                        _ReturnItem = BsonToJObject(Document);
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                _ErrorMessageAction?.Invoke($"{ex.Message} : \n {ex.StackTrace}");
            }
            return(false);
        }
Exemplo n.º 4
0
        private BWebServiceResponse OnRequest_Internal(HttpListenerContext _Context, Action <string> _ErrorMessageAction)
        {
            if (_Context.Request.HttpMethod != "POST")
            {
                _ErrorMessageAction?.Invoke("StartProcessRequest: POST methods is accepted. But received request method:  " + _Context.Request.HttpMethod);
                return(BWebResponse.MethodNotAllowed("POST methods is accepted. But received request method: " + _Context.Request.HttpMethod));
            }

            var NewDBEntry = new FileConversionDBEntry()
            {
                ConversionStatus = (int)EInternalProcessStage.Queued
            };

            string NewConversionID_FromRelativeUrl_UrlEncoded = null;
            string BucketName       = null;
            string RelativeFileName = null;
            string ZipMainAssembly  = "";

            using (var InputStream = _Context.Request.InputStream)
            {
                var NewObjectJson = new JObject();

                using (var ResponseReader = new StreamReader(InputStream))
                {
                    try
                    {
                        var ParsedBody = JObject.Parse(ResponseReader.ReadToEnd());

                        if (!ParsedBody.ContainsKey("bucketName") ||
                            !ParsedBody.ContainsKey("rawFileRelativeUrl"))
                        {
                            return(BWebResponse.BadRequest("Request body must contain all necessary fields."));
                        }
                        var BucketNameToken         = ParsedBody["bucketName"];
                        var RawFileRelativeUrlToken = ParsedBody["rawFileRelativeUrl"];

                        if (BucketNameToken.Type != JTokenType.String ||
                            RawFileRelativeUrlToken.Type != JTokenType.String)
                        {
                            return(BWebResponse.BadRequest("Request body contains invalid fields."));
                        }

                        if (ParsedBody.ContainsKey("zipTypeMainAssemblyFileNameIfAny"))
                        {
                            var ZipMainAssemblyToken = ParsedBody["zipTypeMainAssemblyFileNameIfAny"];

                            if (ZipMainAssemblyToken.Type != JTokenType.String)
                            {
                                return(BWebResponse.BadRequest("Request body contains invalid fields."));
                            }

                            ZipMainAssembly = (string)ZipMainAssemblyToken;
                        }

                        NewDBEntry.BucketName = (string)BucketNameToken;
                        NewConversionID_FromRelativeUrl_UrlEncoded = WebUtility.UrlEncode((string)RawFileRelativeUrlToken);

                        BucketName       = (string)BucketNameToken;
                        RelativeFileName = (string)RawFileRelativeUrlToken;
                    }
                    catch (Exception e)
                    {
                        _ErrorMessageAction?.Invoke("Read request body stage has failed. Exception: " + e.Message + ", Trace: " + e.StackTrace);
                        return(BWebResponse.BadRequest("Malformed request body. Request must be a valid json form."));
                    }
                }
            }


            if (BucketName == null || RelativeFileName == null)
            {
                return(BWebResponse.InternalError("No BucketName or FileName"));
            }

            BDatabaseAttributeCondition UpdateCondition = DatabaseService.BuildAttributeNotExistCondition(FileConversionDBEntry.KEY_NAME_CONVERSION_ID);


            //If a process was completed (success or failure) then allow reprocessing
            //Only stop if a process is currently busy processing or already queued
            if (DatabaseService.GetItem(
                    FileConversionDBEntry.DBSERVICE_FILE_CONVERSIONS_TABLE(),
                    FileConversionDBEntry.KEY_NAME_CONVERSION_ID,
                    new BPrimitiveType(NewConversionID_FromRelativeUrl_UrlEncoded),
                    FileConversionDBEntry.Properties,
                    out JObject ConversionObject
                    ))
            {
                if (ConversionObject != null && ConversionObject.ContainsKey("conversionStatus"))
                {
                    EInternalProcessStage ExistingStatus = (EInternalProcessStage)(int)ConversionObject["conversionStatus"];

                    if (ExistingStatus == EInternalProcessStage.ProcessFailed || ExistingStatus == EInternalProcessStage.ProcessComplete)
                    {
                        UpdateCondition = null;
                    }
                }
            }

            if (!DatabaseService.UpdateItem(
                    FileConversionDBEntry.DBSERVICE_FILE_CONVERSIONS_TABLE(),
                    FileConversionDBEntry.KEY_NAME_CONVERSION_ID,
                    new BPrimitiveType(NewConversionID_FromRelativeUrl_UrlEncoded),
                    JObject.Parse(JsonConvert.SerializeObject(NewDBEntry)),
                    out JObject _ExistingObject, EBReturnItemBehaviour.DoNotReturn,
                    UpdateCondition,
                    _ErrorMessageAction))
            {
                return(BWebResponse.Conflict("File is already being processed/queued."));
            }

            try
            {
                if (BatchProcessingCreationService.Instance.StartBatchProcess(BucketName, RelativeFileName, ZipMainAssembly, out string _PodName, _ErrorMessageAction))
                {
                    //Code for initial method of starting optimizer after pixyz completes
                    //return BWebResponse.StatusAccepted("Request has been accepted; process is now being started.");
                    if (BatchProcessingCreationService.Instance.StartFileOptimizer(BucketName, RelativeFileName, _ErrorMessageAction))
                    {
                        return(BWebResponse.StatusAccepted("Request has been accepted; process is now being started."));
                    }
                    else
                    {
                        NewDBEntry.ConversionStatus = (int)EInternalProcessStage.ProcessFailed;

                        if (!DatabaseService.UpdateItem(
                                FileConversionDBEntry.DBSERVICE_FILE_CONVERSIONS_TABLE(),
                                FileConversionDBEntry.KEY_NAME_CONVERSION_ID,
                                new BPrimitiveType(NewConversionID_FromRelativeUrl_UrlEncoded),
                                JObject.Parse(JsonConvert.SerializeObject(NewDBEntry)),
                                out JObject _, EBReturnItemBehaviour.DoNotReturn,
                                null,
                                _ErrorMessageAction))
                        {
                            return(BWebResponse.InternalError("Failed to start the batch process and experienced a Database error"));
                        }

                        //Try kill pixyz pod that we have succeeded in creating
                        if (!BatchProcessingCreationService.Instance.TryKillPod(_PodName, "cip-batch"))
                        {
                            return(BWebResponse.InternalError("Failed to start the unreal optimizer and failed to kill pixyz pod"));
                        }

                        return(BWebResponse.InternalError("Failed to start the batch process and experienced a Database error"));
                    }
                }
                else
                {
                    NewDBEntry.ConversionStatus = (int)EInternalProcessStage.ProcessFailed;

                    if (!DatabaseService.UpdateItem(
                            FileConversionDBEntry.DBSERVICE_FILE_CONVERSIONS_TABLE(),
                            FileConversionDBEntry.KEY_NAME_CONVERSION_ID,
                            new BPrimitiveType(NewConversionID_FromRelativeUrl_UrlEncoded),
                            JObject.Parse(JsonConvert.SerializeObject(NewDBEntry)),
                            out JObject _, EBReturnItemBehaviour.DoNotReturn,
                            null,
                            _ErrorMessageAction))
                    {
                        return(BWebResponse.InternalError("Failed to start the batch process and experienced a Database error"));
                    }

                    return(BWebResponse.InternalError("Failed to start the batch process"));
                }
            }
Exemplo n.º 5
0
        /// <summary>
        ///
        /// <para>UpdateItem</para>
        ///
        /// <para>Updates an item in a table</para>
        ///
        /// <para>Check <seealso cref="IBDatabaseServiceInterface.UpdateItem"/> for detailed documentation</para>
        ///
        /// </summary>
        public bool UpdateItem(string _Table, string _KeyName, BPrimitiveType _KeyValue, JObject _UpdateItem, out JObject _ReturnItem, EBReturnItemBehaviour _ReturnItemBehaviour = EBReturnItemBehaviour.DoNotReturn, BDatabaseAttributeCondition _ConditionExpression = null, Action <string> _ErrorMessageAction = null)
        {
            _ReturnItem = null;

            var Table = GetTable(_Table);

            if (Table == null)
            {
                return(false);
            }

            try
            {
                var Filter = Builders <BsonDocument> .Filter.Eq(_KeyName, _KeyValue.ToString());

                if (_ConditionExpression != null)
                {
                    Filter = Builders <BsonDocument> .Filter.And(Filter, (_ConditionExpression as BDatabaseAttributeConditionMongo).Filter);
                }

                JObject NewObject = (JObject)_UpdateItem.DeepClone();
                AddKeyToJson(NewObject, _KeyName, _KeyValue);

                BsonDocument Document = new BsonDocument {
                    { "$set", JObjectToBson(NewObject) }
                };                                                                                 //use $set for preventing to get element name is not valid exception. more info https://stackoverflow.com/a/35441075

                if (_ReturnItemBehaviour == EBReturnItemBehaviour.DoNotReturn)
                {
                    Table.UpdateOne(Filter, Document, new UpdateOptions()
                    {
                        IsUpsert = true
                    });
                    return(true);
                }
                else
                {
                    BsonDocument OldDocument = Table.FindOneAndUpdate(Filter, Document);

                    if (Document != null)
                    {
                        _ReturnItem = JObject.Parse(Document.ToJson());
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                _ErrorMessageAction?.Invoke($"BDatabaseServiceMongoDB->UpdateItem: {ex.Message} : \n {ex.StackTrace}");
            }

            return(false);
        }