问:
Rubocop error on Rspec/DescribeClass
I've recently updated to the most recent version of Rubocop (0.51) due to a bug in the previos version, but now it marks the following as error:

```<code>spec/controllers/loans_controller_spec.rb:3:10: C: RSpec/DescribeClass: The first argument to describe should be the class or module being tested.
describe LoansController do
         ^^^^^^^^^^^^^^^
</code>```

This is the way I've been doing it since forever, and is in fact the class I want to test.

The same is happening with all files in my spec folder. They all have the correspondent class name.

答:
I've managed to fix the erro by updating rubocop-rspec version.

It was version 1.10 and updated it to 1.20.1 the error did not show again.

Title: Rubocop error on Rspec/DescribeClass

Body:
I've recently updated to the most recent version of Rubocop (0.51) due to a bug in the previos version, but now it marks the following as error:

spec/controllers/loans_controller_spec.rb:3:10: C: RSpec/DescribeClass: The first argument to describe should be the class or module being tested.
describe LoansController do
         ^^^^^^^^^^^^^^^

This is the way I've been doing it since forever, and is in fact the class I want to test.

The same is happening with all files in my spec folder. They all have the correspondent class name.


来源: stackexchange

问:
batch file to run a sequence of tasks
I have hundreds of swfs I want to convert to mp4s using a program called Swivel. The problem is that there is no option to bulk convert, so I'm trying to write a batch script. I'm very clueless so please forgive blunders.
I have this so far

```<code>@echo off
"C:\Users\me\Desktop\Swivel - Shortcut.lnk" C:\Users\me\Documents\swfs\fluent_5_5.swf
</code>```

This opens and converts one swf. Is there a way of writing the script so that it would convert all swfs in a given folder one after the other?

Many thanks

答:
```<code>for %%a in (C:\Users\me\Documents\swfs\*swf) do "C:\Users\me\Desktop\Swivel - Shortcut.lnk" %%a
</code>```

should apply each matching filename to %%a

ContentLicense: CC BY-SA 3.0

Title: batch file to run a sequence of tasks

Body:
I have hundreds of swfs I want to convert to mp4s using a program called Swivel. The problem is that there is no option to bulk convert, so I'm trying to write a batch script. I'm very clueless so please forgive blunders.
I have this so far

@echo off
"C:\Users\me\Desktop\Swivel - Shortcut.lnk" C:\Users\me\Documents\swfs\fluent_5_5.swf

This opens and converts one swf. Is there a way of writing the script so that it would convert all swfs in a given folder one after the other?

Many thanks

Body:
for %%a in (C:\Users\me\Documents\swfs\*swf) do "C:\Users\me\Desktop\Swivel - Shortcut.lnk" %%a

should apply each matching filename to %%a


问:
Google Charts - Responsive Issue - Dynamically Resize
I am trying to make my Google Column Material Chart responsive, but it seems a bit choppy to me and doesnt work well at all. Can anyone help me make this flow well

The chart will only appear in that format when the page is loaded. It won’t dynamically resize when the browser window width is changed.

JSFIDDLE
https://jsfiddle.net/rbla/Le8g0sw0/8/

HTML

```<code><div id="chart">
    <div id="chart_div"></div>
</div>
</code>```

CSS

```<code>#chart {
   border:5px solid #AAA;
   width: 80%;
   min-height: 450px;
}
</code>```

JS

```<code><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

<script>

  google.charts.load('current', {'packages':['bar']});
  google.charts.setOnLoadCallback(drawChart);

  function drawChart() {
    var data = google.visualization.arrayToDataTable([
      ['Year', 'Sales', 'Expenses', 'Profit'],
      ['2014', 1000, 400, 200],
      ['2015', 1170, 460, 250],
      ['2016', 660, 1120, 300],
      ['2017', 1030, 540, 350]
    ]);

    var options = {
      chart: {
        title: 'Company Performance',
        subtitle: 'Sales, Expenses, and Profit: 2014-2017',
      },
      bars: 'vertical',
      vAxis: {format: 'decimal'},
      height: 400,
      colors: ['#1b9e77', '#d95f02', '#7570b3']
    };

    var chart = new google.charts.Bar(document.getElementById('chart_div'));

    chart.draw(data, google.charts.Bar.convertOptions(options));

  }

     // REFLOW
    $(window).resize(function(){
        drawChart();
    });
</code>```

答:
Change the width from 80% to 100% like this example:</p>

```<code>#chart {
    border: 5px solid #AAA;
    width: 100%;
    min-height: 450px;
}
</code>```

Body:
I am trying to make my Google Column Material Chart responsive, but it seems a bit choppy to me and doesnt work well at all. Can anyone help me make this flow well

The chart will only appear in that format when the page is loaded. It won’t dynamically resize when the browser window width is changed.

JSFIDDLE
https://jsfiddle.net/rbla/Le8g0sw0/8/

HTML

CSS

#chart {
   border:5px solid #AAA;
   width: 80%;
   min-height: 450px;
}

JS

Body:
Change the width from 80% to 100% like this example:

#chart {
    border: 5px solid #AAA;
    width: 100%;
    min-height: 450px;
}


问:
How can I create continuous border line for table?

I am creating a table using html. I want to create continuous lines for different table rows without connecting lines for the first row. Using border-collapse attribute of table could help join the lines but this would also join the line of header, leaving no space between cells of header. What can I do to connect lines with blue circle without affecting the highest one?
Here's the fiddle, html and css codes.

```<code>    <table id="table">
        <tr>
            <td>ENTERPRISE</td>
            <td>PRIMARY VARIABLES</td>
            <td>SECONDARY VARIABLES</td>
        </tr>
        <tr>
            <td>Labor Market</td>
            <td>N/A</td>
            <td>Both events had an excellent attendance from C-level executives, with the CFO Forum having its highest attendance rate so far.</td>
        </tr>
        <tr>
            <td>Economic Diversification</td>
            <td>recently hosted two of its flagship events in Hong Kong, the INED Forum and CBA Forum.</td>
            <td>For further information on previous INED and CFO events, please click here to access the Pursuits Resource Centre.</td>
        </tr>
        <tr>
            <td>Innovation and R&D</td>
            <td>He has vast experience working with insurance audit clients, and will be working on two global accounts.</td>
            <td>Sandy specialises in tax, and brings vast knowledge with her in terms of asset management and private equity</td>
        </tr>
    </table>

#table tr:first-of-type td{
    font-size: 18pt;
    color: rgb(0,37,122);
    background-color: rgb(152,198,234);
    text-align: center;
    font-family: "Arial Bold";
    height: 18px;
}
#table tr:not(:first-of-type){
    font-family: "Arial Regular";
    font-size: 14pt;
    color: rgb(51,51,51);
}
td{
    padding-top: 18px;/*2.3653%*/
    padding-bottom: 18px;
    padding-right: 57px; /*3.5625%*/
    border-style:none none solid none;
}
tr td:last-of-type{
    padding-right: 0;
}
</code>```

答:
Thicken your td border until it blurs it out! (worked for me)

```<code>table td{
   border: 2px solid black;
}
</code>```

And then make the transparent lines thinner (1px) if necessary.

Body:
I am creating a table using html. I want to create continuous lines for different table rows without connecting lines for the first row. Using border-collapse attribute of table could help join the lines but this would also join the line of header, leaving no space between cells of header. What can I do to connect lines with blue circle without affecting the highest one?
Here's the fiddle, html and css codes.

            ENTERPRISE
            PRIMARY VARIABLES
            SECONDARY VARIABLES

            Labor Market
            N/A
            Both events had an excellent attendance from C-level executives, with the CFO Forum having its highest attendance rate so far.

            Economic Diversification
            recently hosted two of its flagship events in Hong Kong, the INED Forum and CBA Forum.
            For further information on previous INED and CFO events, please click here to access the Pursuits Resource Centre.

            Innovation and R&D
            He has vast experience working with insurance audit clients, and will be working on two global accounts.
            Sandy specialises in tax, and brings vast knowledge with her in terms of asset management and private equity

#table tr:first-of-type td{
    font-size: 18pt;
    color: rgb(0,37,122);
    background-color: rgb(152,198,234);
    text-align: center;
    font-family: "Arial Bold";
    height: 18px;
}
#table tr:not(:first-of-type){
    font-family: "Arial Regular";
    font-size: 14pt;
    color: rgb(51,51,51);
}
td{
    padding-top: 18px;/*2.3653%*/
    padding-bottom: 18px;
    padding-right: 57px; /*3.5625%*/
    border-style:none none solid none;
}
tr td:last-of-type{
    padding-right: 0;
}

Body:
Thicken your td border until it blurs it out! (worked for me)

table td{
   border: 2px solid black;
}

And then make the transparent lines thinner (1px) if necessary.


问:
Activiti parallel Service Tasks
I am trying to implement two service tasks in Activiti that are supposed to run in parallel. The code written below works fine randomly (and interestingly).

What I mean by that is that it occasionally prints only "first" (or "second") OR it prints two "first" one "second" etc.

QUESTION: How can I make these services run in parallel constantly; regardless of the number of services currently running?

PS: When I DELETED activiti:async="true" from the process definition, it only printed "first" or "second". I guess I need that :)

Process Definition

<?xml version='1.0' encoding='UTF-8'?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:activiti="http://activiti.org/bpmn" targetNamespace="Examples">

    <process id='testparallelact' name="Developer Hiring" isExecutable="true"
             activiti:exclusive="false" activiti:async="true">

        <startEvent id="theStart" />
        <sequenceFlow id="flow1" sourceRef="theStart" targetRef="fork" />

        <parallelGateway id="fork"  activiti:async="true" />
        <sequenceFlow sourceRef="fork" targetRef="receivePayment" />
        <sequenceFlow sourceRef="fork" targetRef="shipOrder" />

        <serviceTask id="receivePayment" name="Receive Payment"
                     activiti:async="true" activiti:exclusive="false"
                     activiti:expression="${serviceConnections.runThis2('First')}"/>

        <sequenceFlow sourceRef="receivePayment" targetRef="join" />

        <serviceTask id="shipOrder" name="Ship Order"
                     activiti:async="true" activiti:exclusive="false"
                     activiti:expression="${serviceConnections.runThis2('Second')}"/>

        <sequenceFlow sourceRef="shipOrder" targetRef="join" />

        <parallelGateway id="join" />
        <sequenceFlow sourceRef="join" targetRef="theEnd" />
        <endEvent id="theEnd" />
    </process>
</definitions>
```
<h3>Graphical rendering of process definition</h3>
<p><img src="https://i.stack.imgur.com/9aiwJ.png" alt="Graphical rendering of process definition" /></p>
<h2>Code for "runThis2"</h2>
<pre class="lang-java prettyprint-override"><code>public void runThis2(String test1) throws InterruptedException {
    while(true)
    {
        Thread.sleep(1000);
        System.out.println(test1);
    }
}
</code>```

答:
Combination of "async" and "exclusive" flags matters

It's important to understand how jobs are executed inside the Activiti engine.
The following forum thread does a pretty good job describing it:

https://community.alfresco.com/thread/221453-multiinstance-wont-run-task-in-parallel

A key excerpt from one of the original Activiti architects, Tijs Rademakers, on 2013-10-25:

The parallel gateway and multiinstance constructs are able to run
multiple user tasks in parallel for example. But for service and
script tasks they are basically executed serial still. Async can
change this behavior if you also set exclusive to false (the default
is true). Then the job executor will just execute all jobs available
and not serially. So give it a try to set async to true and exclusive
to false.

Now, by setting activiti:async="true" and activiti:exclusive="false", what you have effectively done is create a "wait state" in the process by allocating the service tasks (usually processed serially) to the Job Executor.

But:

How many jobs are executed in parallel and

when they are executed by the Job Scheduler

is now completely controlled by the Job Executor configuration. (Size of thread pool, timeouts, number of concurrent jobs, size of job block are all configurable.)

Now, this isn't exactly what you expect, meaning, it depends on the size of your job queue, how many jobs are taken in a single sweep and the duration of each job as to when the service tasks will be executed. Meaning, they MIGHT be executed in parallel and they MIGHT be executed serially. Equally, you can't control their order since once again it is on the Job Executor to determine what it does and when.

Ok, so assuming this meets your requirement...

Optimistic Locking concept

...there is still one more issue you may experience (in fact, it is the reason the activiti:exclusive flag was introduced in the first place). On completion of the service tasks, the execution context will be committed to the process instance record in the database as well as to the history records. Activiti uses "Optimistic Locking" for records for performance purposes.

Now if your process branches complete relatively close to each other in time, then it is possible (actually highly likely) you WILL receive an Optimistic Locking Exception on the DB update, like this:

09:59:52,432 [flowable-async-job-executor-thread-2] ERROR org.flowable.job.service.impl.asyncexecutor.DefaultAsyncRunnableExecutionExceptionHandler  - Job 12575 failed org.flowable.engine.common.api.FlowableOptimisticLockingException: ProcessInstance[12567] was updated by another transaction concurrently

(Note: The error above is not actually from Activiti but instead from a project called "Flowable". However they both had essentially the same codebase as Activiti 6 at the time this question was originally asked. (November 2017).)

This will cause the service task to be tagged as FAILED and it will be re-attempted. This can be problematic if you are making external calls to SOR (System of Record) or other legacy systems. (Consider what would happen if your flight was actually successfully booked, but the call to reserve it is made a second time because it was perceived to have failed.)

All good fun stuff and all things that can be resolved with good design and use of best practices.

Hope this helps you understand what is going on.

Greg@BP3

Further reading

The Alfresco forums post contains several dead links. Below are live links.

Dead Issue Tracker links

Dead: http://jira.codehaus.org/browse/ACT-1814 =>

Alive: https://activiti.atlassian.net/browse/ACT-1814

Dead: http://jira.codehaus.org/browse/ACT-2126 =>

Alive: https://activiti.atlassian.net/browse/ACT-2126

Additional note: Activiti currently (2019) uses neither of those two (codehaus.org or atlassian.net) trackers anymore. Instead they use this GitHub tracker: https://github.com/Activiti/Activiti/issues

Dead FAQ link

Dead: http://activiti.org/faq.html#WhatIsTheDifferenceBetweenProcessConcurrencyAndJavaConcurrency =>

Alive: https://web.archive.org/web/20170830013058/https://www.activiti.org/about

Activiti flags

activiti:async flag: https://www.activiti.org/userguide/#asyncContinuations

activiti:exclusive flag: https://www.activiti.org/userguide/#exclusiveJobs

Comment by developer Joram Barrez on using activiti:async="true" with activiti:exclusive="false":

Dead: https://community.alfresco.com/thread/220483-what-is-activitiexclusivefalse-mean

Alive: https://archive.ph/zldgz

Camunda Manual

"Camunda" is yet another "Activiti" fork. So the technical details will not be completely identical between the two. However inside the Camunda manual there is a nice general discussion about transactions and the concept of "Optimistic Locking":

https://docs.camunda.org/manual/latest/user-guide/process-engine/transactions-in-processes/#optimistic-locking

https://docs.camunda.org/manual/latest/user-guide/process-engine/the-job-executor/#exclusive-jobs

ContentLicense: CC BY-SA 4.0

Title: Activiti parallel Service Tasks

Body:
I am trying to implement two service tasks in Activiti that are supposed to run in parallel. The code written below works fine randomly (and interestingly).

What I mean by that is that it occasionally prints only "first" (or "second") OR it prints two "first" one "second" etc.

QUESTION: How can I make these services run in parallel constantly; regardless of the number of services currently running?

PS: When I DELETED activiti:async="true" from the process definition, it only printed "first" or "second". I guess I need that :)

Process Definition

<?xml version='1.0' encoding='UTF-8'?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:activiti="http://activiti.org/bpmn" targetNamespace="Examples">

    <process id='testparallelact' name="Developer Hiring" isExecutable="true"
             activiti:exclusive="false" activiti:async="true">

        <startEvent id="theStart" />
        <sequenceFlow id="flow1" sourceRef="theStart" targetRef="fork" />

        <parallelGateway id="fork"  activiti:async="true" />
        <sequenceFlow sourceRef="fork" targetRef="receivePayment" />
        <sequenceFlow sourceRef="fork" targetRef="shipOrder" />

        <serviceTask id="receivePayment" name="Receive Payment"
                     activiti:async="true" activiti:exclusive="false"
                     activiti:expression="${serviceConnections.runThis2('First')}"/>

        <sequenceFlow sourceRef="receivePayment" targetRef="join" />

        <serviceTask id="shipOrder" name="Ship Order"
                     activiti:async="true" activiti:exclusive="false"
                     activiti:expression="${serviceConnections.runThis2('Second')}"/>

        <sequenceFlow sourceRef="shipOrder" targetRef="join" />

        <parallelGateway id="join" />
        <sequenceFlow sourceRef="join" targetRef="theEnd" />
        <endEvent id="theEnd" />
    </process>
</definitions>

Graphical rendering of process definition

Code for "runThis2"

public void runThis2(String test1) throws InterruptedException {
    while(true)
    {
        Thread.sleep(1000);
        System.out.println(test1);
    }
}


问:
Powershell move output of loop into Array
It seems like a simple answer but for the life of me I cant figure it out. What I am trying to do is get a bunch of email addresses out of a text file and strip out the bit of the email past the @ (e.g if the email address was adam@home.com I am interested in the adam bit).

The script below outputs the values that I want however I would like to have the output written into a array rather than to the console. What would be the best way of doing this?

Code below, thanks.

```<code># Import the text file into a variable
$import = Get-Content c:\\temp\test.txt
# Variable to hold total number of emails
$totalEmails = $import.Count
#Variable to run loop
$start = 0
#Used as a separator to look at the first part of the email address
$separator = "@"
# Will increment by two to show the first part of the array in parts which     holds the text before the @
$even = 0
# Username after @
$userName

#Strip out the text before the @
do {

    $parts = $import.split($separator)
    $even+=2

    # This line here is whats not working, I would like to have the output of     each echo written into the array $userName
    $userName = echo $parts[$even]

    $start++

} while ($totalEmails -gt $start)
</code>```

答:
Your email parsing doesn't make a  whole lot of sense.  Here's a one-liner for your question:</p>

```<code>$Arr = @(Get-Content -Path 'C:\temp\test.txt') -replace '@.+'
</code>```

Body:
It seems like a simple answer but for the life of me I cant figure it out. What I am trying to do is get a bunch of email addresses out of a text file and strip out the bit of the email past the @ (e.g if the email address was adam@home.com I am interested in the adam bit).

The script below outputs the values that I want however I would like to have the output written into a array rather than to the console. What would be the best way of doing this?

Code below, thanks.

# Import the text file into a variable
$import = Get-Content c:\\temp\test.txt
# Variable to hold total number of emails
$totalEmails = $import.Count
#Variable to run loop
$start = 0
#Used as a separator to look at the first part of the email address
$separator = "@"
# Will increment by two to show the first part of the array in parts which     holds the text before the @
$even = 0
# Username after @
$userName

#Strip out the text before the @
do {

    $parts = $import.split($separator)
    $even+=2

    # This line here is whats not working, I would like to have the output of     each echo written into the array $userName
    $userName = echo $parts[$even]

    $start++

} while ($totalEmails -gt $start)

Body:
Your email parsing doesn't make a  whole lot of sense.  Here's a one-liner for your question:

$Arr = @(Get-Content -Path 'C:\temp\test.txt') -replace '@.+'


问:
Can I know how much of a XML document is processed by an XSL transformation?
I have rather large input XML files that must be converted to a given format with XSL. These XSL files have been hand-crafted, and we want to make sure that every non-empty text node in the original document is taken into account, i.e. either value-of'ed, copy-of'ed, or read and concatenated with something else.

Is this even possible? I.e., is it possible to know how much of an input document is "covered" by an XSL transformation?

答: The only way I have done this this in the past is by stepping through the XSL with a product like Altova's XMLSpy. This is very tedious for large XSL and XML documents of course but have found it necessary sometimes to find out what is going on with transformations.</p>

OwnerDisplayName: lindelof

ContentLicense: CC BY-SA 2.5

OwnerDisplayName: Jim Anderson


问:
PHP Display as long as field is unique
In my MySQL table, there are several rows which share the same "case number". I want to display the case numbers on a page but do not want cases which have multiple rows to be displayed more than once. This is basically the code I am using:

```<code>//select all records
$query="SELECT * FROM cases ";
$rt=mysql_query($query);
echo mysql_error();

while($nt=mysql_fetch_array($rt)){

echo $nt['case'];
echo $nt['date_created'];

}
</code>```

Any ideas?

答:
their is two ways you can do it.

either use distinct or group by like

```<code>SELECT
  DISTINCT(case),
  date_created
FROM
  cases;
</code>```

or

```<code>SELECT
  *
FROM
  cases
GROUP BY
  case;
</code>```

Title: PHP Display as long as field is unique

Body:
In my MySQL table, there are several rows which share the same "case number". I want to display the case numbers on a page but do not want cases which have multiple rows to be displayed more than once. This is basically the code I am using:

//select all records
$query="SELECT * FROM cases ";
$rt=mysql_query($query);
echo mysql_error();

while($nt=mysql_fetch_array($rt)){

echo $nt['case'];
echo $nt['date_created'];

}

Any ideas?

Body:
their is two ways you can do it.

either use distinct or group by like

SELECT
  DISTINCT(case),
  date_created
FROM
  cases;

or

SELECT
  *
FROM
  cases
GROUP BY
  case;


问:
How to search specific row for first 5 consecutive blank cells?
I am having trouble figuring out how to locate the first 5 consecutive blank cells in a specific row after column F.

The specific row number is found by searching column B in Sheet2 for a value selected from a drop down list in Sheet1, cell A1:

```<code>Dim myValue As Range 'value selected from a drop down list
Dim findRow As Range
Dim targetRow As Long

Set myValue = Sheets("Sheet1").Range("A1") 'location of drop down list
Set findRow = Sheets("Sheet2").Range("B:B").Find(What:=myValue, LookIn:=xlValues) 'searches column B in Sheet2 for the selected drop down list value
targetRow = findRow.Row

Sheets("Sheet2").Select

'// code to locate first 5 consecutive blank cells after column F //
</code>```

How would I then search targetRow for the first 5 consecutive blank cells after column F?

答:
Maybe like so. Use SpecialCells to look for blank areas. Is it exactly 5 blanks or at least 5?</p>

```<code>Sub x()

Dim myValue As Range 'value selected from a drop down list
Dim findRow As Range
Dim targetRow As Long
Dim r As Range

Set myValue = Sheets("Sheet1").Range("A1") 'location of drop down list
Set findRow = Sheets("Sheet2").Range("B:B").Find(What:=myValue, LookIn:=xlValues) 'searches column B in Sheet2 for the selected drop down list value
If Not findRow Is Nothing Then
    targetRow = findRow.Row
    With Sheets("Sheet2")
        For Each r In .Range(.Cells(targetRow, "F"), .Cells(targetRow, .Columns.Count)).SpecialCells(xlCellTypeBlanks).Areas
            If r.Count = 5 Then
                MsgBox r.Address
                Exit Sub
            End If
        Next r
    End With
End If

End Sub
</code>```

Body:
I am having trouble figuring out how to locate the first 5 consecutive blank cells in a specific row after column F.

The specific row number is found by searching column B in Sheet2 for a value selected from a drop down list in Sheet1, cell A1:

Dim myValue As Range 'value selected from a drop down list
Dim findRow As Range
Dim targetRow As Long

Set myValue = Sheets("Sheet1").Range("A1") 'location of drop down list
Set findRow = Sheets("Sheet2").Range("B:B").Find(What:=myValue, LookIn:=xlValues) 'searches column B in Sheet2 for the selected drop down list value
targetRow = findRow.Row

Sheets("Sheet2").Select

'// code to locate first 5 consecutive blank cells after column F //

How would I then search targetRow for the first 5 consecutive blank cells after column F?

Body:
Maybe like so. Use SpecialCells to look for blank areas. Is it exactly 5 blanks or at least 5?

Sub x()

Dim myValue As Range 'value selected from a drop down list
Dim findRow As Range
Dim targetRow As Long
Dim r As Range

Set myValue = Sheets("Sheet1").Range("A1") 'location of drop down list
Set findRow = Sheets("Sheet2").Range("B:B").Find(What:=myValue, LookIn:=xlValues) 'searches column B in Sheet2 for the selected drop down list value
If Not findRow Is Nothing Then
    targetRow = findRow.Row
    With Sheets("Sheet2")
        For Each r In .Range(.Cells(targetRow, "F"), .Cells(targetRow, .Columns.Count)).SpecialCells(xlCellTypeBlanks).Areas
            If r.Count = 5 Then
                MsgBox r.Address
                Exit Sub
            End If
        Next r
    End With
End If

End Sub


问:
How to repair a Parallel for()-loop not to return a different value than a serial for()-loop?
I'm having an issue with the following code. The code works with no errors but I'm receiving different output values when using a parallel for loop vs a regular for loop. I need to get the parallel for loop working properly because I run this code thousands of times. Does anyone know why my parallel for loop is returning different outputs?

```<code>private object _lock = new object();

public double CalculatePredictedRSquared()
    {
        double press = 0, tss = 0, press2 = 0, press1 = 0;
        Vector<double> output = CreateVector.Dense(Enumerable.Range(0, 400).Select(i => Convert.ToDouble(i)).ToArray());
        List<double> input1 = new List<double>(Enumerable.Range(0, 400).Select(i => Convert.ToDouble(i)));
        List<double> input2 = new List<double>(Enumerable.Range(200, 400).Select(i => Convert.ToDouble(i)));

            Parallel.For(0, output.Count, i =>
            {
                ConcurrentBag<MultipleRegressionInfo> listMRInfoBag = new ConcurrentBag<MultipleRegressionInfo>(listMRInfo);
                ConcurrentBag<double> vectorArrayBag = new ConcurrentBag<double>(output);
                ConcurrentBag<double[]> matrixList = new ConcurrentBag<double[]>();

                lock (_lock)
                {
                    matrixList.Add(input1.Where((v, k) => k != i).ToArray());
                    matrixList.Add(input2.Where((v, k) => k != i).ToArray());
                }

                var matrixArray2 = CreateMatrix.DenseOfColumnArrays(matrixList);
                var actualResult = vectorArrayBag.ElementAt(i);
                var newVectorArray = CreateVector.Dense(vectorArrayBag.Where((v, j) => j != i).ToArray());
                var items = FindBestMRSolution(matrixArray2, newVectorArray);
                double estimate1 = 0;

                if (items != null)
                {
                    lock (_lock)
                    {
                        var y = 0d;
                        var independentCount = matrixArray2.RowCount;
                        var dependentCount = newVectorArray.Count;

                        if (independentCount == dependentCount)
                        {
                            var populationCount = independentCount;
                            y = newVectorArray.Average();

                            for (int l = 0; l < matrixArray2.ColumnCount; l++)
                            {
                                var avg = matrixArray2.Column(l).Average();
                                y -= avg * items[l];
                            }
                        }

                        for (int m = 0; m < 2; m++)
                        {
                            var coefficient = items[m];

                            if (m == 0)
                            {
                                estimate1 += input1.ElementAt(i) * coefficient;
                            }
                            else
                            {
                                estimate1 += input2.ElementAt(i) * coefficient;
                            }
                        }

                        estimate1 += y;
                    }
                }
                else
                {
                    lock (_lock)
                    {
                        estimate1 = 0;
                    }
                }

                lock (_lock)
                {
                    press1 += Math.Pow(actualResult - estimate1, 2);
                }
            });

            for (int i = 0; i < output.Count; i++)
            {
                List<double[]> matrixList = new List<double[]>();
                matrixList.Add(input1.Where((v, k) => k != i).ToArray());
                matrixList.Add(input2.Where((v, k) => k != i).ToArray());
                var matrixArray = CreateMatrix.DenseOfColumnArrays(matrixList);
                var actualResult = output.ElementAt(i);
                var newVectorArray = CreateVector.Dense(output.Where((v, j) => j != i).ToArray());
                var items = FindBestMRSolution(matrixArray, newVectorArray);
                double estimate = 0;

                if (items != null)
                {
                    var y = CalculateYIntercept(matrixArray, newVectorArray, items);
                    for (int m = 0; m < 2; m++)
                    {
                        var coefficient = items[m];

                        if (m == 0)
                        {
                            estimate += input1.ElementAt(i) * coefficient;
                        }
                        else
                        {
                            estimate += input2.ElementAt(i) * coefficient;
                        }
                    }
                }
                else
                {
                    estimate = 0;
                }

                press2 += Math.Pow(actualResult - estimate, 2);
            }

            tss = CalculateTotalSumOfSquares(vectorArray.ToList());
            var test1 = 1 - (press1 / tss);
            var test2 = 1 - (press2 / tss);
}

public Vector<double> CalculateWithQR(Matrix<double> x, Vector<double> y)
    {
        Vector<double> result = null;

            result = MultipleRegression.QR(x, y);

            for (int i = 0; i < result.Count; i++)
            {
                var value = result.ElementAt(i);

                if (Double.IsNaN(value) || Double.IsInfinity(value))
                {
                    return null;
                }
            }

        return result;
    }

    public Vector<double> CalculateWithNormal(Matrix<double> x, Vector<double> y)
    {
        Vector<double> result = null;

            result = MultipleRegression.NormalEquations(x, y);

            for (int i = 0; i < result.Count; i++)
            {
                var value = result.ElementAt(i);

                if (Double.IsNaN(value) || Double.IsInfinity(value))
                {
                    return null;
                }
            }

        return result;
    }

    public Vector<double> CalculateWithSVD(Matrix<double> x, Vector<double> y)
    {
        Vector<double> result = null;

            result = MultipleRegression.Svd(x, y);

            for (int i = 0; i < result.Count; i++)
            {
                var value = result.ElementAt(i);

                if (Double.IsNaN(value) || Double.IsInfinity(value))
                {
                    return null;
                }
            }

        return result;
    }

    public Vector<double> FindBestMRSolution(Matrix<double> x, Vector<double> y)
    {
        Vector<double> result = null;

            result = CalculateWithNormal(x, y);

            if (result != null)
            {
                return result;
            }
            else
            {
                result = CalculateWithSVD(x, y);

                if (result != null)
                {
                    return result;
                }
                else
                {
                    result = CalculateWithQR(x, y);

                    if (result != null)
                    {
                        return result;
                    }
                }
            }

        return result;
    }

public double CalculateTotalSumOfSquares(List<double> dependentVariables)
    {
        double tts = 0;

            for (int i = 0; i < dependentVariables.Count; i++)
            {
                tts += Math.Pow(dependentVariables.ElementAt(i) - dependentVariables.Average(), 2);
            }

        return tts;
    }
</code>```

Actual Output (Updated results):

```<code>test1 = 137431.12889999992 (parallel for loop)

test2 = 7.3770258447689254E- (regular for loop)
</code>```

Epilogue: How to setup an MCVE-compliant testing

This may be a fair way to prepare an indeed fully reproducible setup of an MCVE-code + A/B/C/... DataSET-s, put inside a ready-to-run [IDE & Testing Sandbox, hyperlinked here][1], so that Community members can click a re-run button and focus on root-cause analysis, not on decoding and re-engineering the heaps of incomplete SLOCs.

If this runs for the O/P, it will run for other Community Members, whom the O/P has asked for an answer or help.

Try it online!

My new version of the code:

```<code>public double CalculatePredictedRSquared()
    {
        Vector<double> output = CreateVector.Dense(Enumerable.Range(0, 400).Select(i => Convert.ToDouble(i)).ToArray());
        List<double> input1 = new List<double>(Enumerable.Range(0, 400).Select(i => Convert.ToDouble(i)));
        List<double> input2 = new List<double>(Enumerable.Range(200, 400).Select(i => Convert.ToDouble(i)));
        double tss = CalculateTotalSumOfSquares(output.ToList());

        IEnumerable<int> range = Enumerable.Range(0, output.Count);
        var query = range.Select(i => DoIt(i, output, input1, input2));
        var result = 1 - (query.Sum() / tss);
        return result;
    }

    public double DoIt(int i, Vector<double> output, List<double> input1, List<double> input2)
    {
        List<double[]> matrixList = new List<double[]>
                {
                    input1.Where((v, k) => k != i).ToArray(),
                    input2.Where((v, k) => k != i).ToArray()
                };
        var matrixArray = CreateMatrix.DenseOfColumnArrays(matrixList);
        var actualResult = output.ElementAt(i);
        var newVectorArray = CreateVector.Dense(output.Where((v, j) => j != i).ToArray());
        var items = FindBestMRSolution(matrixArray, newVectorArray);
        double estimate = 0;

        if (items != null)
        {
            var y = CalculateYIntercept(matrixArray, newVectorArray, items);
            for (int m = 0; m < 2; m++)
            {
                var coefficient = items[m];

                if (m == 0)
                {
                    estimate += input1.ElementAt(i) * coefficient;
                }
                else
                {
                    estimate += input2.ElementAt(i) * coefficient;
                }
            }
        }
        else
        {
            estimate = 0;
        }

       return Math.Pow(actualResult - estimate, 2);
    }
</code>```

答:
A few things:

```<code>lock (_lock)
{
   matrixList.Add(input1.Where((v, k) => k != i).ToArray());
   matrixList.Add(input2.Where((v, k) => k != i).ToArray());
}
</code>```

You're adding items to a collection that is already thread-safe by design, so no need to lock. While List is not thread-safe, it should be OK to read from it concurrently. From the documentation:

It is safe to perform multiple read operations on a List, but issues can occur if the collection is modified while it’s being read. To ensure thread safety, lock the collection during a read or write operation. To enable a collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.

Also note that matrixList is stored in a local variable; in this case, the collection can't be called from multiple threads because the entire body of the delegate is guaranteed to run on the same thread - it won't be the case that half of the body of the Parallel.For loop will be run on thread A and that the other half will be run on thread B, for example.

Similarly, there's no reason to lock while making changes to estimate1 because it can't possibly be modified from other threads.

Disclaimer: there's no guarantee as to the degree of parallelism of the Parallel.For loop overall. There's not even a guarantee that it will run in parallel at all.

press1 and press2, however, are not local variables, so you do need to synchronize these somehow. (It would be better if you found some way to avoid locking every time, though, because that'll at least partially kill the point of multithreading).

Perhaps most critically, ConcurrentBag is an unordered collection. You don't show all of the operations you're doing on your matrices, but if you're doing matrix multiplication anywhere this could easily cause wrong results. There is no guarantee that matrix multiplication will commute. While A * B = B * A for integers, this is not true in general for matrices. It's quite possible that your logic is subtly dependent on operations occurring in a particular order (and they won't because ConcurrentBag is unordered).

Body:
I'm having an issue with the following code. The code works with no errors but I'm receiving different output values when using a parallel for loop vs a regular for loop. I need to get the parallel for loop working properly because I run this code thousands of times. Does anyone know why my parallel for loop is returning different outputs?

private object _lock = new object();

public double CalculatePredictedRSquared()
    {
        double press = 0, tss = 0, press2 = 0, press1 = 0;
        Vector<double> output = CreateVector.Dense(Enumerable.Range(0, 400).Select(i => Convert.ToDouble(i)).ToArray());
        List<double> input1 = new List<double>(Enumerable.Range(0, 400).Select(i => Convert.ToDouble(i)));
        List<double> input2 = new List<double>(Enumerable.Range(200, 400).Select(i => Convert.ToDouble(i)));

            Parallel.For(0, output.Count, i =>
            {
                ConcurrentBag<MultipleRegressionInfo> listMRInfoBag = new ConcurrentBag<MultipleRegressionInfo>(listMRInfo);
                ConcurrentBag<double> vectorArrayBag = new ConcurrentBag<double>(output);
                ConcurrentBag<double[]> matrixList = new ConcurrentBag<double[]>();

                lock (_lock)
                {
                    matrixList.Add(input1.Where((v, k) => k != i).ToArray());
                    matrixList.Add(input2.Where((v, k) => k != i).ToArray());
                }

                var matrixArray2 = CreateMatrix.DenseOfColumnArrays(matrixList);
                var actualResult = vectorArrayBag.ElementAt(i);
                var newVectorArray = CreateVector.Dense(vectorArrayBag.Where((v, j) => j != i).ToArray());
                var items = FindBestMRSolution(matrixArray2, newVectorArray);
                double estimate1 = 0;

                if (items != null)
                {
                    lock (_lock)
                    {
                        var y = 0d;
                        var independentCount = matrixArray2.RowCount;
                        var dependentCount = newVectorArray.Count;

                        if (independentCount == dependentCount)
                        {
                            var populationCount = independentCount;
                            y = newVectorArray.Average();

                            for (int l = 0; l < matrixArray2.ColumnCount; l++)
                            {
                                var avg = matrixArray2.Column(l).Average();
                                y -= avg * items[l];
                            }
                        }

                        for (int m = 0; m < 2; m++)
                        {
                            var coefficient = items[m];

                            if (m == 0)
                            {
                                estimate1 += input1.ElementAt(i) * coefficient;
                            }
                            else
                            {
                                estimate1 += input2.ElementAt(i) * coefficient;
                            }
                        }

                        estimate1 += y;
                    }
                }
                else
                {
                    lock (_lock)
                    {
                        estimate1 = 0;
                    }
                }

                lock (_lock)
                {
                    press1 += Math.Pow(actualResult - estimate1, 2);
                }
            });

            for (int i = 0; i < output.Count; i++)
            {
                List<double[]> matrixList = new List<double[]>();
                matrixList.Add(input1.Where((v, k) => k != i).ToArray());
                matrixList.Add(input2.Where((v, k) => k != i).ToArray());
                var matrixArray = CreateMatrix.DenseOfColumnArrays(matrixList);
                var actualResult = output.ElementAt(i);
                var newVectorArray = CreateVector.Dense(output.Where((v, j) => j != i).ToArray());
                var items = FindBestMRSolution(matrixArray, newVectorArray);
                double estimate = 0;

                if (items != null)
                {
                    var y = CalculateYIntercept(matrixArray, newVectorArray, items);
                    for (int m = 0; m < 2; m++)
                    {
                        var coefficient = items[m];

                        if (m == 0)
                        {
                            estimate += input1.ElementAt(i) * coefficient;
                        }
                        else
                        {
                            estimate += input2.ElementAt(i) * coefficient;
                        }
                    }
                }
                else
                {
                    estimate = 0;
                }

                press2 += Math.Pow(actualResult - estimate, 2);
            }

            tss = CalculateTotalSumOfSquares(vectorArray.ToList());
            var test1 = 1 - (press1 / tss);
            var test2 = 1 - (press2 / tss);
}

public Vector<double> CalculateWithQR(Matrix<double> x, Vector<double> y)
    {
        Vector<double> result = null;

            result = MultipleRegression.QR(x, y);

            for (int i = 0; i < result.Count; i++)
            {
                var value = result.ElementAt(i);

                if (Double.IsNaN(value) || Double.IsInfinity(value))
                {
                    return null;
                }
            }

        return result;
    }

    public Vector<double> CalculateWithNormal(Matrix<double> x, Vector<double> y)
    {
        Vector<double> result = null;

            result = MultipleRegression.NormalEquations(x, y);

            for (int i = 0; i < result.Count; i++)
            {
                var value = result.ElementAt(i);

                if (Double.IsNaN(value) || Double.IsInfinity(value))
                {
                    return null;
                }
            }

        return result;
    }

    public Vector<double> CalculateWithSVD(Matrix<double> x, Vector<double> y)
    {
        Vector<double> result = null;

            result = MultipleRegression.Svd(x, y);

            for (int i = 0; i < result.Count; i++)
            {
                var value = result.ElementAt(i);

                if (Double.IsNaN(value) || Double.IsInfinity(value))
                {
                    return null;
                }
            }

        return result;
    }

    public Vector<double> FindBestMRSolution(Matrix<double> x, Vector<double> y)
    {
        Vector<double> result = null;

            result = CalculateWithNormal(x, y);

            if (result != null)
            {
                return result;
            }
            else
            {
                result = CalculateWithSVD(x, y);

                if (result != null)
                {
                    return result;
                }
                else
                {
                    result = CalculateWithQR(x, y);

                    if (result != null)
                    {
                        return result;
                    }
                }
            }

        return result;
    }

public double CalculateTotalSumOfSquares(List<double> dependentVariables)
    {
        double tts = 0;

            for (int i = 0; i < dependentVariables.Count; i++)
            {
                tts += Math.Pow(dependentVariables.ElementAt(i) - dependentVariables.Average(), 2);
            }

        return tts;
    }

Actual Output (Updated results):

test1 = 137431.12889999992 (parallel for loop)

test2 = 7.3770258447689254E- (regular for loop)

Epilogue: How to setup an MCVE-compliant testing

This may be a fair way to prepare an indeed fully reproducible setup of an MCVE-code + A/B/C/... DataSET-s, put inside a ready-to-run [IDE & Testing Sandbox, hyperlinked here][1], so that Community members can click a re-run button and focus on root-cause analysis, not on decoding and re-engineering the heaps of incomplete SLOCs.

If this runs for the O/P, it will run for other Community Members, whom the O/P has asked for an answer or help.

<a href="https://tio.run/##7VnfT@M4EH7vX@F7S46SK9W9QZFYYE9IC8u1iNVphU4mdYvBSbq2U6h2@du5sZ3fcdIUyu3L5qFN7PE342/G44nji11f@C8vsaDhHE1WQpJgv1d88o4jxogvaRQK7y8SEk79isQnGn6rNF2RJ1ltuuMET6HBu8LiQaS951jeXRDpXcSBghbeCRWS09tYa2yUAp0E8yM2J7ccr5EakzknQgBey9SOo9CPOSdho91NWiryJxTPw0hI3dMLcUDEAvsEXXIypb4k07H4FmN46H3vIYQW8S2jPvIZFgIdY@bHDGuLVKeWUEKcLrEkKLq9B3vRvyzyH9AIheQxaXJc0JVATSP4JxkWyRVPjGLH1bApOEpHLBRLADvoI5neEJhIoFTrJy0xLNzvqfv9DOgaTIn4gcE7RFEsF7EEkWOgUBLT652QUBDnNFT0YZDzxjicEwcg/xwMXG9ClE8cikaHCJyyJFx6V9GJhnSo68LDEed45bhurvgTBE2mloagdS/hp9jzeqWtqoadVA0HGyjLtKnrEnMMgcq8jxFXFhtaIWLjUPaRgimJfy89qSuP7Q94fnAeM0kXjOTL4iycRYeIgfnnY3UPUsmUOo108pEFnuzKU@KWOha0I5u0pUya6XZE/npziCBeOX1S3mjBBUGnSrS69NJy9Apza511atWV6/OOplPHhJ/35Y5w4jjLPnpwlacf0G8jRO3huw5t@Cq05/rslpgnCvSwYbY2z3WjWZufZ5AY4yDUIsLJDbLoUIDYlzFmYyIgPACw7FrvlJEAmD@CgG8YDy66zsfY00UFNKfjXtNx34EOpYpChlaZ7SMNpx@IitoJTFXlW6dIS79ikwUtyZlpfkwSYU2MzpBjlIKBYcxY16BqjcPmYelEFY2D6X6rDHBAFgR@QqlzCQwpkuCNo0fd3o5SwyhT5yUQjRiaoZolowqu2zi@mYfUwkW0SDbV1MSqvv1WjFV9UkeQtDEkdbdlYuqaRVzNTiKm4wP@Dsocm5VmjEBsZ8dthWufa7Yel/OqK40ah7lFy9dhrdDuSIP9btbNV3bTPui519zTW0tRYCgKgKIh/LVxsd7nfkRmM@pTYvytzQ9u1nhLhWKgYm/wVjfkaWFnlNQjpUQIlBYsfC2rWhMTZOvGDt/B2JYQKBmwsit47q1vsXKx9fRaSfldjd1GpZHU3MCSegnxLqNHp7T57ubG9dHQWhKUnqrpK1uL1KxFCmuxWG1CS3VZ1g0tlMHWaqzcb0tE2yuotldMVSqn9yicEqa3UzAlYO9bKFnqpO1VQGkZk73I/nMWSsJ9spBtNvSN7Q3726a7TXuVtfEu02WHaU/Y2Qv52zaW5jzduqFsaNtwi7ZteQMoHmx0enVKzj465N566q3AmdOVLK6vIonZJA4@z8wBjSi@7cBKVRmktlBV@EnQqHahPVDuJHvDHwq9QXZYkh1mss/Z2VHlACcz8QuVd3@PHZPssu6nfnXAqnq0VOnnaa5TiaCyTrK@@jGDB7pB2arLfmVgNtivFDtLzGIVCsngcgq2pjRzXOOdiQt84ejhLvrxA2XNZ@GMhlSukr6uSY8TGfMwoWfN7p0blowy1ptxSW8Hv15EPMDs5/nW6D@F0Nfnnr8c/V6Onlyf/DwvT5bTX57domcthdn/41tb7rA5VjGcDLIWfnXyLBTYaatt8zYsm7lqBdhs7WLvWofb7H5zmWKfR8N2uMlU2qu5NVPqUo@tje2Gz1X1aqj0xSU7uLvGnKrPK6Lha5aUwnIma0s3dcgNUo9SU6wJLWClGnjXpi47lGstHRMaQWPKof55fnn5Dw" rel="nofollow noreferrer" title="C# (Visual C# Compiler) – Try It Online">Try it online!

My new version of the code:

public double CalculatePredictedRSquared()
    {
        Vector<double> output = CreateVector.Dense(Enumerable.Range(0, 400).Select(i => Convert.ToDouble(i)).ToArray());
        List<double> input1 = new List<double>(Enumerable.Range(0, 400).Select(i => Convert.ToDouble(i)));
        List<double> input2 = new List<double>(Enumerable.Range(200, 400).Select(i => Convert.ToDouble(i)));
        double tss = CalculateTotalSumOfSquares(output.ToList());

        IEnumerable<int> range = Enumerable.Range(0, output.Count);
        var query = range.Select(i => DoIt(i, output, input1, input2));
        var result = 1 - (query.Sum() / tss);
        return result;
    }

    public double DoIt(int i, Vector<double> output, List<double> input1, List<double> input2)
    {
        List<double[]> matrixList = new List<double[]>
                {
                    input1.Where((v, k) => k != i).ToArray(),
                    input2.Where((v, k) => k != i).ToArray()
                };
        var matrixArray = CreateMatrix.DenseOfColumnArrays(matrixList);
        var actualResult = output.ElementAt(i);
        var newVectorArray = CreateVector.Dense(output.Where((v, j) => j != i).ToArray());
        var items = FindBestMRSolution(matrixArray, newVectorArray);
        double estimate = 0;

        if (items != null)
        {
            var y = CalculateYIntercept(matrixArray, newVectorArray, items);
            for (int m = 0; m < 2; m++)
            {
                var coefficient = items[m];

                if (m == 0)
                {
                    estimate += input1.ElementAt(i) * coefficient;
                }
                else
                {
                    estimate += input2.ElementAt(i) * coefficient;
                }
            }
        }
        else
        {
            estimate = 0;
        }

       return Math.Pow(actualResult - estimate, 2);
    }

Body:
A few things:

lock (_lock)
{
   matrixList.Add(input1.Where((v, k) => k != i).ToArray());
   matrixList.Add(input2.Where((v, k) => k != i).ToArray());
}

You're adding items to a collection that is already thread-safe by design, so no need to lock. While List is not thread-safe, it should be OK to read from it concurrently. From the documentation:

It is safe to perform multiple read operations on a List, but issues can occur if the collection is modified while it’s being read. To ensure thread safety, lock the collection during a read or write operation. To enable a collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.

Also note that matrixList is stored in a local variable; in this case, the collection can't be called from multiple threads because the entire body of the delegate is guaranteed to run on the same thread - it won't be the case that half of the body of the Parallel.For loop will be run on thread A and that the other half will be run on thread B, for example.

Similarly, there's no reason to lock while making changes to estimate1 because it can't possibly be modified from other threads.

Disclaimer: there's no guarantee as to the degree of parallelism of the Parallel.For loop overall. There's not even a guarantee that it will run in parallel at all.

press1 and press2, however, are not local variables, so you do need to synchronize these somehow. (It would be better if you found some way to avoid locking every time, though, because that'll at least partially kill the point of multithreading).

Perhaps most critically, ConcurrentBag is an unordered collection. You don't show all of the operations you're doing on your matrices, but if you're doing matrix multiplication anywhere this could easily cause wrong results. There is no guarantee that matrix multiplication will commute. While A * B = B * A for integers, this is not true in general for matrices. It's quite possible that your logic is subtly dependent on operations occurring in a particular order (and they won't because ConcurrentBag is unordered).


问:
Using IEX API for real-time stock info (Yahoo Finance replacement)?
Just like the title says, I'm looking for a replacement source for stock info now that Yahoo has disabled the API many people have been using. The new source I've been looking at is found here: https://iextrading.com/developer/

My question is how to actually get the data in to Excel...I was thinking through VBA since that is what I had used to get the data from Yahoo. However, I think what I would like to do is well beyond my current abilities...I also tried using Excel's WEBSERVICE() function with the following URL to simply look at a price: https://api.iextrading.com/1.0/stock/aapl/price but that didn't work. From my understanding, IEX has made a plethora of data available to us for free, I just don't know how to access it. My reasoning for VBA is so that I am able to use an input list from a workbook for tickers, and would be able to put this data access in many workbooks. Any help is much appreciated. Additionally, any sort of direction as to where I can look to begin learning this on my own would be similarly welcome. Thanks.

Update: Code mentioned in my comment

```<code>Function StockPrice(ticker As String, item As String) As Double

Dim strURL As String, strCSV As Double, itemFound As Integer, tag As String

itemFound = 0
If item = "lastprice" Then
    tag = "price"
    itemFound = 1
ElseIf item = "pe" Then
    tag = "peRatio"
    itemFound = 1

End If

If itemFound = 1 Then

    strURL = "https://api.iextrading.com/1.0/stock/" & ticker & "/" & tag
    Set XMLHTTP = CreateObject("MSXML2.XMLHTTP")
    XMLHTTP.Open "GET", strURL, False
    XMLHTTP.send
    StockPrice = XMLHTTP.responseText
    Set XMLHTTP = Nothing

Else

    StockPrice = "Item Not Found"

End If

End Function
</code>```

答:
This maybe a bit simplistic, but it's a start:</p>

```<code>Sub IEX()
Dim Price As Single

    Price = Application.WebService("https://api.iextrading.com/1.0/stock/aapl/price")

End Sub
</code>```

Body:
Just like the title says, I'm looking for a replacement source for stock info now that Yahoo has disabled the API many people have been using. The new source I've been looking at is found here: https://iextrading.com/developer/

My question is how to actually get the data in to Excel...I was thinking through VBA since that is what I had used to get the data from Yahoo. However, I think what I would like to do is well beyond my current abilities...I also tried using Excel's WEBSERVICE() function with the following URL to simply look at a price: https://api.iextrading.com/1.0/stock/aapl/price but that didn't work. From my understanding, IEX has made a plethora of data available to us for free, I just don't know how to access it. My reasoning for VBA is so that I am able to use an input list from a workbook for tickers, and would be able to put this data access in many workbooks. Any help is much appreciated. Additionally, any sort of direction as to where I can look to begin learning this on my own would be similarly welcome. Thanks.

Update: Code mentioned in my comment

Function StockPrice(ticker As String, item As String) As Double

Dim strURL As String, strCSV As Double, itemFound As Integer, tag As String

itemFound = 0
If item = "lastprice" Then
    tag = "price"
    itemFound = 1
ElseIf item = "pe" Then
    tag = "peRatio"
    itemFound = 1

End If

If itemFound = 1 Then

    strURL = "https://api.iextrading.com/1.0/stock/" & ticker & "/" & tag
    Set XMLHTTP = CreateObject("MSXML2.XMLHTTP")
    XMLHTTP.Open "GET", strURL, False
    XMLHTTP.send
    StockPrice = XMLHTTP.responseText
    Set XMLHTTP = Nothing

Else

    StockPrice = "Item Not Found"

End If

End Function

Body:
This maybe a bit simplistic, but it's a start:

Sub IEX()
Dim Price As Single

    Price = Application.WebService("https://api.iextrading.com/1.0/stock/aapl/price")

End Sub


问:
OPENROWSET - Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query
Hi I am trying to insert results of a stored procedure into a table called MyBinaryTable. MyBinaryTable contains two columns: (FileId [type:int], and BulkColumn [type:varbinary(max)]). The stored procedure returns two columns called FileId and BulkColumn. When Inserting the values into MyBinaryTable from the stored procedure, I am greeted with this error:

```<code>Implicit conversion from data type varchar to varbinary(max) is not allowed.
Use the CONVERT function to run this query.
</code>```

Here is my stored procedure:

```<code>CREATE PROCEDURE [dbo].[GenerateBinary] @Route VARCHAR(300), @FileId
VARCHAR(10)
AS
Declare @sql varchar(max)
Set @sql='SELECT convert(varbinary(max),((SELECT BulkColumn FROM OPENROWSET(
BULK ''' + @Route + ''' , SINGLE_BLOB) as Data)), 0) as ''BulkColumn'',''' +
@FileId + ''' as ''FileId'''
Print @sql
Exec(@sql)
</code>```

Here is how I am inserting the values:

```<code>INSERT INTO MyBinaryTable
EXEC GenerateBinary 'xyz.docx', @FileId = 254
</code>```

答:
Do not develop bad habits. You did not specify the column list in the insert statement - that is a bad habit and the cause of our error. The resultset of your procedure is [varbinary(max), varchar(10)]. Is that the same as the structure of your table? No. And you also force another implicit conversion with your fileid argument defined as varchar. So change your procedure to define fileid as int and specify the columns in the insert statement: </p>

```<code>insert dbo.MyBinaryTable(BulkColumn, FileId)
select ...;
</code>```

Body:
Hi I am trying to insert results of a stored procedure into a table called MyBinaryTable. MyBinaryTable contains two columns: (FileId [type:int], and BulkColumn [type:varbinary(max)]). The stored procedure returns two columns called FileId and BulkColumn. When Inserting the values into MyBinaryTable from the stored procedure, I am greeted with this error:

Implicit conversion from data type varchar to varbinary(max) is not allowed.
Use the CONVERT function to run this query.

Here is my stored procedure:

CREATE PROCEDURE [dbo].[GenerateBinary] @Route VARCHAR(300), @FileId
VARCHAR(10)
AS
Declare @sql varchar(max)
Set @sql='SELECT convert(varbinary(max),((SELECT BulkColumn FROM OPENROWSET(
BULK ''' + @Route + ''' , SINGLE_BLOB) as Data)), 0) as ''BulkColumn'',''' +
@FileId + ''' as ''FileId'''
Print @sql
Exec(@sql)

Here is how I am inserting the values:

INSERT INTO MyBinaryTable
EXEC GenerateBinary 'xyz.docx', @FileId = 254

Body:
Do not develop bad habits. You did not specify the column list in the insert statement - that is a bad habit and the cause of our error. The resultset of your procedure is [varbinary(max), varchar(10)]. Is that the same as the structure of your table? No. And you also force another implicit conversion with your fileid argument defined as varchar. So change your procedure to define fileid as int and specify the columns in the insert statement:

insert dbo.MyBinaryTable(BulkColumn, FileId)
select ...;


问:
Sum list of Option[Int] Scala using fold
I was trying to sum all elements in a list, they are of type Option[Int], if any of them are None, the whole result is None, if all are Some(value), I want the sum of all of them:

```<code>val ls = List(Some(2), Some(5), Some(4), Some(1))

ls.fold(None)((rs,x) => for(n <- x; m <- rs) yield {n+m})
</code>```

but I always get None, no matter what.

答:
First use forall to check whether all values are defined or not,and then apply fold to get sum if defined else None.</p>

```<code>scala> val ls = List(Some(2), Some(5), Some(4), Some(1))
ls: List[Some[Int]] = List(Some(2), Some(5), Some(4), Some(1))

scala> if(ls.forall(_.isDefined)) Some(ls.flatten.foldLeft(0){_ + _ }) else None
res22: Option[Int] = Some(12)

scala> val ls = List(Some(2), None,Some(5), Some(4), Some(1),None)
ls: List[Option[Int]] = List(Some(2), None, Some(5), Some(4), Some(1), None)

scala> if(ls.forall(_.isDefined)) Some(ls.flatten.foldLeft(0){_ + _ }) else None
res23: Option[Int] = None

scala>
</code>```

Body:
I was trying to sum all elements in a list, they are of type Option[Int], if any of them are None, the whole result is None, if all are Some(value), I want the sum of all of them:

val ls = List(Some(2), Some(5), Some(4), Some(1))

ls.fold(None)((rs,x) => for(n <- x; m <- rs) yield {n+m})

but I always get None, no matter what.

Body:
First use forall to check whether all values are defined or not,and then apply fold to get sum if defined else None.

scala> val ls = List(Some(2), Some(5), Some(4), Some(1))
ls: List[Some[Int]] = List(Some(2), Some(5), Some(4), Some(1))

scala> if(ls.forall(_.isDefined)) Some(ls.flatten.foldLeft(0){_ + _ }) else None
res22: Option[Int] = Some(12)

scala> val ls = List(Some(2), None,Some(5), Some(4), Some(1),None)
ls: List[Option[Int]] = List(Some(2), None, Some(5), Some(4), Some(1), None)

scala> if(ls.forall(_.isDefined)) Some(ls.flatten.foldLeft(0){_ + _ }) else None
res23: Option[Int] = None

scala>


问:
ReSharper (or something like it) for SQL Server / T-SQL?
Are there any IDEs or extensions to IDEs that allow editing stored procedures as if they were programs so that if I were to place my cursor on a call to a stored procedure when editing a stored procedure, I could hit F12 and it would load the stored procedure under the cursor into the IDE?

Being able to refactor out a block of code into a new stored procedure would also be a welcome improvement.

答: The redgate toolbelt has a nice range of features.</p>


问:
Is it possible to use a user input's value in sass?
I am creating a colour palette and i have seen a lot of ways online to it using sass which i am using in the project, however i am taking a user input (hex code) and wish to print them out with different percentage variants - to show the user a lighter/darker version of #ff0000 for example.

Is this possible? I have seen things such as:

```<code>$color: #3cb878;
$light: lighten($color,15%);
$dark: darken($color,15%);
</code>```

And ideally i would like "$color" to be the user input and then i can print multiple percentage variants out.

Thanks!

答:
Not really performant and complicated and thus not recommended - but what would work if you can script the server: post the user input to server, write it to sass files and recompile the css.

Maybe a predefined set of colors the user can select from is enough here?

Body:
I am creating a colour palette and i have seen a lot of ways online to it using sass which i am using in the project, however i am taking a user input (hex code) and wish to print them out with different percentage variants - to show the user a lighter/darker version of #ff0000 for example.

Is this possible? I have seen things such as:

$color: #3cb878;
$light: lighten($color,15%);
$dark: darken($color,15%);

And ideally i would like "$color" to be the user input and then i can print multiple percentage variants out.

Thanks!


问:
Reading tomcat server.xml in junit test case
I am using java 7, and tomcat 7. I am writing few tests for my application in jUnit which uses tomcat/conf/server.xml for jndi. Here is the maven suggest folder structure.

```<code>src
|___test
    |___java
    |       |___Testcase.java
    |___resources
            |___conf
                   |___server.xml
</code>```

My sample server.xml would look like this,

```<code><Resource name="jdbc/junit_db"
        type="javax.sql.DataSource"
        factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/junit_db?zeroDateTimeBehavior=round&autoReconnect=true&dumpQueriesOnException=true"
        username="root"
        password="password"
        maxIdle="0"
        minIdle="0"
        initialSize="1"
        maxWait="5000"
        maxActive="50"
        loginTimeout="1000"
        minEvictableIdleTimeMillis="2000"
        timeBetweenEvictionRunsMillis="5000"
        validationQuery="SELECT 1"
        testOnBorrow="true"
        testOnReturn="true"
        testWhileIdle="false"
        logAbandoned="true"
        removeAbandoned="true"
        poolPreparedStatements="true"
        maxOpenPreparedStatements="10000"
        accessToUnderlyingConnectionAllowed="false"
        defaultAutoCommit="false"
        defaultReadOnly="false"
        defaultTransactionIsolation="4"/>

<Resource name="jdbc/junit_hive_db"
            type="javax.sql.DataSource"
            factory="com.office.hive.HiveDataSourceFactory"
            driverClassName="org.apache.hive.jdbc.HiveDriver"
            url="jdbc:hive2://localhost:10000/default?zeroDateTimeBehavior=round"
            username=""
            password="" />
</code>```

I want to load this server.xml into the IntialContext before running jUnit test cases. How to achieve this?

答:
Give TomcatJNDI a try. When fed with Tomcat's configuration files it will deliver all JNDI based objects that are declared in these files as soon as they are looked up. The code to achieve this is for example

```<code>TomcatJNDI tomcatJNDI = new TomcatJNDI();
tomcatJNDI.processServerXml(serverXmlFile)
tomcatJNDI.processContextXml(contextXmlFile);
tomcatJNDI.start();
</code>```

Then you can lookup the objects as you are used to:

```<code>DataSource ds = (DataSource) InitialContext.doLookup("java:comp/env/path/to/datasource")
</code>```

More about TomcatJNDI can be found here.

Body:
I am using java 7, and tomcat 7. I am writing few tests for my application in jUnit which uses tomcat/conf/server.xml for jndi. Here is the maven suggest folder structure.

src
|___test
    |___java
    |       |___Testcase.java
    |___resources
            |___conf
                   |___server.xml

My sample server.xml would look like this,

<Resource name="jdbc/junit_db"
        type="javax.sql.DataSource"
        factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/junit_db?zeroDateTimeBehavior=round&autoReconnect=true&dumpQueriesOnException=true"
        username="root"
        password="password"
        maxIdle="0"
        minIdle="0"
        initialSize="1"
        maxWait="5000"
        maxActive="50"
        loginTimeout="1000"
        minEvictableIdleTimeMillis="2000"
        timeBetweenEvictionRunsMillis="5000"
        validationQuery="SELECT 1"
        testOnBorrow="true"
        testOnReturn="true"
        testWhileIdle="false"
        logAbandoned="true"
        removeAbandoned="true"
        poolPreparedStatements="true"
        maxOpenPreparedStatements="10000"
        accessToUnderlyingConnectionAllowed="false"
        defaultAutoCommit="false"
        defaultReadOnly="false"
        defaultTransactionIsolation="4"/>

<Resource name="jdbc/junit_hive_db"
            type="javax.sql.DataSource"
            factory="com.office.hive.HiveDataSourceFactory"
            driverClassName="org.apache.hive.jdbc.HiveDriver"
            url="jdbc:hive2://localhost:10000/default?zeroDateTimeBehavior=round"
            username=""
            password="" />

I want to load this server.xml into the IntialContext before running jUnit test cases. How to achieve this?

Body:
Give TomcatJNDI a try. When fed with Tomcat's configuration files it will deliver all JNDI based objects that are declared in these files as soon as they are looked up. The code to achieve this is for example

TomcatJNDI tomcatJNDI = new TomcatJNDI();
tomcatJNDI.processServerXml(serverXmlFile)
tomcatJNDI.processContextXml(contextXmlFile);
tomcatJNDI.start();

Then you can lookup the objects as you are used to:

DataSource ds = (DataSource) InitialContext.doLookup("java:comp/env/path/to/datasource")

More about TomcatJNDI can be found here.


问:
How to make a dictionary from this data?
So the part of my data.dat looks like this:

carine        mite, tick
accipitrine   bird: hawk, falcon, or bird of prey
acipenserine  sturgeon
aedine        mosquito (genus Aedes)
alaudine      bird: skylark
alcelaphine   antelopes
alcidine      bird: auk, puffin, or murre
alectorine    crane
anatine       duck, mallard
anguilline    snake
```

<p>How could I create a dictionary that would map multiple values (e.g bird: hawk, falcon, or bird of prey maps to accipitrine) to single keys?</p>

<p>This is my code so far:</p>

```with open ('collateralAdjectives.dat', 'r') as a:
    adjectives = a.read().splitlines()

d = dict()
for item in collateralAdjectives:
    (s, c) = item.split(' ')
    d[s] = c
```

<p>This is the error that I get:</p>

```(s, c) = item.split(' ')

ValueError: too many values to unpack (expected 2)
```

答:
Assuming:

```<code>import io

s = """\
carine        mite, tick
accipitrine   bird: hawk, falcon, or bird of prey
acipenserine  sturgeon
aedine        mosquito (genus Aedes)
alaudine      bird: skylark
alcelaphine   antelopes
alcidine      bird: auk, puffin, or murre
alectorine    crane
anatine       duck, mallard
anguilline    snake"""
</code>```

Can do like this:

```<code>{i.split(' ')[0]:' '.join((i.strip() for i in i.split(' ')[1:] if i)) for i in io.StringIO(s)}
</code>```

Output:

```<code>{'accipitrine': 'bird: hawk, falcon, or bird of prey',
 'acipenserine': 'sturgeon',
 'aedine': 'mosquito (genus Aedes)',
 'alaudine': 'bird: skylark',
 'alcelaphine': 'antelopes',
 'alcidine': 'bird: auk, puffin, or murre',
 'alectorine': 'crane',
 'anatine': 'duck, mallard',
 'anguilline': 'snake',
 'carine': 'mite, tick'}
</code>```

Body:
So the part of my data.dat looks like this:

carine        mite, tick
accipitrine   bird: hawk, falcon, or bird of prey
acipenserine  sturgeon
aedine        mosquito (genus Aedes)
alaudine      bird: skylark
alcelaphine   antelopes
alcidine      bird: auk, puffin, or murre
alectorine    crane
anatine       duck, mallard
anguilline    snake

How could I create a dictionary that would map multiple values (e.g bird: hawk, falcon, or bird of prey maps to accipitrine) to single keys?

This is my code so far:

with open ('collateralAdjectives.dat', 'r') as a:
    adjectives = a.read().splitlines()

d = dict()
for item in collateralAdjectives:
    (s, c) = item.split(' ')
    d[s] = c

This is the error that I get:

(s, c) = item.split(' ')

ValueError: too many values to unpack (expected 2)

Body:
Assuming:

import io

s = """\
carine        mite, tick
accipitrine   bird: hawk, falcon, or bird of prey
acipenserine  sturgeon
aedine        mosquito (genus Aedes)
alaudine      bird: skylark
alcelaphine   antelopes
alcidine      bird: auk, puffin, or murre
alectorine    crane
anatine       duck, mallard
anguilline    snake"""

Can do like this:

{i.split(' ')[0]:' '.join((i.strip() for i in i.split(' ')[1:] if i)) for i in io.StringIO(s)}

Output:

{'accipitrine': 'bird: hawk, falcon, or bird of prey',
 'acipenserine': 'sturgeon',
 'aedine': 'mosquito (genus Aedes)',
 'alaudine': 'bird: skylark',
 'alcelaphine': 'antelopes',
 'alcidine': 'bird: auk, puffin, or murre',
 'alectorine': 'crane',
 'anatine': 'duck, mallard',
 'anguilline': 'snake',
 'carine': 'mite, tick'}


问:
How do I use starts-with with an attribute value?
My xml look a bit like this ...

```<code><DataItems>
  <DataItem name="Order Number" type="string">ABC1234</DataItem>
  <DataItem name="Customer Id" type="integer">667744</DataItem>
  <DataItem name="Customer Name" type="string">Ronnie Pickering</DataItem>
</DataItems>
</code>```

I'm trying to use XPath 1.0 to return a boolean based on whether the Order Number start with a certain string - "ABC"

I can't figure it out at all.  Help, please.

答:
This XPath,

```<code>//DataItem[@name='Order Number' and starts-with(., 'ABC')]
</code>```

will return all DataItem elements whose name is 'Order Number' and whose string value starts with 'ABC'.

Body:
My xml look a bit like this ...

<DataItems>
  <DataItem name="Order Number" type="string">ABC1234</DataItem>
  <DataItem name="Customer Id" type="integer">667744</DataItem>
  <DataItem name="Customer Name" type="string">Ronnie Pickering</DataItem>
</DataItems>

I'm trying to use XPath 1.0 to return a boolean based on whether the Order Number start with a certain string - "ABC"

I can't figure it out at all.  Help, please.

Body:
This XPath,

//DataItem[@name='Order Number' and starts-with(., 'ABC')]

will return all DataItem elements whose name is 'Order Number' and whose string value starts with 'ABC'.


问:
Why is validate() method accessible via request()?
Quoting the Laravel documentation:

By default, Laravel's base controller class uses a ValidatesRequests trait which provides a convenient method to validate incoming HTTP request with a variety of powerful validation rules

It's true, reading the code, App\Http\Controllers\Controller actually uses ValidatesRequests trait. And ValidatesRequests has a validate method.

What is really strange for me is that everywhere else in the documentation, the validate method is called on $request object. And it works this way. I can validate a form with this code:

```<code>public function store()
{
    $attributes = request()->validate([
        'name' => 'required|string|max:255',
    ]);
    // ...
}
</code>```

But I don't see any presence of a validate method on the Request class. Just a strange comment line at the beginning of the file:

```<code>/**
 * @method array validate(array $rules, array $messages = [], array $customAttributes = [])
 */
</code>```

So there are two things:

I don't know what to trust in Laravel documentation.

And I don't understand how the validation works on the $request object.

And my actual question is:

Is the initial quote I pasted from the documentation still true if I use the validate method through $request object? If so, how does it works?

答:
That "strange comment" was removed a couple days ago.

I believe Request gets its validate function from the Request::macro('validate', ...) call in FoundationServiceProvider.php. See this article for more on macros.

Body:
Quoting the Laravel documentation:

By default, Laravel's base controller class uses a ValidatesRequests trait which provides a convenient method to validate incoming HTTP request with a variety of powerful validation rules

It's true, reading the code, App\Http\Controllers\Controller actually uses ValidatesRequests trait. And ValidatesRequests has a validate method.

What is really strange for me is that everywhere else in the documentation, the validate method is called on $request object. And it works this way. I can validate a form with this code:

public function store()
{
    $attributes = request()->validate([
        'name' => 'required|string|max:255',
    ]);
    // ...
}

But I don't see any presence of a validate method on the Request class. Just a strange comment line at the beginning of the file:

/**
 * @method array validate(array $rules, array $messages = [], array $customAttributes = [])
 */

So there are two things:

I don't know what to trust in Laravel documentation.

And I don't understand how the validation works on the $request object.

And my actual question is:

Is the initial quote I pasted from the documentation still true if I use the validate method through $request object? If so, how does it works?


问:
Find a database with a particular table OR Find a table in every database of SQL Server
I have a SQL Server with hundreds of databases and each database having hundreds of tables.
Now I would like to find where in these databases is a table that I am looking for.

I could find if a table existed in individual database using

```<code>use myDatabase
select * from sys.tables  where name = 'mytable'
GO
</code>```

but using this means I have to manually change the database for hundreds of times .
I would like to find the database name only.
Is there a way out ?

答:
Okay, if you're just wanting to find each database that contains a particular table, and aren't going to be querying the table, then you can just do:

```<code>create table #t (
    DBName sysname not null
)
go
exec sp_MSforeachdb 'use [?]; if OBJECT_ID(''dbo.mytable'') is not null insert into #t (DBName) select ''?'''
go
select * from #t
go
drop table #t
</code>```

(If you're not using multiple schemas in your databases, you won't need to specify dbo in the OBJECT_ID call, otherwise I use it to avoid finding tables in the wrong schema)

Body:
I have a SQL Server with hundreds of databases and each database having hundreds of tables.
Now I would like to find where in these databases is a table that I am looking for.

I could find if a table existed in individual database using

use myDatabase
select * from sys.tables  where name = 'mytable'
GO

but using this means I have to manually change the database for hundreds of times .
I would like to find the database name only.
Is there a way out ?

Body:
Okay, if you're just wanting to find each database that contains a particular table, and aren't going to be querying the table, then you can just do:

create table #t (
    DBName sysname not null
)
go
exec sp_MSforeachdb 'use [?]; if OBJECT_ID(''dbo.mytable'') is not null insert into #t (DBName) select ''?'''
go
select * from #t
go
drop table #t

(If you're not using multiple schemas in your databases, you won't need to specify dbo in the OBJECT_ID call, otherwise I use it to avoid finding tables in the wrong schema)


问:
Creating a 2-node CouchDB 2.1 cluster
I tried to create a 2-node cluster with CouchDB 2.1 multiple times (both on windows and ubuntu) and it never worked. I did exactly as described in the official documentation here.

When I finish the cluster-configuration of the two nodes, I expect to create a database on node1, which should show up on node2. Verification on both nodes via fauxton fails also "internal server error" - that happens both under Linux (ubuntu 14.04) and windows (10, Server 2012, Server 2016) with version 2.1

Configuring both CouchDB-Nodes via API:

node1: (10.0.0.1)
1. POST {"action": "enable_cluster", "bind_address":"0.0.0.0", "username": "admin", "password":"mypassword", "node_count":"2"}

POST {"action": "enable_cluster", "bind_address":"0.0.0.0", "username": "admin", "password":"mypassword", "port": 5984, "node_count": "2", "remote_node": "10.0.0.2", "remote_current_user": "admin", "remote_current_password": "mypassword" }

POST {"action": "add_node", "host":"10.0.0.2", "port": "5984", "username": "admin", "password":"mypassword"}

POST {"action": "finish_cluster"}

http://10.0.0.1:5984/_membership
  {"all_nodes":["couchdb@localhost"],"cluster_nodes":["couchdb@10.0.0.2","couchdb@localhost"]}

node2(10.0.0.2)
same configuration as node1, but IP address for other node changes to 10.0.0.1

http://10.0.0.2:5984/_membership
  {"all_nodes":["couchdb@localhost"],"cluster_nodes":["couchdb@10.0.0.1","couchdb@localhost"]}

I never defined a zone - is this mandatory for the cluster to work?
Is anybody here who already set up a working Couchdb-Cluster with 2 or more nodes ?
Is anybody able to see a mistake I made whilst configuring the nodes? Please let me know if I can provide more information.

Help would be greatly appreciated.

best regards,
Harald

答:
I've configured a 3 node cluster of CouchDB 2

In my opinion the main difficulty is to correctly setting the networking configuration between nodes for the erlang communication.

http://docs.couchdb.org/en/2.1.1/cluster/setup.html#cluster-setup

First, you should be sure that erlang is communicating between nodes. (http://docs.couchdb.org/en/2.1.1/cluster/setup.html#first-time-in-erlang-time-to-play)

You should set the erlang node name value in the vm.args file of your couchdb installation.

The name you use should be resolvable by a DNS or local hosts file in both nodes.

Finally, when you are sure that erlang is communicating you should register both nodes in the cluster.

Title: Creating a 2-node CouchDB 2.1 cluster


问:
Meaning of "open_file:" in c?
I was going through some code demonstrating error handling in file operations.
I can't understand what does open_file: mean and what does the colon after "open_file" signify ?

```<code>#include  <stdio.h>

main()
{
   char  *filename;
    FILE  *fp1, *fp2;
    int   i, number;

    fp1 = fopen("TEST", "w");
    for(i = 10; i <= 100; i += 10)
       putw(i, fp1);

    fclose(fp1);

    printf("\nInput filename\n");

open_file:                           //What does this mean ?
    scanf("%s", filename);

    if((fp2 = fopen(filename,"r")) == NULL)
    {
       printf("Cannot open the file.\n");
       printf("Type filename again.\n\n");
       goto open_file;
    }
    elsefor(i = 1; i <= 20; i++)
    {  number = getw(fp2);
       if(feof(fp2))
       {
          printf("\nRan out of data.\n");
          break;
       }
       else
          printf("%d\n", number);
    }

    fclose(fp2);
}
</code>```

答: It's a goto label, used 7 lines below. It could be called sandeep: for all the compiler cares.</p>

Title: Meaning of "open_file:" in c?

Body:
I was going through some code demonstrating error handling in file operations.
I can't understand what does open_file: mean and what does the colon after "open_file" signify ?

#include  <stdio.h>

main()
{
   char  *filename;
    FILE  *fp1, *fp2;
    int   i, number;

    fp1 = fopen("TEST", "w");
    for(i = 10; i <= 100; i += 10)
       putw(i, fp1);

    fclose(fp1);

    printf("\nInput filename\n");

open_file:                           //What does this mean ?
    scanf("%s", filename);

    if((fp2 = fopen(filename,"r")) == NULL)
    {
       printf("Cannot open the file.\n");
       printf("Type filename again.\n\n");
       goto open_file;
    }
    elsefor(i = 1; i <= 20; i++)
    {  number = getw(fp2);
       if(feof(fp2))
       {
          printf("\nRan out of data.\n");
          break;
       }
       else
          printf("%d\n", number);
    }

    fclose(fp2);
}


问:
improve locality and decrease Cache pollution in a medical image reconstruction implementation
I'm doing a research for my University related to an Image reconstruction algorithm for medical usage.

I'm stuck in something up to 3 weeks, I need to improve the performance of the following code:

```<code>for (lor=lor0[mypid]; lor <= lor1[mypid]; lor++)
{
  LOR_X = P.symmLOR[lor].x;
  LOR_Y = P.symmLOR[lor].y;
  LOR_XY = P.symmLOR[lor].xy;
  lor_z = P.symmLOR[lor].z;
  LOR_Z_X = P.symmLOR[lor_z].x;
  LOR_Z_Y = P.symmLOR[lor_z].y;
  LOR_Z_XY = P.symmLOR[lor_z].xy;

  s0 = P.a2r[lor];
  s1 = P.a2r[lor+1];

  for (s=s0; s < s1; s++)
  {
    pixel     = P.a2b[s];
    v         = P.a2p[s];

    b[lor]    += v * x[pixel];

    p          = P.symm_Xpixel[pixel];
    b[LOR_X]  += v * x[p];

    p          = P.symm_Ypixel[pixel];
    b[LOR_Y]  += v * x[p];

    p          = P.symm_XYpixel[pixel];
    b[LOR_XY] += v * x[p];

    // do Z symmetry.
    pixel_z    = P.symm_Zpixel[pixel];
    b[lor_z]  += v * x[pixel_z];

    p          = P.symm_Xpixel[pixel_z];
    b[LOR_Z_X]  += v * x[p];

    p          = P.symm_Ypixel[pixel_z];
    b[LOR_Z_Y]  += v * x[p];

    p          = P.symm_XYpixel[pixel_z];
    b[LOR_Z_XY] += v * x[p];

   }

}
</code>```

for anyone who wants to know, The code implements the MLEM forward function and all the variables are FLOAT.

After several tests, I had notice that the big delay was on this part of the code. (you know, the 90 - 10 rule).

Later, I used Papi (http://cl.cs.utk.edu/papi/) to measure L1D cache misses. As I thought, Papi confirm that the performance decreases due to higher amount of misses, particularly for the random access to b vector (huge in size).

Reading information on the Internet I just know two options to improve performance so far: improve data locality or decrease data pollution.

To do the first improvement, I'll try to change the code to be cache aware, just like was propossed by Ulrich Drepper on What every programmer should know about the memory (www.akkadia.org/drepper/cpumemory.pdf) A.1 Matrix multiplication.

I believe that blocking the SpMV (sparse matrix-vector Multiplication) will improve the performance.

On the other hand, every time the program tried to access to b vector, we had what is known as cache pollution.

Is there's a way to do a load a value from the b vector with SIMD instruction without using the Cache?

Also, it is possible to use a function like void _mm_stream_ps(float * p , __m128 a ) to store ONE float value on the vector b without polluting the Cache?

I can't use _mm_stream_ps because always store 4 floats but the access to the b vector is clearly random.

I'd hope to be clear in my dilemma.

More info: v is the column value of an Sparse Matrix store with CRS format. I realize that other optimization could be done if I tried to change CRS format to other, however, like I said before, I'd made several test for months and I know that the performance decrease is related to random access on vector b. from 400.000.000 L1D Misses I can go to 100~ Misses when I don't store in vector b.

Thanks.

答:
I'd say, at first try to help your compiler a bit.

Declare the bounds for the outer loop
as const before the loop.

Declare all variables that you may
(all the LOR_..) as local variables,
something like:
float LOR_X = P.symmLOR[lor].x; or
size_t s0 = P.a2r[lor];

This also in particular for loop
variables if you happen to have
modern, C99 compliant, compiler: for
(size_t s=s0; s < s1; s++)

Separate load and store for your b
vector. The locations of the items
that you access, there, do not depend
on s. So create a local variable to
hold the actual value for all the
distinct cases that you handle
before the inner loop, update these local variables inside the inner
loop, and store the results after the
inner loop.

Perhaps separate your inner loop in
several. The index computations
are relatively cheap, and then your
system might better recognize the
streaming access to some of your
vectors.

Look at the assembler that your
compiler produces and identify the
code for the inner loop. Experiment a
bit to move as many of the "far" load
and stores out of that loop.

Edit: after re-reading gravitron's answer and your comment, the important thing here is really to declare the variables as local as possible and to check the assembler that the compiler succeeds in having the cache-missing loads and stores outside the inner loop.

Body:
I'm doing a research for my University related to an Image reconstruction algorithm for medical usage.

I'm stuck in something up to 3 weeks, I need to improve the performance of the following code:

for (lor=lor0[mypid]; lor <= lor1[mypid]; lor++)
{
  LOR_X = P.symmLOR[lor].x;
  LOR_Y = P.symmLOR[lor].y;
  LOR_XY = P.symmLOR[lor].xy;
  lor_z = P.symmLOR[lor].z;
  LOR_Z_X = P.symmLOR[lor_z].x;
  LOR_Z_Y = P.symmLOR[lor_z].y;
  LOR_Z_XY = P.symmLOR[lor_z].xy;

  s0 = P.a2r[lor];
  s1 = P.a2r[lor+1];

  for (s=s0; s < s1; s++)
  {
    pixel     = P.a2b[s];
    v         = P.a2p[s];

    b[lor]    += v * x[pixel];

    p          = P.symm_Xpixel[pixel];
    b[LOR_X]  += v * x[p];

    p          = P.symm_Ypixel[pixel];
    b[LOR_Y]  += v * x[p];

    p          = P.symm_XYpixel[pixel];
    b[LOR_XY] += v * x[p];

    // do Z symmetry.
    pixel_z    = P.symm_Zpixel[pixel];
    b[lor_z]  += v * x[pixel_z];

    p          = P.symm_Xpixel[pixel_z];
    b[LOR_Z_X]  += v * x[p];

    p          = P.symm_Ypixel[pixel_z];
    b[LOR_Z_Y]  += v * x[p];

    p          = P.symm_XYpixel[pixel_z];
    b[LOR_Z_XY] += v * x[p];

   }

}

for anyone who wants to know, The code implements the MLEM forward function and all the variables are FLOAT.

After several tests, I had notice that the big delay was on this part of the code. (you know, the 90 - 10 rule).

Later, I used Papi (http://cl.cs.utk.edu/papi/) to measure L1D cache misses. As I thought, Papi confirm that the performance decreases due to higher amount of misses, particularly for the random access to b vector (huge in size).

Reading information on the Internet I just know two options to improve performance so far: improve data locality or decrease data pollution.

To do the first improvement, I'll try to change the code to be cache aware, just like was propossed by Ulrich Drepper on What every programmer should know about the memory (www.akkadia.org/drepper/cpumemory.pdf) A.1 Matrix multiplication.

I believe that blocking the SpMV (sparse matrix-vector Multiplication) will improve the performance.

On the other hand, every time the program tried to access to b vector, we had what is known as cache pollution.

Is there's a way to do a load a value from the b vector with SIMD instruction without using the Cache?

Also, it is possible to use a function like void _mm_stream_ps(float * p , __m128 a ) to store ONE float value on the vector b without polluting the Cache?

I can't use _mm_stream_ps because always store 4 floats but the access to the b vector is clearly random.

I'd hope to be clear in my dilemma.

More info: v is the column value of an Sparse Matrix store with CRS format. I realize that other optimization could be done if I tried to change CRS format to other, however, like I said before, I'd made several test for months and I know that the performance decrease is related to random access on vector b. from 400.000.000 L1D Misses I can go to 100~ Misses when I don't store in vector b.

Thanks.


问:
In LLVM, what "type" is a bitcast of a function inside a call? How to access this function?
In my llvm IR code, I have the following line:

```<code>%tmp = call i32 @decf1(void (i8*)* bitcast (void (%a_type*)* @decf2 to void (i8*)*), i8 %x3, i8* @external_type)
</code>```

I am trying to extract a_type and decf2 programmatically, but I seem not to get access to them.

```<code>bool runOnFunction(Function &F) override {
errs() << "Initializing Test pass\n";

for (BasicBlock &BB : F) {
  for (Instruction &I : BB) {
    // New Instruction
    errs() << "\n\n"
           << "=====================\n"
           << "- - - - - - - - - - -\n"
           << "NewInstruction:\n";
    I.dump();
    errs() << "\n";

    // New Operands
    errs() << "- - - - - - - - - - -\n"
           << "Operands:\n";
    for (Use &U : I.operands()) {
      errs() << "Type: ";
      U->getType()->print(errs());
      errs() << "\n";
      errs() << "Name: " << U->getName() << "\n";
    }
    errs() << "\n";
}
</code>```

This pass produces me the following output for the instruction containing the cast.

```<code>=====================
- - - - - - - - - - -
NewInstruction:
  %tmp = call i32 @decf1(void (i8*)* bitcast (void (%a_type*)* @decf2 to void (i8*)*), i8 %x3, i8* @external_type)

- - - - - - - - - - -
Operands:
Type: void (i8*)*
Name:
Is Instruction: No
Is Function: No

Type: i8
Name: x3
Is Instruction: Yes
          %x3 = mul i8 %x2, %x2
Is Function: No

Type: i8*
Name: external_type
Is Instruction: No
Is Function: No

Type: i32 (void (i8*)*, i8, i8*)*
Name: decf1
Is Instruction: No
Is Function: Yes
        Is Declaration: Yes
</code>```

It seems that the first printed operand has to do with the bitcast. How can I get the bitcast and the operands/type/function it is casting?

答:
It seems that Value::stripPointerCasts() is a way to get the the cast decf2 function as a Function *.

Still need to elaborate on how to get the a_type from there.

Body:
In my llvm IR code, I have the following line:

%tmp = call i32 @decf1(void (i8*)* bitcast (void (%a_type*)* @decf2 to void (i8*)*), i8 %x3, i8* @external_type)

I am trying to extract a_type and decf2 programmatically, but I seem not to get access to them.

bool runOnFunction(Function &F) override {
errs() << "Initializing Test pass\n";

for (BasicBlock &BB : F) {
  for (Instruction &I : BB) {
    // New Instruction
    errs() << "\n\n"
           << "=====================\n"
           << "- - - - - - - - - - -\n"
           << "NewInstruction:\n";
    I.dump();
    errs() << "\n";

    // New Operands
    errs() << "- - - - - - - - - - -\n"
           << "Operands:\n";
    for (Use &U : I.operands()) {
      errs() << "Type: ";
      U->getType()->print(errs());
      errs() << "\n";
      errs() << "Name: " <getName() << "\n";
    }
    errs() << "\n";
}

This pass produces me the following output for the instruction containing the cast.

=====================
- - - - - - - - - - -
NewInstruction:
  %tmp = call i32 @decf1(void (i8*)* bitcast (void (%a_type*)* @decf2 to void (i8*)*), i8 %x3, i8* @external_type)

- - - - - - - - - - -
Operands:
Type: void (i8*)*
Name:
Is Instruction: No
Is Function: No

Type: i8
Name: x3
Is Instruction: Yes
          %x3 = mul i8 %x2, %x2
Is Function: No

Type: i8*
Name: external_type
Is Instruction: No
Is Function: No

Type: i32 (void (i8*)*, i8, i8*)*
Name: decf1
Is Instruction: No
Is Function: Yes
        Is Declaration: Yes

It seems that the first printed operand has to do with the bitcast. How can I get the bitcast and the operands/type/function it is casting?


问:
Deployed EAR file does not include 'Web Service Properties' section
I had a EAR deployed to a Websphere server by our middleware group. When I look at the EAR properties in my local server console they show the Web Service Properties like this screenshot.  This is a web service application that works using my local server in RAD.

The screenshot from the deployed application is missing the Web Services Properties section. I do not have direct access to the server, this was sent to me by my middleware resource.

There is only one EAR file that was sent to be deployed. The problem of course is that the services of the deployed application are not there.  I believe that the EAR configuration is the root cause, and I am asking how to fix this issue.  I am fairly new to Websphere, and at a loss on how to proceed. Thanks in advance!

答: The issue is the EAR is not deployed correctly. You may have to check the logs during deployment. The logs will give you the exact reason why the EAR is not completely deployed.</p>