问:
REST .NET Core 2.2 ~ Why Get Exception "Multipart body length limit 16384 exceeded."?
I have a .NET Core 2.2 API with a POST for a large file upload via stream.

I'm getting the exception "Multipart body length limit 16384 exceeded."

The exception appears at the line...

```<code>section = await reader.ReadNextSectionAsync();
</code>```

...at the FileStreamHelper.cs (see below).

I use postman to try the upload. My file has 10 MB.
I get this exception when I choose Body/binary.
When I choose Body/form-data instead I get the exception "Unexpected end of Stream, the content may have already been read by another component.". But I did not read the file by a different component or before. I guess binary should be right. Right?

At my Startup.cs I define

```<code>        services.Configure<FormOptions>(options =>
        {
            options.MemoryBufferThreshold = Int32.MaxValue;
            options.ValueCountLimit = 10; //default 1024
            options.ValueLengthLimit = int.MaxValue; //not recommended value
            options.MultipartBodyLengthLimit = long.MaxValue; //not recommended value
        });
</code>```

I'm using also at the Startup.cs

```<code>            app.UseWhen(context => context.Request.Path.StartsWithSegments("/File"),
            appBuilder =>
            {
                appBuilder.Run(async context =>
                {
                    context.Features.Get<IHttpMaxRequestBodySizeFeature>().MaxRequestBodySize = (long)( 2 * System.Math.Pow( 1024, 3 )); // = 2GB
                    await Task.CompletedTask;
                });
            });
</code>```

At Program.cs I use

```<code>            .UseKestrel(options =>
            {
                options.Limits.MaxRequestBodySize = (long)(2 * System.Math.Pow(1024, 3)); // = 2 GB
            })
</code>```

At my Controller I use the Attribute  [DisableRequestSizeLimit].

What could be the reason for this exception?

How to solve this problem?

All code:
Controller

```<code>[Route("api/v1/dataPicker/fileUploadStream")]
public class FileStreamUploadController : ControllerBase
{
    [HttpPost]
    [DisableRequestSizeLimit]
    [DisableFormValueModelBinding]
    public async Task<IActionResult> Upload()
    {
        var streamer = new FileStreamingHelper();
        var paths = "c:\\temp\\";
        var tempName = Guid.NewGuid().ToString("N");
        using
            (
            var stream = System.IO.File.Create($"{paths}tempName")
            )
            await streamer.StreamFile(Request, stream);
        string from = $"{paths}tempName";
        string to = $"{paths}{streamer.FileNames.FirstOrDefault() ?? tempName}";
        System.IO.File.Move(from, to);
        return Ok($"Uploaded File {paths}{streamer.FileNames.FirstOrDefault() ?? tempName}");
    }
}
</code>```

FileStreamHelper

```<code>    public async Task<FormValueProvider> StreamFile(HttpRequest request, Stream targetStream)
    {
        if (!MultipartRequestHelper.IsMultipartContentType(request.ContentType))
        {
            throw new Exception($"Expected a multipart request, but got {request.ContentType}");
        }

        var formAccumulator = new KeyValueAccumulator();

        var boundary = MultipartRequestHelper.GetBoundary(
            MediaTypeHeaderValue.Parse(request.ContentType),
            _defaultFormOptions.MultipartBoundaryLengthLimit);
        var reader = new MultipartReader(boundary, request.Body);

        MultipartSection section;
        try
        {
            section = await reader.ReadNextSectionAsync();
        }
        catch (Exception ex)
        {
            throw;
        }
        while (section != null) ...}

    public static string GetBoundary(MediaTypeHeaderValue contentType, int lengthLimit)
    {
        var boundary = HeaderUtilities.RemoveQuotes(contentType.Boundary);
        if (StringSegment.IsNullOrEmpty(boundary))
        {
            throw new InvalidDataException("Missing content-type boundary.");
        }

        if (boundary.Length > lengthLimit)
        {
            throw new InvalidDataException(
                $"Multipart boundary length limit {lengthLimit} exceeded.");
        }

        return boundary.Value;
    }
</code>```

You need more code to see? Please tell me!

Here the postman Headers...

postman Body...

答:
Please try to retrieve the boundary this way by parsing the content type first:

```<code> var contentType = MediaTypeHeaderValue.Parse(context.Request.ContentType);
 var boundary = HeaderUtilities.RemoveQuotes(contentType .Boundary);

 var reader = new MultipartReader(boundary.Value, request.Body);

 //Rest of the code
</code>```

Also, please attach a screenshot of your postman request, especially the headers

Body:
I have a .NET Core 2.2 API with a POST for a large file upload via stream.

I'm getting the exception "Multipart body length limit 16384 exceeded."

The exception appears at the line...

section = await reader.ReadNextSectionAsync();

...at the FileStreamHelper.cs (see below).

I use postman to try the upload. My file has 10 MB.
I get this exception when I choose Body/binary.
When I choose Body/form-data instead I get the exception "Unexpected end of Stream, the content may have already been read by another component.". But I did not read the file by a different component or before. I guess binary should be right. Right?

At my Startup.cs I define

        services.Configure<FormOptions>(options =>
        {
            options.MemoryBufferThreshold = Int32.MaxValue;
            options.ValueCountLimit = 10; //default 1024
            options.ValueLengthLimit = int.MaxValue; //not recommended value
            options.MultipartBodyLengthLimit = long.MaxValue; //not recommended value
        });

I'm using also at the Startup.cs

            app.UseWhen(context => context.Request.Path.StartsWithSegments("/File"),
            appBuilder =>
            {
                appBuilder.Run(async context =>
                {
                    context.Features.Get<IHttpMaxRequestBodySizeFeature>().MaxRequestBodySize = (long)( 2 * System.Math.Pow( 1024, 3 )); // = 2GB
                    await Task.CompletedTask;
                });
            });

At Program.cs I use

            .UseKestrel(options =>
            {
                options.Limits.MaxRequestBodySize = (long)(2 * System.Math.Pow(1024, 3)); // = 2 GB
            })

At my Controller I use the Attribute  [DisableRequestSizeLimit].

What could be the reason for this exception?

How to solve this problem?

All code:
Controller

[Route("api/v1/dataPicker/fileUploadStream")]
public class FileStreamUploadController : ControllerBase
{
    [HttpPost]
    [DisableRequestSizeLimit]
    [DisableFormValueModelBinding]
    public async Task<IActionResult> Upload()
    {
        var streamer = new FileStreamingHelper();
        var paths = "c:\\temp\\";
        var tempName = Guid.NewGuid().ToString("N");
        using
            (
            var stream = System.IO.File.Create($"{paths}tempName")
            )
            await streamer.StreamFile(Request, stream);
        string from = $"{paths}tempName";
        string to = $"{paths}{streamer.FileNames.FirstOrDefault() ?? tempName}";
        System.IO.File.Move(from, to);
        return Ok($"Uploaded File {paths}{streamer.FileNames.FirstOrDefault() ?? tempName}");
    }
}

FileStreamHelper

    public async Task<FormValueProvider> StreamFile(HttpRequest request, Stream targetStream)
    {
        if (!MultipartRequestHelper.IsMultipartContentType(request.ContentType))
        {
            throw new Exception($"Expected a multipart request, but got {request.ContentType}");
        }

        var formAccumulator = new KeyValueAccumulator();

        var boundary = MultipartRequestHelper.GetBoundary(
            MediaTypeHeaderValue.Parse(request.ContentType),
            _defaultFormOptions.MultipartBoundaryLengthLimit);
        var reader = new MultipartReader(boundary, request.Body);

        MultipartSection section;
        try
        {
            section = await reader.ReadNextSectionAsync();
        }
        catch (Exception ex)
        {
            throw;
        }
        while (section != null) ...}

    public static string GetBoundary(MediaTypeHeaderValue contentType, int lengthLimit)
    {
        var boundary = HeaderUtilities.RemoveQuotes(contentType.Boundary);
        if (StringSegment.IsNullOrEmpty(boundary))
        {
            throw new InvalidDataException("Missing content-type boundary.");
        }

        if (boundary.Length > lengthLimit)
        {
            throw new InvalidDataException(
                $"Multipart boundary length limit {lengthLimit} exceeded.");
        }

        return boundary.Value;
    }

You need more code to see? Please tell me!

Here the postman Headers...

postman Body...

Body:
Please try to retrieve the boundary this way by parsing the content type first:

 var contentType = MediaTypeHeaderValue.Parse(context.Request.ContentType);
 var boundary = HeaderUtilities.RemoveQuotes(contentType .Boundary);

 var reader = new MultipartReader(boundary.Value, request.Body);

 //Rest of the code

Also, please attach a screenshot of your postman request, especially the headers


来源: stackexchange

问:
How can i reload the data in jquery data table on button click?
Onbutton click I trigger the ajax.reload method which is given below:

and I get the attached error.

```<code>$("#btnGenerate").on("click", function (event) {

            $('#tblDataTable').DataTable().ajax.reload({ url: "/Home/GetStatus", type: "GET", datatype: "json" })
        });
</code>```

答:
The documentation for ajax.reload() says it expects a function and a bool.  Just as the error states, this is indeed not a function:

```<code>{ url: "/Home/GetStatus", type: "GET", datatype: "json" }
</code>```

The options for how to get the data from AJAX are in the initialization of the DataTables plugin.  All you need to do here is reload it:

```<code>$('#tblDataTable').DataTable().ajax.reload();
</code>```

Ideally when you initialize the plugin you store the table in a variable:

```<code>let myTable = $('#tblDataTable').DataTable({
    // your initialization options
});
</code>```

Then you can use the variable to easily reload:

```<code>myTable.ajax.reload();
</code>```

That way you don't have to invoke the jQuery selector again, invoke the plugin again, etc.

ContentLicense: CC BY-SA 4.0

Body:
Onbutton click I trigger the ajax.reload method which is given below:

and I get the attached error.

$("#btnGenerate").on("click", function (event) {

            $('#tblDataTable').DataTable().ajax.reload({ url: "/Home/GetStatus", type: "GET", datatype: "json" })
        });

Body:
The documentation for ajax.reload() says it expects a function and a bool.  Just as the error states, this is indeed not a function:

{ url: "/Home/GetStatus", type: "GET", datatype: "json" }

The options for how to get the data from AJAX are in the initialization of the DataTables plugin.  All you need to do here is reload it:

$('#tblDataTable').DataTable().ajax.reload();

Ideally when you initialize the plugin you store the table in a variable:

let myTable = $('#tblDataTable').DataTable({
    // your initialization options
});

Then you can use the variable to easily reload:

myTable.ajax.reload();

That way you don't have to invoke the jQuery selector again, invoke the plugin again, etc.


问:
How to create one "unit" test per query to display the result in the Test Explorer
Right now my job is to test all the queries stored in a database, and display the result in the Test Explorer, I've managed to test every query (I just run it, check for errors and store a custom error message for each error in a text file so it work for now but the output is not as I want it to be).

DISCLAMER

I guess unit testing may not be the smartest way to test the queries but that is the way i was asked to do it so I'm giving it a try.

The thing is that I test all queries within one [TestMethod] and the result look like this : (only one test method in the Test Explorer)

But I would want to know if it is possible to have multiple [TestMethod], so we can see which methods work and which don't within the Test Explorer.

My researches led me to this code :

```<code>[TestMethod]
[DataSource("System.Data.SqlClient",
            @"Data Source=****;Initial Catalog=****;Persist Security Info=True;User ID=sa;Password=****;Pooling=False",
            "Query",
            DataAccessMethod.Sequential)]    // My DataSource works
public void MyTestMethod()
{
        var q = new QueryDto
        {
            // Create my query with the Id, Name, Query, Parameters...
        };

        ExecTest(q);
}

public void ExecTest(QueryDto query)
{
       // my testing here
}
</code>```

I've tried adding [TestMethod] to the ExecTest() method but it did not helped very much with what I'm trying to achieve.

So please tell me if what I'm looking for is possible, and if it is, how to do it.

Feel free to ask questions \ edit I'll stay tuned so you don't have to wait if you try to help me.

EDIT

I'm posting another question because this one have some mistakes
Thanks for the help btw ^^.

答:
For now i'll stick to the answer i got here create test programmatically

my code :

```<code>[TestMethod]
[DataSource("System.Data.SqlClient",
            @"Data Source=****;Initial Catalog=****;Persist Security Info=True;User ID=sa;Password=****;Pooling=False",
            "Query",
            DataAccessMethod.Sequential)]
public void MyTestMethod()
{
           // my testing here using TestContext.DataRow["columnName"] to get the Name, id, Parameters...
}
</code>```

This displays the tests like shown :

In the top part of the Test Explorer it is still displayed as a single test but at least all the lines are detailled in the lower part and i can display a custom message to give some info about which query failed so it's fine.

Body:
Right now my job is to test all the queries stored in a database, and display the result in the Test Explorer, I've managed to test every query (I just run it, check for errors and store a custom error message for each error in a text file so it work for now but the output is not as I want it to be).

DISCLAMER

I guess unit testing may not be the smartest way to test the queries but that is the way i was asked to do it so I'm giving it a try.

The thing is that I test all queries within one [TestMethod] and the result look like this : (only one test method in the Test Explorer)

But I would want to know if it is possible to have multiple [TestMethod], so we can see which methods work and which don't within the Test Explorer.

My researches led me to this code :

[TestMethod]
[DataSource("System.Data.SqlClient",
            @"Data Source=****;Initial Catalog=****;Persist Security Info=True;User ID=sa;Password=****;Pooling=False",
            "Query",
            DataAccessMethod.Sequential)]    // My DataSource works
public void MyTestMethod()
{
        var q = new QueryDto
        {
            // Create my query with the Id, Name, Query, Parameters...
        };

        ExecTest(q);
}

public void ExecTest(QueryDto query)
{
       // my testing here
}

I've tried adding [TestMethod] to the ExecTest() method but it did not helped very much with what I'm trying to achieve.

So please tell me if what I'm looking for is possible, and if it is, how to do it.

Feel free to ask questions \ edit I'll stay tuned so you don't have to wait if you try to help me.

EDIT

I'm posting another question because this one have some mistakes
Thanks for the help btw ^^.

Body:
For now i'll stick to the answer i got here create test programmatically

my code :

[TestMethod]
[DataSource("System.Data.SqlClient",
            @"Data Source=****;Initial Catalog=****;Persist Security Info=True;User ID=sa;Password=****;Pooling=False",
            "Query",
            DataAccessMethod.Sequential)]
public void MyTestMethod()
{
           // my testing here using TestContext.DataRow["columnName"] to get the Name, id, Parameters...
}

This displays the tests like shown :

In the top part of the Test Explorer it is still displayed as a single test but at least all the lines are detailled in the lower part and i can display a custom message to give some info about which query failed so it's fine.


问:
How to configure next.config.js file on right way
In my next.config.js I have the following configuration:

```<code>const withCSS = require('@zeit/next-css');
const withSass = require('@zeit/next-sass');
const withImages = require('next-images');
const withFonts = require('next-fonts');

module.exports = withCSS(
  withSass(
    withFonts(),  // <=== this is wrong?
    withImages({
      distDir: '../_next',
      webpack(config) {
        return config;
      }
    })
  )
);
</code>```

Last thing which I need to add here is next-fonts plugin.

I am not sure how to export withFounts in the right way.

I need next-fonts plugin to be able to use 'fonts' icons.

答:
You can do this:

```<code>module.exports = withCSS(withFonts(withSass(
    withImages({
        distDir: '../_next',
        webpack(config) {
            return config;
        }
    })
)))
</code>```

You can try to use next-compose

Body:
In my next.config.js I have the following configuration:

const withCSS = require('@zeit/next-css');
const withSass = require('@zeit/next-sass');
const withImages = require('next-images');
const withFonts = require('next-fonts');

module.exports = withCSS(
  withSass(
    withFonts(),  // <=== this is wrong?
    withImages({
      distDir: '../_next',
      webpack(config) {
        return config;
      }
    })
  )
);

Last thing which I need to add here is next-fonts plugin.

I am not sure how to export withFounts in the right way.

I need next-fonts plugin to be able to use 'fonts' icons.

Body:
You can do this:

module.exports = withCSS(withFonts(withSass(
    withImages({
        distDir: '../_next',
        webpack(config) {
            return config;
        }
    })
)))

You can try to use next-compose


问:
Components do not render through <router-view /> in Laravel 5.8 with Vuejs
I'm trying to create an SPA using Vuejs and Laravel. I've installed vue-router to accomplish that. The component that has to be rendered inside the

```<code><router-view />
</code>```

tag is not rendering.

I've tried creating a new project and installing npm and vue-router again.
It still does not work.

Laravel Version : 5.8.18
vue-router : 3.0.6

welcome.blade.php

```<code><!DOCTYPE html>

<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Laravel</title>
    </head>
    <body>
        <div id = "app">
            <router-view></router-view>
        </div>

        <script src = "js/app.js"></script>
    </body>
</html>
</code>```

app.js(./resources/js/app.js)

```<code>import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes'

Vue.use(VueRouter)

 const app = new Vue({
    el: '#app',
    router: new VueRouter(routes)
 });
</code>```

routes.js(./resources/js/routes.js)

```<code>import Home from './components/Home'

export default {
    mode: 'history',
    routes: [
    {
        path: '/',
        component: Home
    }
   ]
 }
</code>```

Home.vue(./resources.components/Home.vue)

```<code><template>
    <p>Home</p>
</template>

<script>
    export default {}
</script>
</code>```

web.php

```<code><?php

Route::get('/{any?}', function () {
    return view('welcome');
});
</code>```

答:
Your code works for me. Remember to use </p>

```<code>npm run watch
</code>```

Body:
I'm trying to create an SPA using Vuejs and Laravel. I've installed vue-router to accomplish that. The component that has to be rendered inside the

<router-view />

tag is not rendering.

I've tried creating a new project and installing npm and vue-router again.
It still does not work.

Laravel Version : 5.8.18
vue-router : 3.0.6

welcome.blade.php

<!DOCTYPE html>

        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Laravel</title>

            <router-view></router-view>

app.js(./resources/js/app.js)

import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes'

Vue.use(VueRouter)

 const app = new Vue({
    el: '#app',
    router: new VueRouter(routes)
 });

routes.js(./resources/js/routes.js)

import Home from './components/Home'

export default {
    mode: 'history',
    routes: [
    {
        path: '/',
        component: Home
    }
   ]
 }

Home.vue(./resources.components/Home.vue)

<template>

Home

</template>

web.php

<?php

Route::get('/{any?}', function () {
    return view('welcome');
});

Body:
Your code works for me. Remember to use

npm run watch


问:
Appending the contents of a UNIX command to a div tag
I'm making a UNIX web-based terminal for learning purposes. So far I have made a text box and the output is being displayed. Sort of like this.

```<code><?php
$output = shell_exec('ls');
echo "```$output```";
?>
</code>```

Form

```<code><html>
<head>
<link href="/css/webterminal.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function shell_execute(str)
{
if (str.length==0)
  {
  document.getElementById("txtOut").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtOut").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","exec.php?q="+str,true);
xmlhttp.send();
}
</script>
</head
<body onLoad="setUser();">
    <div class="container">

    <h2>UNIX Web Based Terminal 1.0</h2>
        <br />
    <p><b>output</b></p>
<form>
<span id="User"></span>< <input type="text" class="textbox" onkeyup="shell_execute(this.value)" size="20" />
</form>
<div class="output">
<p><span id="txtOut"></span></p>

    </div>
</div>
</body>
</html>
</code>```

But I want it to look as if the page was really a terminal. When I type a command, it should store the result of the shell command, and then append it to a div tag. So as I am typing in commands, it will keep on showing the output. Just like in the UNIX terminal.

How can I append the output of the commands to a div tag?

答:
change:

```<code>document.getElementById("txtOut").innerHTML=xmlhttp.responseText;
</code>```

to:

```<code>document.getElementById("txtOut").innerHTML += xmlhttp.responseText;
</code>```

On a sidenote, why are you not using any of the well established javascript frameworks?

With jQuery for example you could reduce your code to maybe 4 lines.

Edit - using jQuery:
http://api.jquery.com/jQuery.ajax/

```<code><html>
    <head>
        <link href="/css/webterminal.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.5.2.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $('#cmd').bind('keydown', function (evt) {
                    if (evt.keyCode === 13) { // enter key
                        var cmdStr = $(this).val();

                        $.ajax({
                            url: 'exec.php',
                            dataType: 'text',
                            data: {
                                q: cmdStr
                            },
                            success: function (response) {
                                $('#txtOut').append(response);
                            }
                        });
                    }
                });
            });
        </script>
    </head>

    <body>
        <div class="container">
            <h2>UNIX Web Based Terminal 1.0</h2>
            <br />
            <p><b>output</b></p>

            <span id="User"></span>
            <input id="cmd" type="text" class="textbox" size="20" />

            <div class="output">
                <p><span id="txtOut"></span></p>
            </div>
        </div>
    </body>
</html>
</code>```

ContentLicense: CC BY-SA 3.0

Body:
I'm making a UNIX web-based terminal for learning purposes. So far I have made a text box and the output is being displayed. Sort of like this.

<?php
$output = shell_exec('ls');
echo "
$output
";
?>

Form

<link href="/css/webterminal.css" rel="stylesheet" type="text/css" />

</head

UNIX Web Based Terminal 1.0

output

<form>
< <input type="text" class="textbox" onkeyup="shell_execute(this.value)" size="20" />
</form>

But I want it to look as if the page was really a terminal. When I type a command, it should store the result of the shell command, and then append it to a div tag. So as I am typing in commands, it will keep on showing the output. Just like in the UNIX terminal.

How can I append the output of the commands to a div tag?

Body:
change:

document.getElementById("txtOut").innerHTML=xmlhttp.responseText;

to:

document.getElementById("txtOut").innerHTML += xmlhttp.responseText;

On a sidenote, why are you not using any of the well established javascript frameworks?

With jQuery for example you could reduce your code to maybe 4 lines.

Edit - using jQuery:
http://api.jquery.com/jQuery.ajax/

<html>
    <head>
        <link href="/css/webterminal.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.5.2.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $('#cmd').bind('keydown', function (evt) {
                    if (evt.keyCode === 13) { // enter key
                        var cmdStr = $(this).val();

                        $.ajax({
                            url: 'exec.php',
                            dataType: 'text',
                            data: {
                                q: cmdStr
                            },
                            success: function (response) {
                                $('#txtOut').append(response);
                            }
                        });
                    }
                });
            });
        </script>
    </head>

    <body>
        <div class="container">
            <h2>UNIX Web Based Terminal 1.0</h2>
            <br />
            <p><b>output</b></p>

            <span id="User"></span>
            <input id="cmd" type="text" class="textbox" size="20" />

            <div class="output">
                <p><span id="txtOut"></span></p>
            </div>
        </div>
    </body>
</html>


问:
A lot of libraries lead to Cause: java.lang.OutOfMemoryError
I've had a lot of applications left alone and given up because they got a lot of libraries and classes so eventually I got this memory error. Recently I started using JProfiler. But still didn't help not to cause it the app got a little big and now again this happend. Check out my setting:

in gradle.properties

```<code>org.gradle.jvmargs=-Xmx16g -XX:MaxPermSize=16g -XX:+HeapDumpOnOutOfMemoryError
GRADLE_OPTS=-Xmx16g
</code>```

in gradle(app)

```<code>dexOptions { javaMaxHeapSize "16g" }
</code>```

it sounds like these values don't make no difference give them 1G or 100G.T his is not important then what is?T here's a committed size in JProfiler it's only 230 MB. Before even running the App 80MB is used already! Check out the pictures:

Idle:

The memory Error:

This committed size is where the Out-Of-Memory error happens. As you see in the pictures. This happens when I press the run button and it doesn't run it gives the memory error but in the mean time in JProfiler the used size gets close to committed size and when it's close enough boom the error comes.
How to increase this committed size then?It's not my device left memory that is around 1.55 GB now. Then what is it? I'm not supposed to create a hello world application. I wrote totally clean code and I'm not causing this the app worked perfectly nothing's slow nothing gets overflowed. But when I add more libraries not even using them yet! I get memory error. Here's the heap walker:

looks pretty normal to me.char and String are at the top what do I do?Please help me. Thank you!

答:
I don't know about gradle specifics, but the standard switch for setting the MaxHeapSize is -Xmx; looking here may help: Run application via gradlew with -Xmx and -Xms.

If you are talking about running the app (as opposed to "building it") you could try to build the final JAR first and run it as a standalone application (via java -Xmx... -jar).

Do you get any specific OOM error?

Btw. based on the -XX:MaxPermSize you posted it seems you're using a really old pre-Java 8 version (or you don't set JVM args properly). You should consider an upgrade.

Body:
I've had a lot of applications left alone and given up because they got a lot of libraries and classes so eventually I got this memory error. Recently I started using JProfiler. But still didn't help not to cause it the app got a little big and now again this happend. Check out my setting:

in gradle.properties

org.gradle.jvmargs=-Xmx16g -XX:MaxPermSize=16g -XX:+HeapDumpOnOutOfMemoryError
GRADLE_OPTS=-Xmx16g

in gradle(app)

dexOptions { javaMaxHeapSize "16g" }

it sounds like these values don't make no difference give them 1G or 100G.T his is not important then what is?T here's a committed size in JProfiler it's only 230 MB. Before even running the App 80MB is used already! Check out the pictures:

Idle:

The memory Error:

This committed size is where the Out-Of-Memory error happens. As you see in the pictures. This happens when I press the run button and it doesn't run it gives the memory error but in the mean time in JProfiler the used size gets close to committed size and when it's close enough boom the error comes.
How to increase this committed size then?It's not my device left memory that is around 1.55 GB now. Then what is it? I'm not supposed to create a hello world application. I wrote totally clean code and I'm not causing this the app worked perfectly nothing's slow nothing gets overflowed. But when I add more libraries not even using them yet! I get memory error. Here's the heap walker:

looks pretty normal to me.char and String are at the top what do I do?Please help me. Thank you!


问:
Get compile time in react-native?
Is there a way to get the compile time (time at which i compiled the app, e.g. in utc or local). The reason I want this is that I often am not sure what version my emulator is running right now and adding a text widget at the top with the compile time would give me this security while developing.</p>

答:
One way I would do that :

make a shell script that runs before build, and gets current system time
(for iOS, this can be done adding a script phase before your build phases, on android, you need to change your gradle, see : execute task before android gradle build?)

Inject that time into a .env file, then use react-native-config to recover this 'buildTime' variable and display it.

There must be other ways to do that, but no ready-to-use tool to my knownledge, you will have to dive into Platforms specific build scripts ...

Title: Get compile time in react-native?


问:
how to get return response from AWS Lambda function
I have a simple lambda function that returns a dict response and another lambda function invokes that function and prints the response.

lambda function A

```<code>def handler(event,context):
    params = event['list']
    return {"params" : params + ["abc"]}
</code>```

lambda function B invoking A

```<code>a=[1,2,3]
x = {"list" : a}
invoke_response = lambda_client.invoke(FunctionName="monitor-workspaces-status",
                                       InvocationType='Event',
                                       Payload=json.dumps(x))
print (invoke_response)
</code>```

invoke_response

```<code>{u'Payload': <botocore.response.StreamingBody object at 0x7f47c58a1e90>, 'ResponseMetadata': {'HTTPStatusCode': 202, 'RequestId': '9a6a6820-0841-11e6-ba22-ad11a929daea'}, u'StatusCode': 202}
</code>```

Why is the response status 202? Also, how can I get the response data from invoke_response? I could not find a clear documentation of how to do it.

答:
try: data = invoke_response['Payload'].read()
read() because it is a StreamingBody object

```<code><botocore.response.StreamingBody object at 0x110b91c50>
</code>```

It is in the boto3 docs. You can find more details about this here: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/resources.html#actions

Body:
I have a simple lambda function that returns a dict response and another lambda function invokes that function and prints the response.

lambda function A

def handler(event,context):
    params = event['list']
    return {"params" : params + ["abc"]}

lambda function B invoking A

a=[1,2,3]
x = {"list" : a}
invoke_response = lambda_client.invoke(FunctionName="monitor-workspaces-status",
                                       InvocationType='Event',
                                       Payload=json.dumps(x))
print (invoke_response)

invoke_response

{u'Payload': <botocore.response.StreamingBody object at 0x7f47c58a1e90>, 'ResponseMetadata': {'HTTPStatusCode': 202, 'RequestId': '9a6a6820-0841-11e6-ba22-ad11a929daea'}, u'StatusCode': 202}

Why is the response status 202? Also, how can I get the response data from invoke_response? I could not find a clear documentation of how to do it.

Body:
try: data = invoke_response['Payload'].read()
read() because it is a StreamingBody object

<botocore.response.StreamingBody object at 0x110b91c50>

It is in the boto3 docs. You can find more details about this here: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/resources.html#actions


问:
Pull huge data from MySQL
Trying to pull huge data [about 200,000 rows] from MySQL using PHP.
Getting the error: PHP Fatal error:  Allowed memory size of xxxxx bytes exhausted

I have an application which generates almost 80-90 thousand trasnactions every month. This is a growing application, so I can expect the numbers to grow rapidly.
While generating the monthly or quaterly report, I am getting the memory size error. I tried to increase the allowed memory and it worked.
But I know allowing more memory is a temprary fix and is not a good practice in long run.

I need some help in scaling this application.

In my opinion, breaking the query result into multiple manageable chunks and join them together should solve the problem.

Is there any alternative/better solution?

Does NoSQL helps here? how?

Edit:

Here is the way data is used:
The transaction table containes all transaction details along with the references to master data like user-id, place/location id, type of transacation.

The report will:

Pull the records from transaction table

Replace master references appropriately [replace user-id with name, address, etc]

Dump the data in csv/xls format

Store/download the file

Thanks

答:
Could you not use Mysql Aggregate functions? They should always be quicker than to pull out all records and then process them.
https://dev.mysql.com/doc/refman/8.0/en/group-by-functions-and-modifiers.html</p>

Title: Pull huge data from MySQL


问:
ActiveMQ or any messaging queue - FIFO requirement
Question on messaging queues. I've been reading around for a while (couple of days) on the number of messaging queue products - totally astounding!

I need to have the ability to have FIFO in a queue. and i read that the messaging product guarantee delivery but not FIFO.

Any ideas on possible messaging implementations that guarantee FIFO? Or any other implementation ideas that don't use messaging systems?

Looking forward to hear from you.

Yakult121

答:
ActiveMQ guarrantees the message order of all messages sent by the same producer but also supports total ordering. For an example how to configure total ordering take a look at the documentation:

http://activemq.apache.org/total-ordering.html


问:
Can't retrieve data from Firebase using ValueEventListener

I'm trying to retrieve data from database and show it in an activity. The data is retrieved from database with a parent which corresponds to the previously clicked RecyclerView's position. When the data is retrieved using ValueEventListener, it shows that the variables are null.

I'm using intent to send the RecyclerView's position to other activity so it is able to retrieve the data from the clicked RecyclerView's child.

```<code>public void changeData(){
    final Intent intent = getIntent();
    Log.v("Locationsdsa", intent.getStringExtra("EXTRA_LOCATION"));
    Log.v("LocationsdsaLOLOL", intent.getStringExtra("EXTRA_POSITION"));
    DatabaseReference db = FirebaseDatabase.getInstance().getReference("Data");
    db.child(intent.getStringExtra("EXTRA_LOCATION")).child(intent.getStringExtra("EXTRA_POSITION")).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            placeName = dataSnapshot.child("nameResto").getValue(String.class);
            String restoAddress = dataSnapshot.child("restoAddress").getValue(String.class);
            String restoReview = dataSnapshot.child("restoReviews").getValue(String.class);
            String imgUrl = dataSnapshot.child("restoThumbnail").getValue(String.class);
            langitude = dataSnapshot.child("restoLangitude").getValue(Double.class);
            longitude = dataSnapshot.child("restoLongitude").getValue(Double.class);

            Log.v("langitude", String.valueOf(langitude));
            Log.v("longitude", String.valueOf(longitude));
            Log.v("name", placeName);
            Log.v("restoaddress", restoAddress);
            Log.v("reviewresto", restoReview);
            tvPlace.setText(placeName);
            tvAddress.setText(restoAddress);
            tvRating.setText(restoReview);
            Picasso.get().load(imgUrl).resize(200, 200).centerInside().into(ivPlace);

        }
</code>```

I expect the data can be retrieved using ValueEventListener

答:
Here are some things users often misses:

Firebase rules are missing, the default is to block

Forgot to download the google-services.json

Spelling error in keys names vs code

the marshal Java pojo has wrong method declaration or missing the set method

Another thing often overlooked is that if Offline Capabilities is on like:

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

While debugging the app one can forget that data changed in the Firebase need to be propagated to the device before the device can react to the change. This is happening seemingly but if internet is flaky or other Intern hick ups strange things can happen that your not aware of right away

Like this Firebase offline capabilities causing memory problems answer by @AlexMamo and the discussion show..

If you experience strangness you can always uninstall app to release the local persisted version of the app db..

When developing one can by mistake view the wrong DB in Firestore console, lets say your app have a developer version and a production version, to little coffee and one might looking at wrong stuff

Body:
I'm trying to retrieve data from database and show it in an activity. The data is retrieved from database with a parent which corresponds to the previously clicked RecyclerView's position. When the data is retrieved using ValueEventListener, it shows that the variables are null.

I'm using intent to send the RecyclerView's position to other activity so it is able to retrieve the data from the clicked RecyclerView's child.

public void changeData(){
    final Intent intent = getIntent();
    Log.v("Locationsdsa", intent.getStringExtra("EXTRA_LOCATION"));
    Log.v("LocationsdsaLOLOL", intent.getStringExtra("EXTRA_POSITION"));
    DatabaseReference db = FirebaseDatabase.getInstance().getReference("Data");
    db.child(intent.getStringExtra("EXTRA_LOCATION")).child(intent.getStringExtra("EXTRA_POSITION")).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            placeName = dataSnapshot.child("nameResto").getValue(String.class);
            String restoAddress = dataSnapshot.child("restoAddress").getValue(String.class);
            String restoReview = dataSnapshot.child("restoReviews").getValue(String.class);
            String imgUrl = dataSnapshot.child("restoThumbnail").getValue(String.class);
            langitude = dataSnapshot.child("restoLangitude").getValue(Double.class);
            longitude = dataSnapshot.child("restoLongitude").getValue(Double.class);

            Log.v("langitude", String.valueOf(langitude));
            Log.v("longitude", String.valueOf(longitude));
            Log.v("name", placeName);
            Log.v("restoaddress", restoAddress);
            Log.v("reviewresto", restoReview);
            tvPlace.setText(placeName);
            tvAddress.setText(restoAddress);
            tvRating.setText(restoReview);
            Picasso.get().load(imgUrl).resize(200, 200).centerInside().into(ivPlace);

        }

I expect the data can be retrieved using ValueEventListener


问:
XML file lines In android studio are giving an error It says URI not registered
I am receiving an error in the XML lines saying URI not registered. I've tried everything from restarting to invalidating caches. The program doesn't run because of this error. The lines are:</p>

```<code>Xmlns:android="http://schemas.android.com/apk/res/android"
</code>```

答:
Please replace Xmlns:android="http://schemas.android.com/apk/res/android" with xmlns:android="http://schemas.android.com/apk/res/android".

The 'X' should not be capital. Replace 'X' with 'x'.

Unless you made the typo here and not in your code...

Body:
I am receiving an error in the XML lines saying URI not registered. I've tried everything from restarting to invalidating caches. The program doesn't run because of this error. The lines are:

Xmlns:android="http://schemas.android.com/apk/res/android"


问:
I could not set which elements would display on the page in which js pagination react
I tried to do Pagination but it didn't happen.
I guess I couldn't figure out how many pages on json's data would appear.

Below is the link to the example I was trying to do.

Thanks to those interested.

https://codesandbox.io/embed/kategori-detay-pagination-zpzdt

```<code> handlePageChange(pageNumber) {
    console.log(`active page is ${pageNumber}`);
    this.setState({ activePage: pageNumber });
  }
</code>```
```<code> <Pagination
          prevPageText="prev"
          nextPageText="next"
          firstPageText="first"
          lastPageText="last"
          pageRangeDisplayed={10}
          activePage={this.state.activePage}
          itemsCountPerPage={10}
          totalItemsCount={totalPosts}
          pageRangeDisplayed={10}
          onChange={this.handlePageChange}
        />
</code>```

答:
You did not bind the function handlePageChange() with proper 'this'

```<code>constructor(props) {
    super(props);
    this.state = {
      CategorySlug: "",
      CategoryBrief: [],
      Posts: []
    };

    // Need to bind the function to proper context
    this.handlePageChange = this.handlePageChange.bind(this);
}

handlePageChange(pageNumber) {
    console.log(`active page is ${pageNumber}`);

    // Without binding, 'this' refers <Pagination /> instance
    // With binding, 'this' refers to current <PageCategoryDetail /> instance
    this.setState({ activePage: pageNumber });
}
</code>```

Or if you prefer, you could use at newer syntax, then you don't have to bind
each function individually

```<code>handlePageChange = (pageNumber) => {
    // Your code here
}
</code>```

Edit

To show the items, according to the selected page, you need to manipulate the Posts array and filter out the required items

```<code>// You only filter by the selected Category, did not handle the paging
{Posts.filter(b => b.CategoryName === CategorySlug).map(item => (
   <li key={item.PostID}>{item.Title}</li>
))}

// You need further handle the pagination
const POST_PER_PAGE = 5
const pageOffset = this.state.activePage > 0 ? this.state.activePage - 1 : 0;
const startIndex = pageOffset * POST_PER_PAGE;
const endIndex = startIndex + POST_PER_PAGE;
{Posts.filter(b => b.CategoryName === CategorySlug).slice(startIndex, endIndex).map(item => (
   <li key={item.PostID}>{item.Title}</li>
))}

</code>```

https://codesandbox.io/s/kategori-detay-pagination-f72px

Body:
I tried to do Pagination but it didn't happen.
I guess I couldn't figure out how many pages on json's data would appear.

Below is the link to the example I was trying to do.

Thanks to those interested.

https://codesandbox.io/embed/kategori-detay-pagination-zpzdt

 handlePageChange(pageNumber) {
    console.log(`active page is ${pageNumber}`);
    this.setState({ activePage: pageNumber });
  }

 <Pagination
          prevPageText="prev"
          nextPageText="next"
          firstPageText="first"
          lastPageText="last"
          pageRangeDisplayed={10}
          activePage={this.state.activePage}
          itemsCountPerPage={10}
          totalItemsCount={totalPosts}
          pageRangeDisplayed={10}
          onChange={this.handlePageChange}
        />

Body:
You did not bind the function handlePageChange() with proper 'this'

constructor(props) {
    super(props);
    this.state = {
      CategorySlug: "",
      CategoryBrief: [],
      Posts: []
    };

    // Need to bind the function to proper context
    this.handlePageChange = this.handlePageChange.bind(this);
}

handlePageChange(pageNumber) {
    console.log(`active page is ${pageNumber}`);

    // Without binding, 'this' refers <Pagination /> instance
    // With binding, 'this' refers to current <PageCategoryDetail /> instance
    this.setState({ activePage: pageNumber });
}

Or if you prefer, you could use at newer syntax, then you don't have to bind
each function individually

handlePageChange = (pageNumber) => {
    // Your code here
}

Edit

To show the items, according to the selected page, you need to manipulate the Posts array and filter out the required items

// You only filter by the selected Category, did not handle the paging
{Posts.filter(b => b.CategoryName === CategorySlug).map(item => (
   <li key={item.PostID}>{item.Title}</li>
))}

// You need further handle the pagination
const POST_PER_PAGE = 5
const pageOffset = this.state.activePage > 0 ? this.state.activePage - 1 : 0;
const startIndex = pageOffset * POST_PER_PAGE;
const endIndex = startIndex + POST_PER_PAGE;
{Posts.filter(b => b.CategoryName === CategorySlug).slice(startIndex, endIndex).map(item => (
   <li key={item.PostID}>{item.Title}</li>
))}

https://codesandbox.io/s/kategori-detay-pagination-f72px


问:
How to sort WBS format string in Realm?
I want to sort my Realm object, using one of it properties. It has WBS like format (1.1.3 ,1.1.11, etc) using String as it type.

I'm using RealmSwift 3.11.2 and I've already tried using sorted(by:) and it works! But the 1.1.10 and 1.1.11 will be sorted ahead of 1.1.2

This is the code that I'm using

```<code>tasks = tasks.sorted(byKeyPath: "wbs", ascending: true)
</code>```

I expect the output will be ordered correctly, like [1.1.2, 1.1.10, 1.1.11].

Any help is appreciated, I'm drawing a blank here, is there any way I can  do it in Realm ?

答:
You may want to take a minute and do an Internet search on sorting strings.

The issue is that if you have a list of strings like this 1, 2, 3, 10 and they are sorted, they will sort to 1, 10, 2, 3.

This is because how strings are sorted; for this example they are they are generally sorted by the first letter/object in the string (1's) and then by the second letter (0) and so on, so then the strings that start with 1 get grouped up. Then 2's etc.

So you need to either pad them so the formats are the same like

```<code>01.01.02, 01.01.10, 01.01.11 etc
</code>```

which will sort correctly or store the three sections in separate properties as numbers (int's)

```<code>class TriadClass {
    var first = 0
    var second = 0
    var third = 0

    init(x: Int, y: Int, z: Int) {
        self.first = x
        self.second = y
        self.third = z
    }

    func getPaddedString() -> String {
        let formatter = NumberFormatter()
        formatter.format = "00"

        let firstNum = NSNumber(value: self.first)
        let secondNum = NSNumber(value: self.second)
        let thirdNum = NSNumber(value: self.third)

        let firstString = formatter.string(from: firstNum)
        let secondString = formatter.string(from: secondNum)
        let thirdString = formatter.string(from: thirdNum)

        let finalString = "\(firstString!).\(secondString!).\(thirdString!)"

        return finalString
    }

    func getPureString() -> String {
        let formatter = NumberFormatter()
        formatter.format = "0"

        let firstNum = NSNumber(value: self.first)
        let secondNum = NSNumber(value: self.second)
        let thirdNum = NSNumber(value: self.third)

        let firstString = formatter.string(from: firstNum)
        let secondString = formatter.string(from: secondNum)
        let thirdString = formatter.string(from: thirdNum)

        let finalString = "\(firstString!).\(secondString!).\(thirdString!)"

        return finalString
    }
}
</code>```

Then sort by first, second and third. To get the number for display in the UI I included to functions to format them. Note both functions could be greatly improved and shortened by I left them verbose so you could step through.

The usage would be

```<code>let myNum = TriadClass(x: 1, y: 1, z: 10)
let paddedString = myNum.getPaddedString()
print(paddedString)

let nonPaddedString = myNum.getPureString()
print(nonPaddedString)
</code>```

and an output of

```<code>01.01.10
1.1.10
</code>```

There are a number of other solutions as well but these two are a place to start.

Title: How to sort WBS format string in Realm?

Body:
I want to sort my Realm object, using one of it properties. It has WBS like format (1.1.3 ,1.1.11, etc) using String as it type.

I'm using RealmSwift 3.11.2 and I've already tried using sorted(by:) and it works! But the 1.1.10 and 1.1.11 will be sorted ahead of 1.1.2

This is the code that I'm using

tasks = tasks.sorted(byKeyPath: "wbs", ascending: true)

I expect the output will be ordered correctly, like [1.1.2, 1.1.10, 1.1.11].

Any help is appreciated, I'm drawing a blank here, is there any way I can  do it in Realm ?

Body:
You may want to take a minute and do an Internet search on sorting strings.

The issue is that if you have a list of strings like this 1, 2, 3, 10 and they are sorted, they will sort to 1, 10, 2, 3.

This is because how strings are sorted; for this example they are they are generally sorted by the first letter/object in the string (1's) and then by the second letter (0) and so on, so then the strings that start with 1 get grouped up. Then 2's etc.

So you need to either pad them so the formats are the same like

01.01.02, 01.01.10, 01.01.11 etc

which will sort correctly or store the three sections in separate properties as numbers (int's)

class TriadClass {
    var first = 0
    var second = 0
    var third = 0

    init(x: Int, y: Int, z: Int) {
        self.first = x
        self.second = y
        self.third = z
    }

    func getPaddedString() -> String {
        let formatter = NumberFormatter()
        formatter.format = "00"

        let firstNum = NSNumber(value: self.first)
        let secondNum = NSNumber(value: self.second)
        let thirdNum = NSNumber(value: self.third)

        let firstString = formatter.string(from: firstNum)
        let secondString = formatter.string(from: secondNum)
        let thirdString = formatter.string(from: thirdNum)

        let finalString = "\(firstString!).\(secondString!).\(thirdString!)"

        return finalString
    }

    func getPureString() -> String {
        let formatter = NumberFormatter()
        formatter.format = "0"

        let firstNum = NSNumber(value: self.first)
        let secondNum = NSNumber(value: self.second)
        let thirdNum = NSNumber(value: self.third)

        let firstString = formatter.string(from: firstNum)
        let secondString = formatter.string(from: secondNum)
        let thirdString = formatter.string(from: thirdNum)

        let finalString = "\(firstString!).\(secondString!).\(thirdString!)"

        return finalString
    }
}

Then sort by first, second and third. To get the number for display in the UI I included to functions to format them. Note both functions could be greatly improved and shortened by I left them verbose so you could step through.

The usage would be

let myNum = TriadClass(x: 1, y: 1, z: 10)
let paddedString = myNum.getPaddedString()
print(paddedString)

let nonPaddedString = myNum.getPureString()
print(nonPaddedString)

and an output of

01.01.10
1.1.10

There are a number of other solutions as well but these two are a place to start.


问:
Select 2 products from each category in MySQL
Pretty similar to MYSQL - select first 4 records for each category in a table but there isn't an accepted answer and the one answer there doesn't make much sense so i'm asking again.

I have a PRODUCTS table with 3 columns: ID, NAME and CATEGORY
What i would like to know now is if it's at all possible to select 2 products for each distinct category without doing queries in a PHP loop.

The order of the selected products is of no importance, they might as well be random. But it's important that i only have max 2 products per category.

So a good result set would be

```<code>ID  ; NAME   ; CATEGORY
:::::::::::::::::::::::
152 ; APPLE  ; FRUIT
185 ; ORANGE ; FRUIT
145 ; BEEF   ; MEAT
141 ; PORK   ; MEAT
410 ; PEPSI  ; DRINKS
585 ; CARROT ; VEGETABLES
585 ; TOMATO ; VEGETABLES
</code>```

答:
FULL SQL:

```<code>SELECT t.id, t.name, t.category
FROM the_table AS t
, (SELECT MIN(id) AS id_min FROM the_table GROUP BY category) AS t1
, (SELECT MAX(id) AS id_max FROM the_table GROUP BY category) AS t2
WHERE (t.id=t1.id_min) OR  (t.id=t2.id_max)
</code>```

The query picks the min and the max of each category and return both of them (or only one if it is the same).

You said "The order of the selected products is of no importance, they might as well be random", so this method should be ok.

Body:
Pretty similar to MYSQL - select first 4 records for each category in a table but there isn't an accepted answer and the one answer there doesn't make much sense so i'm asking again.

I have a PRODUCTS table with 3 columns: ID, NAME and CATEGORY
What i would like to know now is if it's at all possible to select 2 products for each distinct category without doing queries in a PHP loop.

The order of the selected products is of no importance, they might as well be random. But it's important that i only have max 2 products per category.

So a good result set would be

ID  ; NAME   ; CATEGORY
:::::::::::::::::::::::
152 ; APPLE  ; FRUIT
185 ; ORANGE ; FRUIT
145 ; BEEF   ; MEAT
141 ; PORK   ; MEAT
410 ; PEPSI  ; DRINKS
585 ; CARROT ; VEGETABLES
585 ; TOMATO ; VEGETABLES

Body:
FULL SQL:

SELECT t.id, t.name, t.category
FROM the_table AS t
, (SELECT MIN(id) AS id_min FROM the_table GROUP BY category) AS t1
, (SELECT MAX(id) AS id_max FROM the_table GROUP BY category) AS t2
WHERE (t.id=t1.id_min) OR  (t.id=t2.id_max)

The query picks the min and the max of each category and return both of them (or only one if it is the same).

You said "The order of the selected products is of no importance, they might as well be random", so this method should be ok.


问:
GoogleAnalytics API Audience > Technology > Network > Service Provider
There is a proliferation of LeadFeeder, WhoisVisiting, Leady, LeadFuse, LeadForensics type sites.

The founder of LeadFeeder wrote about it here: https://analytics.googleblog.com/2016/09/using-google-analytics-with-leadfeeder.html.

They all use the Google Analytics API to gather the Audience > Technology > Network > Service Provider field.

For the life of me I can't find this in the API documentation.

I went through every core Dimension I could find here: https://developers.google.com/analytics/devguides/reporting/core/dimsmets

The only other thing I can think of is that maybe the Network Audience is only available to Google Analytics 360 Premium?

I've scoured the API documentation and Googled it into the ground.

Not much code to show... unless you know the right API function to call... which I can't figure out!

答:
Thanks!

I finally ended up finding a reference to it here too:

https://groups.google.com/forum/m/#!topic/google-analytics-data-export-api/pdoZrbbC5RQ


问:
Brew install python 3.7 has not updated to location
I installed python 3.7 through brew install python which has offered the following response

```<code>==> python
Python has been installed as
  /usr/local/bin/python3

Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
  /usr/local/opt/python/libexec/bin

If you need Homebrew's Python 2.7 run
  brew install python@2

You can install Python packages with
  pip3 install <package>
They will install into the site-package directory
  /usr/local/lib/python3.7/site-packages
</code>```

but when I call pip3 install neo4j it says requirement already satisfied, pointing to an earlier distribution of python:

```<code> Sffd-MBP:~ Sffd$ pip3 install neo4j
Requirement already satisfied: neo4j in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages (1.7.4)
</code>```

indicating that homebrew has not unversioned symlinks. What is the best way to do this manually and does anyone know why this has not happened automatically?

答:
you have to install it again.
run:

pip3 uninstall neo4j

pip3 install neo4j

Body:
I installed python 3.7 through brew install python which has offered the following response

==> python
Python has been installed as
  /usr/local/bin/python3

Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
  /usr/local/opt/python/libexec/bin

If you need Homebrew's Python 2.7 run
  brew install python@2

You can install Python packages with
  pip3 install <package>
They will install into the site-package directory
  /usr/local/lib/python3.7/site-packages

but when I call pip3 install neo4j it says requirement already satisfied, pointing to an earlier distribution of python:

 Sffd-MBP:~ Sffd$ pip3 install neo4j
Requirement already satisfied: neo4j in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages (1.7.4)

indicating that homebrew has not unversioned symlinks. What is the best way to do this manually and does anyone know why this has not happened automatically?


问:
Unrecognized escape sequence error in AJAX parmeter
I am trying to populate a jQuery data table using AJAX. The parameter I am passing to web service is a file path and I keep getting unrecognized escape sequence error. I tried to escape the some characters to no avail:

```<code>Unrecognized escape sequence. (15): { \\u0027Path\\u0027: \\u0027C:\\\\TEMP\\\\DEV\\\\IDC\\\\2391\\\\GBS_IDC_WS_20190508.log\\u0027}\
</code>```

This is the Submit button click handler:

```<code>$(document).on('click', '#btnSubmit', function (ev) {
    var wsUrl = "services/lv.asmx/ProcessLogFile";
    var wsParams = "{ 'Path': '" + escapeHtml(logPath) + "'}";
    // var wsParams = '{ "Path": "' + logPath + '"}';
    populateTable(wsUrl, wsParams, tblContacts);
    return false;
});

function populateTable(ws_url, parameters, table) {
    debugger
    $.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: ws_url,
        cache: false,
        data: parameters,
    }).done(function (result) {
        table.clear().draw();
        if (!result || result.d === "") {
        }
        else {
            jResult = JSON.parse(result.d);
            table.rows.add(jResult).draw();
        }
    }).fail(function (jqXHR, textStatus, errorThrown) {
        alert(textStatus + ' - ' + errorThrown + '\n' + jqXHR.responseText);
    });
}
</code>```

This is the script I found online to escape some characters:

```<code>var entityMap = {
    '&': '&',
    '<': '<',
    '>': '>',
    '"': '"',
    "'": ''',
    '/': '/',
    '`': '`',
    '=': '='
};

function escapeHtml (string) {
    return String(string).replace(/[&<>"'`=\/]/g, function (s) {
    return entityMap[s];
    });
}
</code>```

答:
The fix was to replace the following line in function populateTable(ws_url, parameters, table), following Mohammad's suggestion as well:

```<code>data: parameters,
</code>```

with:

```<code>data: JSON.stringify(parameters),
</code>```

So, the updated (partial) methods are:

```<code>$(document).on('click', '#btnSubmit', function (ev) {
    var wsUrl = "services/lv.asmx/ProcessLogFile";
    var wsParams = { Path: escapeHtml(logPath) };
    populateTable(wsUrl, wsParams, tblContacts);
    return false;
});

function populateTable(ws_url, parameters, table) {
    $.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: ws_url,
        cache: false,
        data: JSON.stringify(parameters),
    }).done(function (result) {...}
</code>```

Body:
I am trying to populate a jQuery data table using AJAX. The parameter I am passing to web service is a file path and I keep getting unrecognized escape sequence error. I tried to escape the some characters to no avail:

Unrecognized escape sequence. (15): { \\u0027Path\\u0027: \\u0027C:\\\\TEMP\\\\DEV\\\\IDC\\\\2391\\\\GBS_IDC_WS_20190508.log\\u0027}\

This is the Submit button click handler:

$(document).on('click', '#btnSubmit', function (ev) {
    var wsUrl = "services/lv.asmx/ProcessLogFile";
    var wsParams = "{ 'Path': '" + escapeHtml(logPath) + "'}";
    // var wsParams = '{ "Path": "' + logPath + '"}';
    populateTable(wsUrl, wsParams, tblContacts);
    return false;
});

function populateTable(ws_url, parameters, table) {
    debugger
    $.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: ws_url,
        cache: false,
        data: parameters,
    }).done(function (result) {
        table.clear().draw();
        if (!result || result.d === "") {
        }
        else {
            jResult = JSON.parse(result.d);
            table.rows.add(jResult).draw();
        }
    }).fail(function (jqXHR, textStatus, errorThrown) {
        alert(textStatus + ' - ' + errorThrown + '\n' + jqXHR.responseText);
    });
}

This is the script I found online to escape some characters:

var entityMap = {
    '&': '&',
    '<': '<',
    '>': '>',
    '"': '"',
    "'": ''',
    '/': '/',
    '`': '`',
    '=': '='
};

function escapeHtml (string) {
    return String(string).replace(/[&<>"'`=\/]/g, function (s) {
    return entityMap[s];
    });
}

Body:
The fix was to replace the following line in function populateTable(ws_url, parameters, table), following Mohammad's suggestion as well:

data: parameters,

with:

data: JSON.stringify(parameters),

So, the updated (partial) methods are:

$(document).on('click', '#btnSubmit', function (ev) {
    var wsUrl = "services/lv.asmx/ProcessLogFile";
    var wsParams = { Path: escapeHtml(logPath) };
    populateTable(wsUrl, wsParams, tblContacts);
    return false;
});

function populateTable(ws_url, parameters, table) {
    $.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: ws_url,
        cache: false,
        data: JSON.stringify(parameters),
    }).done(function (result) {...}


问:
How faster the map than JSON string in the hive data type?
there.

I'm considering data type in hive column, between json formatted string and map.

In my common sense, there are no arguments that the map type is definitely faster than json string. However, I have no idea how hive treats data in the map type, so I can't prove it.

If I just want to put some single depth nested data into a column, map is the best choice? and how faster than json string?

答: Map is also serialized as string and stored in strings. Data transferred between mapper and reducer is serialized in strings then de-serialized again. You will not notice the difference. But in case of map datatype SerDe will deserialize as Map type and you can directly apply map operators in the query like select m[key] or size(m) or map_keys(m). This looks much simpler than using JSON_TUPLE() probably along with lateral view or GET_JSON_OBJECT(), in this case map may work a bit faster, though JSON can store more complex structures like nested arrays of maps.  Choose map if it suits your data and JSON for complex structures.


问:
How to switch namespace in kubernetes
Say, I have two namespaces k8s-app1 and k8s-app2

I can list all pods from specific namespace using the below command

```<code>kubectl get pods -n <namespace>
</code>```

We need to append namespace to all commands to list objects from the respective namespaces. Is there a way to set specific namespace and list objects without including the namespace explicitly?

答:
I like my answers short, to the point and with references to official documentation:

Answer:

```<code>kubectl config set-context --current --namespace=my-namespace
</code>```

From:

https://kubernetes.io/docs/reference/kubectl/cheatsheet/

```<code># permanently save the namespace for all subsequent kubectl commands in that context.
kubectl config set-context --current --namespace=ggckad-s2
</code>```

Title: How to switch namespace in kubernetes

Body:
Say, I have two namespaces k8s-app1 and k8s-app2

I can list all pods from specific namespace using the below command

kubectl get pods -n <namespace>

We need to append namespace to all commands to list objects from the respective namespaces. Is there a way to set specific namespace and list objects without including the namespace explicitly?

Body:
I like my answers short, to the point and with references to official documentation:

Answer:

kubectl config set-context --current --namespace=my-namespace

From:

https://kubernetes.io/docs/reference/kubectl/cheatsheet/

# permanently save the namespace for all subsequent kubectl commands in that context.
kubectl config set-context --current --namespace=ggckad-s2


问:
How to calculate maximum bounding box based on fixed aspect ratio requirement?
I am building some UI for users to upload photos and crop them, based on a fixed aspect ratio which is a requirement of my product. This fixed aspect ratio happens to be portrait.

I need to select the largest internal bounding box within user-provided photos based on this aspect ratio. Here are two examples (portrait and landscape):

So, given dynamic user-added dimensions, how can I calculate the largest possible crop selection based on my fixed aspect ratio?

```<code>const FIXED_ASPECT_RATIO = 3 / 4

const usersImageWidth = 800
const usersImageHeight = 550

const cropWidth = ?
const cropHeight = ?
const cropX = ?
const cropY = ?
</code>```

答:
I am not sure if i got your question right, but from what I understood, I would try it like this:</p>

```<code>// Check if picture is portrait or landscape oriented
if (userImageWidth >= userImageHeight) {
// If picture is landscape oriented, max crop height = height
  cropHeight = userImageHeight;
// Width is calculated according to ratio:
  cropWidth = (cropHeight / 4) * 3;
// If picture is portrait oriented, max crop width = width
} else {
  cropWidth = userImageWidth;
// Height is calculated according to ratio:
  cropHeight = (cropWidth / 3) * 4
}
</code>```

Body:
I am building some UI for users to upload photos and crop them, based on a fixed aspect ratio which is a requirement of my product. This fixed aspect ratio happens to be portrait.

I need to select the largest internal bounding box within user-provided photos based on this aspect ratio. Here are two examples (portrait and landscape):

So, given dynamic user-added dimensions, how can I calculate the largest possible crop selection based on my fixed aspect ratio?

const FIXED_ASPECT_RATIO = 3 / 4

const usersImageWidth = 800
const usersImageHeight = 550

const cropWidth = ?
const cropHeight = ?
const cropX = ?
const cropY = ?

Body:
I am not sure if i got your question right, but from what I understood, I would try it like this:

// Check if picture is portrait or landscape oriented
if (userImageWidth >= userImageHeight) {
// If picture is landscape oriented, max crop height = height
  cropHeight = userImageHeight;
// Width is calculated according to ratio:
  cropWidth = (cropHeight / 4) * 3;
// If picture is portrait oriented, max crop width = width
} else {
  cropWidth = userImageWidth;
// Height is calculated according to ratio:
  cropHeight = (cropWidth / 3) * 4
}


问:
Saving input data from a program without a database
I am building a simple task tracking WPF application and want to save the data entered by the user without the use of a full on Database.

Here is some sample data that I would like to save.

Date

Creation / completed

Task(Title/description)

Active of Inactive

Maybe Priority

I have been reading posts and some people are suggesting ini, CSV, app.config with XAML. I would just like to know what is a good standard choice for what I am looking to accomplish. Something that is solid so that my app can run with little to no setup from an end-user after its deployed.

答:
As an example, I'll use the following class as the top-tier database "object" you want to save:

```<code>[Serializable]
public class Model
{
    public string Data { get; set; }
}
</code>```

AJAX is probably the most commonly used "simple" solution of your options, which means either JSON or XAML. JSON is more better suited to "data" whereas XML is better suited to "documents".

For JSON you do something like this:

```<code>var serializer = new JsonSerializer();

using (var stream = new StreamWriter("filename.dat"))
    serializer.Serialize(stream, model);

using (var stream = new StreamReader("filename.dat"))
using (var reader = new JsonTextReader(stream))
{
    var copy = serializer.Deserialize<Model>(reader);
}
</code>```

Which produces a file like this:

```<code>{"Data":"Hello World!"}
</code>```

For XAML you do something like this:

```<code>var model = new Model { Data = "Hello World!" };

var serializer = new XmlSerializer(typeof(Model));
using (var stream = new StreamWriter("filename.dat"))
    serializer.Serialize(stream, model);

using (var stream = new StreamReader("filename.dat"))
{
    var copy = serializer.Deserialize(stream) as Model;
}
</code>```

Which produces a file like this:

```<code><?xml version="1.0" encoding="utf-8"?>
<Model xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Data>Hello World!</Data>
</Model>
</code>```

And of course if you're a sucker for punishment you can serialize into a .NET binary format directly:

```<code>BinaryFormatter formatter = new BinaryFormatter();

using (var stream = new FileStream("filename.dat", FileMode.Create))
    formatter.Serialize(stream, model);

using (var stream = new FileStream("filename.dat", FileMode.Open))
{
    var copy = formatter.Deserialize(stream) as Model;
}
</code>```

Which will in turn create a file utterly useless for anything other than this.

All of these "solutions" unfortunately come with the caveat of needing to read and write the entire thing into memory every time you want to work with it, and to get around that you will have to use a database I'm afraid. But if you don't want to use a "full on" database then how about a "Lite" one? Specifically, SQLite? Advantages include:

Data is all stored in a local file, in a location of your own choosing.

It's completely self-contained, no need to install a database server on your target machines.

Supported by virtually every platform under the sun (and they all use the same format).

Integrates easily with ORMs like EntityFramework etc.

There are tons of free 3rd-party utilities like LINQPad etc that allow you to open and work with the database file directly.

Coupling SQLite with something like EntityFramework is particularly handy, because the code to access and write to the database is extremely straightforward:

```<code>using (var db = new DatabaseContext())
{
    foreach (var model in db.Models)
        // do something with the model here
}
</code>```

It also means that once your application makes a million dollars and you want to migrate to a "proper" DB engine you'll already have the ORM architecture in place. The main downside is that the initial setup for SQLite in particular is a little annoying, but you only have to do it once. Here are my own personal notes that I refer to for each project I use it on, enjoy!

Step 1 - Create a console or WPF application

Step 2 - Add Nuget package System.Data.SQLite

Step 3 - Add Nuget package SQLite.CodeFirst (this will also install Entity Framework as a dependency)

Step 4 - Add the models and DatabaseContext classes (plenty of references on the net showing how to do this for EF).

Step 5 - Adjust the App.config file as follows:

```<code><?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
  <connectionStrings>
    <add name="MyDatabase" connectionString="data source=MyDatabase.db;foreign keys=True;password=password" />
  </connectionStrings>
</configuration>
</code>```

Step 6 - Add reference to System.Configuration so that code can read the config file.

Step 7 - Run the application, the database file will be created when you create the DatabaseContext object for the first time.

Body:
As an example, I'll use the following class as the top-tier database "object" you want to save:

[Serializable]
public class Model
{
    public string Data { get; set; }
}

AJAX is probably the most commonly used "simple" solution of your options, which means either JSON or XAML. JSON is more better suited to "data" whereas XML is better suited to "documents".

For JSON you do something like this:

var serializer = new JsonSerializer();

using (var stream = new StreamWriter("filename.dat"))
    serializer.Serialize(stream, model);

using (var stream = new StreamReader("filename.dat"))
using (var reader = new JsonTextReader(stream))
{
    var copy = serializer.Deserialize<Model>(reader);
}

Which produces a file like this:

{"Data":"Hello World!"}

For XAML you do something like this:

var model = new Model { Data = "Hello World!" };

var serializer = new XmlSerializer(typeof(Model));
using (var stream = new StreamWriter("filename.dat"))
    serializer.Serialize(stream, model);

using (var stream = new StreamReader("filename.dat"))
{
    var copy = serializer.Deserialize(stream) as Model;
}

Which produces a file like this:

<?xml version="1.0" encoding="utf-8"?>
<Model xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Data>Hello World!</Data>
</Model>

And of course if you're a sucker for punishment you can serialize into a .NET binary format directly:

BinaryFormatter formatter = new BinaryFormatter();

using (var stream = new FileStream("filename.dat", FileMode.Create))
    formatter.Serialize(stream, model);

using (var stream = new FileStream("filename.dat", FileMode.Open))
{
    var copy = formatter.Deserialize(stream) as Model;
}

Which will in turn create a file utterly useless for anything other than this.

All of these "solutions" unfortunately come with the caveat of needing to read and write the entire thing into memory every time you want to work with it, and to get around that you will have to use a database I'm afraid. But if you don't want to use a "full on" database then how about a "Lite" one? Specifically, SQLite? Advantages include:

Data is all stored in a local file, in a location of your own choosing.

It's completely self-contained, no need to install a database server on your target machines.

Supported by virtually every platform under the sun (and they all use the same format).

Integrates easily with ORMs like EntityFramework etc.

There are tons of free 3rd-party utilities like LINQPad etc that allow you to open and work with the database file directly.

Coupling SQLite with something like EntityFramework is particularly handy, because the code to access and write to the database is extremely straightforward:

using (var db = new DatabaseContext())
{
    foreach (var model in db.Models)
        // do something with the model here
}

It also means that once your application makes a million dollars and you want to migrate to a "proper" DB engine you'll already have the ORM architecture in place. The main downside is that the initial setup for SQLite in particular is a little annoying, but you only have to do it once. Here are my own personal notes that I refer to for each project I use it on, enjoy!

Step 1 - Create a console or WPF application

Step 2 - Add Nuget package System.Data.SQLite

Step 3 - Add Nuget package SQLite.CodeFirst (this will also install Entity Framework as a dependency)

Step 4 - Add the models and DatabaseContext classes (plenty of references on the net showing how to do this for EF).

Step 5 - Adjust the App.config file as follows:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
  <connectionStrings>
    <add name="MyDatabase" connectionString="data source=MyDatabase.db;foreign keys=True;password=password" />
  </connectionStrings>
</configuration>

Step 6 - Add reference to System.Configuration so that code can read the config file.

Step 7 - Run the application, the database file will be created when you create the DatabaseContext object for the first time.


问:
What does XmlReader.Parse() do?
edit: The project is an old project and the below code is one class in there that handles the xml.

There is a question to get the xsd from an old legacy application, but all it seems to be doing is: xmlReader.ParseXml(fileName,outputFile); in the ProcessXml() method.

```<code>using System;
using System.IO;
using System.Configuration;
using Utilities;
namespace JobProcessor
{

public class XmlJob : Job
{
    #region variables
    private string      ftpServer;
    private string      ftpUserName;
    private string      ftpPassWord;
    private string      ftpDestination;
    ///<summary>The xml reader used to get validation information from the xmp parsing</summary>
    protected XmlReader xmlReader   = new XmlReader();

    private string      outputFile;

    public XmlJob(string fileName, string type) : base(fileName, type){
        ftpServer       = ConfigurationManager.AppSettings[type + "-ftp"];
        ftpUserName     = ConfigurationManager.AppSettings[type + "-ftpUser"];
        ftpPassWord     = ConfigurationManager.AppSettings[type + "-ftpPass"];
        ftpDestination  = ConfigurationManager.AppSettings[type + "-ftpDest"];
    }

    protected bool ConfigureXmlReader(){
        int    recType    = 0;

        try{
            //set up the record types (i.e. the elements that trigger a new record in the output
            while(ConfigurationManager.AppSettings[type + "-RecordType" + recType.ToString("00")] != null){
                xmlReader.AddRecordType(ConfigurationManager.AppSettings[type + "-RecordType" + recType.ToString("00")],recType.ToString("00"));
                recType++;
            }
            //store the number of types
            xmlReader.SetNoRecordTypes(recType);
        }catch(Exception e){
            HandleError("Error loading record types\r\n" + e.ToString() + "\r\n" + e.StackTrace + "\r\n");
            return(false);
        }

        try{
            //set up the fields which make up each record type
            for(int i=0;i<recType;i++){
                xmlReader.AddFieldsList(i,ConfigurationManager.AppSettings[type + "-" + i.ToString("00") + "Fields"]);
            }
        }catch(Exception e){
            HandleError("Error loading field list\r\n" + e.ToString() + "\r\n" + e.StackTrace + "\r\n");
            return(false);
        }
        return(true);
    }

    public override void RunJob(){
        outputFile = Path.Combine(destinationDirectory, Path.GetFileNameWithoutExtension(fileName) + ".flat");

        if(!ConfigureXmlReader()){
            return;
        }

        if(!CheckInputFile()){
            return;
        }

        if(!ProcessXml()){
            return;
        }

        if(!Validate()){
            return;
        }

        if(ftpServer!=null && ftpUserName!=null && ftpPassWord!=null && ftpDestination!=null){
            if(!FtpFile()){
                return;
            }
        }

        MoveOriginalFile();
    }

    protected virtual bool Validate(){
        return(true);
    }

    protected bool ProcessXml(){
        /*
         * Process the xml file into a delimited format
         */

        try{
            xmlReader.ParseXml(fileName,outputFile);
        }catch(Exception e){
            HandleError("Error parsing XML file\r\n" + e.ToString() + "\r\n" + e.StackTrace + "\r\n");
            return(false);
        }
        return(true);
    }
    protected bool FtpFile(){
        try{
            FtpMethods.UploadFile(ftpServer,ftpUserName,ftpPassWord,outputFile,ftpDestination);
        }catch(Exception e){
            HandleError("Error FTPing file\r\n" + e.ToString() + "\r\n" + e.StackTrace + "\r\n");
            return(false);
        }
        return(true);
    }

    #endregion
}
</code>```

}

What exactly does this command do? F1 doesn't find anything and searching google gets me to a lot of xmlTextReaders.

答: It seems that the XMLReader type here is from the Utilities library, not the XMLReader type of the .NET.

Title: What does XmlReader.Parse() do?

Body:
edit: The project is an old project and the below code is one class in there that handles the xml.

There is a question to get the xsd from an old legacy application, but all it seems to be doing is: xmlReader.ParseXml(fileName,outputFile); in the ProcessXml() method.

using System;
using System.IO;
using System.Configuration;
using Utilities;
namespace JobProcessor
{

public class XmlJob : Job
{
    #region variables
    private string      ftpServer;
    private string      ftpUserName;
    private string      ftpPassWord;
    private string      ftpDestination;
    ///The xml reader used to get validation information from the xmp parsing
    protected XmlReader xmlReader   = new XmlReader();

    private string      outputFile;

    public XmlJob(string fileName, string type) : base(fileName, type){
        ftpServer       = ConfigurationManager.AppSettings[type + "-ftp"];
        ftpUserName     = ConfigurationManager.AppSettings[type + "-ftpUser"];
        ftpPassWord     = ConfigurationManager.AppSettings[type + "-ftpPass"];
        ftpDestination  = ConfigurationManager.AppSettings[type + "-ftpDest"];
    }

    protected bool ConfigureXmlReader(){
        int    recType    = 0;

        try{
            //set up the record types (i.e. the elements that trigger a new record in the output
            while(ConfigurationManager.AppSettings[type + "-RecordType" + recType.ToString("00")] != null){
                xmlReader.AddRecordType(ConfigurationManager.AppSettings[type + "-RecordType" + recType.ToString("00")],recType.ToString("00"));
                recType++;
            }
            //store the number of types
            xmlReader.SetNoRecordTypes(recType);
        }catch(Exception e){
            HandleError("Error loading record types\r\n" + e.ToString() + "\r\n" + e.StackTrace + "\r\n");
            return(false);
        }

        try{
            //set up the fields which make up each record type
            for(int i=0;i<recType;i++){
                xmlReader.AddFieldsList(i,ConfigurationManager.AppSettings[type + "-" + i.ToString("00") + "Fields"]);
            }
        }catch(Exception e){
            HandleError("Error loading field list\r\n" + e.ToString() + "\r\n" + e.StackTrace + "\r\n");
            return(false);
        }
        return(true);
    }

    public override void RunJob(){
        outputFile = Path.Combine(destinationDirectory, Path.GetFileNameWithoutExtension(fileName) + ".flat");

        if(!ConfigureXmlReader()){
            return;
        }

        if(!CheckInputFile()){
            return;
        }

        if(!ProcessXml()){
            return;
        }

        if(!Validate()){
            return;
        }

        if(ftpServer!=null && ftpUserName!=null && ftpPassWord!=null && ftpDestination!=null){
            if(!FtpFile()){
                return;
            }
        }

        MoveOriginalFile();
    }

    protected virtual bool Validate(){
        return(true);
    }

    protected bool ProcessXml(){
        /*
         * Process the xml file into a delimited format
         */

        try{
            xmlReader.ParseXml(fileName,outputFile);
        }catch(Exception e){
            HandleError("Error parsing XML file\r\n" + e.ToString() + "\r\n" + e.StackTrace + "\r\n");
            return(false);
        }
        return(true);
    }
    protected bool FtpFile(){
        try{
            FtpMethods.UploadFile(ftpServer,ftpUserName,ftpPassWord,outputFile,ftpDestination);
        }catch(Exception e){
            HandleError("Error FTPing file\r\n" + e.ToString() + "\r\n" + e.StackTrace + "\r\n");
            return(false);
        }
        return(true);
    }

    #endregion
}

}

What exactly does this command do? F1 doesn't find anything and searching google gets me to a lot of xmlTextReaders.


问:
Show / hide menu items in Android , based on Php server current time
I was stucking in php code & not able to proceed. Based on selection of timing, menu items should be show /hide.

How to get the PHP current time from the server through API

For example : IDLY - (8 am to 10 am)
               After 11 am, idly should be disable or hidden

答:
You can simply show/hide the menu items using below code

```<code>Menu nav_Menu = navigationView.getMenu();
nav_Menu.findItem(R.id.nav_id1).setVisible(false);
nav_Menu.findItem(R.id.nav_id2).setVisible(true);
.
.
</code>```

Body:
You can simply show/hide the menu items using below code

Menu nav_Menu = navigationView.getMenu();
nav_Menu.findItem(R.id.nav_id1).setVisible(false);
nav_Menu.findItem(R.id.nav_id2).setVisible(true);
.
.


问:
Oracle Age calculation from Date of birth and Today
I want to calculate the current Age from Date of Birth in my Oracle function.

What I am using is (Today-Dob)/30/12, but this is not accurate as some months have 31 days.

I need to get the correct age with the maximum precision. How can I do that?

答:
Suppose that you want to have the age (number of years only, a fixed number) of someone born on June 4, 1996, execute this command :

```<code>SELECT TRUNC(TO_NUMBER(SYSDATE - TO_DATE('04-06-1996')) / 365.25) AS AGE FROM DUAL;
</code>```

Result : (Executed May 28, 2019)

```<code>       AGE
----------
        22
</code>```

Explanation :

SYSDATE : Get system's (OS) actual date.

TO_DATE('04-06-1996') : Convert VARCHAR (string) birthdate into DATE (SQL type).

TO_NUMBER(...) : Convert a date to NUMBER (SQL type)

Devide per 365.25 : To have a bissextile year every four years (4 * 0.25 = 1 more day).

Trunc(...) : Retrieve the entire part only from a number.

Body:
Suppose that you want to have the age (number of years only, a fixed number) of someone born on June 4, 1996, execute this command :

SELECT TRUNC(TO_NUMBER(SYSDATE - TO_DATE('04-06-1996')) / 365.25) AS AGE FROM DUAL;

Result : (Executed May 28, 2019)

       AGE
----------
        22

Explanation :

SYSDATE : Get system's (OS) actual date.

TO_DATE('04-06-1996') : Convert VARCHAR (string) birthdate into DATE (SQL type).

TO_NUMBER(...) : Convert a date to NUMBER (SQL type)

Devide per 365.25 : To have a bissextile year every four years (4 * 0.25 = 1 more day).

Trunc(...) : Retrieve the entire part only from a number.


问:
Sizing of a generation affects throughput of other generation in garbage collection
How the impacting of resizing of a generation size (say younger generation) affect the   collection frequency and pause times of other generations (say tenured generation)?

In this article https://www.oracle.com/technetwork/java/gc-tuning-5-138395.html  , it is approximated to no effect. I want to understand the reason behind this approximation.

答:
What you need to consider here is that the primary factors that control GC pause times are the rate of object allocation and how quickly those objects become garbage.

It's also important to understand that (in Hotspot) young and old generations use different algorithms.

For the young generation, collection is simply copying objects to a survivor space or tenuring into the old generation.  How big the old gen. is will only have an impact if there isn't enough space to promote all the necessary objects.

For the old gen., there are different algorithms but they all need to identify live data and usable space.  Changing the size of the young generation will affect how frequently a minor GC happens but not how much data ultimately ends up in the old gen.  Depending on the algorithm used, it may be necessary to perform a full compacting collection but, again, when this happens will not be affected by the size of the young gen.  The rate of tenuring and fragmentation of the old gen. will be the deciding factors.


问:
Shiny updateSliderInput: Convert double-ended to single-ended slider
I have a double-ended shiny slider input like so:

```<code>sliderInput(inputId = "years",
                             label="Choose base year and target year",
                             min = 2015,
                             max = 2060,
                             value = c(2015,2060),
                             step = 1,
                             sep = ""
                 )
</code>```

After the user uploads a value, I want to fix the lower end of the slider to a certain value; the upper end should stil be moveable. I tried to achieve this by using the updateSliderInput function, and by just providing one value for the "value" attribute (thus, the slider should become a regular one-ended slider):

```<code>  observeEvent(upload(), {
    updateSliderInput(session, inputId = "years", min = base_year(), max = 2060, value = 2060)
  })
</code>```

However, this does not work; the slider is still double-ended, and both handles are set to 2060. How can I convert the double-ended slider to a regular one?

Kind regards,

Requin

答:
Here is another solution closer to your question, triggering only once after pushing the button:

```<code>library(shiny)

ui <- fluidPage(
  uiOutput("Slider_UI"),
  conditionalPanel("input.fixLower == 0", {actionButton("fixLower", "Fix lower range")})
)

server <- function(input, output, session) {

  fixedLower <- reactiveVal()

  observeEvent(input$fixLower, {
    fixedLower(min(isolate({input$years})))
  }, once = TRUE)

  output$Slider_UI <- renderUI({
    if(is.null(fixedLower())){
      sliderInput(inputId = "years",
                  label="Choose base year and target year",
                  min = 2015,
                  max = 2060,
                  value = c(2015, 2060),
                  step = 1,
                  sep = ""
      )
    } else {
      sliderInput(inputId = "years",
                  label="Choose base year and target year",
                  min = fixedLower(),
                  max = 2060,
                  value = fixedLower(),
                  step = 1,
                  sep = ""
      )
    }
  })

}

shinyApp(ui = ui, server = server)
</code>```

As suggested by @DSGym, here is an example using renderUI:

```<code>library(shiny)

ui <- fluidPage(
  radioButtons(inputId="type_select", label="Select slider type", choices = list("Standard slider", "Range slider")),
  uiOutput("Slider_UI")
)

server <- function(input, output, session) {

  output$Slider_UI <- renderUI({
    req(input$type_select)

    if(input$type_select == "Range slider"){
      presetValues <- c(2015, 2060)
    } else {
      presetValues <- 2060
    }

    sliderInput(inputId = "years",
                label="Choose base year and target year",
                min = 2015,
                max = 2060,
                value = presetValues,
                step = 1,
                sep = ""
    )
  })

}

shinyApp(ui = ui, server = server)
</code>```

Body:
I have a double-ended shiny slider input like so:

sliderInput(inputId = "years",
                             label="Choose base year and target year",
                             min = 2015,
                             max = 2060,
                             value = c(2015,2060),
                             step = 1,
                             sep = ""
                 )

After the user uploads a value, I want to fix the lower end of the slider to a certain value; the upper end should stil be moveable. I tried to achieve this by using the updateSliderInput function, and by just providing one value for the "value" attribute (thus, the slider should become a regular one-ended slider):

  observeEvent(upload(), {
    updateSliderInput(session, inputId = "years", min = base_year(), max = 2060, value = 2060)
  })

However, this does not work; the slider is still double-ended, and both handles are set to 2060. How can I convert the double-ended slider to a regular one?

Kind regards,

Requin

Body:
Here is another solution closer to your question, triggering only once after pushing the button:

library(shiny)

ui <- fluidPage(
  uiOutput("Slider_UI"),
  conditionalPanel("input.fixLower == 0", {actionButton("fixLower", "Fix lower range")})
)

server <- function(input, output, session) {

  fixedLower <- reactiveVal()

  observeEvent(input$fixLower, {
    fixedLower(min(isolate({input$years})))
  }, once = TRUE)

  output$Slider_UI <- renderUI({
    if(is.null(fixedLower())){
      sliderInput(inputId = "years",
                  label="Choose base year and target year",
                  min = 2015,
                  max = 2060,
                  value = c(2015, 2060),
                  step = 1,
                  sep = ""
      )
    } else {
      sliderInput(inputId = "years",
                  label="Choose base year and target year",
                  min = fixedLower(),
                  max = 2060,
                  value = fixedLower(),
                  step = 1,
                  sep = ""
      )
    }
  })

}

shinyApp(ui = ui, server = server)

As suggested by @DSGym, here is an example using renderUI:

library(shiny)

ui <- fluidPage(
  radioButtons(inputId="type_select", label="Select slider type", choices = list("Standard slider", "Range slider")),
  uiOutput("Slider_UI")
)

server <- function(input, output, session) {

  output$Slider_UI <- renderUI({
    req(input$type_select)

    if(input$type_select == "Range slider"){
      presetValues <- c(2015, 2060)
    } else {
      presetValues <- 2060
    }

    sliderInput(inputId = "years",
                label="Choose base year and target year",
                min = 2015,
                max = 2060,
                value = presetValues,
                step = 1,
                sep = ""
    )
  })

}

shinyApp(ui = ui, server = server)


问:
Matplotlib plot bad resolution when zooming
I am using matplotlib to plot hierarchical clusterings,

```<code>import numpy as np

from scipy.cluster.hierarchy import dendrogram

def plot_dendrogram(model, **kwargs):

    # Children of hierarchical clustering
    children = model.children_

    # Distances between each pair of children
    # Since we don't have this information, we can use a uniform one for plotting
    distance = np.arange(children.shape[0])

    # The number of observations contained in each cluster level
    no_of_observations = np.arange(2, children.shape[0]+2)

    # Create linkage matrix and then plot the dendrogram
    linkage_matrix = np.column_stack([children, distance, no_of_observations]).astype(float)

    # Plot the corresponding dendrogram
    dendrogram(linkage_matrix, **kwargs)

plt.figure(figsize=(100,100))
plt.title('Hierarchical Clustering Dendrogram')
plot_dendrogram(clustering, labels=liste_tags)
plt.show()
</code>```

I can visualize the hierarchical clustering, however the quality and resolution of the plot is not good. Especially, when I save the plot and try to zoom in to see the labels for example.

I get this image :

答: MatPlotLib Figure object has a dpi parameter. Just set something like plt.figure(figsize=(100,100), dpi=300).

Body:
I am using matplotlib to plot hierarchical clusterings,

import numpy as np

from scipy.cluster.hierarchy import dendrogram

def plot_dendrogram(model, **kwargs):

    # Children of hierarchical clustering
    children = model.children_

    # Distances between each pair of children
    # Since we don't have this information, we can use a uniform one for plotting
    distance = np.arange(children.shape[0])

    # The number of observations contained in each cluster level
    no_of_observations = np.arange(2, children.shape[0]+2)

    # Create linkage matrix and then plot the dendrogram
    linkage_matrix = np.column_stack([children, distance, no_of_observations]).astype(float)

    # Plot the corresponding dendrogram
    dendrogram(linkage_matrix, **kwargs)

plt.figure(figsize=(100,100))
plt.title('Hierarchical Clustering Dendrogram')
plot_dendrogram(clustering, labels=liste_tags)
plt.show()

I can visualize the hierarchical clustering, however the quality and resolution of the plot is not good. Especially, when I save the plot and try to zoom in to see the labels for example.

I get this image :