问:
Scoring results by correct ordering of words in elasticsearch
We have an elasticsearch index with the following configuration:

```<code>PUT phonebook
{
   "settings":{
      "index":{
         "number_of_shards":8,
         "number_of_replicas":1
      }
   },
   "mappings":{
      "person":{
         "_all":{
            "enabled":false
         },
         "_source":{
            "enabled":true
         },
         "properties":{
            "id":{
               "type":"long"
            },
            "name":{
               "type":"text",
               "index_options":"positions"
            },
            "number":{
               "type":"long"
            }
         }
      }
   }
}
</code>```

It's basically a huge phonebook with billions of records. I'm searching on this index with the following query:

```<code>GET /contacts/contact/_search
{
   "size":0,
   "query":{
      "match":{
         "name":{
            "fuzziness":1,
            "query":"george bush",
            "operator":"and"
         }
      }
   },
   "aggs":{
      "by_number":{
         "terms":{
            "field":"number",
            "size":10,
            "order":{
               "max_score":"desc"
            }
         },
         "aggs":{
            "max_score":{
               "max":{
                  "script":"_score"
               }
            },
            "sample":{
               "top_hits":{
                  "size":1
               }
            }
         }
      }
   }
}
</code>```

The results are grouped by the field "number" and best match for each number is returned this way. But what I need is a custom scoring/sorting of the results based on the correctness of the order of words in the results.
So that "George Bush" should always score better than "Bush George" for the query of "George Bush".
match_phrase search is not suitable for me as I use fuzziness on my search.

答:
How about something like this:

```<code>  "query":{
    "simple_query_string": {
      "query": "\"barack~ obama~\"~3",
      "fields": ["name"]
    }
  },
</code>```

The trailing ~ following the tokens are for the fuzzy aspect and the ~3 following the phrase handles slop which is the concept that I think you are looking for with phrase queries.  I think the results will be scored such that "Barack Obama" is scored higher than "Obama Barack" with this.  You can come up with a custom bool query that mimics this where the should clause handles both the fuzziness and the slop aspects.

Some resources:

Simple Query String

Mixing It Up - Elasticsearch documentation on slop

Body:
We have an elasticsearch index with the following configuration:

PUT phonebook
{
   "settings":{
      "index":{
         "number_of_shards":8,
         "number_of_replicas":1
      }
   },
   "mappings":{
      "person":{
         "_all":{
            "enabled":false
         },
         "_source":{
            "enabled":true
         },
         "properties":{
            "id":{
               "type":"long"
            },
            "name":{
               "type":"text",
               "index_options":"positions"
            },
            "number":{
               "type":"long"
            }
         }
      }
   }
}

It's basically a huge phonebook with billions of records. I'm searching on this index with the following query:

GET /contacts/contact/_search
{
   "size":0,
   "query":{
      "match":{
         "name":{
            "fuzziness":1,
            "query":"george bush",
            "operator":"and"
         }
      }
   },
   "aggs":{
      "by_number":{
         "terms":{
            "field":"number",
            "size":10,
            "order":{
               "max_score":"desc"
            }
         },
         "aggs":{
            "max_score":{
               "max":{
                  "script":"_score"
               }
            },
            "sample":{
               "top_hits":{
                  "size":1
               }
            }
         }
      }
   }
}

The results are grouped by the field "number" and best match for each number is returned this way. But what I need is a custom scoring/sorting of the results based on the correctness of the order of words in the results.
So that "George Bush" should always score better than "Bush George" for the query of "George Bush".
match_phrase search is not suitable for me as I use fuzziness on my search.

Body:
How about something like this:

  "query":{
    "simple_query_string": {
      "query": "\"barack~ obama~\"~3",
      "fields": ["name"]
    }
  },

The trailing ~ following the tokens are for the fuzzy aspect and the ~3 following the phrase handles slop which is the concept that I think you are looking for with phrase queries.  I think the results will be scored such that "Barack Obama" is scored higher than "Obama Barack" with this.  You can come up with a custom bool query that mimics this where the should clause handles both the fuzziness and the slop aspects.

Some resources:

Simple Query String

Mixing It Up - Elasticsearch documentation on slop


来源: stackexchange

问:
Spark Standalone Application hangs at the last point
I am relatively new to Spark and wrote a simple script using python and spark SQL. My problem is that it is perfectly allright at the starting phase of the execution but gradually it slowed down and at the end of the last phase the whole application hangs. Here is the code snippet where the application get hanged-

```<code>hivectx.registerDataFrameAsTable(aggregatedDataV1,"aggregatedDataV1")
q1 = "SELECT *, (Total_Sale/Sale_Weeks) as Average_Sale_Per_SaleWeek, (Total_Weeks/Sale_Weeks) as Velocity FROM aggregatedDataV1"
aggregatedData = hivectx.sql(q1)
aggregatedData.show(100)
</code>```

I have got the following on the terminal -

```<code>16/03/29 09:05:50 INFO TaskSetManager: Finished task 96.0 in stage 416.0 (TID 19992) in 41924 ms on 10.9.0.7 (104/200)
16/03/29 09:05:50 INFO TaskSetManager: Finished task 108.0 in stage 416.0 (TID 20004) in 24608 ms on 10.9.0.10 (105/200)
16/03/29 09:05:50 INFO TaskSetManager: Finished task 105.0 in stage 416.0 (TID 20001) in 24610 ms on 10.9.0.10 (106/200)
16/03/29 09:05:55 INFO TaskSetManager: Starting task 116.0 in stage 416.0 (TID 20012, 10.9.0.10, partition 116,NODE_LOCAL, 2240 bytes)
16/03/29 09:06:31 INFO TaskSetManager: Finished task 99.0 in stage 416.0 (TID 19995) in 78435 ms on 10.9.0.7 (110/200)
16/03/29 09:06:40 INFO TaskSetManager: Starting task 119.0 in stage 416.0 (TID 20015, 10.9.0.10, partition 119,NODE_LOCAL, 2240 bytes)
16/03/29 09:07:12 INFO TaskSetManager: Starting task 122.0 in stage 416.0 (TID 20018, 10.9.0.7, partition 122,NODE_LOCAL, 2240 bytes)
16/03/29 09:07:16 INFO TaskSetManager: Starting task 123.0 in stage 416.0 (TID 20019, 10.9.0.7, partition 123,NODE_LOCAL, 2240 bytes)
16/03/29 09:07:28 INFO TaskSetManager: Finished task 111.0 in stage 416.0 (TID 20007) in 110198 ms on 10.9.0.7 (114/200)
16/03/29 09:07:52 INFO TaskSetManager: Starting task 124.0 in stage 416.0 (TID 20020, 10.9.0.10, partition 124,NODE_LOCAL, 2240 bytes)
16/03/29 09:08:08 INFO TaskSetManager: Finished task 110.0 in stage 416.0 (TID 20006) in 150023 ms on 10.9.0.7 (115/200)
16/03/29 09:08:12 INFO TaskSetManager: Finished task 113.0 in stage 416.0 (TID 20009) in 154120 ms on 10.9.0.7 (116/200)
16/03/29 09:08:16 INFO TaskSetManager: Finished task 116.0 in stage 416.0 (TID 20012) in 145691 ms on 10.9.0.10 (117/200)
</code>```

After that the whole application is hanged. I have my spark-env.sh file with the following configuration :

```<code>export SPARK_PUBLIC_DNS="azuremaster.westus.cloudapp.azure.com"
export SPARK_EXECUTOR_INSTANCES=1
export SPARK_EXECUTOR_CORES=2
export SPARK_EXECUTOR_MEMORY=3G
export SPARK_MASTER_OPTS="-Dspark.deploy.defaultCores=1"
export SPARK_WORKER_PORT="8888"
export PYSPARK_PYTHON=/usr/bin/python3
export PYSPARK_DRIVER_PYTHON=python3
export SPARK_HIVE=true
</code>```

I am using 3 Node Clusters. One Master with 2 Cores and 4 GB Memory and other two Workers with 4 Cores and 7 GB Memory.

I have tried the following so far -

Increasing no of executor cores

Increasing/Decreasing executors with different amounts of resources.

I have --

Spark -1.6.0

Java 7

scala 2.10.4

Prebuilt hadoop 2.3

Prebuilt Hive

After running the application for 2 hours it is giving the errors.

The terminal window

I'm guessing a configuration needs to be tuned but have no idea which one. I will be grateful if anyone have knowledge whats is going on and suggest some helping information.

Thanks in advance.

答:
Your out of memory error suggests you may need to bump up the driver-memory. By default this is 2GB. I have run into similar issues and passing custom configurations via a spark-submit flag works.

Try this:

spark-submit --driver-memory = 4g app.py

If this doesn't work; you'll need to look in the spark web UI or ganglia to see how much memory is being used during execution. Ganglia will allow you to see how much memory is being used and if you need more memory for the job.

ContentLicense: CC BY-SA 3.0

Body:
I am relatively new to Spark and wrote a simple script using python and spark SQL. My problem is that it is perfectly allright at the starting phase of the execution but gradually it slowed down and at the end of the last phase the whole application hangs. Here is the code snippet where the application get hanged-

hivectx.registerDataFrameAsTable(aggregatedDataV1,"aggregatedDataV1")
q1 = "SELECT *, (Total_Sale/Sale_Weeks) as Average_Sale_Per_SaleWeek, (Total_Weeks/Sale_Weeks) as Velocity FROM aggregatedDataV1"
aggregatedData = hivectx.sql(q1)
aggregatedData.show(100)

I have got the following on the terminal -

16/03/29 09:05:50 INFO TaskSetManager: Finished task 96.0 in stage 416.0 (TID 19992) in 41924 ms on 10.9.0.7 (104/200)
16/03/29 09:05:50 INFO TaskSetManager: Finished task 108.0 in stage 416.0 (TID 20004) in 24608 ms on 10.9.0.10 (105/200)
16/03/29 09:05:50 INFO TaskSetManager: Finished task 105.0 in stage 416.0 (TID 20001) in 24610 ms on 10.9.0.10 (106/200)
16/03/29 09:05:55 INFO TaskSetManager: Starting task 116.0 in stage 416.0 (TID 20012, 10.9.0.10, partition 116,NODE_LOCAL, 2240 bytes)
16/03/29 09:06:31 INFO TaskSetManager: Finished task 99.0 in stage 416.0 (TID 19995) in 78435 ms on 10.9.0.7 (110/200)
16/03/29 09:06:40 INFO TaskSetManager: Starting task 119.0 in stage 416.0 (TID 20015, 10.9.0.10, partition 119,NODE_LOCAL, 2240 bytes)
16/03/29 09:07:12 INFO TaskSetManager: Starting task 122.0 in stage 416.0 (TID 20018, 10.9.0.7, partition 122,NODE_LOCAL, 2240 bytes)
16/03/29 09:07:16 INFO TaskSetManager: Starting task 123.0 in stage 416.0 (TID 20019, 10.9.0.7, partition 123,NODE_LOCAL, 2240 bytes)
16/03/29 09:07:28 INFO TaskSetManager: Finished task 111.0 in stage 416.0 (TID 20007) in 110198 ms on 10.9.0.7 (114/200)
16/03/29 09:07:52 INFO TaskSetManager: Starting task 124.0 in stage 416.0 (TID 20020, 10.9.0.10, partition 124,NODE_LOCAL, 2240 bytes)
16/03/29 09:08:08 INFO TaskSetManager: Finished task 110.0 in stage 416.0 (TID 20006) in 150023 ms on 10.9.0.7 (115/200)
16/03/29 09:08:12 INFO TaskSetManager: Finished task 113.0 in stage 416.0 (TID 20009) in 154120 ms on 10.9.0.7 (116/200)
16/03/29 09:08:16 INFO TaskSetManager: Finished task 116.0 in stage 416.0 (TID 20012) in 145691 ms on 10.9.0.10 (117/200)

After that the whole application is hanged. I have my spark-env.sh file with the following configuration :

export SPARK_PUBLIC_DNS="azuremaster.westus.cloudapp.azure.com"
export SPARK_EXECUTOR_INSTANCES=1
export SPARK_EXECUTOR_CORES=2
export SPARK_EXECUTOR_MEMORY=3G
export SPARK_MASTER_OPTS="-Dspark.deploy.defaultCores=1"
export SPARK_WORKER_PORT="8888"
export PYSPARK_PYTHON=/usr/bin/python3
export PYSPARK_DRIVER_PYTHON=python3
export SPARK_HIVE=true

I am using 3 Node Clusters. One Master with 2 Cores and 4 GB Memory and other two Workers with 4 Cores and 7 GB Memory.

I have tried the following so far -

Increasing no of executor cores

Increasing/Decreasing executors with different amounts of resources.

I have --

Spark -1.6.0

Java 7

scala 2.10.4

Prebuilt hadoop 2.3

Prebuilt Hive

After running the application for 2 hours it is giving the errors.

The terminal window

I'm guessing a configuration needs to be tuned but have no idea which one. I will be grateful if anyone have knowledge whats is going on and suggest some helping information.

Thanks in advance.


问:
Specifying Azure Kubernetes virtual network address space
How do I specify the address space of the virtual network created when provisioning a Kubernetes cluster? If that is not possible, how do I move the cluster to a different address space?</p>

答:
At this time is not possible. However, you can use ACS Engine to do this. Engine is where our dev work. Vnet support is there now and will come to ACS on the future.

See http://github.com/azure/acs-engine


问:
Center middle of 3 images, left and right images align to middle image
Is it possible to have the middle of 3 images centered horizontally, then have the left image align to the left of the middle image, and the right image align to the right of the middle image?

https://jsfiddle.net/0wk84qaf/

```<code><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<div style="text-align:center;">
    <div style="display:inline-block;">
        <img src="http://rubyandgrace.cdudigital.com/wp-content/uploads/2017/06/award1-2.jpg" style="vertical-align:middle;" />
        <img src="http://rubyandgrace.cdudigital.com/wp-content/uploads/2017/06/award3-2.jpg" style="vertical-align:middle;" />
        <img src="http://rubyandgrace.cdudigital.com/wp-content/uploads/2017/06/award2-2.jpg" style="vertical-align:middle;" />
    </div>
</div>
</code>```

You can see, currently, the whole div is centered, but the middle image is slightly off-center, whereas I'd like it to be bang in the middle...

答:
just make your img tags have width of 33.3%

```

Body:
Is it possible to have the middle of 3 images centered horizontally, then have the left image align to the left of the middle image, and the right image align to the right of the middle image?

https://jsfiddle.net/0wk84qaf/

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

You can see, currently, the whole div is centered, but the middle image is slightly off-center, whereas I'd like it to be bang in the middle...

Body:
just make your img tags have width of 33.3%

<div style="text-align:center;"><div style="display:inline-block;
%"><img src="http://rubyandgrace.cdudigital.com/wp-content/uploads/2017/06/award1-2.jpg" style="vertical-align:middle;width:33.3%" /><img src="http://rubyandgrace.cdudigital.com/wp-content/uploads/2017/06/award3-2.jpg" style="vertical-align:middle;width:33.3%" /><img src="http://rubyandgrace.cdudigital.com/wp-content/uploads/2017/06/award2-2.jpg" style="vertical-align:middle;width:33.3%" /></div></div>


问:
polymer-cli - getting "Can’t find variable: babelHelpers" when I set compile to true
I use Polymer 2.0 and my build setting is:

```<code>"builds": [
{
  "name": "bundled",
  "bundle": true,
  "js": { "compile": true},
  "css": { "minify": true },
  "html": { "minify": true }
}]
</code>```

I get "Can’t find variable: babelHelpers" error after build.

The Polymer CLI version that I use is 1.1.0.

EDIT: I was using polymer-cli locally. After installing latest polymer-cli globally, now I get “Constructor requires ‘new’ operator” on safari and “Failed to construct ‘HTMLElement’: Please use the ‘new’ operator, this DOM object constructor cannot be called as a function.” on chrome.

EDIT2: used webcomponents-loader.js instead of webcomponents-lite.js and my problem solved.

答: I was getting same error.  Problem was that index.html is technically my entrypoint in my polymer.json config, but my actual served up entrypoint is another file that comes from my server (_Layout.cshtml since I'm using .NET on my backend).  What I needed to do was take the code in my compiled index.html file and move it over to my CSHTML file once I set "compile": true.  Problem solved.

Body:
I use Polymer 2.0 and my build setting is:

"builds": [
{
  "name": "bundled",
  "bundle": true,
  "js": { "compile": true},
  "css": { "minify": true },
  "html": { "minify": true }
}]

I get "Can’t find variable: babelHelpers" error after build.

The Polymer CLI version that I use is 1.1.0.

EDIT: I was using polymer-cli locally. After installing latest polymer-cli globally, now I get “Constructor requires ‘new’ operator” on safari and “Failed to construct ‘HTMLElement’: Please use the ‘new’ operator, this DOM object constructor cannot be called as a function.” on chrome.

EDIT2: used webcomponents-loader.js instead of webcomponents-lite.js and my problem solved.


问:
react router + express with webpack
I am working on a blog. I have an express server which has various APIs like /api/getpost , /api/newpost etc etc.

I have my react components and i am using react router 4 ,  my routes are /addpost /home etc. etc.

In my server.js i served my static files(bundel.js)

```<code>app.use(express.static(__dirname+'/dist'));
</code>```

then

```<code>app.get('*', function(res,req){
    res.sendFile(path.resolve(__dirname, 'index.html'));
});
</code>```

localhost:3000/ loads homepage properly
localhost:3000/home also loads

localhost:3000/api/getpost fails !

i couldn't find solution of this after several hours of searching

EDIT :
I am adding more details !

```<code>import { BrowserRouter as Router, Route,Redirect} from 'react-router-dom';
<Router>
 <div>
  <Route exact path="/" component ={App}>
  <Route path="/home" component ={Home}>
 </div>
</Router>
</code>```

and my server.js file

var express  = require('express');
var mongoose = require('mongoose');
    var router = require('./router');
var User = require('./app/components/data');
var path = require('path');

const app = express();

app.use(express.static(__dirname+'/dist'));

app.get('*', function(res,req){
  res.sendFile(path.resolve(__dirname ,'/dist/index.html'));
});

app.use('/api',router);

router.route('/getpost').get(function(req, res) {
   User.find(function(err,user) {
   if (err)
    res.send(err);
   res.json(user);
 });
});

 ```

 error which i got in my app :
Error: Network Error Stack trace: createError@localhost:3000/bundle.js:9088:15 handleError@localhost:3000/bundle.js:8935:14

答: Try moving your api route above your app.get(*, ... route.  That route is catching every request, so when you hit localhost:3000/api/getpost, react-router tries to load in the component that matches /api/getpost and fails since you have not and do not want to define it.

Title: react router + express with webpack

Body:
I am working on a blog. I have an express server which has various APIs like /api/getpost , /api/newpost etc etc.

I have my react components and i am using react router 4 ,  my routes are /addpost /home etc. etc.

In my server.js i served my static files(bundel.js)

app.use(express.static(__dirname+'/dist'));

then

app.get('*', function(res,req){
    res.sendFile(path.resolve(__dirname, 'index.html'));
});

localhost:3000/ loads homepage properly
localhost:3000/home also loads

localhost:3000/api/getpost fails !

i couldn't find solution of this after several hours of searching

EDIT :
I am adding more details !

import { BrowserRouter as Router, Route,Redirect} from 'react-router-dom';
<Router>

  <Route exact path="/" component ={App}>
  <Route path="/home" component ={Home}>

</Router>

and my server.js file

var express  = require('express');
var mongoose = require('mongoose');
    var router = require('./router');
var User = require('./app/components/data');
var path = require('path');

const app = express();

app.use(express.static(__dirname+'/dist'));

app.get('*', function(res,req){
  res.sendFile(path.resolve(__dirname ,'/dist/index.html'));
});

app.use('/api',router);

router.route('/getpost').get(function(req, res) {
   User.find(function(err,user) {
   if (err)
    res.send(err);
   res.json(user);
 });
});

 error which i got in my app :
Error: Network Error Stack trace: createError@localhost:3000/bundle.js:9088:15 handleError@localhost:3000/bundle.js:8935:14


问:
CLOSED: pug - make text italic within h1 tag
I am using the pugjs template engine on my expressjs server, but between the pugjs docs and my own experimentation, I cannot figure out how to make text italicized within an h1 tag.

In regular HTML, what I want looks like this:
This is text: This is Italic Text
. But when I try the equivalent of that in pugjs: h1 This is Text i This is Italic Text, it doesn't work, outputting

This is Text i This is Italic Text

instead of what I want. If I drop the i part to the next line, the text comes out on separate lines, not what i want.

If anyone knows how to do this, please let me know. I am sure I am missing something very basic. Thanks in advance, Neil

答:
From reading a previously deleted answer on this question, I got an idea:

```<code>h1
  |This is Text
  i  This is Italic Text
</code>```

This renders out to:

```<code><h1>This is Text<i> This is Italic Text</i></h1>
</code>```

which is perfect for what I need.

I'm sorry that I lost the alias of the user who posted the previous answer, but thank you.

Body:
From reading a previously deleted answer on this question, I got an idea:

h1
  |This is Text
  i  This is Italic Text

This renders out to:

<h1>This is Text<i> This is Italic Text</i></h1>

which is perfect for what I need.

I'm sorry that I lost the alias of the user who posted the previous answer, but thank you.


问:
Optimizing SQL CONTAINS while using OR
In SQL Server 2014 I'm attempting to use contains for several different terms.  The query is taking an exceptionally long time and I'm curious if it is faster to separate the contains commands or to use them in the same statement.

For example, is it faster to use:

```<code>WHERE CONTAINS(text, '"term1" or "term2" or "term3"')
</code>```

or

```<code>WHERE CONTAINS(text, "term1") OR CONTAINS(text, "term2") OR CONTAINS(text, "term3")
</code>```

Does it make a difference using one vs the other, or am I possibly missing something more fundamental here?

Update:

I ended up running the first and it erred out after 50 minutes (possibly separate server connection issues). I ran the second option and the query completed in 19 minutes. I would still be interested if anybody had a better answer for why one may perform better than the other. Thanks.

答:
I just noticed after doing all this that I performed this test in SqlServer 2016 - so there could possibly be performance improvements between versions

I would say that the first is the better option based on the following experiment, but note that I am basing this off a relatively small set of test data and the set up I did this with may be overly simplified, but for I think that it demonstrates in theory why the first should be better.

You can repeat this test with your own data using Microsoft Management studio by turning on the Execution plan (See Method 1 of the accepted answer in this post)

Running the two queries with only two terms gives the below execution:

As you can see in the first query, there is 68% of the cost is for scanning the index and 32% for the table function (Contains method). And the second, as there is now two function calls to analyse - the cost of the table function is increased. And here are the timings of the queries.

-- Query 1

(296 row(s) affected)

SQL Server Execution Times:
    CPU time = 0 ms,  elapsed time = 73 ms.

-- Query 2

(296 row(s) affected)

SQL Server Execution Times:
    CPU time = 0 ms,  elapsed time = 100 ms.

If I increase the number of terms in the query you can see how this affects the execution.

And gives the timings:

-- Query 1

(441 row(s) affected)

SQL Server Execution Times:
    CPU time = 0 ms,  elapsed time = 80 ms.

-- Query 2

(441 row(s) affected)

SQL Server Execution Times:
    CPU time = 0 ms,  elapsed time = 143 ms.

Comparing the timings, there was a 7ms increase for the first style but 43ms for the second - almost a 50% increase.
The increase between the two sets also shows that the performance should be better for the first query style will scale better as you increase the number of terms.

Title: Optimizing SQL CONTAINS while using OR

Body:
In SQL Server 2014 I'm attempting to use contains for several different terms.  The query is taking an exceptionally long time and I'm curious if it is faster to separate the contains commands or to use them in the same statement.

For example, is it faster to use:

WHERE CONTAINS(text, '"term1" or "term2" or "term3"')

or

WHERE CONTAINS(text, "term1") OR CONTAINS(text, "term2") OR CONTAINS(text, "term3")

Does it make a difference using one vs the other, or am I possibly missing something more fundamental here?

Update:

I ended up running the first and it erred out after 50 minutes (possibly separate server connection issues). I ran the second option and the query completed in 19 minutes. I would still be interested if anybody had a better answer for why one may perform better than the other. Thanks.


问:
Are there any fairly mature Lisp/Scheme/Clojure compilers for .Net CLR?
I am seeing several variants out there; ClojureCLR, LSharp, IronScheme, IronLisp, among others.  Are any of these actively maintained and/or anywhere close to "mature", or are they mostly experiments or dust-gatherers?  Which would be considered the most mature framework for compiling to .Net dll's and referencing other .Net dll's, if any?  Does any integrate well with Visual Studio a la at least a "Create Lisp Project" feature?</p>

答:
IronLisp is dead and superseded by IronScheme, which in turn is still beta.

L Sharp and ClojureCLR are similar and they follow same idea of modern Lisp for CLR (in contrast to IronScheme, which tries to just implement the R6RS standard on the new platform). ClojureCLR seems to be more popular than L Sharp, and Java's Clojure community is growing up quickly, so you can use many of its libraries in your .NET application.

I know that for ClojureCLR there is a VS2010 plugin available.

I believe, ClojureCLR now is the most intensively developed, so I would bet on it. On other hand, Clojure (and so ClojureCLR) still changes, and future versions of it may differ a lot from current state, which is not very good for long term production project. From this point IronScheme, which implements old verified R6RS, is more preferable. I can't say a lot of L#, but I guess it is somewhere between ClojureCLR and IronScheme.

So, actual decision depends on your personal needs: stability, size of a (potential) project, and, of course, language features - don't forget to learn a bit about all of three.

ContentLicense: CC BY-SA 2.5


问:
unable to print object ('po') in xcode6 beta 6 osx swift project: (Error in auto-import: failed to get module '__ObjC' from AST context)
An attempt to print object (po command) in xcode 6 beta 6 OSX Swift project results in this error message:

```<code>(lldb) po managedObject
error: Error in auto-import:
failed to get module '__ObjC' from AST context
</code>```

In this case the object in question is an instance of NSManagedObject.

Any advice on how to help auto-import in getting __ObjC module into LLVM Abstract Syntax Tree context?

答:
I discussed a similar error message with an Apple engineer at WWDC2017. It seems like this issue can have many causes, and I am aware that mine is slightly different than the one described above.

My team spent weeks trying to figure this out, and it ended up being a bug on Apple's compiler, which we could never have figured out by ourselves. Also, it has a VERY easy workaround.

So, this is just me posting the fix here, in order to maximize the probability that someone else does a search for this confusing error message, and finds this answer.

So, here it is. In our case, we had an Objective-C project using a mix of Swift and Objective-C frameworks. This fix might apply in slightly different contexts, just try it.

There happens to be a bug with the way the compiling flags get aggregated from the frameworks and the project, and the "pure Objective-C" project "activates" it.

Solution: add one single, empty Swift file ("Whatever.swift", or whatever) in your Objective-C project, making it not-pure-objective-c any more  (new->file->Swift file, don't create the bridging header. The file will only contain the import of Foundation).

Body:
An attempt to print object (po command) in xcode 6 beta 6 OSX Swift project results in this error message:

(lldb) po managedObject
error: Error in auto-import:
failed to get module '__ObjC' from AST context

In this case the object in question is an instance of NSManagedObject.

Any advice on how to help auto-import in getting __ObjC module into LLVM Abstract Syntax Tree context?


问:
Instantiate GameObject without prefabs
I am trying to make an inventory system for my game, however as a complete beginner, I have some questions after digging in google.

The way I design my inventory system contains 3 main class:

SceneItem <--- this is the gameObject in the scene

ItemData <--- this store all info of the item, and is put into the item class

InventoryHandler

ItemName <--- an enum that store all name of the items available

So every time my character pick up an item, the ItemData inside the SceneItem duplicate itself, and add into the list which is in the InventoryHandler, such that I can store all item's data.

Yet the problem is, if I create it this way, I afraid I will need to create a bunch of prefab, each of them contains their individual script. Which I think is not a good way to do it.

I thought the ItemName enum could help me reduce some work, by connecting the loot of the monster to the prefab's filename, but still I need to create a bunch of prefab for that

Is there anyway to get around it? Or is the way I do it just not optimal by itself?

Thanks in advance.

答:
Everything inside Unity is either Object or GameObject <- which still inherits Object. Meaning if you create a prefab or a non Prefab to instantiate it. It will still have its GameObject class and will have a Transform class inside it. What ever you do, it will contain code or scripts. A Prefab is just a set of data that will be duplicated in the process. Do not worry about making a lot of duplicate items as Unity knows how to deal with memory properly with the same Instantiated items. You don't even have to worry about properties that you will have in your classes. The only thing that you need to think about is the runtime performance not how many Scripts will be included in your single items. It may look big in your scene that you have scripts and models. But when it is compiled, it is not a big deal anymore. Garbage collection helps a lot in runtime but if you have strong references to your objects, the CLR will never know when to collect it, this is something that you would have to do for yourself.

UPDATE

Saying you want the XML to be a part of your solution. It is a good practice but not exactly performance wise, because you will eat up some of the run time process when you instantiate it. Every time you create the item, you will have to tell the GameEngine to look up some data on your XML model. Which is not that bad if you have a few. Remember since we the Language that we use is calculating on Runtime. It is will be slower to look up from XML, it has too be slower. But it any case your, approach is Cleaner, separation of concern and other Developer can work on the separate file of XML.

There are a lot of XML Libraries that you can use or even, why not use JSON instead as they can be both be serialized and deserialized back and forth to your model. Why did I recommend JSON? Well if you plan on using 3rd Party Unity Game API. You will end up have to use JSON instead of XML. But like I said, there is no problem Parsing XML to JSON. It is just better to start on JSON as Other UNITY Game development tools is supporting it already.

Take a look at Firebase for Unity, where you can use RealTime database for your game.
Link in here FireBase SDK for Unity

Infact you can use. FireBase Storage, if you want to save Prefab in here as well.

The solution, that you have in mind is so simple there is no way you can go wrong with it, unless you are performance savvy, But since we have a lot of gimmick to handle performance in the future like, Runtime Prefab Download. Where you can preload the game items while you are running the game. I am sure you are aware of this. Go ahead and implement the XML or as I refer the JSON file instead.

Title: Instantiate GameObject without prefabs


问:
Select last unique polymorphic objects ordered by created at in Rails
I'm trying to get unique polymorphic objects by the value of one of the columns. I'm using Postgres.

The object has the following properties: id, available_type, available_id, value, created_at, updated_at.

I'm looking to get the most recent object per available_id (recency determined by created_at) for records with the available_type of "User".

I've been trying ActiveRecord queries like this:

```<code>Service.where(available_type: "User").order(created_at: :desc).distinct(:available_id)
</code>```

But it isn't limiting to one per available_id.

答:
Try</p>

```<code>Service.where(id: Service
              .where(available_type: "User")
              .group(:available_id)
              .maximum(:id).values)
</code>```

Body:
I'm trying to get unique polymorphic objects by the value of one of the columns. I'm using Postgres.

The object has the following properties: id, available_type, available_id, value, created_at, updated_at.

I'm looking to get the most recent object per available_id (recency determined by created_at) for records with the available_type of "User".

I've been trying ActiveRecord queries like this:

Service.where(available_type: "User").order(created_at: :desc).distinct(:available_id)

But it isn't limiting to one per available_id.

Body:
Try

Service.where(id: Service
              .where(available_type: "User")
              .group(:available_id)
              .maximum(:id).values)


问:
Auto incrementing ID as primary key on all tables
I am designing a database for an e-Commerce website which uses MySQL. I have made the list of tables necessary and all the fields necessary for the table. I have a total of 9 tables.

What I have done is include an auto incrementing ID as primary key on all the tables.

All my tables except 2 are normalized to 3NF. Two tables 'users' and 'outlets' are not normalized to 2NF.

Along the way I realized that normalization is cumbersome when using auto incrementing ID as a primary key. As normalization is not strictly required, I want to know if there are any downsides of using auto incrementing ID as primary key on all the tables?

答:
I have created a lot of tables in my time.  I have used AUTO_INCREMENT in only 1/3 of them.  The rest had what seemed like a "perfectly good 'natural' PK", so I went that way.

"Normal Form" is a textbook way to get you started.  In real life (in my opinion), NF later takes a back seat to performance and other considerations.

For InnoDB tables, you really should have an explicit PRIMARY KEY (either auto_inc or natural).

A generic pattern where auto_inc slows things down is a many:many mapping table, as Renzo points out, and which I discuss here:  http://mysql.rjweb.org/doc.php/index_cookbook_mysql#many_to_many_mapping_table

In InnoDB, the PRIMARY KEY is stored (clustered) with the data, so the index structure (a BTree) occupies virtually no extra space.  Each secondary index occupies a separate BTree that implicitly includes the PK column(s).


问:
Logstash: MySQL to Elasticsearch (large table)?
I am attempting to import a rather chunky database into Elasticsearch. It has 4m rows across 2 columns (VARCHAR(250) & INT(20)).

When I run the logstash.conf file to import the database into Elasticsearch and I add a LIMIT 0,100 in my SQL command the command runs without any problems. All of the rows end up returned by Logstash in Terminal and then I can see them in the relevant node in Elasticsearch.

When I try to run all of the rows through Logstash at once, it outputs:

Settings: Default pipeline workers: 1
Pipeline Main Started

And nothing more happens.

How do I add such a large table into Elasticsearch?

Here's my logstash.conf script:

```<code>input{

  jdbc {
jdbc_driver_library => "/opt/logstash/mysql-connector-java-5.1.39/mysql-connector-java-5.1.39-bin.jar"

jdbc_driver_class => "com.mysql.jdbc.Driver"

jdbc_connection_string => "jdbc:mysql://<ip number>:3306/database"

jdbc_validate_connection => true

jdbc_user => "elastic"

jdbc_password => "password"

schedule => "* * * * *"

    statement => "name, id from master_table"

    }

}

 output

 {
  elasticsearch
    {

    index => "search"
    document_type => "name"
    document_id => "%{id}"
    hosts => "127.0.0.1:9200"
}stdout { codec => json_lines }
}
</code>```

答:
```<code>input {
    jdbc {
        ...
        jdbc_page_size => 100000
        jdbc_paging_enabled => true
    }
}
...
</code>```

Res:

[logstash.inputs.jdbc] SELECT count(*) AS count FROM (SELECT * FROM my_table) AS t1 LIMIT 1

[logstash.inputs.jdbc]  SELECT * FROM (SELECT * FROM my_table) AS t1 LIMIT 100000 OFFSET 0

[logstash.inputs.jdbc]SELECT * FROM (SELECT * FROM my_table) AS t1 LIMIT 100000 OFFSET 100000

...

Body:
I am attempting to import a rather chunky database into Elasticsearch. It has 4m rows across 2 columns (VARCHAR(250) & INT(20)).

When I run the logstash.conf file to import the database into Elasticsearch and I add a LIMIT 0,100 in my SQL command the command runs without any problems. All of the rows end up returned by Logstash in Terminal and then I can see them in the relevant node in Elasticsearch.

When I try to run all of the rows through Logstash at once, it outputs:

Settings: Default pipeline workers: 1
Pipeline Main Started

And nothing more happens.

How do I add such a large table into Elasticsearch?

Here's my logstash.conf script:

input{

  jdbc {
jdbc_driver_library => "/opt/logstash/mysql-connector-java-5.1.39/mysql-connector-java-5.1.39-bin.jar"

jdbc_driver_class => "com.mysql.jdbc.Driver"

jdbc_connection_string => "jdbc:mysql://<ip number>:3306/database"

jdbc_validate_connection => true

jdbc_user => "elastic"

jdbc_password => "password"

schedule => "* * * * *"

    statement => "name, id from master_table"

    }

}

 output

 {
  elasticsearch
    {

    index => "search"
    document_type => "name"
    document_id => "%{id}"
    hosts => "127.0.0.1:9200"
}stdout { codec => json_lines }
}

Body:
input {
    jdbc {
        ...
        jdbc_page_size => 100000
        jdbc_paging_enabled => true
    }
}
...

Res:

[logstash.inputs.jdbc] SELECT count(*) AS count FROM (SELECT * FROM my_table) AS t1 LIMIT 1

[logstash.inputs.jdbc]  SELECT * FROM (SELECT * FROM my_table) AS t1 LIMIT 100000 OFFSET 0

[logstash.inputs.jdbc]SELECT * FROM (SELECT * FROM my_table) AS t1 LIMIT 100000 OFFSET 100000

...


问:
Server-side highscores for a Javascript-written game
I'm implementing a simple game in Javascript, and am interested in having an online highscores table for it, so that players can compete against one another. I've two concerns about this:

What is the simplest server-side program I need for this purpose? I don't need a full-fledged "web application", just something simple that gets POST requests with highscores, updates a database and sends back lists of scores. I'm familiar with Django. What are your suggestions?

How can I make the highscores table reasonably secure? I'm aware that making it bulletproof against competent and dedicated hackers is difficult, but I wouldn't want anyone with access to the Javascript sourcecode to be able to submit fictitious scores too simply. Any tools for this purpose?

答:
You could use a combination of one of the methods above, as well as simply requiring the user to be registered to be able to post high scores. Non registered users could view their current score compared to existing high scores, but in order to post your high score online, you must have already logged in with your registered account, or provide it when the app goes to update the score online.

A simple message along the lines of "Your high score is X, and ranks ### in the high score table. To post this score online, please register with us first".


问:
What is the naming standard for path components?
I keep getting myself in knots when I am manipulating paths and file names because I don’t follow a naming standard for path components.

Consider the following toy problem (Windows example, but hopefully the answer should be platform independent). You have been given the path of a folder:

```<code>C:\Users\OddThinking\Documents\My Source\
</code>```

You want to walk the folders underneath and compile all the .src files to .obj files.

At some point you are looking at the following path:

```<code>C:\Users\OddThinking\Documents\My Source\Widget\foo.src

</code>```

How would you name the following path components?

```<code>A. foo
B. foo.src
C. src
D. .src
E. C:\Users\OddThinking\Documents\My Source\ (absolute path of the root)
F. Widget\foo.src (relative path of the file to absolute path of the root)
G. Widget\
H. C:\Users\OddThinking\Documents\My Source\Widget\
I. C:\Users\OddThinking\Documents\My Source\Widget\foo.src
</code>```

Here is my attempt:

A. Base name? Basename?

B. File name? Filename?

The difference is important when choosing identifier names, and I am never consistent here.

C. Extension?

D. Extension? Wait, that is what I called C.

Should I avoid storing the dot, and just put it in when required?

What if there is no dot on a particular file?

E. ?

F. ?

G. Folder? But isn’t this a Windows-specific term?

H. Path name? Pathname? Path?

I. File name? Wait, that is what I called B.

Path name? Wait, that is what I called H.

答:
No you're not crazy.

In Windows systems, sometimes the path of the directory containing the file is called path, which is how it was from the beginning. So, for example,

```<code>    x:\dir1\dir2\myfile.txt

    Windows:
    --------
        PATH:  x:\dir1\dir2
        FILE:  myfile.txt

    Unix/Linux:
    -----------
        PATH:  /dir1/dir2/myfile.txt
        FILE:  myfile.txt
</code>```

The Unix/Linux approach is a lot more logical, and that's what everyone mentioned above: path including the file name itself. However, if you type "call /?" in the Windows command line, you get this:

```<code>    %~1         - expands %1 removing any surrounding quotes (")
    %~f1        - expands %1 to a fully qualified path name
    %~d1        - expands %1 to a drive letter only
    %~p1        - expands %1 to a path only
    %~n1        - expands %1 to a file name only
    %~x1        - expands %1 to a file extension only
</code>```

So there it is, "path only" and "file name only". At the same time, they refer to the whole string as "fully qualified path name" which is understood as drive letter plus path plus file name. So there's no real truth. It's futile. You've been betrayed.

Anyway,

To answer your question

This is how I'd name your examples:

```<code>A: -
B: basename
C: extension
D: -
E: -
F: -
G: -
H: pathname (or dirname or containing path)
I: full name
</code>```

A-D-E-F have no simple nicknames. And since php is probably the most widely known cross-platform language, everyone understands "basename" and "dirname" so I'd stick with that naming. Full name is also obvious; full path would be a bit ambiguous but most of the time it means the very same thing.

ContentLicense: CC BY-SA 4.0

Body:
I keep getting myself in knots when I am manipulating paths and file names because I don’t follow a naming standard for path components.

Consider the following toy problem (Windows example, but hopefully the answer should be platform independent). You have been given the path of a folder:

C:\Users\OddThinking\Documents\My Source\

You want to walk the folders underneath and compile all the .src files to .obj files.

At some point you are looking at the following path:

C:\Users\OddThinking\Documents\My Source\Widget\foo.src

How would you name the following path components?

A. foo
B. foo.src
C. src
D. .src
E. C:\Users\OddThinking\Documents\My Source\ (absolute path of the root)
F. Widget\foo.src (relative path of the file to absolute path of the root)
G. Widget\
H. C:\Users\OddThinking\Documents\My Source\Widget\
I. C:\Users\OddThinking\Documents\My Source\Widget\foo.src

Here is my attempt:

A. Base name? Basename?

B. File name? Filename?

The difference is important when choosing identifier names, and I am never consistent here.

C. Extension?

D. Extension? Wait, that is what I called C.

Should I avoid storing the dot, and just put it in when required?

What if there is no dot on a particular file?

E. ?

F. ?

G. Folder? But isn’t this a Windows-specific term?

H. Path name? Pathname? Path?

I. File name? Wait, that is what I called B.

Path name? Wait, that is what I called H.

Body:
No you're not crazy.

In Windows systems, sometimes the path of the directory containing the file is called path, which is how it was from the beginning. So, for example,

    x:\dir1\dir2\myfile.txt

    Windows:
    --------
        PATH:  x:\dir1\dir2
        FILE:  myfile.txt

    Unix/Linux:
    -----------
        PATH:  /dir1/dir2/myfile.txt
        FILE:  myfile.txt

The Unix/Linux approach is a lot more logical, and that's what everyone mentioned above: path including the file name itself. However, if you type "call /?" in the Windows command line, you get this:

    %~1         - expands %1 removing any surrounding quotes (")
    %~f1        - expands %1 to a fully qualified path name
    %~d1        - expands %1 to a drive letter only
    %~p1        - expands %1 to a path only
    %~n1        - expands %1 to a file name only
    %~x1        - expands %1 to a file extension only

So there it is, "path only" and "file name only". At the same time, they refer to the whole string as "fully qualified path name" which is understood as drive letter plus path plus file name. So there's no real truth. It's futile. You've been betrayed.

Anyway,

To answer your question

This is how I'd name your examples:

A: -
B: basename
C: extension
D: -
E: -
F: -
G: -
H: pathname (or dirname or containing path)
I: full name

A-D-E-F have no simple nicknames. And since php is probably the most widely known cross-platform language, everyone understands "basename" and "dirname" so I'd stick with that naming. Full name is also obvious; full path would be a bit ambiguous but most of the time it means the very same thing.


问:
Advanced Filters with Multiple Criteria
I'm hoping to combine two advanced filters and can't seem to get it right.

The goal is to filter down to only the rows with both a FIN from a list and a userID from a list.  Below are the filters individually.

When I run them sequentially, only the last filter is applied.

```<code>'   Filter by FINs
LastRow2 = Sheets("FINs").Cells(Rows.Count, 1).End(xlUp).Row
Sheets("Data").Columns("B:B").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
    Sheets("FINs").Range("A1:A" & LastRow2), Unique:=False
'   Filter by userID
LastRow3 = Sheets("Users").Cells(Rows.Count, 1).End(xlUp).Row
Sheets("Data").Columns("C:C").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
    Sheets("Users").Range("A1:A" & LastRow3), Unique:=False
</code>```

Thanks!

答:
I have similar problem with AutoFilter, then pass in Array(FIN 1, FIN 2,..) and Array(User 1, User 2,...), but it didn't work exactly.

Eg,

I want list down only having FIN 1 in Col A, then Col B must be User 1
But the result is not, As long as Col A have FIN 1 it will in the list. Auto Filter will return all combination possible across these 2 arrays.

I recommend add a Unique Column, then filter using the Unique column instead.
Then filter using AutoFilter with Array as below:

```<code>ActiveSheet.Range("$A$1:$C$7").AutoFilter Field:=3,
    Criteria1:=Array("FIN 2|User 2", "FIN 4|User 4", "FIN 5|User 5", "FIN 6|User 6"),
    Operator:=xlFilterValues
</code>```

Title: Advanced Filters with Multiple Criteria

Body:
I'm hoping to combine two advanced filters and can't seem to get it right.

The goal is to filter down to only the rows with both a FIN from a list and a userID from a list.  Below are the filters individually.

When I run them sequentially, only the last filter is applied.

'   Filter by FINs
LastRow2 = Sheets("FINs").Cells(Rows.Count, 1).End(xlUp).Row
Sheets("Data").Columns("B:B").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
    Sheets("FINs").Range("A1:A" & LastRow2), Unique:=False
'   Filter by userID
LastRow3 = Sheets("Users").Cells(Rows.Count, 1).End(xlUp).Row
Sheets("Data").Columns("C:C").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
    Sheets("Users").Range("A1:A" & LastRow3), Unique:=False

Thanks!

Body:
I have similar problem with AutoFilter, then pass in Array(FIN 1, FIN 2,..) and Array(User 1, User 2,...), but it didn't work exactly.

Eg,

I want list down only having FIN 1 in Col A, then Col B must be User 1
But the result is not, As long as Col A have FIN 1 it will in the list. Auto Filter will return all combination possible across these 2 arrays.

I recommend add a Unique Column, then filter using the Unique column instead.
Then filter using AutoFilter with Array as below:

ActiveSheet.Range("$A$1:$C$7").AutoFilter Field:=3,
    Criteria1:=Array("FIN 2|User 2", "FIN 4|User 4", "FIN 5|User 5", "FIN 6|User 6"),
    Operator:=xlFilterValues


问:
Swift: Can't figure out how to save how user left tableview
Using Swift 3

I've been working on a blog reader app for a while but I've been stuck on how to save the tableview like how the user left it.

The app works by me inputing data into the mysql database which is sent using php and received with json and swift 3 to populate the tableview. I expect to update this database everyday. There are two sections in the tableview, top one is followedArray and second is mainArray which is where all the objects are after being in jsonArray. When the user clicks the follow button, that single cell is moved into followedArray where the user can see the blog being updated and receive notifications.

I've tried using UserDefaults by saving the whole array into an object but it didn't work out as it was way to complicated creating the NSData objects using NSCoding, when there is no need to save the entire array (my guess).

My question, is there a simple way that I can save how the user left the tableview?

For example: In the 'All Objects Section' lets say I have 3 cells and the user clicks the follow button on 2 cells, those 2 cells are now in the 'Followed Section' with 1 cell in 'All Objects Section'. How do I save the state of this tableview. I want to save in which section the followed cell is in now and the changed button which is now a Followed Button instead of Follow Button (saving the hidden enabled code as I hide one button to show the other).

Do I use UserDefaults or CoreData? Maybe a CocoaPod library? Do I save the entire array or just which section its in and the .hidden code? How will updating and adding new objects affect it?

I'm fairly new to Swift so if you can help it would be appreciated. Any code that you need to understand how the app works, just ask for it as I don't know what code is necessary to show. I know you're not supposed to ask for code but if you can it would be great as I've been trying for months with no progress. Thank you!

Here is how I handle incoming json data:

MainController.swift

```<code>var jsonArray: NSMutableArray = []
var mainArray = [Blog]()
var followedArray = [Blog]()
var filteredArray = [Blog]()

// Title for Header
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    if !(searchController.isActive && searchController.searchBar.text != "") {

        if section == 0 {
            return "Followed Blogs"
        }
        else {
            return "All Blogs"
        }
    }
    return "Filtered Blogs"
}

// Number of Rows in Section
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if !(searchController.isActive && searchController.searchBar.text != "") {

        if section == 0 {

            return followedArray.count
        }
        else if (section == 1) {

            return mainArray.count
        }

    }
    return filteredArray.count

}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let CellIdentifier = "Cell"
    var cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! CustomCell

    if cell != cell {
        cell = CustomCell(style: UITableViewCellStyle.default, reuseIdentifier: CellIdentifier)
    }

    // Configuring the cell
    var blogObject: Blog

    if !(searchController.isActive && searchController.searchBar.text != "") {
        if indexPath.section == 0 {
            blogObject = followedArray[indexPath.row]
            cell.populateCell(blogObject, isFollowed: true, indexPath: indexPath, parentView: self)
        }
        else if indexPath.section == 1 {
            blogObject = mainArray[indexPath.row]
            cell.populateCell(blogObject, isFollowed: false, indexPath: indexPath, parentView: self)
        }
    }
    else {
        blogObject = filteredArray[indexPath.row]
        cell.populateCell(blogObject, isFollowed: false, indexPath: indexPath, parentView: self)
    }

    return cell
}

@IBAction func followButtonClick(_ sender: UIButton!) {

    // Adding row to tag
    let buttonPosition = (sender as AnyObject).convert(CGPoint.zero, to: self.myTableView)
    if let indexPath = self.myTableView.indexPathForRow(at: buttonPosition) {

        // Showing Status Labels
        let cell = self.myTableView.cellForRow(at: indexPath) as! CustomCell
        cell.firstStatusLabel.isHidden = false
        cell.secondStatusLabel.isHidden = false

        // Change Follow to Following
        (sender as UIButton).setImage(UIImage(named: "follow.png")!, for: .normal)
        cell.followButton.isHidden = true
        cell.followedButton.isHidden = false

        // Checking wether to import from mainArray or filteredArray to followedArray
        if !(searchController.isActive && searchController.searchBar.text != "") {

            self.myTableView.beginUpdates()

            // ----- Inserting Cell to followedArray -----
            followedArray.insert(mainArray[indexPath.row], at: 0)
            myTableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .fade)

            // ----- Removing Cell from mainArray -----
            mainArray.remove(at: indexPath.row)
            let rowToRemove = indexPath.row
            self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 1)], with: .fade)

            self.myTableView.endUpdates()

            myTableView.reloadData()
        }
        else {

            self.myTableView.beginUpdates()

            // ----- Inserting Cell to followedArray -----
            let blogObject: Blog = filteredArray[indexPath.row]
            let indexOfObjectInArray = mainArray.index(of: blogObject)

            followedArray.insert(blogObject, at: 0)

            // ----- Removing Cell from filteredArray -----
            filteredArray.remove(at: indexPath.row)
            blogArray.remove(at: indexOfObjectInArray!)
            let rowToRemove = indexPath.row
            self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 0)], with: .fade)

            self.myTableView.endUpdates()

            myTableView.reloadData()
        }
    }
}

func retrieveDataFromServer() {

    let getDataURL = "http://exampleblog.com/receiving.php"
    let url: NSURL = NSURL(string: getDataURL)!

    do {

        let data: Data = try Data(contentsOf: url as URL)
        jsonArray = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! NSMutableArray

        // Looping through jsonArray
        for i in 0..<jsonArray.count {

            // Create Blog Object
            let bID: String = (jsonArray[i] as AnyObject).object(forKey: "id") as! String
            let bName: String = (jsonArray[i] as AnyObject).object(forKey: "blogName") as! String
            let bStatus1: String = (jsonArray[i] as AnyObject).object(forKey: "blogStatus1") as! String
            let bStatus2: String = (jsonArray[i] as AnyObject).object(forKey: "blogStatus2") as! String
            let bURL: String = (jsonArray[i] as AnyObject).object(forKey: "blogURL") as! String
            // New
            let bType: String = (jsonArray[i] as AnyObject).object(forKey: "blogType") as! String
            let bDate: String = (jsonArray[i] as AnyObject).object(forKey: "blogDate") as! String
            let bPop: String = (jsonArray[i] as AnyObject).object(forKey: "blogPop") as! String

            // Add Blog Objects to mainArray
            mainArray.append(Blog(blogName: bName, andBlogStatus1: bStatus1, andBlogStatus2: bStatus2, andBlogURL: bURL, andBlogID: bID, andBlogType: bType, andBlogDate: bDate, andBlogPop: bPop))

        }
    }
    catch {
        print("Error: (Retrieving Data)")
    }

    myTableView.reloadData()
}
</code>```

CustomCell.swift - How the cells are being populatedenter code here

```<code>class CustomCell: UITableViewCell {

@IBOutlet weak var firstStatusLabel: UILabel!
@IBOutlet weak var secondStatusLabel: UILabel!
@IBOutlet weak var followButton: UIButton!
@IBOutlet weak var followedButton: UIButton!

override func awakeFromNib() {
    super.awakeFromNib()

    self.followButton.isHidden = true
    self.followedButton.isHidden = true
}

func populateCell(_ blogObject: Blog, isFollowed: Bool, indexPath: IndexPath, parentView: Any) {

    // Loading Status Labels
    self.firstStatusLabel.text = blogObject.blogStatus1
    self.secondStatusLabel.text = blogObject.blogStatus2
    self.firstStatusLabel.isHidden = true
    self.secondStatusLabel.isHidden = true

    if isFollowed {
        self.followedButton.tag = indexPath.row
        self.followedButton.addTarget(parentView, action: #selector(MainController.followedButtonClick(_:)), for: .touchUpInside)

        self.followedButton.isHidden = false
        self.followButton.isHidden = true

        // Status Labels
        self.firstStatusLabel.isHidden = false
        self.secondStatusLabel.isHidden = false

    }
    else {
        self.followButton.tag = indexPath.row
        self.followButton.addTarget(parentView, action: #selector(MainController.followButtonClick(_:)), for: .touchUpInside)

        self.followedButton.isHidden = true
        self.followButton.isHidden = false

        // Status Labels
        self.firstStatusLabel.isHidden = true
        self.secondStatusLabel.isHidden = true

    }
}
}
</code>```

Blog.swift

```<code>class Blog: NSObject {

// Strings
var blogName: String?
var blogStatus1: String?
var blogStatus2: String?
var blogURL: String?
var blogID: String?
var blogType: String?
var blogDate: String?
var blogPop: String?

override init() {

}

// Converting Strings into Objects
init(blogName bName: String,
     andBlogStatus1 bStatus1: String,
     andBlogStatus2 bStatus2: String,
     andBlogURL bURL: String,
     andBlogID bID: String,
     andBlogType bType: String,
     andBlogDate bDate: String,
     andBlogPop bPop: String)
{
    super.init()

    self.blogName = bName
    self.blogStatus1 = bStatus1
    self.blogStatus2 = bStatus2
    self.blogURL = bURL
    self.blogID = bID
    self.blogType = bType
    self.blogDate = bDate
    self.blogPop = bPop
}

}
</code>```

答:
There are lots of different ways to accomplish what you want, both storing the information locally or remotely. If the blog ids are unique, you could store them in the defaults as an array of strings:

```<code>UserDefaults.standard.set(followedBlogIds, forKey: "followedBlogIds")
</code>```

You can then grab them anytime, just make sure to unwrap/cast them appropriately:

```<code>if let storedBlogIds = UserDefaults.standard.object(forKey: "followedBlogIds") as? [String] {
    //filter your blogs by id and put them in the appropriate
    //arrays for your table
}
</code>```

Body:
Using Swift 3

I've been working on a blog reader app for a while but I've been stuck on how to save the tableview like how the user left it.

The app works by me inputing data into the mysql database which is sent using php and received with json and swift 3 to populate the tableview. I expect to update this database everyday. There are two sections in the tableview, top one is followedArray and second is mainArray which is where all the objects are after being in jsonArray. When the user clicks the follow button, that single cell is moved into followedArray where the user can see the blog being updated and receive notifications.

I've tried using UserDefaults by saving the whole array into an object but it didn't work out as it was way to complicated creating the NSData objects using NSCoding, when there is no need to save the entire array (my guess).

My question, is there a simple way that I can save how the user left the tableview?

For example: In the 'All Objects Section' lets say I have 3 cells and the user clicks the follow button on 2 cells, those 2 cells are now in the 'Followed Section' with 1 cell in 'All Objects Section'. How do I save the state of this tableview. I want to save in which section the followed cell is in now and the changed button which is now a Followed Button instead of Follow Button (saving the hidden enabled code as I hide one button to show the other).

Do I use UserDefaults or CoreData? Maybe a CocoaPod library? Do I save the entire array or just which section its in and the .hidden code? How will updating and adding new objects affect it?

I'm fairly new to Swift so if you can help it would be appreciated. Any code that you need to understand how the app works, just ask for it as I don't know what code is necessary to show. I know you're not supposed to ask for code but if you can it would be great as I've been trying for months with no progress. Thank you!

Here is how I handle incoming json data:

MainController.swift

var jsonArray: NSMutableArray = []
var mainArray = [Blog]()
var followedArray = [Blog]()
var filteredArray = [Blog]()

// Title for Header
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    if !(searchController.isActive && searchController.searchBar.text != "") {

        if section == 0 {
            return "Followed Blogs"
        }
        else {
            return "All Blogs"
        }
    }
    return "Filtered Blogs"
}

// Number of Rows in Section
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if !(searchController.isActive && searchController.searchBar.text != "") {

        if section == 0 {

            return followedArray.count
        }
        else if (section == 1) {

            return mainArray.count
        }

    }
    return filteredArray.count

}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let CellIdentifier = "Cell"
    var cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! CustomCell

    if cell != cell {
        cell = CustomCell(style: UITableViewCellStyle.default, reuseIdentifier: CellIdentifier)
    }

    // Configuring the cell
    var blogObject: Blog

    if !(searchController.isActive && searchController.searchBar.text != "") {
        if indexPath.section == 0 {
            blogObject = followedArray[indexPath.row]
            cell.populateCell(blogObject, isFollowed: true, indexPath: indexPath, parentView: self)
        }
        else if indexPath.section == 1 {
            blogObject = mainArray[indexPath.row]
            cell.populateCell(blogObject, isFollowed: false, indexPath: indexPath, parentView: self)
        }
    }
    else {
        blogObject = filteredArray[indexPath.row]
        cell.populateCell(blogObject, isFollowed: false, indexPath: indexPath, parentView: self)
    }

    return cell
}

@IBAction func followButtonClick(_ sender: UIButton!) {

    // Adding row to tag
    let buttonPosition = (sender as AnyObject).convert(CGPoint.zero, to: self.myTableView)
    if let indexPath = self.myTableView.indexPathForRow(at: buttonPosition) {

        // Showing Status Labels
        let cell = self.myTableView.cellForRow(at: indexPath) as! CustomCell
        cell.firstStatusLabel.isHidden = false
        cell.secondStatusLabel.isHidden = false

        // Change Follow to Following
        (sender as UIButton).setImage(UIImage(named: "follow.png")!, for: .normal)
        cell.followButton.isHidden = true
        cell.followedButton.isHidden = false

        // Checking wether to import from mainArray or filteredArray to followedArray
        if !(searchController.isActive && searchController.searchBar.text != "") {

            self.myTableView.beginUpdates()

            // ----- Inserting Cell to followedArray -----
            followedArray.insert(mainArray[indexPath.row], at: 0)
            myTableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .fade)

            // ----- Removing Cell from mainArray -----
            mainArray.remove(at: indexPath.row)
            let rowToRemove = indexPath.row
            self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 1)], with: .fade)

            self.myTableView.endUpdates()

            myTableView.reloadData()
        }
        else {

            self.myTableView.beginUpdates()

            // ----- Inserting Cell to followedArray -----
            let blogObject: Blog = filteredArray[indexPath.row]
            let indexOfObjectInArray = mainArray.index(of: blogObject)

            followedArray.insert(blogObject, at: 0)

            // ----- Removing Cell from filteredArray -----
            filteredArray.remove(at: indexPath.row)
            blogArray.remove(at: indexOfObjectInArray!)
            let rowToRemove = indexPath.row
            self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 0)], with: .fade)

            self.myTableView.endUpdates()

            myTableView.reloadData()
        }
    }
}

func retrieveDataFromServer() {

    let getDataURL = "http://exampleblog.com/receiving.php"
    let url: NSURL = NSURL(string: getDataURL)!

    do {

        let data: Data = try Data(contentsOf: url as URL)
        jsonArray = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! NSMutableArray

        // Looping through jsonArray
        for i in 0..<jsonArray.count {

            // Create Blog Object
            let bID: String = (jsonArray[i] as AnyObject).object(forKey: "id") as! String
            let bName: String = (jsonArray[i] as AnyObject).object(forKey: "blogName") as! String
            let bStatus1: String = (jsonArray[i] as AnyObject).object(forKey: "blogStatus1") as! String
            let bStatus2: String = (jsonArray[i] as AnyObject).object(forKey: "blogStatus2") as! String
            let bURL: String = (jsonArray[i] as AnyObject).object(forKey: "blogURL") as! String
            // New
            let bType: String = (jsonArray[i] as AnyObject).object(forKey: "blogType") as! String
            let bDate: String = (jsonArray[i] as AnyObject).object(forKey: "blogDate") as! String
            let bPop: String = (jsonArray[i] as AnyObject).object(forKey: "blogPop") as! String

            // Add Blog Objects to mainArray
            mainArray.append(Blog(blogName: bName, andBlogStatus1: bStatus1, andBlogStatus2: bStatus2, andBlogURL: bURL, andBlogID: bID, andBlogType: bType, andBlogDate: bDate, andBlogPop: bPop))

        }
    }
    catch {
        print("Error: (Retrieving Data)")
    }

    myTableView.reloadData()
}

CustomCell.swift - How the cells are being populatedenter code here

class CustomCell: UITableViewCell {

@IBOutlet weak var firstStatusLabel: UILabel!
@IBOutlet weak var secondStatusLabel: UILabel!
@IBOutlet weak var followButton: UIButton!
@IBOutlet weak var followedButton: UIButton!

override func awakeFromNib() {
    super.awakeFromNib()

    self.followButton.isHidden = true
    self.followedButton.isHidden = true
}

func populateCell(_ blogObject: Blog, isFollowed: Bool, indexPath: IndexPath, parentView: Any) {

    // Loading Status Labels
    self.firstStatusLabel.text = blogObject.blogStatus1
    self.secondStatusLabel.text = blogObject.blogStatus2
    self.firstStatusLabel.isHidden = true
    self.secondStatusLabel.isHidden = true

    if isFollowed {
        self.followedButton.tag = indexPath.row
        self.followedButton.addTarget(parentView, action: #selector(MainController.followedButtonClick(_:)), for: .touchUpInside)

        self.followedButton.isHidden = false
        self.followButton.isHidden = true

        // Status Labels
        self.firstStatusLabel.isHidden = false
        self.secondStatusLabel.isHidden = false

    }
    else {
        self.followButton.tag = indexPath.row
        self.followButton.addTarget(parentView, action: #selector(MainController.followButtonClick(_:)), for: .touchUpInside)

        self.followedButton.isHidden = true
        self.followButton.isHidden = false

        // Status Labels
        self.firstStatusLabel.isHidden = true
        self.secondStatusLabel.isHidden = true

    }
}
}

Blog.swift

class Blog: NSObject {

// Strings
var blogName: String?
var blogStatus1: String?
var blogStatus2: String?
var blogURL: String?
var blogID: String?
var blogType: String?
var blogDate: String?
var blogPop: String?

override init() {

}

// Converting Strings into Objects
init(blogName bName: String,
     andBlogStatus1 bStatus1: String,
     andBlogStatus2 bStatus2: String,
     andBlogURL bURL: String,
     andBlogID bID: String,
     andBlogType bType: String,
     andBlogDate bDate: String,
     andBlogPop bPop: String)
{
    super.init()

    self.blogName = bName
    self.blogStatus1 = bStatus1
    self.blogStatus2 = bStatus2
    self.blogURL = bURL
    self.blogID = bID
    self.blogType = bType
    self.blogDate = bDate
    self.blogPop = bPop
}

}

Body:
There are lots of different ways to accomplish what you want, both storing the information locally or remotely. If the blog ids are unique, you could store them in the defaults as an array of strings:

UserDefaults.standard.set(followedBlogIds, forKey: "followedBlogIds")

You can then grab them anytime, just make sure to unwrap/cast them appropriately:

if let storedBlogIds = UserDefaults.standard.object(forKey: "followedBlogIds") as? [String] {
    //filter your blogs by id and put them in the appropriate
    //arrays for your table
}


问:
Ruby -FileUtils creating a directory ,permission denied issue on windows
Am writing a script which creates a directory but when i tried (Ruby script running on windows)

destination = "path to the folder"

FileUtils.mkdir_p destination

it gives me an error saying Permission Denied (Errno::EACCES) at mkdir_p

Any help is appreciated . Thank you

答:
open your cmd terminal with administrator privileges, and then run the script again. It seems like your folder is in a place that need admin privileges, so you can create it. Also add it to your script:

```<code>require 'win32ole'

shell = WIN32OLE.new('Shell.Application')
shell.ShellExecute('path_to_ruby_program', nil, nil, 'runas')
</code>```

And see this two questions:

Run ruby script in elevated mode

Detect if running with administrator privileges under Windows XP

Body:
open your cmd terminal with administrator privileges, and then run the script again. It seems like your folder is in a place that need admin privileges, so you can create it. Also add it to your script:

require 'win32ole'

shell = WIN32OLE.new('Shell.Application')
shell.ShellExecute('path_to_ruby_program', nil, nil, 'runas')

And see this two questions:

Run ruby script in elevated mode

Detect if running with administrator privileges under Windows XP


问:
Calculate distance between latitude and longitude in dataframe
I have 4 columns in my dataframe containing the following data:

```<code>Start_latitude<br>
Start_longitude<br>
Stop_latitude<br>
Stop_longitude<br>
</code>```

I need to compute distance between the latitude longitude pair and create a new column with the distance computed.

I came across a package (geopy) which can do this for me. But I need to pass a tuple to geopy. How do i apply this function (geopy) across the dataframe in pandas for all the records?

答:
I'd recommend you use pyproj instead of geopy. geopy relies on online services whereas pyproj is local (meaning it will be faster and won't rely on an internet connection) and more transparent about its methods (see here for instance), which are based on the Proj4 codebase that underlies essentially all open-source GIS software and, probably, many of the web services you'd use.

```<code>#!/usr/bin/env python3

import pandas as pd
import numpy as np
from pyproj import Geod

wgs84_geod = Geod(ellps='WGS84') #Distance will be measured on this ellipsoid - more accurate than a spherical method

#Get distance between pairs of lat-lon points
def Distance(lat1,lon1,lat2,lon2):
  az12,az21,dist = wgs84_geod.inv(lon1,lat1,lon2,lat2) #Yes, this order is correct
  return dist

#Create test data
lat1 = np.random.uniform(-90,90,100)
lon1 = np.random.uniform(-180,180,100)
lat2 = np.random.uniform(-90,90,100)
lon2 = np.random.uniform(-180,180,100)

#Package as a dataframe
df = pd.DataFrame({'lat1':lat1,'lon1':lon1,'lat2':lat2,'lon2':lon2})

#Add/update a column to the data frame with the distances (in metres)
df['dist'] = Distance(df['lat1'].tolist(),df['lon1'].tolist(),df['lat2'].tolist(),df['lon2'].tolist())
</code>```

PyProj has some documentation here.

Body:
I have 4 columns in my dataframe containing the following data:

Start_latitude

Start_longitude

Stop_latitude

Stop_longitude

I need to compute distance between the latitude longitude pair and create a new column with the distance computed.

I came across a package (geopy) which can do this for me. But I need to pass a tuple to geopy. How do i apply this function (geopy) across the dataframe in pandas for all the records?

Body:
I'd recommend you use pyproj instead of geopy. geopy relies on online services whereas pyproj is local (meaning it will be faster and won't rely on an internet connection) and more transparent about its methods (see here for instance), which are based on the Proj4 codebase that underlies essentially all open-source GIS software and, probably, many of the web services you'd use.

#!/usr/bin/env python3

import pandas as pd
import numpy as np
from pyproj import Geod

wgs84_geod = Geod(ellps='WGS84') #Distance will be measured on this ellipsoid - more accurate than a spherical method

#Get distance between pairs of lat-lon points
def Distance(lat1,lon1,lat2,lon2):
  az12,az21,dist = wgs84_geod.inv(lon1,lat1,lon2,lat2) #Yes, this order is correct
  return dist

#Create test data
lat1 = np.random.uniform(-90,90,100)
lon1 = np.random.uniform(-180,180,100)
lat2 = np.random.uniform(-90,90,100)
lon2 = np.random.uniform(-180,180,100)

#Package as a dataframe
df = pd.DataFrame({'lat1':lat1,'lon1':lon1,'lat2':lat2,'lon2':lon2})

#Add/update a column to the data frame with the distances (in metres)
df['dist'] = Distance(df['lat1'].tolist(),df['lon1'].tolist(),df['lat2'].tolist(),df['lon2'].tolist())

PyProj has some documentation here.


问:
ONOS not detecting hosts with multiple links in network created using Mininet
I am new with Mininet and ONOS, so I have trouble with really simple things. In this case, I have created the following topology in Mininet.

Mininet topology

The script that is generated is the following.

```<code>#!/usr/bin/python

from mininet.net import Mininet
from mininet.node import Controller, RemoteController, OVSController
from mininet.node import CPULimitedHost, Host, Node
from mininet.node import OVSKernelSwitch, UserSwitch
from mininet.node import IVSSwitch
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from mininet.link import TCLink, Intf
from subprocess import call

def myNetwork():

    net = Mininet( topo=None,
                   build=False,
                   ipBase='10.0.0.0/8')

    info( '*** Adding controller\n' )
    c0=net.addController(name='c0',
                      controller=RemoteController,
                      protocol='tcp',
                      port=6633)

    info( '*** Add switches\n')
    s2 = net.addSwitch('s2', cls=OVSKernelSwitch)
    s3 = net.addSwitch('s3', cls=OVSKernelSwitch)
    s4 = net.addSwitch('s4', cls=OVSKernelSwitch)
    s1 = net.addSwitch('s1', cls=OVSKernelSwitch)

    info( '*** Add hosts\n')
    h15 = net.addHost('h15', cls=Host, ip='10.0.0.15', defaultRoute=None)
    h8 = net.addHost('h8', cls=Host, ip='10.0.0.8', defaultRoute=None)
    h9 = net.addHost('h9', cls=Host, ip='10.0.0.9', defaultRoute=None)
    h10 = net.addHost('h10', cls=Host, ip='10.0.0.10', defaultRoute=None)
    h11 = net.addHost('h11', cls=Host, ip='10.0.0.11', defaultRoute=None)
    h16 = net.addHost('h16', cls=Host, ip='10.0.0.16', defaultRoute=None)
    h2 = net.addHost('h2', cls=Host, ip='10.0.0.2', defaultRoute=None)
    h3 = net.addHost('h3', cls=Host, ip='10.0.0.3', defaultRoute=None)
    h4 = net.addHost('h4', cls=Host, ip='10.0.0.4', defaultRoute=None)
    h12 = net.addHost('h12', cls=Host, ip='10.0.0.12', defaultRoute=None)
    h5 = net.addHost('h5', cls=Host, ip='10.0.0.5', defaultRoute=None)
    h13 = net.addHost('h13', cls=Host, ip='10.0.0.13', defaultRoute=None)
    h1 = net.addHost('h1', cls=Host, ip='10.0.0.1', defaultRoute=None)
    h6 = net.addHost('h6', cls=Host, ip='10.0.0.6', defaultRoute=None)
    h14 = net.addHost('h14', cls=Host, ip='10.0.0.14', defaultRoute=None)
    h7 = net.addHost('h7', cls=Host, ip='10.0.0.7', defaultRoute=None)

    info( '*** Add links\n')
    net.addLink(h1, s1)
    net.addLink(h2, s1)
    net.addLink(h3, s1)
    net.addLink(h4, s1)
    net.addLink(h5, s1)
    net.addLink(h6, s1)
    net.addLink(h7, s1)
    net.addLink(h8, s1)
    net.addLink(h9, s1)
    net.addLink(h10, s1)
    net.addLink(h1, s2)
    net.addLink(h2, s2)
    net.addLink(h3, s2)
    net.addLink(h4, s2)
    net.addLink(h5, s2)
    net.addLink(h6, s2)
    net.addLink(h7, s2)
    net.addLink(h8, s2)
    net.addLink(h9, s2)
    net.addLink(h10, s2)
    net.addLink(h11, s3)
    net.addLink(h12, s3)
    net.addLink(h13, s3)
    net.addLink(s4, h14)
    net.addLink(s4, h15)
    net.addLink(s4, h16)
    net.addLink(s1, s3)
    net.addLink(s2, s4)

    info( '*** Starting network\n')
    net.build()
    info( '*** Starting controllers\n')
    for controller in net.controllers:
        controller.start()

    info( '*** Starting switches\n')
    net.get('s2').start([c0])
    net.get('s3').start([c0])
    net.get('s4').start([c0])
    net.get('s1').start([c0])

    info( '*** Post configure switches and hosts\n')

    CLI(net)
    net.stop()

if __name__ == '__main__':
    setLogLevel( 'info' )
    myNetwork()
</code>```

But then, ONOS displays the following topology.

ONOS topology

I don't know if I have to change something in the script or in ONOS. I would appreciate a simple and step by step answer, because, as I said, I am really new with this technology.

Thank you very much for your help.

EDITED **************************************************************************************************************

I edited the second interface on each host and gave them an IP address with ifconfig hx-eth1 10.0.0.x, but this didn't solve the problem. The topology in ONOS looks exactly the same. Here's the code that I added to the Python script.

```<code>h1.cmd('ifconfig h1-eth1 10.0.0.17')
h2.cmd('ifconfig h2-eth1 10.0.0.18')
h3.cmd('ifconfig h3-eth1 10.0.0.19')
h4.cmd('ifconfig h4-eth1 10.0.0.20')
h5.cmd('ifconfig h5-eth1 10.0.0.21')
h6.cmd('ifconfig h6-eth1 10.0.0.22')
h7.cmd('ifconfig h7-eth1 10.0.0.23')
h8.cmd('ifconfig h8-eth1 10.0.0.24')
h9.cmd('ifconfig h9-eth1 10.0.0.25')
h10.cmd('ifconfig h10-eth1 10.0.0.26')
</code>```

答:
ONOS has application for detecting hosts and this application works based on ARP and DHCP packets so you need to do things like ping these hosts with each other and then these hosts appear on your ONOS topology (to show hosts on your topology panel use shift + H).

By default, mininet chooses a single interface on each host and assigns an IP address so if a host has multiple interfaces, then mininet will only auto assign an interface to one of them. You must configure the other interface manually with commands like ifconfig.

```<code>h1 = net.addHost( 'h1', mac='00:00:00:00:00:01', ip='10.0.0.1/24' )
s4 = net.addSwitch( 's4', listenPort=6673, mac='00:00:00:00:00:04' )
s5 = net.addSwitch( 's5', listenPort=6674, mac='00:00:00:00:00:05' )
Link(h1, s4, intfName1='h1-eth0')
Link(h1, s5, intfName1='h1-eth1')
h1.cmd('ifconfig h1-eth1 10.0.10.1 netmask 255.255.255.0')
</code>```

Body:
I am new with Mininet and ONOS, so I have trouble with really simple things. In this case, I have created the following topology in Mininet.

Mininet topology

The script that is generated is the following.

#!/usr/bin/python

from mininet.net import Mininet
from mininet.node import Controller, RemoteController, OVSController
from mininet.node import CPULimitedHost, Host, Node
from mininet.node import OVSKernelSwitch, UserSwitch
from mininet.node import IVSSwitch
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from mininet.link import TCLink, Intf
from subprocess import call

def myNetwork():

    net = Mininet( topo=None,
                   build=False,
                   ipBase='10.0.0.0/8')

    info( '*** Adding controller\n' )
    c0=net.addController(name='c0',
                      controller=RemoteController,
                      protocol='tcp',
                      port=6633)

    info( '*** Add switches\n')
    s2 = net.addSwitch('s2', cls=OVSKernelSwitch)
    s3 = net.addSwitch('s3', cls=OVSKernelSwitch)
    s4 = net.addSwitch('s4', cls=OVSKernelSwitch)
    s1 = net.addSwitch('s1', cls=OVSKernelSwitch)

    info( '*** Add hosts\n')
    h15 = net.addHost('h15', cls=Host, ip='10.0.0.15', defaultRoute=None)
    h8 = net.addHost('h8', cls=Host, ip='10.0.0.8', defaultRoute=None)
    h9 = net.addHost('h9', cls=Host, ip='10.0.0.9', defaultRoute=None)
    h10 = net.addHost('h10', cls=Host, ip='10.0.0.10', defaultRoute=None)
    h11 = net.addHost('h11', cls=Host, ip='10.0.0.11', defaultRoute=None)
    h16 = net.addHost('h16', cls=Host, ip='10.0.0.16', defaultRoute=None)
    h2 = net.addHost('h2', cls=Host, ip='10.0.0.2', defaultRoute=None)
    h3 = net.addHost('h3', cls=Host, ip='10.0.0.3', defaultRoute=None)
    h4 = net.addHost('h4', cls=Host, ip='10.0.0.4', defaultRoute=None)
    h12 = net.addHost('h12', cls=Host, ip='10.0.0.12', defaultRoute=None)
    h5 = net.addHost('h5', cls=Host, ip='10.0.0.5', defaultRoute=None)
    h13 = net.addHost('h13', cls=Host, ip='10.0.0.13', defaultRoute=None)
    h1 = net.addHost('h1', cls=Host, ip='10.0.0.1', defaultRoute=None)
    h6 = net.addHost('h6', cls=Host, ip='10.0.0.6', defaultRoute=None)
    h14 = net.addHost('h14', cls=Host, ip='10.0.0.14', defaultRoute=None)
    h7 = net.addHost('h7', cls=Host, ip='10.0.0.7', defaultRoute=None)

    info( '*** Add links\n')
    net.addLink(h1, s1)
    net.addLink(h2, s1)
    net.addLink(h3, s1)
    net.addLink(h4, s1)
    net.addLink(h5, s1)
    net.addLink(h6, s1)
    net.addLink(h7, s1)
    net.addLink(h8, s1)
    net.addLink(h9, s1)
    net.addLink(h10, s1)
    net.addLink(h1, s2)
    net.addLink(h2, s2)
    net.addLink(h3, s2)
    net.addLink(h4, s2)
    net.addLink(h5, s2)
    net.addLink(h6, s2)
    net.addLink(h7, s2)
    net.addLink(h8, s2)
    net.addLink(h9, s2)
    net.addLink(h10, s2)
    net.addLink(h11, s3)
    net.addLink(h12, s3)
    net.addLink(h13, s3)
    net.addLink(s4, h14)
    net.addLink(s4, h15)
    net.addLink(s4, h16)
    net.addLink(s1, s3)
    net.addLink(s2, s4)

    info( '*** Starting network\n')
    net.build()
    info( '*** Starting controllers\n')
    for controller in net.controllers:
        controller.start()

    info( '*** Starting switches\n')
    net.get('s2').start([c0])
    net.get('s3').start([c0])
    net.get('s4').start([c0])
    net.get('s1').start([c0])

    info( '*** Post configure switches and hosts\n')

    CLI(net)
    net.stop()

if __name__ == '__main__':
    setLogLevel( 'info' )
    myNetwork()

But then, ONOS displays the following topology.

ONOS topology

I don't know if I have to change something in the script or in ONOS. I would appreciate a simple and step by step answer, because, as I said, I am really new with this technology.

Thank you very much for your help.

EDITED **************************************************************************************************************

I edited the second interface on each host and gave them an IP address with ifconfig hx-eth1 10.0.0.x, but this didn't solve the problem. The topology in ONOS looks exactly the same. Here's the code that I added to the Python script.

h1.cmd('ifconfig h1-eth1 10.0.0.17')
h2.cmd('ifconfig h2-eth1 10.0.0.18')
h3.cmd('ifconfig h3-eth1 10.0.0.19')
h4.cmd('ifconfig h4-eth1 10.0.0.20')
h5.cmd('ifconfig h5-eth1 10.0.0.21')
h6.cmd('ifconfig h6-eth1 10.0.0.22')
h7.cmd('ifconfig h7-eth1 10.0.0.23')
h8.cmd('ifconfig h8-eth1 10.0.0.24')
h9.cmd('ifconfig h9-eth1 10.0.0.25')
h10.cmd('ifconfig h10-eth1 10.0.0.26')

Body:
ONOS has application for detecting hosts and this application works based on ARP and DHCP packets so you need to do things like ping these hosts with each other and then these hosts appear on your ONOS topology (to show hosts on your topology panel use shift + H).

By default, mininet chooses a single interface on each host and assigns an IP address so if a host has multiple interfaces, then mininet will only auto assign an interface to one of them. You must configure the other interface manually with commands like ifconfig.

h1 = net.addHost( 'h1', mac='00:00:00:00:00:01', ip='10.0.0.1/24' )
s4 = net.addSwitch( 's4', listenPort=6673, mac='00:00:00:00:00:04' )
s5 = net.addSwitch( 's5', listenPort=6674, mac='00:00:00:00:00:05' )
Link(h1, s4, intfName1='h1-eth0')
Link(h1, s5, intfName1='h1-eth1')
h1.cmd('ifconfig h1-eth1 10.0.10.1 netmask 255.255.255.0')


问:
Is it possible that I request an EC2 instance but can't get fulfilled?
Edit: This question is NOT to ask about "Spot Instances"; this question is to ask regular "On Demand Instance". I think I need to clarify this, after reading comments below.

Basically, my question is about whether I should consider the risk that when I need to launch an EC2 instance, but that EC2 region has run out of capacity and can't fulfill my request.

I understand the chance for the above situation is extremely low, but I'd like to understand if AWS has any SLA to make sure that situation/risk won't happen.

答:
There are protective controls in place to make the unavailability of a particular type of instance in a particular availability zone at a particular time unlikely... but it's possible, and there is no assurance provided by AWS that a given type of EC2 instance will be available for launch, on-demand, at any particular time, in any particular Availability Zone unless you purchased reserved instances of that type, specifically in that availability zone.  In that case, there is supposed to always be sufficient hardware available so that you can have the number of paid reserved instances running, at minimum, including the ability to launch enough new instances to bring the total up to that minimum.

Reserved instances are commonly discussed in the context of their associated discount, but they have two purposes:

Reserved Instances are not physical instances, but rather a billing discount applied to the use of On-Demand Instances in your account. These On-Demand Instances must match certain attributes in order to benefit from the billing discount.

When you purchase Reserved Instances in a specific Availability Zone, they provide a capacity reservation. (emphasis added)

For example, if you purchased 4 reserved t2.2xlarge instances in us-east-2a, the assurance is that you will always be able to launch enough to bring the total running instances of that type in that zone to 4.  If you already have 4, there's not an assurance of being able to start more, but there is an assurance that if you stop them, you will be able to start them again.

Pricing models for reserved instances has changed over the years, such that reserved instances are generally billed at the same rate whether they're running or not, so you can look at it one of two ways:

If you need the capacity all the time, you're getting a substantial discount... or, if you don't need the capacity all the time, you're technically paying all the time for capacity that is largely unneeded, but you are still paying less than you'd pay for on-demand instances without reservations, and you can either leave it running or launch it when you need it.

Should you consider the risk that an entire region has widespread capacity issues?  You should consider it, but there are, historically speaking, other significant outage scenarios that are more likely... EBS and S3 have both had failures that impaired the ability to launch instances, even though the capacity was idle in EC2.


问:
SQL Plus - how to pass big string to procedure CLOB's parameter
I have procedure that receives an XML:

```<code>CREATE OR REPLACE PROCEDURE PROCESS_XML(xml IN CLOB) AS
BEGIN
  DBMS_OUTPUT.PUT_LINE('XML processing started');
END;
</code>```

Now I'm doing a bash script that will download some XML files from a server and, for each one, I will call the above procedure using SQL Plus.

```<code>#!/bin/bash
file=$(curl -s "http://example.com/someFile.xml");
sqlplus myuser/mypass@myhost:1521/myscheme <<< "EXECUTE PROCESS_XML('$file')";
</code>```

It works fine for small file but for big ones, I'm getting the following error:

```<code>SQL*Plus: Release 12.1.0.2.0 Production on Thu Jun 8 18:28:19 2017
Copyright (c) 1982, 2016, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP
and Data Mining options
SQL> Input truncated to 7499 characters
SP2-0027: Input is too long (> 2499 characters) - line ignored
</code>```

Is there anything I could in order to send those big XML?

Thanks

答:
You can try to loop through the input file appending 2.4k chunks at a time, probably:

```<code>variable l_var clob;
exec :l_var := '';

-- loop here
exec :l_var := :l_var || '$chunk';
---

exec process_xml(:l_var);
</code>```

And instead of shell scripting, you can also form the clob in Java, for instance, reading the XML line-by-line, which doesn't have a limit of the size of the variable.

Body:
I have procedure that receives an XML:

CREATE OR REPLACE PROCEDURE PROCESS_XML(xml IN CLOB) AS
BEGIN
  DBMS_OUTPUT.PUT_LINE('XML processing started');
END;

Now I'm doing a bash script that will download some XML files from a server and, for each one, I will call the above procedure using SQL Plus.

#!/bin/bash
file=$(curl -s "http://example.com/someFile.xml");
sqlplus myuser/mypass@myhost:1521/myscheme <<< "EXECUTE PROCESS_XML('$file')";

It works fine for small file but for big ones, I'm getting the following error:

SQL*Plus: Release 12.1.0.2.0 Production on Thu Jun 8 18:28:19 2017
Copyright (c) 1982, 2016, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP
and Data Mining options
SQL> Input truncated to 7499 characters
SP2-0027: Input is too long (> 2499 characters) - line ignored

Is there anything I could in order to send those big XML?

Thanks

Body:
You can try to loop through the input file appending 2.4k chunks at a time, probably:

variable l_var clob;
exec :l_var := '';

-- loop here
exec :l_var := :l_var || '$chunk';
---

exec process_xml(:l_var);

And instead of shell scripting, you can also form the clob in Java, for instance, reading the XML line-by-line, which doesn't have a limit of the size of the variable.


问:
How to remove value from Multidimensional Array based on Key and Value - PHP
How can I remove value "288" from "pecan" and return the updated array.

Array like so:

```<code>a: 10: {
s: 13: "Fraser Island";
a: 7: {
    i: 0;
    i: 303;
    i: 1;
    i: 34;
    i: 2;
    i: 27;
    i: 3;
    i: 291;
    i: 4;
    i: 293;
    i: 5;
    i: 37;
    i: 6;
    i: 288;
}
s: 5: "pecan";
a: 5: {
    i: 0;
    i: 34;
    i: 1;
    i: 27;
    i: 2;
    i: 291;
    i: 3;
    i: 59;
    i: 4;
    i: 288;
}
s: 11: "New Triper";
a: 3: {
    i: 0;
    i: 291;
    i: 1;
    i: 293;
    i: 2;
    i: 288;
}
</code>```

}

Which translates to this in PHP:

```<code>Array (
[0] => Array (
    [Fraser Island] => Array (
        [0] => 303
        [1] => 34
        [2] => 27
        [3] => 291
        [4] => 293
        [5] => 37
        [6] => 288
    )
    [pecan] => Array (
        [0] => 34
        [1] => 27
        [2] => 291
        [3] => 59
        [4] => 288
    )
    [New Triper!] => Array (
        [0] => 291
        [1] => 293
        [2] => 288
    )
)
</code>```

)

Delete/remove a value from a multidimensional array by finding the array column name and the value. I use this code:

```<code>// DELETE VALUE FROM ARRAY
function remove_element_by_value($arr, $val) {
    $return = array();
    foreach($arr as $k => $v) {
        if(is_array($v)) {
            $return[$k] = remove_element_by_value($v, $val); //recursion
            continue;
        }
        if($v == $val) continue;
            $return[$k] = $v;
        }
   return $return;
}
</code>```

However it removes all matching values from all array columns.

答:
So you know you are looking under the pecan key and you know you are looking for 288, so just search to return the key and unset():

```<code>$key = 'pecan';
$val = '288';

unset($array[0][$key][array_search($val, $array[0][$key])]);
</code>```

To re-index:

```<code>$array[0][$key] = array_values($array[0][$key]);
</code>```

Or to unwind it:

```<code>$found = array_search(288, $array[0]['pecan']);
unset($array[0]['pecan'][$found]);
$array[0]['pecan'] = array_values($array[0]['pecan']);
</code>```

Body:
How can I remove value "288" from "pecan" and return the updated array.

Array like so:

a: 10: {
s: 13: "Fraser Island";
a: 7: {
    i: 0;
    i: 303;
    i: 1;
    i: 34;
    i: 2;
    i: 27;
    i: 3;
    i: 291;
    i: 4;
    i: 293;
    i: 5;
    i: 37;
    i: 6;
    i: 288;
}
s: 5: "pecan";
a: 5: {
    i: 0;
    i: 34;
    i: 1;
    i: 27;
    i: 2;
    i: 291;
    i: 3;
    i: 59;
    i: 4;
    i: 288;
}
s: 11: "New Triper";
a: 3: {
    i: 0;
    i: 291;
    i: 1;
    i: 293;
    i: 2;
    i: 288;
}

}

Which translates to this in PHP:

Array (
[0] => Array (
    [Fraser Island] => Array (
        [0] => 303
        [1] => 34
        [2] => 27
        [3] => 291
        [4] => 293
        [5] => 37
        [6] => 288
    )
    [pecan] => Array (
        [0] => 34
        [1] => 27
        [2] => 291
        [3] => 59
        [4] => 288
    )
    [New Triper!] => Array (
        [0] => 291
        [1] => 293
        [2] => 288
    )
)

)

Delete/remove a value from a multidimensional array by finding the array column name and the value. I use this code:

// DELETE VALUE FROM ARRAY
function remove_element_by_value($arr, $val) {
    $return = array();
    foreach($arr as $k => $v) {
        if(is_array($v)) {
            $return[$k] = remove_element_by_value($v, $val); //recursion
            continue;
        }
        if($v == $val) continue;
            $return[$k] = $v;
        }
   return $return;
}

However it removes all matching values from all array columns.

Body:
So you know you are looking under the pecan key and you know you are looking for 288, so just search to return the key and unset():

$key = 'pecan';
$val = '288';

unset($array[0][$key][array_search($val, $array[0][$key])]);

To re-index:

$array[0][$key] = array_values($array[0][$key]);

Or to unwind it:

$found = array_search(288, $array[0]['pecan']);
unset($array[0]['pecan'][$found]);
$array[0]['pecan'] = array_values($array[0]['pecan']);