问:
This AsyncTask class should be static or leaks might occur
I have created as asynctask inside a method. But it shows a warning.

This AsyncTask class should be static or leaks might occur (anonymous
  android.os.AsyncTask) less.. A static field will leak contexts.
  Non-static inner classes have an implicit reference to their outer
  class. If that outer class is for example a Fragment or Activity, then
  this reference means that the long-running handler/loader/task will
  hold a reference to the activity which prevents it from getting
  garbage collected.  Similarly, direct field references to activities
  and fragments from these longer running instances can cause leaks.
  ViewModel classes should never point to Views or non-application
  Contexts.

How can I avoid this warning?

答:
what this warning is trying to say, is that you can leak your activity/fragment in your phones memory.

if you create an AsyncTask that uses an activity context or the AsyncTask is and child of the activity/fragment, you keep the activity/fragment in memory. When you rotate or change activity in the periode the AsyncTask is running. you keep your old activity instance in memory and are leaking the activity/fragment.


来源: stackexchange

问:
How to figure out used JARs?
In a bigger project we might be using tons of JARs. How do I find out which JARs are being used by a certain module/package in the project (not the whole project). Any tool, technique,  etc?</p>

答:
The various code obfuscators, reducers etc can help.

Check out:

http://jarg.sourceforge.net/

http://www.alphaworks.ibm.com/tech/jax/

http://www.e-t.com/jshrink.html

http://proguard.sourceforge.net/

http://www.fightingquaker.com/jaropt/

btw, this list was acquired from http://www.javakb.com/Uwe/Forum.aspx/java-programmer/34365/How-to-create-a-JAR-that-contains-only-the-class-files

ContentLicense: CC BY-SA 2.5

Title: How to figure out used JARs?


问:
Generate a heatmap using a scatter data set
I have a set of X,Y data points (about 10k) that are easy to plot as a scatter plot but that I would like to represent as a heatmap.

I looked through the examples in Matplotlib and they all seem to already start with heatmap cell values to generate the image.

Is there a method that converts a bunch of x, y, all different, to a heatmap (where zones with higher frequency of x, y would be "warmer")?

答:
Edit: For a better approximation of Alejandro's answer, see below.

I know this is an old question, but wanted to add something to Alejandro's anwser: If you want a nice smoothed image without using py-sphviewer you can instead use np.histogram2d and apply a gaussian filter (from scipy.ndimage.filters) to the heatmap:

```<code>import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from scipy.ndimage.filters import gaussian_filter

def myplot(x, y, s, bins=1000):
    heatmap, xedges, yedges = np.histogram2d(x, y, bins=bins)
    heatmap = gaussian_filter(heatmap, sigma=s)

    extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
    return heatmap.T, extent

fig, axs = plt.subplots(2, 2)

# Generate some test data
x = np.random.randn(1000)
y = np.random.randn(1000)

sigmas = [0, 16, 32, 64]

for ax, s in zip(axs.flatten(), sigmas):
    if s == 0:
        ax.plot(x, y, 'k.', markersize=5)
        ax.set_title("Scatter plot")
    else:
        img, extent = myplot(x, y, s)
        ax.imshow(img, extent=extent, origin='lower', cmap=cm.jet)
        ax.set_title("Smoothing with  $\sigma$ = %d" % s)

plt.show()
</code>```

Produces:

The scatter plot and s=16 plotted on top of eachother for Agape Gal'lo (click for better view):

One difference I noticed with my gaussian filter approach and Alejandro's  approach was that his method shows local structures much better than mine. Therefore I implemented a simple nearest neighbour method at pixel level. This method calculates for each pixel the inverse sum of the distances of the n closest points in the data. This method is at a high resolution pretty computationally expensive and I think there's a quicker way, so let me know if you have any improvements.

Update: As I suspected, there's a much faster method using Scipy's scipy.cKDTree. See Gabriel's answer for the implementation.

Anyway, here's my code:

```<code>import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

def data_coord2view_coord(p, vlen, pmin, pmax):
    dp = pmax - pmin
    dv = (p - pmin) / dp * vlen
    return dv

def nearest_neighbours(xs, ys, reso, n_neighbours):
    im = np.zeros([reso, reso])
    extent = [np.min(xs), np.max(xs), np.min(ys), np.max(ys)]

    xv = data_coord2view_coord(xs, reso, extent[0], extent[1])
    yv = data_coord2view_coord(ys, reso, extent[2], extent[3])
    for x in range(reso):
        for y in range(reso):
            xp = (xv - x)
            yp = (yv - y)

            d = np.sqrt(xp**2 + yp**2)

            im[y][x] = 1 / np.sum(d[np.argpartition(d.ravel(), n_neighbours)[:n_neighbours]])

    return im, extent

n = 1000
xs = np.random.randn(n)
ys = np.random.randn(n)
resolution = 250

fig, axes = plt.subplots(2, 2)

for ax, neighbours in zip(axes.flatten(), [0, 16, 32, 64]):
    if neighbours == 0:
        ax.plot(xs, ys, 'k.', markersize=2)
        ax.set_aspect('equal')
        ax.set_title("Scatter Plot")
    else:
        im, extent = nearest_neighbours(xs, ys, resolution, neighbours)
        ax.imshow(im, origin='lower', extent=extent, cmap=cm.jet)
        ax.set_title("Smoothing over %d neighbours" % neighbours)
        ax.set_xlim(extent[0], extent[1])
        ax.set_ylim(extent[2], extent[3])
plt.show()
</code>```

Result:

ContentLicense: CC BY-SA 4.0

Body:
Edit: For a better approximation of Alejandro's answer, see below.

I know this is an old question, but wanted to add something to Alejandro's anwser: If you want a nice smoothed image without using py-sphviewer you can instead use np.histogram2d and apply a gaussian filter (from scipy.ndimage.filters) to the heatmap:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from scipy.ndimage.filters import gaussian_filter

def myplot(x, y, s, bins=1000):
    heatmap, xedges, yedges = np.histogram2d(x, y, bins=bins)
    heatmap = gaussian_filter(heatmap, sigma=s)

    extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
    return heatmap.T, extent

fig, axs = plt.subplots(2, 2)

# Generate some test data
x = np.random.randn(1000)
y = np.random.randn(1000)

sigmas = [0, 16, 32, 64]

for ax, s in zip(axs.flatten(), sigmas):
    if s == 0:
        ax.plot(x, y, 'k.', markersize=5)
        ax.set_title("Scatter plot")
    else:
        img, extent = myplot(x, y, s)
        ax.imshow(img, extent=extent, origin='lower', cmap=cm.jet)
        ax.set_title("Smoothing with  $\sigma$ = %d" % s)

plt.show()

Produces:

The scatter plot and s=16 plotted on top of eachother for Agape Gal'lo (click for better view):

One difference I noticed with my gaussian filter approach and Alejandro's  approach was that his method shows local structures much better than mine. Therefore I implemented a simple nearest neighbour method at pixel level. This method calculates for each pixel the inverse sum of the distances of the n closest points in the data. This method is at a high resolution pretty computationally expensive and I think there's a quicker way, so let me know if you have any improvements.

Update: As I suspected, there's a much faster method using Scipy's scipy.cKDTree. See Gabriel's answer for the implementation.

Anyway, here's my code:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

def data_coord2view_coord(p, vlen, pmin, pmax):
    dp = pmax - pmin
    dv = (p - pmin) / dp * vlen
    return dv

def nearest_neighbours(xs, ys, reso, n_neighbours):
    im = np.zeros([reso, reso])
    extent = [np.min(xs), np.max(xs), np.min(ys), np.max(ys)]

    xv = data_coord2view_coord(xs, reso, extent[0], extent[1])
    yv = data_coord2view_coord(ys, reso, extent[2], extent[3])
    for x in range(reso):
        for y in range(reso):
            xp = (xv - x)
            yp = (yv - y)

            d = np.sqrt(xp**2 + yp**2)

            im[y][x] = 1 / np.sum(d[np.argpartition(d.ravel(), n_neighbours)[:n_neighbours]])

    return im, extent

n = 1000
xs = np.random.randn(n)
ys = np.random.randn(n)
resolution = 250

fig, axes = plt.subplots(2, 2)

for ax, neighbours in zip(axes.flatten(), [0, 16, 32, 64]):
    if neighbours == 0:
        ax.plot(xs, ys, 'k.', markersize=2)
        ax.set_aspect('equal')
        ax.set_title("Scatter Plot")
    else:
        im, extent = nearest_neighbours(xs, ys, resolution, neighbours)
        ax.imshow(im, origin='lower', extent=extent, cmap=cm.jet)
        ax.set_title("Smoothing over %d neighbours" % neighbours)
        ax.set_xlim(extent[0], extent[1])
        ax.set_ylim(extent[2], extent[3])
plt.show()

Result:


问:
Cannot Open Project the length of the project file exceeds the limit of 100 characters
I am using Android Studio 3.1 Preview and get an error when I attempt to import some projects:

Cannot Open Project
The length of the project file exceeds the limit of 100 characters

This error occurs whether I am importing cloned projects from github or old projects I have saved with Eclipse. Is there a way to import these projects?

答:
I was facing same problem with Android Studio 3.1 in Linux. After some tests I concluded that it considers the FULL path as the project length (from /home/ to Project root), which cant exceed 100 characters.

This warning message is also shown when you try to create a new project.

I didn't found where to customize it. So, as a workaround, check if you can reduce this full path length.

Best Regards

ContentLicense: CC BY-SA 3.0


问:
Room : LiveData from Dao will trigger Observer.onChanged on every Update, even if the LiveData value has no change
I found that the LiveData returned by Dao will call its observer whenever the row is updated in DB, even if the LiveData value is obviously not changed.

Consider a situation like the following example :

Example entity

```<code>@Entity
public class User {
    public long id;
    public String name;
    // example for other variables
    public Date lastActiveDateTime;
}
</code>```

Example Dao

```<code>@Dao
public interface UserDao {
    // I am only interested in the user name
    @Query("SELECT name From User")
    LiveData<List<String>> getAllNamesOfUser();

    @Update(onConflict = OnConflictStrategy.REPLACE)
    void updateUser(User user);
}
</code>```

Somewhere in background thread

```<code>UserDao userDao = //.... getting the dao
User user = // obtain from dao....
user.lastActiveDateTime = new Date(); // no change to user.name
userDao.updateUser(user);
</code>```

Somewhere in UI

```<code>// omitted ViewModel for simplicity
userDao.getAllNamesOfUser().observe(this, new Observer<List<String>> {
    @Override
    public void onChanged(@Nullable List<String> userNames) {
        // this will be called whenever the background thread called updateUser.
        // If user.name is not changed, it will be called with userNames
        // with the same value again and again when lastActiveDateTime changed.
    }
});
</code>```

In this example, the ui is only interested to user name so the query for LiveData only includes the name field. However the observer.onChanged will still be called on Dao Update even only other fields are updated.
(In fact, if I do not make any change to User entity and call UserDao.updateUser, the observer.onChanged will still be called)

Is this the designed behaviour of Dao LiveData in Room? Is there any chance I can work around this, so that the observer will only be called when the selected field is updated?

Edit : I changed to use the following query to update the lastActiveDateTime value as KuLdip PaTel in comment suggest. The observer of LiveData of user name is still called.

```<code>@Query("UPDATE User set lastActiveDateTime = :lastActiveDateTime where id = :id")
void updateLastActiveDateTime(Date lastActiveDateTime, int id);
</code>```

答:
This situation is known as false positive notification of observer.
Please check point number 7 mentioned in the link to avoid such issue.

Below example is written in kotlin but you can use its java version to get it work.

```<code>fun <T> LiveData<T>.getDistinct(): LiveData<T> {
    val distinctLiveData = MediatorLiveData<T>()
    distinctLiveData.addSource(this, object : Observer<T> {
        private var initialized = false
        private var lastObj: T? = null
        override fun onChanged(obj: T?) {
            if (!initialized) {
                initialized = true
                lastObj = obj
                distinctLiveData.postValue(lastObj)
            } else if ((obj == null && lastObj != null)
                       || obj != lastObj) {
                lastObj = obj
                distinctLiveData.postValue(lastObj)
            }
        }
    })
    return distinctLiveData
}
</code>```

Body:
I found that the LiveData returned by Dao will call its observer whenever the row is updated in DB, even if the LiveData value is obviously not changed.

Consider a situation like the following example :

Example entity

@Entity
public class User {
    public long id;
    public String name;
    // example for other variables
    public Date lastActiveDateTime;
}

Example Dao

@Dao
public interface UserDao {
    // I am only interested in the user name
    @Query("SELECT name From User")
    LiveData<List<String>> getAllNamesOfUser();

    @Update(onConflict = OnConflictStrategy.REPLACE)
    void updateUser(User user);
}

Somewhere in background thread

UserDao userDao = //.... getting the dao
User user = // obtain from dao....
user.lastActiveDateTime = new Date(); // no change to user.name
userDao.updateUser(user);

Somewhere in UI

// omitted ViewModel for simplicity
userDao.getAllNamesOfUser().observe(this, new Observer<List<String>> {
    @Override
    public void onChanged(@Nullable List<String> userNames) {
        // this will be called whenever the background thread called updateUser.
        // If user.name is not changed, it will be called with userNames
        // with the same value again and again when lastActiveDateTime changed.
    }
});

In this example, the ui is only interested to user name so the query for LiveData only includes the name field. However the observer.onChanged will still be called on Dao Update even only other fields are updated.
(In fact, if I do not make any change to User entity and call UserDao.updateUser, the observer.onChanged will still be called)

Is this the designed behaviour of Dao LiveData in Room? Is there any chance I can work around this, so that the observer will only be called when the selected field is updated?

Edit : I changed to use the following query to update the lastActiveDateTime value as KuLdip PaTel in comment suggest. The observer of LiveData of user name is still called.

@Query("UPDATE User set lastActiveDateTime = :lastActiveDateTime where id = :id")
void updateLastActiveDateTime(Date lastActiveDateTime, int id);

Body:
This situation is known as false positive notification of observer.
Please check point number 7 mentioned in the link to avoid such issue.

Below example is written in kotlin but you can use its java version to get it work.

fun <T> LiveData<T>.getDistinct(): LiveData<T> {
    val distinctLiveData = MediatorLiveData<T>()
    distinctLiveData.addSource(this, object : Observer<T> {
        private var initialized = false
        private var lastObj: T? = null
        override fun onChanged(obj: T?) {
            if (!initialized) {
                initialized = true
                lastObj = obj
                distinctLiveData.postValue(lastObj)
            } else if ((obj == null && lastObj != null)
                       || obj != lastObj) {
                lastObj = obj
                distinctLiveData.postValue(lastObj)
            }
        }
    })
    return distinctLiveData
}


问:
XSLT copy and sort transform throwing exception in XslCompiledTransform
I have an XSLT transform that is supposed to copy and sort by name - so no content is lost, but the output is sorted alphabetically.
I'm doing this in C# using the XslCompiledTransform.Transform method.

Example input:

```<code><root>
    <test val="1"/>
    <alpha val="2"/>
    <gamma Order="C">
        <aSubGamma val="3"/>
        <cSubGamma val="4"/>
        <bSubGamma val="5"/>
    </gamma>
    <gamma Order="B">
        <aSubGamma val="6"/>
        <cSubGamma val="7"/>
        <bSubGamma val="8"/>
    </gamma>
    <beta val="9"/>
</root>
</code>```

What I'd like to get in the output:

```<code><root>
    <alpha val="2"/>
    <beta val="9"/>
    <gamma Order="B">
        <aSubGamma val="6"/>
        <bSubGamma val="8"/>
        <cSubGamma val="7"/>
    </gamma>
    <gamma Order="C">
        <aSubGamma val="3"/>
        <bSubGamma val="5"/>
        <cSubGamma val="4"/>
    </gamma>
    <test val="1"/>
</root>
</code>```

My XSLT is so far very simple and somewhat right:

```<code><xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
      <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*">
        <xsl:sort select="name()"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
</code>```

If I use it on XML where I have removed the Order attributes on the gamma elements, everything is good.
However, with those attributes present I get the following exception:

Attribute and namespace nodes cannot be added to the parent element after a text, comment, pi, or sub-element node has already been added.

I've tried many variations on my XSLT but they either produce no output or aren't even valid XSLT!

Is my goal achievable, and if it is what XSLT would achieve my desired results?

答:
Don't process attributes and child nodes together, so change

```<code>  <xsl:template match="node()|@*">
<xsl:copy>
  <xsl:apply-templates select="node()|@*">
    <xsl:sort select="name()"/>
  </xsl:apply-templates>
</xsl:copy>
</code>```

to

```<code><xsl:template match="*">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates>
      <xsl:sort select="local-name()"/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

<xsl:template match="@* | text() | processing-instruction() | comment()">
  <xsl:copy/>
</xsl:template>
</code>```

Note also that XslCompiledTransform is an XSLT 1 processor so using version="1.0" on the stylesheet is usually a better idea as otherwise you get forwards compatible processing which might not alert you to the use of unsupported features.

Body:
I have an XSLT transform that is supposed to copy and sort by name - so no content is lost, but the output is sorted alphabetically.
I'm doing this in C# using the XslCompiledTransform.Transform method.

Example input:

<root>
    <test val="1"/>
    <alpha val="2"/>
    <gamma Order="C">
        <aSubGamma val="3"/>
        <cSubGamma val="4"/>
        <bSubGamma val="5"/>
    </gamma>
    <gamma Order="B">
        <aSubGamma val="6"/>
        <cSubGamma val="7"/>
        <bSubGamma val="8"/>
    </gamma>
    <beta val="9"/>
</root>

What I'd like to get in the output:

<root>
    <alpha val="2"/>
    <beta val="9"/>
    <gamma Order="B">
        <aSubGamma val="6"/>
        <bSubGamma val="8"/>
        <cSubGamma val="7"/>
    </gamma>
    <gamma Order="C">
        <aSubGamma val="3"/>
        <bSubGamma val="5"/>
        <cSubGamma val="4"/>
    </gamma>
    <test val="1"/>
</root>

My XSLT is so far very simple and somewhat right:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
      <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*">
        <xsl:sort select="name()"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

If I use it on XML where I have removed the Order attributes on the gamma elements, everything is good.
However, with those attributes present I get the following exception:

Attribute and namespace nodes cannot be added to the parent element after a text, comment, pi, or sub-element node has already been added.

I've tried many variations on my XSLT but they either produce no output or aren't even valid XSLT!

Is my goal achievable, and if it is what XSLT would achieve my desired results?

Body:
Don't process attributes and child nodes together, so change

  <xsl:template match="node()|@*">
<xsl:copy>
  <xsl:apply-templates select="node()|@*">
    <xsl:sort select="name()"/>
  </xsl:apply-templates>
</xsl:copy>

to

<xsl:template match="*">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates>
      <xsl:sort select="local-name()"/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

<xsl:template match="@* | text() | processing-instruction() | comment()">
  <xsl:copy/>
</xsl:template>

Note also that XslCompiledTransform is an XSLT 1 processor so using version="1.0" on the stylesheet is usually a better idea as otherwise you get forwards compatible processing which might not alert you to the use of unsupported features.


问:
Most performant way to filter a huge List in C#?
We have an ASP.NET MVC web application that is hooked up to a SQL Server DB via Entity Framework. One of the main tasks of this application is to allow users to quickly search and filter a huge database table that holds archive values.

The table structure is quite simple: Timestamp (DateTime), StationId (int), DatapointId (int), Value (double). This table holds somewhat between 10 to 100 million rows. I optimized the DB table with a covering index etc., but the user experience was still quite laggy when filtering by DatapointId, StationId, Time and Skipping and Taking only the portion I want to show on the page.

So I tried a different approach: as our server has a lot of RAM, I thought that we could simply load the whole archive table into a List<ArchiveRow> when the web app starts up, then simply get the data directly from this List instead of doing a round-trip to the database. This works quite well, it takes about 9 seconds to load the whole archive table (with currently about 10 million entries) into the List. The ArchiveRow is a simple object that looks like this:

```<code>public class ArchiveResponse {
    public  int Length { get; set; }
    public int numShown { get; set; }
    public  int numFound { get; set; }
    public  int numTotal { get; set; }
    public  List<ArchiveRow> Rows { get; set; }
}
</code>```

and accordingly:

```<code>public class ArchiveRow {
    public  int s { get; set; }
    public  int d { get; set; }
    public  DateTime t { get; set; }
    public  double v { get; set; }
}
</code>```

When I now try to get the desired data from the List with a Linq query, it is already faster the querying the DB, but it is still quite slow when filtering by multiple criteria. For example, when I filter by one StationId and 12 DatapointIds, it takes about 5 seconds to retrieve a window of 25 rows. I already switched from filtering with Where to using joins, but I think there is still room for improvement. Are there maybe better ways to implement such a caching mechanism while keeping memory consumption as low as possible? Are there other collection types that are better suited for this purpose?

So here is the code that filters and fetches the relevant data from the ArchiveCache list:

```<code>// Total number of entries in archive cache
var numTotal = ArchiveCache.Count();

// Initial Linq query
ParallelQuery<ArchiveCacheValue> query = ArchiveCache.AsParallel();

// The request may contain StationIds that the user is interested in,
// so here's the filtering by StationIds with a join:
if (request.StationIds.Count > 0)
{
    query = from a in ArchiveCache.AsParallel()
             join b in request.StationIds.AsParallel()
             on a.StationId equals b
             select a;
}

// The request may contain DatapointIds that the user is interested in,
// so here's the filtering by DatapointIds with a join:
if (request.DatapointIds.Count > 0)
{
    query = from a in query.AsParallel()
             join b in request.DatapointIds.AsParallel()
             on a.DataPointId equals b
             select a;
}

// Number of matching entries after filtering and before windowing
int numFound = query.Count();

// Pagination: Select only the current window that needs to be shown on the page
var result = query.Skip(request.Start == 0 ? 0 : request.Start - 1).Take(request.Length);

// Number of entries on the current page that will be shown
int numShown = result.Count();

// Build a response object, serialize it to Json and return to client
// Note: The projection with the Rows is not a bottleneck, it is only done to
// shorten 'StationId' to 's' etc. At this point there are only 25 to 50 rows,
// so that is no problem and happens in way less than 1 ms
ArchiveResponse myResponse = new ArchiveResponse();
myResponse.Length = request.Length;
myResponse.numShown = numShown;
myResponse.numFound = numFound;
myResponse.numTotal = numTotal;
myResponse.Rows = result.Select(x => new archRow() { s = x.StationId, d = x.DataPointId, t = x.DateValue, v = x.Value }).ToList();

return JsonSerializer.ToJsonString(myResponse);
</code>```

Some more details: the number of stations is usually something between 5 to 50, rarely more than 50. The number of datapoints is <7000. The web application is set to 64 bit with <gcAllowVeryLargeObjects enabled="true" /> set in the web.config.

I'm really looking forward for further improvements and recommendations. Maybe there's a completely different approach based on arrays or similar, that performs way better without linq?

答:
You can adjust your storage to this specific query type. First, create dictionaries from your in-memory archive:

```<code>ArchiveCacheByDatapoint = ArchiveCache.GroupBy(c => c.DataPointId)
            .ToDictionary(c => c.Key, c => c.ToList());
ArchiveCacheByStation = ArchiveCache.GroupBy(c => c.StationId)
            .ToDictionary(c => c.Key, c => c.ToList());
</code>```

Then use those dictionaries in your query:

```<code>bool hasStations = request.StationIds.Length > 0;
bool hasDatapoints = request.DatapointIds.Length > 0;
int numFound = 0;
List<ArchiveCacheValue> result;
if (hasDatapoints && hasStations) {
    // special case - filter by both
    result = new List<ArchiveCacheValue>();
    // store station filter in hash set
    var stationsFilter = new HashSet<int>(request.StationIds);
    // first filter by datapoints, because you have more different datapoints than stations
    foreach (var datapointId in request.DatapointIds.OrderBy(c => c)) {
        foreach (var item in ArchiveCacheByDatapoint[datapointId]) {
            if (stationsFilter.Contains(item.StationId)) {
                // both datapoint and station matches filter - found item
                numFound++;
                if (numFound >= request.Start && result.Count < request.Length) {
                    // add to result list if matches paging criteria
                    result.Add(item);
                }
            }
        }
    }
}
else if (hasDatapoints) {
    var query = Enumerable.Empty<ArchiveCacheValue>();
    foreach (var datapoint in request.DatapointIds.OrderBy(c => c))
    {
        var list = ArchiveCacheByDatapoint[datapoint];
        numFound += list.Count;
        query = query.Concat(list);
    }
    // execute query just once
    result = query.Skip(request.Start).Take(request.Length).ToList();
}
else if (hasStations) {
    var query = Enumerable.Empty<ArchiveCacheValue>();
    foreach (var station in request.StationIds.OrderBy(c => c))
    {
        var list = ArchiveCacheByStation[station];
        numFound += list.Count;
        query = query.Concat(list);
    }
    // execute query just once
    result = query.Skip(request.Start).Take(request.Length).ToList();
}
else {
    // no need to do Count()
    numFound = ArchiveCache.Count;
    // no need to Skip\Take here really, ArchiveCache is list\array
    // so you can use indexes which will be faster
    result = ArchiveCache.Skip(request.Start).Take(request.Length).ToList();
}

// Number of entries on the current page that will be shown
int numShown = result.Count;
</code>```

I've measured it and on my machine it runs in 1ms (sometimes up to 10ms) for all types of queries I tried (by sections only, by datapoints only, by both sections and datapoints), for 100 million items.

Body:
We have an ASP.NET MVC web application that is hooked up to a SQL Server DB via Entity Framework. One of the main tasks of this application is to allow users to quickly search and filter a huge database table that holds archive values.

The table structure is quite simple: Timestamp (DateTime), StationId (int), DatapointId (int), Value (double). This table holds somewhat between 10 to 100 million rows. I optimized the DB table with a covering index etc., but the user experience was still quite laggy when filtering by DatapointId, StationId, Time and Skipping and Taking only the portion I want to show on the page.

So I tried a different approach: as our server has a lot of RAM, I thought that we could simply load the whole archive table into a List<ArchiveRow> when the web app starts up, then simply get the data directly from this List instead of doing a round-trip to the database. This works quite well, it takes about 9 seconds to load the whole archive table (with currently about 10 million entries) into the List. The ArchiveRow is a simple object that looks like this:

public class ArchiveResponse {
    public  int Length { get; set; }
    public int numShown { get; set; }
    public  int numFound { get; set; }
    public  int numTotal { get; set; }
    public  List<ArchiveRow> Rows { get; set; }
}

and accordingly:

public class ArchiveRow {
    public  int s { get; set; }
    public  int d { get; set; }
    public  DateTime t { get; set; }
    public  double v { get; set; }
}

When I now try to get the desired data from the List with a Linq query, it is already faster the querying the DB, but it is still quite slow when filtering by multiple criteria. For example, when I filter by one StationId and 12 DatapointIds, it takes about 5 seconds to retrieve a window of 25 rows. I already switched from filtering with Where to using joins, but I think there is still room for improvement. Are there maybe better ways to implement such a caching mechanism while keeping memory consumption as low as possible? Are there other collection types that are better suited for this purpose?

So here is the code that filters and fetches the relevant data from the ArchiveCache list:

// Total number of entries in archive cache
var numTotal = ArchiveCache.Count();

// Initial Linq query
ParallelQuery<ArchiveCacheValue> query = ArchiveCache.AsParallel();

// The request may contain StationIds that the user is interested in,
// so here's the filtering by StationIds with a join:
if (request.StationIds.Count > 0)
{
    query = from a in ArchiveCache.AsParallel()
             join b in request.StationIds.AsParallel()
             on a.StationId equals b
             select a;
}

// The request may contain DatapointIds that the user is interested in,
// so here's the filtering by DatapointIds with a join:
if (request.DatapointIds.Count > 0)
{
    query = from a in query.AsParallel()
             join b in request.DatapointIds.AsParallel()
             on a.DataPointId equals b
             select a;
}

// Number of matching entries after filtering and before windowing
int numFound = query.Count();

// Pagination: Select only the current window that needs to be shown on the page
var result = query.Skip(request.Start == 0 ? 0 : request.Start - 1).Take(request.Length);

// Number of entries on the current page that will be shown
int numShown = result.Count();

// Build a response object, serialize it to Json and return to client
// Note: The projection with the Rows is not a bottleneck, it is only done to
// shorten 'StationId' to 's' etc. At this point there are only 25 to 50 rows,
// so that is no problem and happens in way less than 1 ms
ArchiveResponse myResponse = new ArchiveResponse();
myResponse.Length = request.Length;
myResponse.numShown = numShown;
myResponse.numFound = numFound;
myResponse.numTotal = numTotal;
myResponse.Rows = result.Select(x => new archRow() { s = x.StationId, d = x.DataPointId, t = x.DateValue, v = x.Value }).ToList();

return JsonSerializer.ToJsonString(myResponse);

Some more details: the number of stations is usually something between 5 to 50, rarely more than 50. The number of datapoints is <7000. The web application is set to 64 bit with <gcAllowVeryLargeObjects enabled="true" /> set in the web.config.

I'm really looking forward for further improvements and recommendations. Maybe there's a completely different approach based on arrays or similar, that performs way better without linq?

Body:
You can adjust your storage to this specific query type. First, create dictionaries from your in-memory archive:

ArchiveCacheByDatapoint = ArchiveCache.GroupBy(c => c.DataPointId)
            .ToDictionary(c => c.Key, c => c.ToList());
ArchiveCacheByStation = ArchiveCache.GroupBy(c => c.StationId)
            .ToDictionary(c => c.Key, c => c.ToList());

Then use those dictionaries in your query:

bool hasStations = request.StationIds.Length > 0;
bool hasDatapoints = request.DatapointIds.Length > 0;
int numFound = 0;
List<ArchiveCacheValue> result;
if (hasDatapoints && hasStations) {
    // special case - filter by both
    result = new List<ArchiveCacheValue>();
    // store station filter in hash set
    var stationsFilter = new HashSet<int>(request.StationIds);
    // first filter by datapoints, because you have more different datapoints than stations
    foreach (var datapointId in request.DatapointIds.OrderBy(c => c)) {
        foreach (var item in ArchiveCacheByDatapoint[datapointId]) {
            if (stationsFilter.Contains(item.StationId)) {
                // both datapoint and station matches filter - found item
                numFound++;
                if (numFound >= request.Start && result.Count < request.Length) {
                    // add to result list if matches paging criteria
                    result.Add(item);
                }
            }
        }
    }
}
else if (hasDatapoints) {
    var query = Enumerable.Empty<ArchiveCacheValue>();
    foreach (var datapoint in request.DatapointIds.OrderBy(c => c))
    {
        var list = ArchiveCacheByDatapoint[datapoint];
        numFound += list.Count;
        query = query.Concat(list);
    }
    // execute query just once
    result = query.Skip(request.Start).Take(request.Length).ToList();
}
else if (hasStations) {
    var query = Enumerable.Empty<ArchiveCacheValue>();
    foreach (var station in request.StationIds.OrderBy(c => c))
    {
        var list = ArchiveCacheByStation[station];
        numFound += list.Count;
        query = query.Concat(list);
    }
    // execute query just once
    result = query.Skip(request.Start).Take(request.Length).ToList();
}
else {
    // no need to do Count()
    numFound = ArchiveCache.Count;
    // no need to Skip\Take here really, ArchiveCache is list\array
    // so you can use indexes which will be faster
    result = ArchiveCache.Skip(request.Start).Take(request.Length).ToList();
}

// Number of entries on the current page that will be shown
int numShown = result.Count;

I've measured it and on my machine it runs in 1ms (sometimes up to 10ms) for all types of queries I tried (by sections only, by datapoints only, by both sections and datapoints), for 100 million items.


问:
Delete first two characters of elements in a list of lists
Let's say I have the following list of lists:

```<code>final_list = [[1,'pppizza',3,4],[1,'mmmonkey',9,10],[1,'dddoublerainbow',8,2]]
</code>```

Now I need to remove the first 2 characters of the second element of every list, so the result will be:

```<code>final_list = [[1,'pizza',3,4],[1,'monkey',9,10],[1,'doublerainbow',8,2]]
</code>```

答:
No need to rebuild the entire list from scratch just to do it in a one-line comprehension while unnecessarily iterating the inner lists. Just modify the elements that need modifying:</p>

```<code>for lst in final_list:
    lst[1] = lst[1][2:]
</code>```

Body:
Let's say I have the following list of lists:

final_list = [[1,'pppizza',3,4],[1,'mmmonkey',9,10],[1,'dddoublerainbow',8,2]]

Now I need to remove the first 2 characters of the second element of every list, so the result will be:

final_list = [[1,'pizza',3,4],[1,'monkey',9,10],[1,'doublerainbow',8,2]]

Body:
No need to rebuild the entire list from scratch just to do it in a one-line comprehension while unnecessarily iterating the inner lists. Just modify the elements that need modifying:

for lst in final_list:
    lst[1] = lst[1][2:]


问:
regex url to image in php
I am making a profile page where users can set an url to their profile image. How do I check this with regex for example?</p>

答: You can't. Any file can be served at any address. You'd need to check the Content-Type returned by the URL, and probably the format of the image too.</p>

Title: regex url to image in php


问:
What would be the exact flow of actions if I click a submit button inside a view?
What would be the exact flow or order of actions and rendering if I click my submit button which calls a javascript function and posts to a controller?

```<code>@section impScripts
     {

            <script type="text/javascript">

                function save() {

                    var u = {
                        UserName: document.getElementById('UserName').value,
                        Password: document.getElementById('Password').value
                    }

                    var users = JSON.stringify(u);

                    $.ajax
                    ({
                        url: '@Url.Action("Authenticate", "Users")',
                        type: 'POST',
                        contentType: "application/json; charset= utf-8",
                        data: users,
                        dataType: 'json',
                        success: function (results) {

                        }
                    });
                };

            </script>
}
</code>```

Button:

```<code><input type="submit" onclick="save();" value="Login" class="btn btn-default" />
</code>```

View:

```<code>@model HimHer.Models.Stories

@{
    ViewBag.Title = "Authenticate";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<script type="text/javascript">

</script>

@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Stories</h4>
    <hr />

    <div>
        <div class="form-group">
            @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.UserName, new { @class = "form-control white" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.Password, new { @class = "form-control white" })
            </div>
        </div>

Controller:

[HttpGet]
        public ActionResult Authenticate()
        {

            return View();
        }

        [HttpPost]
        public JsonResult Authenticate(Stories u)
        {
            if (basicOps.getUsersLogin(u.UserName, u.Password))
            {
               u.UserID = 90099;
               //return RedirectToAction("GetImagesStories", "Stories", u);
               //return View("Authenticate");
               return Json(u);

            }

            //return View("Authenticate");
            return Json(u);

        }
</code>```

Now I don't understand the exact flow after I click the submit button. I tried searching and doing it on my own but couldn't find the concrete knowledge about it. Help me with it.

答:
Each component of MVC interacts with the other. Notice in the diagram below that:

The view sends HTTP requests (POST, GET etc.) to the controller. Controller then updates the model.

Now that the model has been updated, the view can use this model to render the form with the updated data.

How does this compare to what you are doing?

In your view, you pass the form values (mapped to your model) to the server (controller) via AJAX. Note that you can also do this using the @html.beginForm() method instead of AJAX.

When the request arrives server side it knows to check the Users controller as thats where you told it to look, it will also discover two instances of the Authenticate method (one of which is overloaded):

```<code>//notice the [HttpGet] attribute, tells the server to get the view
[HttpGet]
public ActionResult Authenticate(){return View();}

//[HttpPost] tells it to use the overloaded method to accept a parameter (usually a routeValueDictionary object)
[HttpPost]
public JsonResult Authenticate(Stories u){}
</code>```

Using a JsonResult return type for the method you are expecting to return a JsonResult instead of the view itself. From what you are saying you need to return an ActionResult instead returning the view with the updated Model instead, something like this (although I dont know what your model looks like):

```<code>[HttpPost]
public ActionResult Authenticate(Stories u)
{
    if (basicOps.getUsersLogin(u.UserName, u.Password))
    {
       u.UserID = 90099;
       //your view and controller are bound so no need to specify the view name, just pass the model back
       return View(u);
    }
    return View(u);
}
</code>```

Now your model has been updated and your view has the latest model to render the HTML markup via Razor.

Body:
What would be the exact flow or order of actions and rendering if I click my submit button which calls a javascript function and posts to a controller?

@section impScripts
     {

}

Button:

<input type="submit" onclick="save();" value="Login" class="btn btn-default" />

View:

@model HimHer.Models.Stories

@{
    ViewBag.Title = "Authenticate";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@Html.AntiForgeryToken()

Stories

            @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })

                @Html.TextBoxFor(model => model.UserName, new { @class = "form-control white" })

            @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })

                @Html.TextBoxFor(model => model.Password, new { @class = "form-control white" })

Controller:

[HttpGet]
        public ActionResult Authenticate()
        {

            return View();
        }

        [HttpPost]
        public JsonResult Authenticate(Stories u)
        {
            if (basicOps.getUsersLogin(u.UserName, u.Password))
            {
               u.UserID = 90099;
               //return RedirectToAction("GetImagesStories", "Stories", u);
               //return View("Authenticate");
               return Json(u);

            }

            //return View("Authenticate");
            return Json(u);

        }

Now I don't understand the exact flow after I click the submit button. I tried searching and doing it on my own but couldn't find the concrete knowledge about it. Help me with it.

Body:
Each component of MVC interacts with the other. Notice in the diagram below that:

The view sends HTTP requests (POST, GET etc.) to the controller. Controller then updates the model.

Now that the model has been updated, the view can use this model to render the form with the updated data.

How does this compare to what you are doing?

In your view, you pass the form values (mapped to your model) to the server (controller) via AJAX. Note that you can also do this using the @html.beginForm() method instead of AJAX.

When the request arrives server side it knows to check the Users controller as thats where you told it to look, it will also discover two instances of the Authenticate method (one of which is overloaded):

//notice the [HttpGet] attribute, tells the server to get the view
[HttpGet]
public ActionResult Authenticate(){return View();}

//[HttpPost] tells it to use the overloaded method to accept a parameter (usually a routeValueDictionary object)
[HttpPost]
public JsonResult Authenticate(Stories u){}

Using a JsonResult return type for the method you are expecting to return a JsonResult instead of the view itself. From what you are saying you need to return an ActionResult instead returning the view with the updated Model instead, something like this (although I dont know what your model looks like):

[HttpPost]
public ActionResult Authenticate(Stories u)
{
    if (basicOps.getUsersLogin(u.UserName, u.Password))
    {
       u.UserID = 90099;
       //your view and controller are bound so no need to specify the view name, just pass the model back
       return View(u);
    }
    return View(u);
}

Now your model has been updated and your view has the latest model to render the HTML markup via Razor.


问:
SecurityException: opening provider that is not exported from uid
I have an app which is not on Play Store. I was earlier able to start the app installer by the following code:

In Activity:

```<code>Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/app_path/app_name.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
</code>```

Now, I've changed the targetSdkVersion to 25, and made some changes in my code but I'm getting the following error:

FATAL EXCEPTION: AsyncTask #1
      Process: com.android.packageinstaller, PID: 6499
      java.lang.RuntimeException: An error occurred while executing doInBackground()
      at android.os.AsyncTask$3.done(AsyncTask.java:326)
      at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
      at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
      at java.util.concurrent.FutureTask.run(FutureTask.java:242)
      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:244)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
      at java.lang.Thread.run(Thread.java:761)
       Caused by: java.lang.SecurityException: Permission Denial: opening provider in.namespace.appname.comp.GenericFileProvider from ProcessRecord{ba35e80 6499:com.android.packageinstaller/u0a17} (pid=6499, uid=10017) that is not exported from uid 10076
      at android.os.Parcel.readException(Parcel.java:1684)
      at android.os.Parcel.readException(Parcel.java:1637)
      at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:4213)
      at android.app.ActivityThread.acquireProvider(ActivityThread.java:5535)
      at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2239)
      at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1517)
      at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1131)
      at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:984)
      at android.content.ContentResolver.openInputStream(ContentResolver.java:704)
      at com.android.packageinstaller.PackageInstallerActivity$StagingAsyncTask.doInBackground(PackageInstallerActivity.java:804)
      at com.android.packageinstaller.PackageInstallerActivity$StagingAsyncTask.doInBackground(PackageInstallerActivity.java:795)
      at android.os.AsyncTask$2.call(AsyncTask.java:306)
      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:244)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
      at java.lang.Thread.run(Thread.java:761)

These are the changes I've made in my code:

in Manifest:

```<code><permission
    android:name="in.namespace.appname.fp.READ"
    android:description="@string/file_provider_permission_description"
    android:label="in.namespace.appname.READ_FILE"/>

<uses-permission android:name="in.namespace.appname.fp.READ"/>

<application
    ...
    ...
    <provider
        android:name=".comp.GenericFileProvider"
        android:authorities="in.namespace.appname.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true"
        android:readPermission="in.namespace.appname.fp.READ">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>
    .
    .
</application>
</code>```

Create an empty class GenericFileProvider in directory comp:

```<code>public class GenericFileProvider extends FileProvider { }
</code>```

Changed the Activity code to:

```<code>File updatedApk = new File(Environment.getExternalStorageDirectory() + "/file_path/appname.apk");
Uri updatedApkUri =  GenericFileProvider.getUriForFile(
     AppUpdateActivity.this,
     "in.namespace.appname.fileprovider",
     updatedApk);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(updatedApkUri, "application/vnd.android.package-archive");

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
</code>```

What's causing this error and how can I fix this ?

EDIT #1

Changed my manifest:

```<code><provider
        android:name=".comp.GenericFileProvider"
        android:authorities="in.namespace.appname.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>
</code>```

Body:
I have an app which is not on Play Store. I was earlier able to start the app installer by the following code:

In Activity:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/app_path/app_name.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);

Now, I've changed the targetSdkVersion to 25, and made some changes in my code but I'm getting the following error:

FATAL EXCEPTION: AsyncTask #1
      Process: com.android.packageinstaller, PID: 6499
      java.lang.RuntimeException: An error occurred while executing doInBackground()
      at android.os.AsyncTask$3.done(AsyncTask.java:326)
      at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
      at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
      at java.util.concurrent.FutureTask.run(FutureTask.java:242)
      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:244)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
      at java.lang.Thread.run(Thread.java:761)
       Caused by: java.lang.SecurityException: Permission Denial: opening provider in.namespace.appname.comp.GenericFileProvider from ProcessRecord{ba35e80 6499:com.android.packageinstaller/u0a17} (pid=6499, uid=10017) that is not exported from uid 10076
      at android.os.Parcel.readException(Parcel.java:1684)
      at android.os.Parcel.readException(Parcel.java:1637)
      at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:4213)
      at android.app.ActivityThread.acquireProvider(ActivityThread.java:5535)
      at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2239)
      at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1517)
      at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1131)
      at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:984)
      at android.content.ContentResolver.openInputStream(ContentResolver.java:704)
      at com.android.packageinstaller.PackageInstallerActivity$StagingAsyncTask.doInBackground(PackageInstallerActivity.java:804)
      at com.android.packageinstaller.PackageInstallerActivity$StagingAsyncTask.doInBackground(PackageInstallerActivity.java:795)
      at android.os.AsyncTask$2.call(AsyncTask.java:306)
      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:244)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
      at java.lang.Thread.run(Thread.java:761)

These are the changes I've made in my code:

in Manifest:

<permission
    android:name="in.namespace.appname.fp.READ"
    android:description="@string/file_provider_permission_description"
    android:label="in.namespace.appname.READ_FILE"/>

<uses-permission android:name="in.namespace.appname.fp.READ"/>

<application
    ...
    ...
    <provider
        android:name=".comp.GenericFileProvider"
        android:authorities="in.namespace.appname.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true"
        android:readPermission="in.namespace.appname.fp.READ">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>
    .
    .
</application>

Create an empty class GenericFileProvider in directory comp:

public class GenericFileProvider extends FileProvider { }

Changed the Activity code to:

File updatedApk = new File(Environment.getExternalStorageDirectory() + "/file_path/appname.apk");
Uri updatedApkUri =  GenericFileProvider.getUriForFile(
     AppUpdateActivity.this,
     "in.namespace.appname.fileprovider",
     updatedApk);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(updatedApkUri, "application/vnd.android.package-archive");

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);

What's causing this error and how can I fix this ?

EDIT #1

Changed my manifest:

<provider
        android:name=".comp.GenericFileProvider"
        android:authorities="in.namespace.appname.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

Body: Remove android:readPermission="in.namespace.appname.fp.READ".


问:
Enabling Both HTTP and HTTPS for an Application in Tomcat 8
I have one application deployed in Tomcat 8, which i want to run on http and https both. I have configured server.xml to run both on http and https. web.xml for application is also configured to handle both requests. Issue is i am getting response for https requests only. For http it says request timed out.

https port configured is 9999 and http port is 8080.
https://URL:9999/path  => working
http://URL:8080/path   => not working

Below is attached code for server.xml , web.xml inside WEB-INF directory and catalina.out response when i start Tomcat.

SERVER.XML

```<code><Connector port="8080" protocol="HTTP/1.1"
       connectionTimeout="20000"/>
  <Connector
       protocol="org.apache.coyote.http11.Http11NioProtocol"
       port="9999" maxThreads="200"
       scheme="https" secure="true" SSLEnabled="true"
       keystoreFile="/root/.keystore" keystorePass="Pooltooadm!n"
       clientAuth="false" sslProtocol="TLS"/>
</code>```

web.xml

```<code>    <welcome-file-list>
        <welcome-file>/index.html</welcome-file>
        <welcome-file>/index.xhtml</welcome-file>
        <welcome-file>/index.htm</welcome-file>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>

    <security-constraint>
    <web-resource-collection>
    <web-resource-name>HTTP</web-resource-name>
    <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
    <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
    </security-constraint>
    <security-constraint>
    <web-resource-collection>
    <web-resource-name>HTTPS</web-resource-name>
    <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
    <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
    </security-constraint>

    <error-page>
    <error-code>404</error-code>
    <location>/index.html</location>
  </error-page>
</code>```

CATALINA.OUT=>

```<code>17-Nov-2017 14:07:46.349 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/8.5.23
17-Nov-2017 14:07:46.433 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Sep 28 2017 10:30:11 UTC
17-Nov-2017 14:07:46.593 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         8.5.23.0
17-Nov-2017 14:07:46.593 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
17-Nov-2017 14:07:46.593 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            4.4.21-90-default
17-Nov-2017 14:07:46.593 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
17-Nov-2017 14:07:46.593 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /usr/java/jdk1.8.0_151/jre
17-Nov-2017 14:07:46.594 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_151-b12
17-Nov-2017 14:07:46.594 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
17-Nov-2017 14:07:46.594 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /opt/apache-tomcat-8.5.23
17-Nov-2017 14:07:46.594 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /opt/apache-tomcat-8.5.23
17-Nov-2017 14:07:46.594 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/opt/apache-tomcat-8.5.23/conf/logging.properties
17-Nov-2017 14:07:46.595 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
17-Nov-2017 14:07:46.595 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
17-Nov-2017 14:07:46.637 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
17-Nov-2017 14:07:46.637 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/opt/apache-tomcat-8.5.23
17-Nov-2017 14:07:46.637 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/opt/apache-tomcat-8.5.23
17-Nov-2017 14:07:46.637 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/opt/apache-tomcat-8.5.23/temp
17-Nov-2017 14:07:46.637 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
17-Nov-2017 14:07:51.020 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
17-Nov-2017 14:07:51.711 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
17-Nov-2017 14:07:51.832 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["https-jsse-nio-9999"]
17-Nov-2017 14:08:00.507 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
17-Nov-2017 14:08:00.509 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]
17-Nov-2017 14:08:00.510 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
17-Nov-2017 14:08:00.592 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 28123 ms
17-Nov-2017 14:08:01.206 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
17-Nov-2017 14:08:01.207 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.23
17-Nov-2017 14:08:01.495 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/apache-tomcat-8.5.23/webapps/host-manager]
17-Nov-2017 14:08:11.450 WARNING [localhost-startStop-1] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [159] milliseconds.
17-Nov-2017 14:08:12.311 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/apache-tomcat-8.5.23/webapps/host-manager] has finished in [10,816] ms
17-Nov-2017 14:08:12.312 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/apache-tomcat-8.5.23/webapps/manager]
17-Nov-2017 14:08:12.924 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/apache-tomcat-8.5.23/webapps/manager] has finished in [612] ms
17-Nov-2017 14:08:12.924 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/apache-tomcat-8.5.23/webapps/docs]
17-Nov-2017 14:08:13.415 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/apache-tomcat-8.5.23/webapps/docs] has finished in [491] ms
17-Nov-2017 14:08:13.415 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/apache-tomcat-8.5.23/webapps/ROOT]
17-Nov-2017 14:08:13.825 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/apache-tomcat-8.5.23/webapps/ROOT] has finished in [409] ms
17-Nov-2017 14:08:13.825 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/apache-tomcat-8.5.23/webapps/examples]
17-Nov-2017 14:08:20.096 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/apache-tomcat-8.5.23/webapps/examples] has finished in [6,271] ms
17-Nov-2017 14:08:20.097 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/apache-tomcat-8.5.23/webapps/pooltooadmin]
17-Nov-2017 14:08:20.549 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/apache-tomcat-8.5.23/webapps/pooltooadmin] has finished in [451] ms
17-Nov-2017 14:08:20.836 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
17-Nov-2017 14:08:20.999 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["https-jsse-nio-9999"]
17-Nov-2017 14:08:21.163 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
17-Nov-2017 14:08:21.204 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 20612 ms
</code>```

答:
Was able to this by configuring web.xml as follows:-</p>

```<code><security-constraint>
    <web-resource-collection>
        <web-resource-name>Support Both HTTP and HTTPS</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
       <!--   <transport-guarantee>NONE</transport-guarantee> -->
    </user-data-constraint>
</security-constraint>
</code>```

Body:
I have one application deployed in Tomcat 8, which i want to run on http and https both. I have configured server.xml to run both on http and https. web.xml for application is also configured to handle both requests. Issue is i am getting response for https requests only. For http it says request timed out.

https port configured is 9999 and http port is 8080.
https://URL:9999/path  => working
http://URL:8080/path   => not working

Below is attached code for server.xml , web.xml inside WEB-INF directory and catalina.out response when i start Tomcat.

SERVER.XML

<Connector port="8080" protocol="HTTP/1.1"
       connectionTimeout="20000"/>
  <Connector
       protocol="org.apache.coyote.http11.Http11NioProtocol"
       port="9999" maxThreads="200"
       scheme="https" secure="true" SSLEnabled="true"
       keystoreFile="/root/.keystore" keystorePass="Pooltooadm!n"
       clientAuth="false" sslProtocol="TLS"/>

web.xml

    <welcome-file-list>
        <welcome-file>/index.html</welcome-file>
        <welcome-file>/index.xhtml</welcome-file>
        <welcome-file>/index.htm</welcome-file>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>

    <security-constraint>
    <web-resource-collection>
    <web-resource-name>HTTP</web-resource-name>
    <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
    <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
    </security-constraint>
    <security-constraint>
    <web-resource-collection>
    <web-resource-name>HTTPS</web-resource-name>
    <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
    <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
    </security-constraint>

    <error-page>
    <error-code>404</error-code>
    <location>/index.html</location>
  </error-page>

CATALINA.OUT=>

17-Nov-2017 14:07:46.349 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/8.5.23
17-Nov-2017 14:07:46.433 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Sep 28 2017 10:30:11 UTC
17-Nov-2017 14:07:46.593 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         8.5.23.0
17-Nov-2017 14:07:46.593 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
17-Nov-2017 14:07:46.593 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            4.4.21-90-default
17-Nov-2017 14:07:46.593 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
17-Nov-2017 14:07:46.593 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /usr/java/jdk1.8.0_151/jre
17-Nov-2017 14:07:46.594 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_151-b12
17-Nov-2017 14:07:46.594 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
17-Nov-2017 14:07:46.594 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /opt/apache-tomcat-8.5.23
17-Nov-2017 14:07:46.594 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /opt/apache-tomcat-8.5.23
17-Nov-2017 14:07:46.594 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/opt/apache-tomcat-8.5.23/conf/logging.properties
17-Nov-2017 14:07:46.595 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
17-Nov-2017 14:07:46.595 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
17-Nov-2017 14:07:46.637 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
17-Nov-2017 14:07:46.637 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/opt/apache-tomcat-8.5.23
17-Nov-2017 14:07:46.637 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/opt/apache-tomcat-8.5.23
17-Nov-2017 14:07:46.637 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/opt/apache-tomcat-8.5.23/temp
17-Nov-2017 14:07:46.637 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
17-Nov-2017 14:07:51.020 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
17-Nov-2017 14:07:51.711 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
17-Nov-2017 14:07:51.832 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["https-jsse-nio-9999"]
17-Nov-2017 14:08:00.507 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
17-Nov-2017 14:08:00.509 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]
17-Nov-2017 14:08:00.510 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
17-Nov-2017 14:08:00.592 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 28123 ms
17-Nov-2017 14:08:01.206 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
17-Nov-2017 14:08:01.207 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.23
17-Nov-2017 14:08:01.495 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/apache-tomcat-8.5.23/webapps/host-manager]
17-Nov-2017 14:08:11.450 WARNING [localhost-startStop-1] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [159] milliseconds.
17-Nov-2017 14:08:12.311 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/apache-tomcat-8.5.23/webapps/host-manager] has finished in [10,816] ms
17-Nov-2017 14:08:12.312 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/apache-tomcat-8.5.23/webapps/manager]
17-Nov-2017 14:08:12.924 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/apache-tomcat-8.5.23/webapps/manager] has finished in [612] ms
17-Nov-2017 14:08:12.924 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/apache-tomcat-8.5.23/webapps/docs]
17-Nov-2017 14:08:13.415 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/apache-tomcat-8.5.23/webapps/docs] has finished in [491] ms
17-Nov-2017 14:08:13.415 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/apache-tomcat-8.5.23/webapps/ROOT]
17-Nov-2017 14:08:13.825 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/apache-tomcat-8.5.23/webapps/ROOT] has finished in [409] ms
17-Nov-2017 14:08:13.825 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/apache-tomcat-8.5.23/webapps/examples]
17-Nov-2017 14:08:20.096 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/apache-tomcat-8.5.23/webapps/examples] has finished in [6,271] ms
17-Nov-2017 14:08:20.097 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/apache-tomcat-8.5.23/webapps/pooltooadmin]
17-Nov-2017 14:08:20.549 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/apache-tomcat-8.5.23/webapps/pooltooadmin] has finished in [451] ms
17-Nov-2017 14:08:20.836 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
17-Nov-2017 14:08:20.999 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["https-jsse-nio-9999"]
17-Nov-2017 14:08:21.163 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
17-Nov-2017 14:08:21.204 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 20612 ms

Body:
Was able to this by configuring web.xml as follows:-

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Support Both HTTP and HTTPS</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
       <!--   <transport-guarantee>NONE</transport-guarantee> -->
    </user-data-constraint>
</security-constraint>


问:
How to extract elements (title names, votes, views, answers for each title) from https://stackoverflow.com/?tab=month?
I could figure out the reading the page and storing the text to a file part (urllib2) but not the extraction of elements(votes, title ,...).
The page is https://stackoverflow.com/?tab=month

How do I write a python script to get a webpage and then extract all the titles, no. of views, votes and answers received by each question?

答:
If you want to crawl the page, you can use libraries like scrapy , but stackoverflow has very good and easy to use REST API , with sort features, which you can access and read about in here:
https://api.stackexchange.com/docs/
for retrieving question for example, see this page:
https://api.stackexchange.com/docs/questions</p>


问:
Sql Design Question
I have a table with 25 columns where 20 columns can have null values for some (30-40%) rows.
Now what is the cost of having rows with 20 null columns? Is this OK?

Or

is it a good design to have another table to store those 20 columns and add a ref to the first table?
This way I will only write to the second table only when there is are values.

I am using SQL server 2005. Will migrate to 2008 in future.

Only 20 columns are varchar, rest smallint, smalldate

What I am storing:
  These columns store different attributes of the row it belongs to. These attributes can be null sometimes.

The table will hold ~billion of rows

Please comment.

答: Create tables based on the distinct sets of attributes you have. So if you have some data where some of your columns do not apply then it would make sense to have that data in a table which doesn't have those columns. As far as possible, avoid repeating the same attribute in multiple tables. Make sure your data is in at least Boyce-Codd / 5th Normal Form and you won't go far wrong.</p>

Title: Sql Design Question


问:
"Shell Command: Install" command missing from Command Palette - VS code - ubuntu
I cannot find the command to install shell commands to the path on the Command Palette in VS code for Ubuntu.Is it possible that something went wrong during the installation, or is there a solution that I am missing?

When I type in "Shell", my options are "Terminal: Allow Workspace Shell Configuration" and "Python: Run Selection/Line in Django Shell". There is nothing listed to Install a Shell command.

答:
With latest version of vscode you should be able to ctrl + shift + p or cmd + shift + p (or whatever the hotkey is on ubuntu) and search for shell and then get what you see in the image.

It is said on the Vscode documentation: "Note: Users on macOS must first run a command (Shell Command: Install 'code' command in PATH)", so maybe the command is only available when running vscode from mac, and not ubuntu (and windows for that matter)? I don't know


问:
java.io.FileNotFoundException: /storage/emulated/0/Notes/fact50Result.txt: open failed ENOENT (No such file or directory)
I am trying to export the results of a factorial calculation in a txt file.
I found this code that looked quite straight forward from this article here.
However I'm getting the error on the title. What am I doing wrong?

I have typed this into the manifest:

```<code> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</code>```

And this is my code related to saving:

```<code>    public void save(View v) {
     if(numOnRAM!=-1) {
         EditText text = (EditText) findViewById(R.id.resultTextBox);
         String data = text.getText().toString();
         generateNoteOnSD(this,"fact"+numOnRAM+"Results.txt", data);
     }
}

public void generateNoteOnSD(Context context, String sFileName, String sBody) {
    try {
        File root = new File(Environment.getExternalStorageDirectory(), "Notes");
        if (!root.exists()) {
            root.mkdirs();
        }
        File gpxfile = new File(root, sFileName);
        FileWriter writer = new FileWriter(gpxfile);
        writer.append(sBody);
        writer.flush();
        writer.close();
        Toast.makeText(context, "File "+sFileName+" saving success!", Toast.LENGTH_LONG).show();

    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();//After adding this
//toast message did I realise the error thar occurs.

    }
}
</code>```

答:
As discussed in the comments, you need to request permissions at runtime to allow your app to write to external storage. You can check if your app has the permission already by using checkSelfPermission(). For example:

```<code>if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
    // Do the file write
} else {
    // Request permission from the user
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
    ...
}
</code>```

In order to handle this permission request, you'll also need to implement onRequestPermissionsResult() from OnRequestPermissionsResultCallback, for example:

```<code>@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case 0:
            // Re-attempt file write
    }
}
</code>```

Read the documentation for full information.

Body:
I am trying to export the results of a factorial calculation in a txt file.
I found this code that looked quite straight forward from this article here.
However I'm getting the error on the title. What am I doing wrong?

I have typed this into the manifest:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

And this is my code related to saving:

    public void save(View v) {
     if(numOnRAM!=-1) {
         EditText text = (EditText) findViewById(R.id.resultTextBox);
         String data = text.getText().toString();
         generateNoteOnSD(this,"fact"+numOnRAM+"Results.txt", data);
     }
}

public void generateNoteOnSD(Context context, String sFileName, String sBody) {
    try {
        File root = new File(Environment.getExternalStorageDirectory(), "Notes");
        if (!root.exists()) {
            root.mkdirs();
        }
        File gpxfile = new File(root, sFileName);
        FileWriter writer = new FileWriter(gpxfile);
        writer.append(sBody);
        writer.flush();
        writer.close();
        Toast.makeText(context, "File "+sFileName+" saving success!", Toast.LENGTH_LONG).show();

    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();//After adding this
//toast message did I realise the error thar occurs.

    }
}

Body:
As discussed in the comments, you need to request permissions at runtime to allow your app to write to external storage. You can check if your app has the permission already by using checkSelfPermission(). For example:

if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
    // Do the file write
} else {
    // Request permission from the user
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
    ...
}

In order to handle this permission request, you'll also need to implement onRequestPermissionsResult() from OnRequestPermissionsResultCallback, for example:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case 0:
            // Re-attempt file write
    }
}

Read the documentation for full information.


问:
How can I get only jpg or png images from cursor
I want only images with a specific extension from cursor.
If I write a cursor query like this, then it will give me the last added image.
For example if I use a jpg image then it will provide the .jpg image path.
If I add the .png extension, then it will provide a .png image path

```<code>Cursor cur = getContentResolver().query(Images, PROJECTION_BUCKET, "1) GROUP BY 1,(2", null,
                "MAX(datetaken) DESC");
</code>```

But how do I get a image files specific extensions from cursor.
I have tried this code but gives me an error:

```<code>String[] PROJECTION_BUCKET = new String[] { "bucket_id", "bucket_display_name", "datetaken", "_data" };
Uri Images = Media.EXTERNAL_CONTENT_URI;

String where = MediaStore.Images.Media.MIME_TYPE + "='image/jpeg'" +
                " OR " + MediaStore.Images.Media.MIME_TYPE + "='image/png’"+
                        " OR " + MediaStore.Images.Media.MIME_TYPE + "='image/jpg’";

Cursor cur = getContentResolver().query(Images, PROJECTION_BUCKET where,null,MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
</code>```

Error:

```<code>11-06 18:15:10.249: E/SQLiteLog(3167): (1) near "image": syntax error
11-06 18:15:10.249: E/DatabaseUtils(3167): Writing exception to parcel
11-06 18:15:10.249: E/DatabaseUtils(3167): android.database.sqlite.SQLiteException: near "image": syntax error (code 1): , while compiling: SELECT bucket_id, bucket_display_name, datetaken, _data FROM images WHERE (mime_type='image/jpeg' OR mime_type='image/png’ OR mime_type='image/jpg’) ORDER BY datetaken DESC
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:400)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:333)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at com.android.providers.media.MediaProvider.query(MediaProvider.java:2700)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.content.ContentProvider.query(ContentProvider.java:1010)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.content.ContentProvider$Transport.query(ContentProvider.java:214)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.os.Binder.execTransact(Binder.java:446)
</code>```

Does anybody know how write a query for this?

答:
right query is below i found solution of my own quesion

```<code>String where = MediaStore.Images.Media.MIME_TYPE + "='image/jpeg'" + " OR " + MediaStore.Images.Media.MIME_TYPE + "='image/png'"+ " OR " + MediaStore.Images.Media.MIME_TYPE + "='image/jpg'";
</code>```

there is only one mistake i have done in my question code and mistake is i have write ’ this at the end of "png" and "jpg" i just only replace ’ this to ' that's it

Body:
I want only images with a specific extension from cursor.
If I write a cursor query like this, then it will give me the last added image.
For example if I use a jpg image then it will provide the .jpg image path.
If I add the .png extension, then it will provide a .png image path

Cursor cur = getContentResolver().query(Images, PROJECTION_BUCKET, "1) GROUP BY 1,(2", null,
                "MAX(datetaken) DESC");

But how do I get a image files specific extensions from cursor.
I have tried this code but gives me an error:

String[] PROJECTION_BUCKET = new String[] { "bucket_id", "bucket_display_name", "datetaken", "_data" };
Uri Images = Media.EXTERNAL_CONTENT_URI;

String where = MediaStore.Images.Media.MIME_TYPE + "='image/jpeg'" +
                " OR " + MediaStore.Images.Media.MIME_TYPE + "='image/png’"+
                        " OR " + MediaStore.Images.Media.MIME_TYPE + "='image/jpg’";

Cursor cur = getContentResolver().query(Images, PROJECTION_BUCKET where,null,MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");

Error:

11-06 18:15:10.249: E/SQLiteLog(3167): (1) near "image": syntax error
11-06 18:15:10.249: E/DatabaseUtils(3167): Writing exception to parcel
11-06 18:15:10.249: E/DatabaseUtils(3167): android.database.sqlite.SQLiteException: near "image": syntax error (code 1): , while compiling: SELECT bucket_id, bucket_display_name, datetaken, _data FROM images WHERE (mime_type='image/jpeg' OR mime_type='image/png’ OR mime_type='image/jpg’) ORDER BY datetaken DESC
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:400)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:333)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at com.android.providers.media.MediaProvider.query(MediaProvider.java:2700)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.content.ContentProvider.query(ContentProvider.java:1010)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.content.ContentProvider$Transport.query(ContentProvider.java:214)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.os.Binder.execTransact(Binder.java:446)

Does anybody know how write a query for this?

Body:
right query is below i found solution of my own quesion

String where = MediaStore.Images.Media.MIME_TYPE + "='image/jpeg'" + " OR " + MediaStore.Images.Media.MIME_TYPE + "='image/png'"+ " OR " + MediaStore.Images.Media.MIME_TYPE + "='image/jpg'";

there is only one mistake i have done in my question code and mistake is i have write ’ this at the end of "png" and "jpg" i just only replace ’ this to ' that's it


问:
Getting empty array in PHP while using $.ajax
I have this jQuery $.ajax script:

```<code>var data = JSON.stringify( selected_products );

$.ajax({ type: "POST",
    url: "addtocart.php",
    data: data,
    dataType: 'application/json'
});
</code>```

And I am posting this to a really simple PHP script:

```<code>$selected_products = json_decode($_POST['data'], true);
print_r($selected_products);
</code>```

This is an example of what I'm sending; this is outgoing data in Chrome DevTools:

```<code>data:[{"id":"RECOLOURBALM","option":"Black","quantity":1},{"id":"TOWELS","quantity":1}]
</code>```

However this is the console readout for the response:

```<code>Array
(
)
</code>```

I think the request is succeeding and data is being passed, but the responses are all empty.

What am I doing wrong?

答:
If the Content-Type is set to application/json, you have to receive data as follows.

$data = json_decode(file_get_contents('php://input'), true);

$_POST will receive the data from request, which has Content-Type: x-www-form-urlencoded(Form Data)

Body:
I have this jQuery $.ajax script:

var data = JSON.stringify( selected_products );

$.ajax({ type: "POST",
    url: "addtocart.php",
    data: data,
    dataType: 'application/json'
});

And I am posting this to a really simple PHP script:

$selected_products = json_decode($_POST['data'], true);
print_r($selected_products);

This is an example of what I'm sending; this is outgoing data in Chrome DevTools:

data:[{"id":"RECOLOURBALM","option":"Black","quantity":1},{"id":"TOWELS","quantity":1}]

However this is the console readout for the response:

Array
(
)

I think the request is succeeding and data is being passed, but the responses are all empty.

What am I doing wrong?


问:
How to scan only dis,flt files
I want to use only with JFileChooser I need to scan only dis,flt files:
I have code like this:

```<code>JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter xmlfilter = new FileNameExtensionFilter(
 "dis files (*.dis)", "dis");
FileNameExtensionFilter xmlfilter2 = new FileNameExtensionFilter(
 "flt files (*.flt)", "flt");
   chooser.setFileFilter(xmlfilter);
   chooser.setFileFilter(xmlfilter2);
   chooser.setDialogTitle("Open a file");

chooser.showOpenDialog(null);
    java.io.File f= chooser.getSelectedFile();
    String filename = f.getAbsolutePath();
</code>```

In the filter it show that but it didnt scan this file why?

答:
You should use addChoosableFileFilter:

```<code>chooser.addChoosableFileFilter(xmlfilter);
chooser.addChoosableFileFilter(xmlfilter2);
</code>```

Title: How to scan only dis,flt files

Body:
I want to use only with JFileChooser I need to scan only dis,flt files:
I have code like this:

JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter xmlfilter = new FileNameExtensionFilter(
 "dis files (*.dis)", "dis");
FileNameExtensionFilter xmlfilter2 = new FileNameExtensionFilter(
 "flt files (*.flt)", "flt");
   chooser.setFileFilter(xmlfilter);
   chooser.setFileFilter(xmlfilter2);
   chooser.setDialogTitle("Open a file");

chooser.showOpenDialog(null);
    java.io.File f= chooser.getSelectedFile();
    String filename = f.getAbsolutePath();

In the filter it show that but it didnt scan this file why?

Body:
You should use addChoosableFileFilter:

chooser.addChoosableFileFilter(xmlfilter);
chooser.addChoosableFileFilter(xmlfilter2);


问:
Is it possible to apply dynamic style as string in Angular?
I have a  that I want to apply dynamic style to.

Style is stored in a css-like string variable and can be arbitrary e.g.

myStyle = 'color: white; font-weight: bold;'

or

myStyle = 'background-color: red;'

I expected it to work like

but it didn't.

I tried different options but none seem to work for me for different reasons:

I can't put styles in a .css file and use class names because style is coming from server in the form of aforementioned string

Using [style.color] etc. doesn't suit me because I don't know what the style can be

Using [ngStyle] doesn't suit me because it expects object like {'color': 'red', 'font-weight': 'bold'} and I only have string

The reason I have a style stored in a string is because I need to apply it in HTML generated on the server where I simply pass that string to a placeholder variable in a velocity template.

I am almost confident that it can't be done the way I want but probably I am overlooking some solution.

答:
Angular provides the DomSanitizer service which can convert strings into style objects. I think this is exactly your case.</p>

```<code>constructor(private sanitizer: DomSanitizer) {
}

sanitizeStyle() {
    return this.sanitizer.bypassSecurityTrustStyle('background-color: red');
}

<span [style]="sanitizeStyle()">
</code>```

Body:
Angular provides the DomSanitizer service which can convert strings into style objects. I think this is exactly your case.

constructor(private sanitizer: DomSanitizer) {
}

sanitizeStyle() {
    return this.sanitizer.bypassSecurityTrustStyle('background-color: red');
}

<span [style]="sanitizeStyle()">


问:
How to Launch specific activity when my Application is in Recent Apps List?
in my android application 3 activities are there.those are A,B&C.
Initial landing screen is A.then navigate to B.
But I want to display Activity C when this app is in recent appslist kind of App Masking.
Any one have idea please share me.
Thanks in Advance!!!</p>

答: you can StartActivity C Activity in onResume of A Or B activity.


问:
Filter for the List<List<String>>
I am trying to write a method that would filter the duplicate inputs in a list of lists of Strings. The problem is that by duplicates I mean also the lists that have the same elemets buts in a different order. So I would like that if the input list is :

```<code>[["1", "2","3"], ["2","1","3"],["3","1","2"]]
</code>```

it would return only

```<code>[["1","2","3"]]
</code>```

I tried to do something like:

```<code>public List<List<String>> filter (List<List<String>> list){
    List<List<String>> filteredList = new LinkedList<>();
    filteredList.addAll(list);
    for(List<String> i:list){
        for(List<String> j:list){
            if (!i.equals(j)&&i.containsAll(j)){
                filteredList.remove(j);
            }
        }
    }
    return filteredList;
}
</code>```

but it would remove too much cause it would still iterate through the whole original list and would find that element "a" is duplicate of element "b" and vice versa. On the other side when I try to do that on the same list (without a temp) I get ConcurrentModificationException cause I can't modify and iterate over the same collection at the same time. Adding the list to a Set would not solve the issue because they are not exact duplicates (.equals would return false). Is there any possible solution to achieve at?

答:
First of all to avoid ConcurrentModificationException - do a new list and copy to it only values iterated on the original list, that were not yet copied before.

Second of all You can create a map/set that would contain values that were already copied to check for duplicates. To make this work, You need to sort the inner list before putting the values into the map/set and before checking if they are already there

Title: Filter for the List<List<String>>

Body:
I am trying to write a method that would filter the duplicate inputs in a list of lists of Strings. The problem is that by duplicates I mean also the lists that have the same elemets buts in a different order. So I would like that if the input list is :

[["1", "2","3"], ["2","1","3"],["3","1","2"]]

it would return only

[["1","2","3"]]

I tried to do something like:

public List<List<String>> filter (List<List<String>> list){
    List<List<String>> filteredList = new LinkedList<>();
    filteredList.addAll(list);
    for(List<String> i:list){
        for(List<String> j:list){
            if (!i.equals(j)&&i.containsAll(j)){
                filteredList.remove(j);
            }
        }
    }
    return filteredList;
}

but it would remove too much cause it would still iterate through the whole original list and would find that element "a" is duplicate of element "b" and vice versa. On the other side when I try to do that on the same list (without a temp) I get ConcurrentModificationException cause I can't modify and iterate over the same collection at the same time. Adding the list to a Set would not solve the issue because they are not exact duplicates (.equals would return false). Is there any possible solution to achieve at?