问:
Using thenAccept after supplyAsync blocks the main thread

I am developing a web application which communicates with other web applications. From time to time, my system sends HTTP request as notification to other systems. Since their responses are not essential to me, I send the requests with Java 8 CompletableFuture supplyAsync and prints their responses with thenAccept so that my main thread will not get blocked. However, I found the CompletableFuture function chains took around 100 to 200 ms each time, which confused me because from my understanding thenAccept() should run in the same thread with supplyAsync()'s.

I mocked my process with below codes

```<code>public static void run() {
    long start = System.currentTimeMillis();
    log.info("run start -> " + new Timestamp(start));
    CompletableFuture.supplyAsync(() -> {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return 42;
    }).thenAccept(res -> log.info("run result -> " + res + ", time -> " + new Timestamp(System.currentTimeMillis())));
    log.info("run duration ->" + (System.currentTimeMillis() - start));
}

public static void runAsync() {
    long start = System.currentTimeMillis();
    log.info("runAsync start -> " + new Timestamp(start));
    CompletableFuture.supplyAsync(() -> {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return 42;
    }).thenAcceptAsync(res -> log.info("runAsync result -> " + res + ", time ->" + new Timestamp(System.currentTimeMillis())));
    log.info("runAsync duration ->" + (System.currentTimeMillis() - start));
}

public static void main(String[] args) throws InterruptedException {
    Test.run();
    Test.runAsync();
    Thread.sleep(1000);
}
</code>```

the run() method uses thenAccept() with supplyAsync() while runAsync() uses thenAcceptAsync(). I expected both of them should take just a few milliseconds. However, the real outputs are:

```<code>10:04:54.632 [main] INFO Test - run start -> 2017-12-08 10:04:54.622
10:04:54.824 [main] INFO Test - run duration ->202
10:04:54.824 [main] INFO Test - runAsync start -> 2017-12-08 10:04:54.824
10:04:54.826 [main] INFO Test - runAsync duration ->2
10:04:55.333 [ForkJoinPool.commonPool-worker-1] INFO Test - run result -> 42, time -> 2017-12-08 10:04:55.333
10:04:55.333 [ForkJoinPool.commonPool-worker-3] INFO Test - runAsync result -> 42, time ->2017-12-08 10:04:55.333
</code>```

We can see run() takes 202 ms which is 100 times of the duration of runAsync() which uses only 2 ms.

I don't understand where is the 202 ms overhead comes from, and obviously it is not the lambda function in supplyAysnc() which sleeps 500 ms.

Could anyone explain why the run() method blocks, and should I always use thenAcceptAsync() over thenAccept() ?

Many thanks.

答:
…because from my understanding thenAccept() should run in the same thread with supplyAsync()'s

Your understanding is wrong.

From the documentation of CompletableFuture:

Actions supplied for dependent completions of non-async methods may be performed by the thread that completes the current CompletableFuture, or by any other caller of a completion method.

The most obvious consequence is that when a future is already completed, the function passed to thenAccept() will be evaluated directly in the caller’s thread, as the future has no possibility to command the thread which completed it. In fact, there is no association of a CompletableFuture with a thread at all, as anyone could call complete on it, not just the thread executing the Supplier you passed to supplyAsync. That’s also the reason why cancel does not support interruption. The future doesn’t know which thread(s) could potentially try to complete it.

The not so obvious consequence is that even the behavior described above is not guaranteed. The phrase “or by any other caller of a completion method” does not restrict it to the caller of the completion method registering the dependent action. It could also be any other caller registering a dependent action on the same future. So if two threads are calling thenApply concurrently on the same future, either of them could end up evaluating both functions or even weirder, each thread could end up executing the other thread’s action. The specification does not preclude it.

For the test case you have provided in your question, you are more likely to measure the initialization overhead, as described in this answer. But for the actual problem in your web application where the framework will be initialized only once, you’re likely stumbling over the wrong understanding of thenApply’s behavior (or any non-async chaining method in general). If you want to be sure that the evaluation does not happen in the caller’s thread, you must use thenApplyAsync.

Body:
I am developing a web application which communicates with other web applications. From time to time, my system sends HTTP request as notification to other systems. Since their responses are not essential to me, I send the requests with Java 8 CompletableFuture supplyAsync and prints their responses with thenAccept so that my main thread will not get blocked. However, I found the CompletableFuture function chains took around 100 to 200 ms each time, which confused me because from my understanding thenAccept() should run in the same thread with supplyAsync()'s.

I mocked my process with below codes

public static void run() {
    long start = System.currentTimeMillis();
    log.info("run start -> " + new Timestamp(start));
    CompletableFuture.supplyAsync(() -> {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return 42;
    }).thenAccept(res -> log.info("run result -> " + res + ", time -> " + new Timestamp(System.currentTimeMillis())));
    log.info("run duration ->" + (System.currentTimeMillis() - start));
}

public static void runAsync() {
    long start = System.currentTimeMillis();
    log.info("runAsync start -> " + new Timestamp(start));
    CompletableFuture.supplyAsync(() -> {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return 42;
    }).thenAcceptAsync(res -> log.info("runAsync result -> " + res + ", time ->" + new Timestamp(System.currentTimeMillis())));
    log.info("runAsync duration ->" + (System.currentTimeMillis() - start));
}

public static void main(String[] args) throws InterruptedException {
    Test.run();
    Test.runAsync();
    Thread.sleep(1000);
}

the run() method uses thenAccept() with supplyAsync() while runAsync() uses thenAcceptAsync(). I expected both of them should take just a few milliseconds. However, the real outputs are:

10:04:54.632 [main] INFO Test - run start -> 2017-12-08 10:04:54.622
10:04:54.824 [main] INFO Test - run duration ->202
10:04:54.824 [main] INFO Test - runAsync start -> 2017-12-08 10:04:54.824
10:04:54.826 [main] INFO Test - runAsync duration ->2
10:04:55.333 [ForkJoinPool.commonPool-worker-1] INFO Test - run result -> 42, time -> 2017-12-08 10:04:55.333
10:04:55.333 [ForkJoinPool.commonPool-worker-3] INFO Test - runAsync result -> 42, time ->2017-12-08 10:04:55.333

We can see run() takes 202 ms which is 100 times of the duration of runAsync() which uses only 2 ms.

I don't understand where is the 202 ms overhead comes from, and obviously it is not the lambda function in supplyAysnc() which sleeps 500 ms.

Could anyone explain why the run() method blocks, and should I always use thenAcceptAsync() over thenAccept() ?

Many thanks.


来源: stackexchange

问:
Pass facebook AccessToken by Retrofit
I'm trying to get posts from facebook page using Retrofit but I can't pass Access token and every time I get an error io.reactivex.exceptions.OnErrorNotImplementedException: HTTP 400 Bad Request

This is my code:

RetroAPI:

```<code>@GET("{page_id}/feed")
fun getPosts(@Path("page_id") pageId : String,
             @Header("access_token") authToken : AccessToken)
             : Observable<Posts>
</code>```

Set Access Token:

```<code> AccessToken.setCurrentAccessToken(AccessToken("AppToken", "AppID","userID",null,null,null,null,null))
</code>```

Get data:

```<code>  var pagePosts : Observable<Posts> = facebook.getPosts("pageID", AccessToken.getCurrentAccessToken())
    pagePosts.observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe({result ->
                var a : Posts = result
                var b : List<Data> = result.data
                Log.d("Posts A","${a.data[1].id}")
                Log.d("Data B", "$b")
            })
</code>```

Set RetrofitAPI:

```<code>private val facebook : RetroAPI
    init{
        val retrofit = Retrofit.Builder()
                .baseUrl("https://graph.facebook.com/")
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build()

        facebook = retrofit.create(RetroAPI::class.java)
    }
</code>```

答:
Normally access_token is sent as a query param to authenticate your request.

However you can also send a header in the form

Authorization: Bearer XXX

But AFAIK sending access_token as a header is unsupported.

ContentLicense: CC BY-SA 3.0

Title: Pass facebook AccessToken by Retrofit

Body:
I'm trying to get posts from facebook page using Retrofit but I can't pass Access token and every time I get an error io.reactivex.exceptions.OnErrorNotImplementedException: HTTP 400 Bad Request

This is my code:

RetroAPI:

@GET("{page_id}/feed")
fun getPosts(@Path("page_id") pageId : String,
             @Header("access_token") authToken : AccessToken)
             : Observable<Posts>

Set Access Token:

 AccessToken.setCurrentAccessToken(AccessToken("AppToken", "AppID","userID",null,null,null,null,null))

Get data:

  var pagePosts : Observable<Posts> = facebook.getPosts("pageID", AccessToken.getCurrentAccessToken())
    pagePosts.observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe({result ->
                var a : Posts = result
                var b : List<Data> = result.data
                Log.d("Posts A","${a.data[1].id}")
                Log.d("Data B", "$b")
            })

Set RetrofitAPI:

private val facebook : RetroAPI
    init{
        val retrofit = Retrofit.Builder()
                .baseUrl("https://graph.facebook.com/")
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build()

        facebook = retrofit.create(RetroAPI::class.java)
    }


问:
How decode windowsCP1251 percent encoded String ? Swift 4 ios 11
How to decode String from percent encoded windowsCP1251?

replacingPercentEscapes(using: String.Encoding.windowsCP1251) now is obsolete

removingPercentEncoding uses utf8

答:
I have found the solution. It works for me. Welcome to refactor my example.</p>

```<code>extension String {
    func removingPercentEncoding(using encoding: String.Encoding) -> String {
        let firstChar = self.first
        let percentCharacter = Character("%")

        var encodedPrefix: String.SubSequence? = nil
        var encodedSuffix = self
        if firstChar != percentCharacter {
            if let indexOfFirstPercentChar = index(of: percentCharacter) {
                encodedPrefix = self[..<indexOfFirstPercentChar]
                encodedSuffix = String(self[indexOfFirstPercentChar...])
            } else {
                //no % char at all. Nothing encoded
                return self
            }
        }

        let substrings = encodedSuffix.components(separatedBy: "%")
        let arr = substrings.map{ substring -> (String) in
            switch substring.count {
            case let count where count < 2:
                return substring
            case let count where count == 2:
                let decodedArr = substring.hexa2Bytes
                let data = Data(decodedArr)
                if let decodedStr = String(data: data, encoding: encoding) {
                    return decodedStr
                }

                return substring
            default: //>2
                let thirdSymbolIndex = index(startIndex, offsetBy: 2)
                let firstTwo = substring[..<thirdSymbolIndex]
                let furhter = substring[thirdSymbolIndex...]
                let decodedArr = String(firstTwo).hexa2Bytes
                let data = Data(decodedArr)
                if let decodedStr = String(data: data, encoding: encoding) {
                    return decodedStr + furhter
                }

                return substring
            }
        }

        let result = arr.joined()

        return String(encodedPrefix ?? "") + result
    }

    var hexa2Bytes: [UInt8] {
        let hexa = Array(characters)
        return stride(from: 0, to: characters.count, by: 2).flatMap { UInt8(String(hexa[$0..<$0.advanced(by: 2)]), radix: 16) }
    }

}
</code>```

Body:
I have found the solution. It works for me. Welcome to refactor my example.

extension String {
    func removingPercentEncoding(using encoding: String.Encoding) -> String {
        let firstChar = self.first
        let percentCharacter = Character("%")

        var encodedPrefix: String.SubSequence? = nil
        var encodedSuffix = self
        if firstChar != percentCharacter {
            if let indexOfFirstPercentChar = index(of: percentCharacter) {
                encodedPrefix = self[..<indexOfFirstPercentChar]
                encodedSuffix = String(self[indexOfFirstPercentChar...])
            } else {
                //no % char at all. Nothing encoded
                return self
            }
        }

        let substrings = encodedSuffix.components(separatedBy: "%")
        let arr = substrings.map{ substring -> (String) in
            switch substring.count {
            case let count where count < 2:
                return substring
            case let count where count == 2:
                let decodedArr = substring.hexa2Bytes
                let data = Data(decodedArr)
                if let decodedStr = String(data: data, encoding: encoding) {
                    return decodedStr
                }

                return substring
            default: //>2
                let thirdSymbolIndex = index(startIndex, offsetBy: 2)
                let firstTwo = substring[..<thirdSymbolIndex]
                let furhter = substring[thirdSymbolIndex...]
                let decodedArr = String(firstTwo).hexa2Bytes
                let data = Data(decodedArr)
                if let decodedStr = String(data: data, encoding: encoding) {
                    return decodedStr + furhter
                }

                return substring
            }
        }

        let result = arr.joined()

        return String(encodedPrefix ?? "") + result
    }

    var hexa2Bytes: [UInt8] {
        let hexa = Array(characters)
        return stride(from: 0, to: characters.count, by: 2).flatMap { UInt8(String(hexa[$0..<$0.advanced(by: 2)]), radix: 16) }
    }

}


问:
In one UIViewController, how can I judge it is to to next UIViewController, or return to last UIViewController
For iOS UIViewController ,  when go to next UIViewController, or return to last UIViewController,  the viewDidDisappear and viewWillDisappear Life Cycle  will all appear, so how can I judge  it is to to next UIViewController, or return to last UIViewController ?

答:
You can use the isMovingToParentViewController and isMovingFromParentViewController methods of UIViewController to determine if a view controller is appearing or disappearing as a result of a push or a pop.

More explanation can be found in Apple Documentation.


问:
How can i make my program run faster: Is using threads a solution?
I've written a program building a huge search tree. Because my tree is too huge i expected that my program will use over 40 % of cpu. instead of this my program uses not over 10 % of cpu even it runs with high priority.

If threads will use more cpu while running parallel, let me please know it; i can divide my program in threads, i need really reduce the search time.

Thanks !

答:
Using threads would be a viable solution if you had some way of parallelizing the task of building up the search tree.  This really depends on what sort of tree you're building.  If you're building some flavor of binary search tree, it might be possible, but you'll have to be careful to ensure that as you're inserting nodes you don't end up having any data races.  This probably would require each thread to acquire a lock on each node it visits.  If you're planning on using some sort of balanced tree (AVL, red/black, splay, AA, etc.) this is not likely to work very well, since the rotations could very easily interfere with other threads.

One option might be to split the work up.  There are implementations of balanced binary search trees that support fast merging (for example, some variants of red/black trees can be merged in O(lg n) time), which means that you could try splitting the data into groups, building up a balanced binary search tree from each group, then merging them all together into one balanced tree.  This would take advantage of parallelism.

ContentLicense: CC BY-SA 2.5


问:
What is the difference between io.sockets.emit and socket.emit in io.on('connection', function(socket){})?
I noticed if I want to emit a message like

```<code>io.on('connection', function(socket){
    socket.emit('signal')
})
</code>```

Somehow I have to refresh the HTML page to get the ejs file work on the signal. But If I directly emit it like:

```<code>io.sockets.emit('signal')
</code>```

Then I do not have to refresh the html to get the action work.

How come?

答:
Here

```<code>io.on('connection', function(socket){
}
</code>```

listen for the connection event for incoming sockets.

if you directly use io.sockets.emit('signal') then you must need event on connection as it check for connection establish or not.

Body:
I noticed if I want to emit a message like

io.on('connection', function(socket){
    socket.emit('signal')
})

Somehow I have to refresh the HTML page to get the ejs file work on the signal. But If I directly emit it like:

io.sockets.emit('signal')

Then I do not have to refresh the html to get the action work.

How come?

Body:
Here

io.on('connection', function(socket){
}

listen for the connection event for incoming sockets.

if you directly use io.sockets.emit('signal') then you must need event on connection as it check for connection establish or not.


问:
Filebeat Kubernetes Processor and filtering
I am trying to ship my K8s pod logs to Elasticsearch using Filebeat.

I am following the guide online here: https://www.elastic.co/guide/en/beats/filebeat/6.0/running-on-kubernetes.html

Everything works as expected however I want to filter out events from system pods. My updated config looks like:

```<code>apiVersion: v1
kind: ConfigMap
metadata:
  name: filebeat-prospectors
  namespace: kube-system
  labels:
    k8s-app: filebeat
    kubernetes.io/cluster-service: "true"
data:
  kubernetes.yml: |-
    - type: log
      paths:
        - /var/lib/docker/containers/*/*.log
  multiline.pattern: '^\s'
  multiline.match: after
  json.message_key: log
  json.keys_under_root: true
  processors:
    - add_kubernetes_metadata:
        in_cluster: true
        namespace: ${POD_NAMESPACE}
    - drop_event.when.regexp:
        or:
          kubernetes.pod.name: "weave-net.*"
          kubernetes.pod.name: "external-dns.*"
          kubernetes.pod.name: "nginx-ingress-controller.*"
          kubernetes.pod.name: "filebeat.*"
</code>```

I am trying to ignore weave-net, external-dns, ingress-controller and filebeat events via:

```<code>- drop_event.when.regexp:
    or:
      kubernetes.pod.name: "weave-net.*"
      kubernetes.pod.name: "external-dns.*"
      kubernetes.pod.name: "nginx-ingress-controller.*"
      kubernetes.pod.name: "filebeat.*"
</code>```

However they continue to arrive in Elasticsearch.

答:
I am using a different approach, that is less efficient in terms on the number of logs that transit in the logging pipeline.

Similarly on how you did, I deployed one instance of filebeat on my nodes, using a daemonset. Nothing special here, this is the configuration I am using:

```<code>apiVersion: v1
data:
  filebeat.yml: |-
    filebeat.config:
      prospectors:
        # Mounted `filebeat-prospectors` configmap:
        path: ${path.config}/prospectors.d/*.yml
        # Reload prospectors configs as they change:
        reload.enabled: false
      modules:
        path: ${path.config}/modules.d/*.yml
        # Reload module configs as they change:
        reload.enabled: false

    processors:
      - add_cloud_metadata:

    output.logstash:
      hosts: ['logstash.elk.svc.cluster.local:5044']
kind: ConfigMap
metadata:
  labels:
    k8s-app: filebeat
    kubernetes.io/cluster-service: "true"
  name: filebeat-config
</code>```

And this one for the prospectors:

```<code>apiVersion: v1
data:
  kubernetes.yml: |-
    - type: log
      paths:
        - /var/lib/docker/containers/*/*.log
      json.message_key: log
      json.keys_under_root: true
      processors:
        - add_kubernetes_metadata:
            in_cluster: true
            namespace: ${POD_NAMESPACE}
kind: ConfigMap
metadata:
  labels:
    k8s-app: filebeat
    kubernetes.io/cluster-service: "true"
  name: filebeat-prospectors
</code>```

The Daemonset spec:

```<code>apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  labels:
    k8s-app: filebeat
    kubernetes.io/cluster-service: "true"
  name: filebeat
spec:
  selector:
    matchLabels:
      k8s-app: filebeat
      kubernetes.io/cluster-service: "true"
  template:
    metadata:
      labels:
        k8s-app: filebeat
        kubernetes.io/cluster-service: "true"
    spec:
      containers:
      - args:
        - -c
        - /etc/filebeat.yml
        - -e
        command:
        - /usr/share/filebeat/filebeat
        env:
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: metadata.namespace
        image: docker.elastic.co/beats/filebeat:6.0.1
        imagePullPolicy: IfNotPresent
        name: filebeat
        resources:
          limits:
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 100Mi
        securityContext:
          runAsUser: 0
        volumeMounts:
        - mountPath: /etc/filebeat.yml
          name: config
          readOnly: true
          subPath: filebeat.yml
        - mountPath: /usr/share/filebeat/prospectors.d
          name: prospectors
          readOnly: true
        - mountPath: /usr/share/filebeat/data
          name: data
        - mountPath: /var/lib/docker/containers
          name: varlibdockercontainers
          readOnly: true
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      volumes:
      - configMap:
          name: filebeat-config
        name: config
      - hostPath:
          path: /var/lib/docker/containers
          type: ""
        name: varlibdockercontainers
      - configMap:
          defaultMode: 384
          name: filebeat-prospectors
        name: prospectors
      - emptyDir: {}
        name: data
</code>```

Basically, all data from all logs from all containers gets forwarded to logstash, reachable at the service endpoint: logstash.elk.svc.cluster.local:5044 (service called "logstash" in the "elk" namespace).

For brevity, I'm gonna give you only the configuration for logstash (if you need more specific help with kubernetes, please ask in the comments):

The logstash.yml file is very basic:

```<code>http.host: "0.0.0.0"
path.config: /usr/share/logstash/pipeline
</code>```

Just indicating the mountpoint of the directory where I mounted the pipeline config files, which are the following:

10-beats.conf:
declares an input for filebeat (port 5044 has to be exposed with a service called "logstash")

```<code>input {
  beats {
    port => 5044
    ssl => false
  }
}
</code>```

49-filter-logs.conf:
this filter basically drops logs coming from pods that don't have the "elk" label. For the pods that do have the "elk" label, it keeps the logs from containers named in the "elk" label of the pod. For instance, if a Pod has two containers, called "nginx" and "python", putting a label "elk" with value "nginx" will only keep the logs coming from the nginx container and drop the python ones. The type of the log is set as the namespace the pod is running in.
This might not be a good fit for everybody (you're going to have a single index in elasticsearch for all logs belonging to a namespace) but it works for me because my logs are homologous.

```<code>filter {
    if ![kubernetes][labels][elk] {
        drop {}
    }
    if [kubernetes][labels][elk] {
        # check if kubernetes.labels.elk contains this container name
        mutate {
          split => { "kubernetes[labels][elk]" => "." }
        }
        if [kubernetes][container][name] not in [kubernetes][labels][elk] {
          drop {}
        }
        mutate {
          replace => { "@metadata[type]" => "%{kubernetes[namespace]}" }
          remove_field => [ "beat", "host", "kubernetes[labels][elk]", "kubernetes[labels][pod-template-hash]", "kubernetes[namespace]", "kubernetes[pod][name]", "offset", "prospector[type]", "source", "stream", "time" ]
          rename => { "kubernetes[container][name]" => "container"  }
          rename => { "kubernetes[labels][app]" => "app"  }
        }
    }
}
</code>```

The rest of the configuration is about log parsing and is not relevant in this context.
The only other important part is the output:

99-output.conf:
Send data to elasticsearch:

```<code>output {
  elasticsearch {
    hosts => ["http://elasticsearch.elk.svc.cluster.local:9200"]
    manage_template => false
    index => "%{[@metadata][type]}-%{+YYYY.MM.dd}"
    document_type => "%{[@metadata][type]}"
  }
}
</code>```

Hope you got the point here.

PROs of this approach

Once deployed filebeat and logstash, as long as you don't need to parse a new type of log, you don't need to update filebeat nor logstash configuration in order to get a new log in kibana. You just need to add a label in the pod template.

All log files get dropped by default, as long as you don't explicitly put the labels.

CONs of this approach

ALL logs from ALL pods come through filebeat and logstash, and get dropped only in logstash. This is a lot of work for logstash and can be resource consuming depending on the number of pods you have in your cluster.

I am sure there are better approaches to this problem, but I think this solution is quite handy, at least for my use case.

ContentLicense: CC BY-SA 4.0

Body:
I am trying to ship my K8s pod logs to Elasticsearch using Filebeat.

I am following the guide online here: https://www.elastic.co/guide/en/beats/filebeat/6.0/running-on-kubernetes.html

Everything works as expected however I want to filter out events from system pods. My updated config looks like:

apiVersion: v1
kind: ConfigMap
metadata:
  name: filebeat-prospectors
  namespace: kube-system
  labels:
    k8s-app: filebeat
    kubernetes.io/cluster-service: "true"
data:
  kubernetes.yml: |-
    - type: log
      paths:
        - /var/lib/docker/containers/*/*.log
  multiline.pattern: '^\s'
  multiline.match: after
  json.message_key: log
  json.keys_under_root: true
  processors:
    - add_kubernetes_metadata:
        in_cluster: true
        namespace: ${POD_NAMESPACE}
    - drop_event.when.regexp:
        or:
          kubernetes.pod.name: "weave-net.*"
          kubernetes.pod.name: "external-dns.*"
          kubernetes.pod.name: "nginx-ingress-controller.*"
          kubernetes.pod.name: "filebeat.*"

I am trying to ignore weave-net, external-dns, ingress-controller and filebeat events via:

- drop_event.when.regexp:
    or:
      kubernetes.pod.name: "weave-net.*"
      kubernetes.pod.name: "external-dns.*"
      kubernetes.pod.name: "nginx-ingress-controller.*"
      kubernetes.pod.name: "filebeat.*"

However they continue to arrive in Elasticsearch.

Body:
I am using a different approach, that is less efficient in terms on the number of logs that transit in the logging pipeline.

Similarly on how you did, I deployed one instance of filebeat on my nodes, using a daemonset. Nothing special here, this is the configuration I am using:

apiVersion: v1
data:
  filebeat.yml: |-
    filebeat.config:
      prospectors:
        # Mounted `filebeat-prospectors` configmap:
        path: ${path.config}/prospectors.d/*.yml
        # Reload prospectors configs as they change:
        reload.enabled: false
      modules:
        path: ${path.config}/modules.d/*.yml
        # Reload module configs as they change:
        reload.enabled: false

    processors:
      - add_cloud_metadata:

    output.logstash:
      hosts: ['logstash.elk.svc.cluster.local:5044']
kind: ConfigMap
metadata:
  labels:
    k8s-app: filebeat
    kubernetes.io/cluster-service: "true"
  name: filebeat-config

And this one for the prospectors:

apiVersion: v1
data:
  kubernetes.yml: |-
    - type: log
      paths:
        - /var/lib/docker/containers/*/*.log
      json.message_key: log
      json.keys_under_root: true
      processors:
        - add_kubernetes_metadata:
            in_cluster: true
            namespace: ${POD_NAMESPACE}
kind: ConfigMap
metadata:
  labels:
    k8s-app: filebeat
    kubernetes.io/cluster-service: "true"
  name: filebeat-prospectors

The Daemonset spec:

apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  labels:
    k8s-app: filebeat
    kubernetes.io/cluster-service: "true"
  name: filebeat
spec:
  selector:
    matchLabels:
      k8s-app: filebeat
      kubernetes.io/cluster-service: "true"
  template:
    metadata:
      labels:
        k8s-app: filebeat
        kubernetes.io/cluster-service: "true"
    spec:
      containers:
      - args:
        - -c
        - /etc/filebeat.yml
        - -e
        command:
        - /usr/share/filebeat/filebeat
        env:
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: metadata.namespace
        image: docker.elastic.co/beats/filebeat:6.0.1
        imagePullPolicy: IfNotPresent
        name: filebeat
        resources:
          limits:
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 100Mi
        securityContext:
          runAsUser: 0
        volumeMounts:
        - mountPath: /etc/filebeat.yml
          name: config
          readOnly: true
          subPath: filebeat.yml
        - mountPath: /usr/share/filebeat/prospectors.d
          name: prospectors
          readOnly: true
        - mountPath: /usr/share/filebeat/data
          name: data
        - mountPath: /var/lib/docker/containers
          name: varlibdockercontainers
          readOnly: true
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      volumes:
      - configMap:
          name: filebeat-config
        name: config
      - hostPath:
          path: /var/lib/docker/containers
          type: ""
        name: varlibdockercontainers
      - configMap:
          defaultMode: 384
          name: filebeat-prospectors
        name: prospectors
      - emptyDir: {}
        name: data

Basically, all data from all logs from all containers gets forwarded to logstash, reachable at the service endpoint: logstash.elk.svc.cluster.local:5044 (service called "logstash" in the "elk" namespace).

For brevity, I'm gonna give you only the configuration for logstash (if you need more specific help with kubernetes, please ask in the comments):

The logstash.yml file is very basic:

http.host: "0.0.0.0"
path.config: /usr/share/logstash/pipeline

Just indicating the mountpoint of the directory where I mounted the pipeline config files, which are the following:

10-beats.conf:
declares an input for filebeat (port 5044 has to be exposed with a service called "logstash")

input {
  beats {
    port => 5044
    ssl => false
  }
}

49-filter-logs.conf:
this filter basically drops logs coming from pods that don't have the "elk" label. For the pods that do have the "elk" label, it keeps the logs from containers named in the "elk" label of the pod. For instance, if a Pod has two containers, called "nginx" and "python", putting a label "elk" with value "nginx" will only keep the logs coming from the nginx container and drop the python ones. The type of the log is set as the namespace the pod is running in.
This might not be a good fit for everybody (you're going to have a single index in elasticsearch for all logs belonging to a namespace) but it works for me because my logs are homologous.

filter {
    if ![kubernetes][labels][elk] {
        drop {}
    }
    if [kubernetes][labels][elk] {
        # check if kubernetes.labels.elk contains this container name
        mutate {
          split => { "kubernetes[labels][elk]" => "." }
        }
        if [kubernetes][container][name] not in [kubernetes][labels][elk] {
          drop {}
        }
        mutate {
          replace => { "@metadata[type]" => "%{kubernetes[namespace]}" }
          remove_field => [ "beat", "host", "kubernetes[labels][elk]", "kubernetes[labels][pod-template-hash]", "kubernetes[namespace]", "kubernetes[pod][name]", "offset", "prospector[type]", "source", "stream", "time" ]
          rename => { "kubernetes[container][name]" => "container"  }
          rename => { "kubernetes[labels][app]" => "app"  }
        }
    }
}

The rest of the configuration is about log parsing and is not relevant in this context.
The only other important part is the output:

99-output.conf:
Send data to elasticsearch:

output {
  elasticsearch {
    hosts => ["http://elasticsearch.elk.svc.cluster.local:9200"]
    manage_template => false
    index => "%{[@metadata][type]}-%{+YYYY.MM.dd}"
    document_type => "%{[@metadata][type]}"
  }
}

Hope you got the point here.

PROs of this approach

Once deployed filebeat and logstash, as long as you don't need to parse a new type of log, you don't need to update filebeat nor logstash configuration in order to get a new log in kibana. You just need to add a label in the pod template.

All log files get dropped by default, as long as you don't explicitly put the labels.

CONs of this approach

ALL logs from ALL pods come through filebeat and logstash, and get dropped only in logstash. This is a lot of work for logstash and can be resource consuming depending on the number of pods you have in your cluster.

I am sure there are better approaches to this problem, but I think this solution is quite handy, at least for my use case.


问:
Iterate over utf-8 characters in python
I am using python 3.6 to read a file encoded in utf-8, in Spanish (thus, including letter ñ). I open the file with the utf-8 codec, and it loads correctly: while debugging, I can see ñ in the loaded text.

However, when I iterate over characters, ñ is read as two characters, n and ~. Concretely, when I run:

```<code>for c in text:
        hexc = int(hex(ord(c)), 16)
        if U_LETTERS[lang][0] <= hexc <= U_LETTERS[lang][1] \
            or hexc in U_LETTERS[lang][2:] \
            or hexc == U_SPACE:
                filtered_text+=c
</code>```

and text includes an ñ, the variable c takes it as an n (and therefore, hexc is 110 instead of 241), and then it takes ~ (and hexc is 771). I guess there is an internal conversion to an 8 bit char when iterating in this way.  What is the proper way to do this?

Thanks in advance.

答:
This has to do with Unicode normalisation. The letter "ñ" can be expressed either with a single character with the codepoint 0xF1 (241), or with the two character "n" and a combining character for the superposed tilde, ie. the codepoints 0x6E and 0x0303 (110 and 771).

These two ways of expressing the letter are considered equivalent; however, they are not the same in string comparison.
Python provides functionality to convert from one form to the other by means of the unicodedata module.
The first form is called composed (NFC), the second one decomposed (NFD) normalised form.

An example explains it the easiest way:

```<code>>>> import unicodedata
>>> '\xf1'
'ñ'
>>> [ord(c) for c in '\xf1']
[241]
>>> [ord(c) for c in unicodedata.normalize('NFD', '\xf1')]
[110, 771]
>>> [ord(c) for c in unicodedata.normalize('NFC', 'n\u0303')]
[241]
>>>
</code>```

So, to solve your problem, convert all of the text to the desired normalisation form before any further processing.

Note: Unicode normalisation is a problem separate from encoding. You can have this with UTF16 or UTF32 just as well. In the decomposed form, you actually have two (or more) characters (each of which might be represented with multiple bytes, depending on the encoding). It's up the displaying device (the terminal emulator, an editor...) to show this as a single letter with marks above/below the base character.

Title: Iterate over utf-8 characters in python

Body:
I am using python 3.6 to read a file encoded in utf-8, in Spanish (thus, including letter ñ). I open the file with the utf-8 codec, and it loads correctly: while debugging, I can see ñ in the loaded text.

However, when I iterate over characters, ñ is read as two characters, n and ~. Concretely, when I run:

for c in text:
        hexc = int(hex(ord(c)), 16)
        if U_LETTERS[lang][0] <= hexc <= U_LETTERS[lang][1] \
            or hexc in U_LETTERS[lang][2:] \
            or hexc == U_SPACE:
                filtered_text+=c

and text includes an ñ, the variable c takes it as an n (and therefore, hexc is 110 instead of 241), and then it takes ~ (and hexc is 771). I guess there is an internal conversion to an 8 bit char when iterating in this way.  What is the proper way to do this?

Thanks in advance.

Body:
This has to do with Unicode normalisation. The letter "ñ" can be expressed either with a single character with the codepoint 0xF1 (241), or with the two character "n" and a combining character for the superposed tilde, ie. the codepoints 0x6E and 0x0303 (110 and 771).

These two ways of expressing the letter are considered equivalent; however, they are not the same in string comparison.
Python provides functionality to convert from one form to the other by means of the unicodedata module.
The first form is called composed (NFC), the second one decomposed (NFD) normalised form.

An example explains it the easiest way:

>>> import unicodedata
>>> '\xf1'
'ñ'
>>> [ord(c) for c in '\xf1']
[241]
>>> [ord(c) for c in unicodedata.normalize('NFD', '\xf1')]
[110, 771]
>>> [ord(c) for c in unicodedata.normalize('NFC', 'n\u0303')]
[241]
>>>

So, to solve your problem, convert all of the text to the desired normalisation form before any further processing.

Note: Unicode normalisation is a problem separate from encoding. You can have this with UTF16 or UTF32 just as well. In the decomposed form, you actually have two (or more) characters (each of which might be represented with multiple bytes, depending on the encoding). It's up the displaying device (the terminal emulator, an editor...) to show this as a single letter with marks above/below the base character.


问:
Why isn't F# able to resolve overload between Async<> and Async<Result<>>?
I want to understand the F# overload resolution a little better in a specific context.

I'm writing a simple asyncResult workflow/computation expression to make error handling in the style of railway-oriented programming easier to use when combined with async workflows. I do this by overloading the Bind method on the workflow builder. This is fairly standard and used in all guides I've seen (and is also used in e.g. Chessie/ErrorHandling.fs).

I have one overload that accepts an Async<_> and one that accepts a Result<_,_>. Now, ideally, I'd like a third overload that accepts Async<Result<_,_>>. However, when I try to use let! or do! with an expression that returns Async<'a>, F# complains that a unique overload could not be determined, because both Async<_> and Async<Result<_,_>> fits, which of course they do (though one fits more specifically than the other). The only way I seem to be able to do this, is to do like Chessie (link above) and define a wrapper type:

```<code>type AsyncResult<'a, 'b> = AR of Async<Result<'a, 'b>>
</code>```

This again requires me to wrap all calls to methods that return Async<Result<_,_>> in this new type:

```<code>asyncResult {
  let! foo = funcReturningResultInsideAsync() |> AR
  ...
}
</code>```

AFAIK, C# will select the most specific overload. If F# did the same, this would not be a problem.

Why can't F# select the most specific overload?

Can anything be done to avoid the wrapper type in this specific situation?

Edit: As requested in the comments, here's non-compiling code that shows what I ideally would like, but doesn't work.

```<code>module AsyncResult =

  let liftAsync x =
    async { return x }

  let pure (value: 'a) : Async<Result<'a, 'b>> =
    async { return Ok value }

  let returnFrom (value: Async<Result<'a, 'b>>) : Async<Result<'a, 'b>> =
    value

  let bind (binder: 'a -> Async<Result<'b, 'c>>) (asyncResult: Async<Result<'a, 'c>>) : Async<Result<'b, 'c>> =
    async {
      let! result = asyncResult
      match result with
      | Ok x -> return! binder x
      | Error x -> return! Error x |> liftAsync
    }

  let bindResult (binder: 'a -> Async<Result<'b, 'c>>) (result: Result<'a, 'c>) : Async<Result<'b, 'c>> =
    bind binder (liftAsync result)

  let bindAsync (binder: 'a -> Async<Result<'b, 'c>>) (asnc: Async<'a>) : Async<Result<'b, 'c>> =
    bind binder (Async.map Ok asnc)

  type AsyncResultBuilder() =

    member __.Return value = pure value
    member __.ReturnFrom value = returnFrom value
    member __.Bind (asyncResult, binder) = bind binder asyncResult
    member __.Bind (result, binder) = bindResult binder result
    member __.Bind (async, binder) = bindAsync binder async

  let asyncResult = AsyncResultBuilder()

  // Usage

  let functionReturningAsync () =
    async { return 2 }

  let errorHandlingFunction () =
    asyncResult {
      // Error: A unique overload for method 'Bind' could not be determined ...
      do! functionReturningAsync()
    }
</code>```

答:
F# overload resolution is very buggy, it has some rules in the spec but in practice it doesn't respect them. I'm tired of reporting bugs about it and seeing how they're being closed in many cases with a (nonsense) 'by-design' resolution.

You can use some tricks to make an overload preferable over the other. One common trick for Builders is to define it as an extension member, so it will have less priority:

```<code>module AsyncResult =
  let AsyncMap f x = async.Bind(x, async.Return << f)

  let liftAsync x =
    async { return x }

  let pure (value: 'a) : Async<Result<'a, 'b>> =
    async { return Ok value }

  let returnFrom (value: Async<Result<'a, 'b>>) : Async<Result<'a, 'b>> =
    value

  let bind (binder: 'a -> Async<Result<'b, 'c>>) (asyncResult: Async<Result<'a, 'c>>) : Async<Result<'b, 'c>> =
    async {
      let! result = asyncResult
      match result with
      | Ok x -> return! binder x
      | Error x -> return! Error x |> liftAsync
    }

  let bindResult (binder: 'a -> Async<Result<'b, 'c>>) (result: Result<'a, 'c>) : Async<Result<'b, 'c>> =
    bind binder (liftAsync result)

  let bindAsync (binder: 'a -> Async<Result<'b, 'c>>) (asnc: Async<'a>) : Async<Result<'b, 'c>> =
    bind binder (AsyncMap Ok asnc)

  type AsyncResultBuilder() =

    member __.Return value = pure value
    member __.ReturnFrom value = returnFrom value
    member __.Bind (result, binder) = bindResult binder result
    member __.Bind (asyncResult, binder) = bind binder asyncResult

  let asyncResult = AsyncResultBuilder()

open AsyncResult
  type AsyncResultBuilder with
    member __.Bind (async, binder) = bindAsync binder async

  // Usage

  let functionReturningAsync () =
    async { return 2 }

  let functionReturningAsynResult () =
    async { return Ok 'a' }

  let errorHandlingFunction () =
    asyncResult {
      let! x = functionReturningAsync()
      let! y = functionReturningAsynResult()
      let! z = Ok "worked"
      return x, y, z
    }
</code>```

Having said that, I agree 100% with @fyodor-soikin in that it's not a good idea to do this kind of magic for the reasons he explained.

But looks like not everybody agrees with this, apart from Chessie if you have a look at AsyncSeq for example it does some of this magic.

I was criticized over years for abusing of overloading though I was doing it in a consistent way following strict and common accepted rules in general. So I think there are contradictory approaches in the community.

Body:
I want to understand the F# overload resolution a little better in a specific context.

I'm writing a simple asyncResult workflow/computation expression to make error handling in the style of railway-oriented programming easier to use when combined with async workflows. I do this by overloading the Bind method on the workflow builder. This is fairly standard and used in all guides I've seen (and is also used in e.g. Chessie/ErrorHandling.fs).

I have one overload that accepts an Async<_> and one that accepts a Result<_,_>. Now, ideally, I'd like a third overload that accepts Async<Result<_,_>>. However, when I try to use let! or do! with an expression that returns Async<'a>, F# complains that a unique overload could not be determined, because both Async<_> and Async<Result<_,_>> fits, which of course they do (though one fits more specifically than the other). The only way I seem to be able to do this, is to do like Chessie (link above) and define a wrapper type:

type AsyncResult<'a, 'b> = AR of Async<Result<'a, 'b>>

This again requires me to wrap all calls to methods that return Async<Result<_,_>> in this new type:

asyncResult {
  let! foo = funcReturningResultInsideAsync() |> AR
  ...
}

AFAIK, C# will select the most specific overload. If F# did the same, this would not be a problem.

Why can't F# select the most specific overload?

Can anything be done to avoid the wrapper type in this specific situation?

Edit: As requested in the comments, here's non-compiling code that shows what I ideally would like, but doesn't work.

module AsyncResult =

  let liftAsync x =
    async { return x }

  let pure (value: 'a) : Async<Result<'a, 'b>> =
    async { return Ok value }

  let returnFrom (value: Async<Result<'a, 'b>>) : Async<Result<'a, 'b>> =
    value

  let bind (binder: 'a -> Async<Result<'b, 'c>>) (asyncResult: Async<Result<'a, 'c>>) : Async<Result<'b, 'c>> =
    async {
      let! result = asyncResult
      match result with
      | Ok x -> return! binder x
      | Error x -> return! Error x |> liftAsync
    }

  let bindResult (binder: 'a -> Async<Result<'b, 'c>>) (result: Result<'a, 'c>) : Async<Result<'b, 'c>> =
    bind binder (liftAsync result)

  let bindAsync (binder: 'a -> Async<Result<'b, 'c>>) (asnc: Async<'a>) : Async<Result<'b, 'c>> =
    bind binder (Async.map Ok asnc)

  type AsyncResultBuilder() =

    member __.Return value = pure value
    member __.ReturnFrom value = returnFrom value
    member __.Bind (asyncResult, binder) = bind binder asyncResult
    member __.Bind (result, binder) = bindResult binder result
    member __.Bind (async, binder) = bindAsync binder async

  let asyncResult = AsyncResultBuilder()

  // Usage

  let functionReturningAsync () =
    async { return 2 }

  let errorHandlingFunction () =
    asyncResult {
      // Error: A unique overload for method 'Bind' could not be determined ...
      do! functionReturningAsync()
    }

Body:
F# overload resolution is very buggy, it has some rules in the spec but in practice it doesn't respect them. I'm tired of reporting bugs about it and seeing how they're being closed in many cases with a (nonsense) 'by-design' resolution.

You can use some tricks to make an overload preferable over the other. One common trick for Builders is to define it as an extension member, so it will have less priority:

module AsyncResult =
  let AsyncMap f x = async.Bind(x, async.Return << f)

  let liftAsync x =
    async { return x }

  let pure (value: 'a) : Async<Result<'a, 'b>> =
    async { return Ok value }

  let returnFrom (value: Async<Result<'a, 'b>>) : Async<Result<'a, 'b>> =
    value

  let bind (binder: 'a -> Async<Result<'b, 'c>>) (asyncResult: Async<Result<'a, 'c>>) : Async<Result<'b, 'c>> =
    async {
      let! result = asyncResult
      match result with
      | Ok x -> return! binder x
      | Error x -> return! Error x |> liftAsync
    }

  let bindResult (binder: 'a -> Async<Result<'b, 'c>>) (result: Result<'a, 'c>) : Async<Result<'b, 'c>> =
    bind binder (liftAsync result)

  let bindAsync (binder: 'a -> Async<Result<'b, 'c>>) (asnc: Async<'a>) : Async<Result<'b, 'c>> =
    bind binder (AsyncMap Ok asnc)

  type AsyncResultBuilder() =

    member __.Return value = pure value
    member __.ReturnFrom value = returnFrom value
    member __.Bind (result, binder) = bindResult binder result
    member __.Bind (asyncResult, binder) = bind binder asyncResult

  let asyncResult = AsyncResultBuilder()

open AsyncResult
  type AsyncResultBuilder with
    member __.Bind (async, binder) = bindAsync binder async

  // Usage

  let functionReturningAsync () =
    async { return 2 }

  let functionReturningAsynResult () =
    async { return Ok 'a' }

  let errorHandlingFunction () =
    asyncResult {
      let! x = functionReturningAsync()
      let! y = functionReturningAsynResult()
      let! z = Ok "worked"
      return x, y, z
    }

Having said that, I agree 100% with @fyodor-soikin in that it's not a good idea to do this kind of magic for the reasons he explained.

But looks like not everybody agrees with this, apart from Chessie if you have a look at AsyncSeq for example it does some of this magic.

I was criticized over years for abusing of overloading though I was doing it in a consistent way following strict and common accepted rules in general. So I think there are contradictory approaches in the community.


问:
Compile libxml2 get "error: storage size of ‘hints’ isn’t known"
I download libxml2 2.9.4 in here, when i ./configure; make, i get following error:

```<code>nanohttp.c: In function ‘xmlNanoHTTPConnectHost’:
nanohttp.c:1073:18: error: storage size of ‘hints’ isn’t known
  struct addrinfo hints, *res, *result;
                  ^
nanohttp.c:1079:11: warning: implicit declaration of function ‘getaddrinfo’ [-Wimplicit-function-declaration]
  status = getaddrinfo (host, NULL, &hints, &result);
           ^
nanohttp.c:1079:2: warning: nested extern declaration of ‘getaddrinfo’ [-Wnested-externs]
  status = getaddrinfo (host, NULL, &hints, &result);
  ^
nanohttp.c:1085:35: error: dereferencing pointer to incomplete type ‘struct addrinfo’
  for (res = result; res; res = res->ai_next) {
                                   ^
nanohttp.c:1089:7: warning: implicit declaration of function ‘freeaddrinfo’ [-Wimplicit-function-declaration]
       freeaddrinfo (result);
       ^
nanohttp.c:1089:7: warning: nested extern declaration of ‘freeaddrinfo’ [-Wnested-externs]
nanohttp.c:1073:18: warning: unused variable ‘hints’ [-Wunused-variable]
  struct addrinfo hints, *res, *result;
                  ^
</code>```

the full output is here

here is my environment:

```<code>roroco@roroco ~/Downloads $ lsb_release  -a
No LSB modules are available.
Distributor ID: LinuxMint
Description:    Linux Mint 17.3 Rosa
Release:    17.3
Codename:   rosa
roroco@roroco ~/Downloads $ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.5-2ubuntu1~14.04.1' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.5 (Ubuntu 4.8.5-2ubuntu1~14.04.1)
</code>```

I try version 2.9.2 and 2.9.3, i get same error, how to compile libxml2 success?

答:
For using "getaddrinfo" you must use POSIX.
For clean C, use gcc flag -std=gnu11 or -std=gnu99 instead с11/с99.
Checking for POSIX you can using this code:

```<code>#ifdef  __USE_POSIX
    #warning POSIX compatible
#else
    #warning no support POSIX
#endif
</code>```

Body:
I download libxml2 2.9.4 in here, when i ./configure; make, i get following error:

nanohttp.c: In function ‘xmlNanoHTTPConnectHost’:
nanohttp.c:1073:18: error: storage size of ‘hints’ isn’t known
  struct addrinfo hints, *res, *result;
                  ^
nanohttp.c:1079:11: warning: implicit declaration of function ‘getaddrinfo’ [-Wimplicit-function-declaration]
  status = getaddrinfo (host, NULL, &hints, &result);
           ^
nanohttp.c:1079:2: warning: nested extern declaration of ‘getaddrinfo’ [-Wnested-externs]
  status = getaddrinfo (host, NULL, &hints, &result);
  ^
nanohttp.c:1085:35: error: dereferencing pointer to incomplete type ‘struct addrinfo’
  for (res = result; res; res = res->ai_next) {
                                   ^
nanohttp.c:1089:7: warning: implicit declaration of function ‘freeaddrinfo’ [-Wimplicit-function-declaration]
       freeaddrinfo (result);
       ^
nanohttp.c:1089:7: warning: nested extern declaration of ‘freeaddrinfo’ [-Wnested-externs]
nanohttp.c:1073:18: warning: unused variable ‘hints’ [-Wunused-variable]
  struct addrinfo hints, *res, *result;
                  ^

the full output is here

here is my environment:

roroco@roroco ~/Downloads $ lsb_release  -a
No LSB modules are available.
Distributor ID: LinuxMint
Description:    Linux Mint 17.3 Rosa
Release:    17.3
Codename:   rosa
roroco@roroco ~/Downloads $ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.5-2ubuntu1~14.04.1' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.5 (Ubuntu 4.8.5-2ubuntu1~14.04.1)

I try version 2.9.2 and 2.9.3, i get same error, how to compile libxml2 success?

Body:
For using "getaddrinfo" you must use POSIX.
For clean C, use gcc flag -std=gnu11 or -std=gnu99 instead с11/с99.
Checking for POSIX you can using this code:

#ifdef  __USE_POSIX
    #warning POSIX compatible
#else
    #warning no support POSIX
#endif


问:
Get the content of iCloud Drive
In my viewDidLoad, I need to verify if the files that I saved to the iCloud Drive are still available and have not been deleted. As I have read, I can not use standard FileManager calls:

```<code>FileManager.default.fileExists(atPath: filePath)
</code>```

What would be the alternative. I do use NSMetadataQuery, but I wanted to know if there is an easy way to query the content of the App's UbiquitousDocument prior to the notifications kicking in.

Also, I am using the ios11 facility of sharing files between different users, and again I need to be able to verify if those files are still available when my App comes to the foreground. Using the NSMetadataQuery and searching in NSMetadataQueryUbiquitousDocumentsScope the shared documents are not displayed.

Any suggestions

Best

Reza

答:
Try this:

```<code>let DOCUMENTS_DIRECTORY = "Documents"
if let iCloudDocumentsURL = NSFileManager.defaultManager().URLForUbiquityContainerIdentifier(nil)?.URLByAppendingPathComponent(DOCUMENTS_DIRECTORY)?.URLByAppendingPathComponent(fileName) {
    if let pathComponent = iCloudDocumentsURL.path {
        if (NSFileManager.defaultManager().fileExistsAtPath(pathComponent, isDirectory: nil)) {
        }
    }
}
</code>```

This checks the file under the public Document directory.

Title: Get the content of iCloud Drive

Body:
In my viewDidLoad, I need to verify if the files that I saved to the iCloud Drive are still available and have not been deleted. As I have read, I can not use standard FileManager calls:

FileManager.default.fileExists(atPath: filePath)

What would be the alternative. I do use NSMetadataQuery, but I wanted to know if there is an easy way to query the content of the App's UbiquitousDocument prior to the notifications kicking in.

Also, I am using the ios11 facility of sharing files between different users, and again I need to be able to verify if those files are still available when my App comes to the foreground. Using the NSMetadataQuery and searching in NSMetadataQueryUbiquitousDocumentsScope the shared documents are not displayed.

Any suggestions

Best

Reza

Body:
Try this:

let DOCUMENTS_DIRECTORY = "Documents"
if let iCloudDocumentsURL = NSFileManager.defaultManager().URLForUbiquityContainerIdentifier(nil)?.URLByAppendingPathComponent(DOCUMENTS_DIRECTORY)?.URLByAppendingPathComponent(fileName) {
    if let pathComponent = iCloudDocumentsURL.path {
        if (NSFileManager.defaultManager().fileExistsAtPath(pathComponent, isDirectory: nil)) {
        }
    }
}

This checks the file under the public Document directory.


问:
Converting Java date string to JavaScript date
Is there any JavaScript library that takes Java's SimpleDateFormat pattern and parses a date/time string into a Date object?

For example, given "2010-12-20" and "yyyy-MM-dd", the function would parse "2010-12-20" and return a JavaScript Date.

I noticed that jQuery UI's Date Picker can format and parse a date string, but it doesn't work with a time string.

Thanks.

答: I guess you are not the only looking for it. See this thread that has similar content. In the end, the author had to role his/her own but there are some good suggestions in there that might be of benefit.</p>


问:
disable other input[file] untill the first ( file #1 ) one is selected
i have multiple inputs for file upload and i'm trying by jquery to disable the rest until the first ( file #1 ) one is selected then enable the rest

My code :

<input type="file" id="checkupload" name="image" />
<input type="file" id="checkupload" name="image2" />
<input type="file" id="checkupload" name="image3" />
<input type="file" id="checkupload" name="image4" />
<input type="file" id="checkupload" name="image5" />
<input type="file" id="checkupload" name="image6" />
<input type="file" id="checkupload" name="image7" />
<input type="file" id="checkupload" name="image8" />```

答:
A simple solution

Disable by default all inputs but not the first one.
Add classes to your input files in order to distinguish disabled style.
Change style as you wish.
Add

```<code> <input type="file" onchange="fileSelected(this)" id="checkupload1" class="file-design" name="image" />
 <input type="file" id="checkupload2" class="file-design" name="image2"  disabled="disabled"/>
 <input type="file" id="checkupload3" class="file-design" name="image3"  disabled="disabled"/>
 <input type="file" id="checkupload4" class="file-design" name="image4"  disabled="disabled"/>
 <input type="file" id="checkupload5" class="file-design" name="image5"  disabled="disabled"/>
 <input type="file" id="checkupload6" class="file-design" name="image6"  disabled="disabled"/>
 <input type="file" id="checkupload7" class="file-design" name="image7"  disabled="disabled"/>
 <input type="file" id="checkupload8" class="file-design" name="image8"  disabled="disabled"/>
</code>```

And by this code, you can do what you want.

```<code> <script type="text/javascript">

  function fileSelected(sender) {

   $(".file-design").prop('disabled', false);

  }

 </script>
</code>```

Body:
A simple solution

Disable by default all inputs but not the first one.
Add classes to your input files in order to distinguish disabled style.
Change style as you wish.
Add

 <input type="file" onchange="fileSelected(this)" id="checkupload1" class="file-design" name="image" />
 <input type="file" id="checkupload2" class="file-design" name="image2"  disabled="disabled"/>
 <input type="file" id="checkupload3" class="file-design" name="image3"  disabled="disabled"/>
 <input type="file" id="checkupload4" class="file-design" name="image4"  disabled="disabled"/>
 <input type="file" id="checkupload5" class="file-design" name="image5"  disabled="disabled"/>
 <input type="file" id="checkupload6" class="file-design" name="image6"  disabled="disabled"/>
 <input type="file" id="checkupload7" class="file-design" name="image7"  disabled="disabled"/>
 <input type="file" id="checkupload8" class="file-design" name="image8"  disabled="disabled"/>

And by this code, you can do what you want.

 <script type="text/javascript">

  function fileSelected(sender) {

   $(".file-design").prop('disabled', false);

  }

 </script>


问:
Splitting LuisDialog methods (LuisIntents) into multiple files or categories
I'm working on a Microsoft bot framework project where I create a LuisDialog to interact with the user. I'm probably going to have a lot of different intents which can be sorted into multiple categories.

What is the best way to organize this?

Should I make one big LuisDialog class with all the LuisIntent methods?

This seems kind of unorganized to me.

Should I make multiple LuisDialog classes for each category?

This might give a lot of problems. What if I have an intent that should be covered by all categories? How can I make sure the user message is checked by every LuisDialog? This is probably not a good idea.

Should I make one big LuisDialog class that somehow 'includes' methods from other files/classes, where each file/class represents a category?

This seems like a good idea, however, I have no idea how to do this in C#

If you have other suggestions, please let me know, thanks in advance!

答:
I actually did this differently.

I have a main Luis Dialog, with the intent detection of all intents.
Based on the intent I am forwarding the message to my 2nd LUIS dialog.
This captures related intents and an empty string intent to "catch all the rest"

If I get to the "all the rest", I set the context.DOne, returning to my root LUIS dialog, which reinterprets the LuisResult received from the 2nd one and handles that.

An example is 'weather' intent. It gets to LuisDialog1
I do a context.forward(new LuisDialog2(), ResumeAfter...)

LuisDialog2() handles also the 'weather' intent, but for example also 'MoreWeather'
If none of those is detected, it gets into the LuisDialog2 [LuisIntent("")] handler method.
This sets a context.Done(luisresultobject) returning to the LuisDialog1 ResumeAfter method where this code resides

```<code>var lLuisResult = await result as LuisResult;
IntentRecommendation lRecommendation = this.BestIntentFrom(lLuisResult);
await DispatchToIntentHandler(context, null, lRecommendation, lLuisResult);
</code>```

Still not sure about the null parameter, but it is working correctly in all my tests so far

Hope it is clear :(

Body:
I actually did this differently.

I have a main Luis Dialog, with the intent detection of all intents.
Based on the intent I am forwarding the message to my 2nd LUIS dialog.
This captures related intents and an empty string intent to "catch all the rest"

If I get to the "all the rest", I set the context.DOne, returning to my root LUIS dialog, which reinterprets the LuisResult received from the 2nd one and handles that.

An example is 'weather' intent. It gets to LuisDialog1
I do a context.forward(new LuisDialog2(), ResumeAfter...)

LuisDialog2() handles also the 'weather' intent, but for example also 'MoreWeather'
If none of those is detected, it gets into the LuisDialog2 [LuisIntent("")] handler method.
This sets a context.Done(luisresultobject) returning to the LuisDialog1 ResumeAfter method where this code resides

var lLuisResult = await result as LuisResult;
IntentRecommendation lRecommendation = this.BestIntentFrom(lLuisResult);
await DispatchToIntentHandler(context, null, lRecommendation, lLuisResult);

Still not sure about the null parameter, but it is working correctly in all my tests so far

Hope it is clear :(


问:
Sonar does not shows up Code Coverage after build successful with Jenkins Sonar plugin
I am trying to get code coverage with Sonar and Jenkins. I see Jenkins' Sonar plugin successfully executes JUnit test cases and completes build successfully. But Sonar does not show Code Coverage results (always shows 0.0% as the code coverage) on the project. But Sonar does show "Unit test success".

I am using Maven with Jenkins and Sonar.

I get the below message in Jenkins logs while executing the Sonar plugin:

```
Project coverage is set to 0% as no JaCoCo execution data has been dumped: .../sonar/target/jacoco.exec
```

Can any one help me how to get correct code coverage on any Sonar project.

答:
According to this blog post, you probably forget to set the sonar.binaries property in your project properties

Don’t forget sonar.binaries, otherwise, you might get something like « Project coverage is set to 0% since there is no directories with classes. » in your logs.

```<code>[...]
sonar.surefire.reportsPath=target/surefire-reports
sonar.jacoco.reportPath=target/jacoco.exec
sonar.binaries=target/classes
[...]
</code>```

Body:
I am trying to get code coverage with Sonar and Jenkins. I see Jenkins' Sonar plugin successfully executes JUnit test cases and completes build successfully. But Sonar does not show Code Coverage results (always shows 0.0% as the code coverage) on the project. But Sonar does show "Unit test success".

I am using Maven with Jenkins and Sonar.

I get the below message in Jenkins logs while executing the Sonar plugin:

Project coverage is set to 0% as no JaCoCo execution data has been dumped: .../sonar/target/jacoco.exec

Can any one help me how to get correct code coverage on any Sonar project.

Body:
According to this blog post, you probably forget to set the sonar.binaries property in your project properties

Don’t forget sonar.binaries, otherwise, you might get something like « Project coverage is set to 0% since there is no directories with classes. » in your logs.

[...]
sonar.surefire.reportsPath=target/surefire-reports
sonar.jacoco.reportPath=target/jacoco.exec
sonar.binaries=target/classes
[...]


问:
How to reuse a single Postgres DB connection for row inserts in Go?
I'm trying to use Go to insert a row of data into a Postgres table for each new message received from rabbitmq, using a single connection to the DB which is  opened in the init function of the code below.

Rather than opening just one connection, the code opens 497 and maxes out which causes the row inserts to stop...

I have tried using the info in these questions opening and closing DB connection in Go app and open database connection inside a function which say I should open one connection and use global db to allow the main function to pass the sql statement to the connection opened in the init function.

I thought I had done this, however a new connection is being opened for each new row so the code stops working once the postgres connection limit is reached...

I am new to Go and have limited programming experience, I have been trying to understand/resolve this issue for the past two days and I could really do with some help understanding where I am going wrong with this...

```<code>var db *sql.DB

func init() {
    var err error
    db, err = sql.Open ( "postgres", "postgres://postgres:postgres@SERVER/PORT/DB")
    if err != nil {
        log.Fatal("Invalid DB config:", err)
    }
    if err = db.Ping(); err != nil {
        log.Fatal("DB unreachable:", err)
    }
}

func main() {

// RABBITMQ CONNECTION CODE IS HERE

// EACH MESSAGE RECEIVED IS SPLIT TO LEGEND, STATUS, TIMESTAMP VARIABLES

// VARIABLES ARE PASSED TO sqlSatement

        sqlStatement := `
        INSERT INTO heartbeat ("Legend", "Status", "TimeStamp")
        VALUES ($1, $2, $3)
`
        // sqlStatement IS THEN PASSED TO db.QueryRow

        db.QueryRow(sqlStatement, Legend, Status, TimeStamp)
    }
}()

<-forever
}
</code>```

Full code is shown below:

```<code>package main

import (
    "database/sql"
    "log"
    _ "github.com/lib/pq"

    "github.com/streadway/amqp"
    "strings"
)
var db *sql.DB

func failOnError(err error, msg string) {
    if err != nil {
        log.Fatalf("%s: %s", msg, err)
    }
}

func init() {
    var err error
    db, err = sql.Open ( "postgres", "postgres://postgres:postgres@192.168.1.69:5432/test?sslmode=disable")
    if err != nil {
        log.Fatal("Invalid DB config:", err)
    }
    if err = db.Ping(); err != nil {
        log.Fatal("DB unreachable:", err)
    }
}

func main() {
    conn, err := amqp.Dial("amqp://Admin:Admin@192.168.1.69:50003/")
    failOnError(err, "Failed to connect to RabbitMQ")
    defer conn.Close()

    ch, err := conn.Channel()
    failOnError(err, "Failed to open a channel")
    defer ch.Close()

    q, err := ch.QueueDeclare(
        "HEARTBEAT", // name
        false,       // durable
        false,       // delete when unused
        false,       // exclusive
        false,       // no-wait
        nil,         // arguments
    )
    failOnError(err, "Failed to declare a queue")

    msgs, err := ch.Consume(
        q.Name, // queue
        "",     // consumer
        false,  // auto-ack
        false,  // exclusive
        false,  // no-local
        false,  // no-wait
        nil,    // args
    )
    failOnError(err, "Failed to register a consumer")

    forever := make(chan bool)

    go func() {

        for d := range msgs {
            myString := string(d.Body[:])
            result := strings.Split(myString, ",")
            Legend := result[0]
            Status := result[1]
            TimeStamp := result[2]

            sqlStatement := `
    INSERT INTO heartbeat ("Legend", "Status", "TimeStamp")
    VALUES ($1, $2, $3)
    `
            //
            db.QueryRow(sqlStatement, Legend, Status, TimeStamp)
        }
    }()

    <-forever
}
</code>```

答:
First off, *sql.DB is not a connection but a pool of connections, it will open as many connection as it needs to and as many as the postgres server allows. It only opens new connections when there is no idle one in the pool ready for use.

So the issue is that the connections that DB opens aren't being released, why? Because you're using QueryRow without calling Scan on the returned *Row value.

Under the hood *Row holds a *Rows instance which has access to its own connection and that connection is released automatically when Scan is called. If Scan is not called then the connection is not released which causes the DB pool to open a new connection on the next call to QueryRow. So since you're not releasing any connections DB keeps opening new ones until it hits the limit specified by the postgres settings and then the next call to QueryRow hangs because it waits for a connection to become idle.

So you either need to use Exec if you don't care about the output, or you need to call Scan on the returned *Row.

Body:
I'm trying to use Go to insert a row of data into a Postgres table for each new message received from rabbitmq, using a single connection to the DB which is  opened in the init function of the code below.

Rather than opening just one connection, the code opens 497 and maxes out which causes the row inserts to stop...

I have tried using the info in these questions opening and closing DB connection in Go app and open database connection inside a function which say I should open one connection and use global db to allow the main function to pass the sql statement to the connection opened in the init function.

I thought I had done this, however a new connection is being opened for each new row so the code stops working once the postgres connection limit is reached...

I am new to Go and have limited programming experience, I have been trying to understand/resolve this issue for the past two days and I could really do with some help understanding where I am going wrong with this...

var db *sql.DB

func init() {
    var err error
    db, err = sql.Open ( "postgres", "postgres://postgres:postgres@SERVER/PORT/DB")
    if err != nil {
        log.Fatal("Invalid DB config:", err)
    }
    if err = db.Ping(); err != nil {
        log.Fatal("DB unreachable:", err)
    }
}

func main() {

// RABBITMQ CONNECTION CODE IS HERE

// EACH MESSAGE RECEIVED IS SPLIT TO LEGEND, STATUS, TIMESTAMP VARIABLES

// VARIABLES ARE PASSED TO sqlSatement

        sqlStatement := `
        INSERT INTO heartbeat ("Legend", "Status", "TimeStamp")
        VALUES ($1, $2, $3)
`
        // sqlStatement IS THEN PASSED TO db.QueryRow

        db.QueryRow(sqlStatement, Legend, Status, TimeStamp)
    }
}()

<-forever
}

Full code is shown below:

package main

import (
    "database/sql"
    "log"
    _ "github.com/lib/pq"

    "github.com/streadway/amqp"
    "strings"
)
var db *sql.DB

func failOnError(err error, msg string) {
    if err != nil {
        log.Fatalf("%s: %s", msg, err)
    }
}

func init() {
    var err error
    db, err = sql.Open ( "postgres", "postgres://postgres:postgres@192.168.1.69:5432/test?sslmode=disable")
    if err != nil {
        log.Fatal("Invalid DB config:", err)
    }
    if err = db.Ping(); err != nil {
        log.Fatal("DB unreachable:", err)
    }
}

func main() {
    conn, err := amqp.Dial("amqp://Admin:Admin@192.168.1.69:50003/")
    failOnError(err, "Failed to connect to RabbitMQ")
    defer conn.Close()

    ch, err := conn.Channel()
    failOnError(err, "Failed to open a channel")
    defer ch.Close()

    q, err := ch.QueueDeclare(
        "HEARTBEAT", // name
        false,       // durable
        false,       // delete when unused
        false,       // exclusive
        false,       // no-wait
        nil,         // arguments
    )
    failOnError(err, "Failed to declare a queue")

    msgs, err := ch.Consume(
        q.Name, // queue
        "",     // consumer
        false,  // auto-ack
        false,  // exclusive
        false,  // no-local
        false,  // no-wait
        nil,    // args
    )
    failOnError(err, "Failed to register a consumer")

    forever := make(chan bool)

    go func() {

        for d := range msgs {
            myString := string(d.Body[:])
            result := strings.Split(myString, ",")
            Legend := result[0]
            Status := result[1]
            TimeStamp := result[2]

            sqlStatement := `
    INSERT INTO heartbeat ("Legend", "Status", "TimeStamp")
    VALUES ($1, $2, $3)
    `
            //
            db.QueryRow(sqlStatement, Legend, Status, TimeStamp)
        }
    }()

    <-forever
}


问:
How can I express `find` conditions in MongoDB accessing document's values?
For example, let's put the case I want all documents inside a collection which are inside a range, whose maximum value is inside the document itself:

```<code>myDocument {
   qty: Number,
   maxValue: Number,
}

db.collection.find( { field: { $gt: 0, $lt: maxValue } } );
</code>```

Is it possible to do something like that in MongoDB?

答:
First, run this command to get max value of "maxValue" field

```<code>db.collection.find({},{maxValue:1}).sort({"maxValue":-1}).limit(1)
</code>```

Let's assume max value is 3000.

Then run this

```<code>db.collection.find({"maxValue":{"$gte":0 ,"$lte":3000}})
</code>```

Body:
For example, let's put the case I want all documents inside a collection which are inside a range, whose maximum value is inside the document itself:

myDocument {
   qty: Number,
   maxValue: Number,
}

db.collection.find( { field: { $gt: 0, $lt: maxValue } } );

Is it possible to do something like that in MongoDB?

Body:
First, run this command to get max value of "maxValue" field

db.collection.find({},{maxValue:1}).sort({"maxValue":-1}).limit(1)

Let's assume max value is 3000.

Then run this

db.collection.find({"maxValue":{"$gte":0 ,"$lte":3000}})


答: Running multi-threads doesn't mean use the CPU more, have a look on parallel programming in .Net framework.</p>

Title: How can i make my program run faster: Is using threads a solution?

Body:
I've written a program building a huge search tree. Because my tree is too huge i expected that my program will use over 40 % of cpu. instead of this my program uses not over 10 % of cpu even it runs with high priority.

If threads will use more cpu while running parallel, let me please know it; i can divide my program in threads, i need really reduce the search time.

Thanks !


问:
creating a new derived type based on another class

I am trying to get a new derived class pointer (with new memory location) from a parent class pointer that points to a derived class.

```<code>#include <iostream>
#include <random>
class parent{
public:
    virtual void print(){
        printf("p\n");
    }
};

class dev1: public parent{
public:
    void print(){
        printf("d1\n");
    }
};

class dev2:public parent{
public:
    void print(){
        printf("d2\n");
    }
};

int main(int argc, const char * argv[]) {
    srand(time(NULL));

    parent*bob;

    if(rand()%2){
        bob = new dev1();
    }else{
        bob = new dev2();
    }
    bob->print();

    parent * bobwannabe;

    bobwannabe = new typeof(*bob);

    bobwannabe->print();

    return 0;
}
</code>```

I was hoping bobwannabe would print what bob prints. bobwannabe shouldn't point to the same object as bob but should be the same class as bob.

Right now, bobwannabe becomes a new parent. Is there a way to turn it into a new bob with it's current type or is there a specific term I should be searching up?
 Thank you!

答:
What you want is to clone the object. You need to do it yourself by adding a virtual clone method and copy constructor to the parent class:

```<code> virtual parent * clone() const { return new parent(*this); }
</code>```

And override in all derived classes like for dev1:

```<code> virtual parent * clone() const override { return new dev1(*this); }
</code>```

and use with:

```<code> parent *bobwannabe = bob->clone();
</code>```

See also 'typeid' versus 'typeof' in C++ and don't forget to delete your objects or use smart pointers.

Body:
I am trying to get a new derived class pointer (with new memory location) from a parent class pointer that points to a derived class.

#include <iostream>
#include <random>
class parent{
public:
    virtual void print(){
        printf("p\n");
    }
};

class dev1: public parent{
public:
    void print(){
        printf("d1\n");
    }
};

class dev2:public parent{
public:
    void print(){
        printf("d2\n");
    }
};

int main(int argc, const char * argv[]) {
    srand(time(NULL));

    parent*bob;

    if(rand()%2){
        bob = new dev1();
    }else{
        bob = new dev2();
    }
    bob->print();

    parent * bobwannabe;

    bobwannabe = new typeof(*bob);

    bobwannabe->print();

    return 0;
}

I was hoping bobwannabe would print what bob prints. bobwannabe shouldn't point to the same object as bob but should be the same class as bob.

Right now, bobwannabe becomes a new parent. Is there a way to turn it into a new bob with it's current type or is there a specific term I should be searching up?
 Thank you!

Body:
What you want is to clone the object. You need to do it yourself by adding a virtual clone method and copy constructor to the parent class:

 virtual parent * clone() const { return new parent(*this); }

And override in all derived classes like for dev1:

 virtual parent * clone() const override { return new dev1(*this); }

and use with:

 parent *bobwannabe = bob->clone();

See also 'typeid' versus 'typeof' in C++ and don't forget to delete your objects or use smart pointers.


问:
How to add a tag to the product listing title?
I'm working on a Shopify store and the client requested to add a particular tag (Notes_tag) after the product title on the collection listing page.
How can I check for this condition (if that particular tag exists) and if so, add it to the product title?

I have tried this:

```<code>  {% for tag in collection.tags %}
    {% if tag contains 'Notes_' %}
      <h1>Have notes</h1>
    {% endif %}
  {% endfor %}
</code>```

答:
You have to find tag in product not in collection.
Please try this below code</p>

```<code>  {% for tag in product.tags %}
    {% if tag contains 'Notes_' %}
      <h1>Have notes</h1>
    {% endif %}
  {% endfor %}
</code>```

Body:
I'm working on a Shopify store and the client requested to add a particular tag (Notes_tag) after the product title on the collection listing page.
How can I check for this condition (if that particular tag exists) and if so, add it to the product title?

I have tried this:

  {% for tag in collection.tags %}
    {% if tag contains 'Notes_' %}

Have notes

    {% endif %}
  {% endfor %}

Body:
You have to find tag in product not in collection.
Please try this below code

  {% for tag in product.tags %}
    {% if tag contains 'Notes_' %}
      <h1>Have notes</h1>
    {% endif %}
  {% endfor %}


问:
Does AWS S3 Have a concept of files being 'updated'?
I'd like to write a Lambda function that is triggered when files are added or modified in an s3 bucket and processes them and moves them elsewhere, clobbering older versions of the files.

I'm wondering if AWS Lambda can be configured to trigger when files are updated?

After reviewing the Boto3 documentation for s3 it looks like the only things that could happen in a s3 bucket would be creations and deletions.

Additionally, the AWS documentation seems to indicate there is no way to trigger things on 'updates' to S3.

Am I correct in thinking there is no real concept of an 'update' to a file in S3 and that an update would actually be when something was destroyed and recreated? If I'm mistaken, how can I trigger a Lambda function when an S3 file is changed in a bucket?

答: There is now a new functionality for S3 buckets. Under properties there is the possibility to enable versioning for this bucket. And if you set a trigger for creating on S3 assigned to your Lambda function - this will executed every time if you 'update' the same file as it is a new version. </p>


问:
CodeIgniter: Cannot set_userdata
My codeigniter app is a subdomain of wordpress site.

When I set a session, it's not working.

I already autoloaded the session library and trace other possible problems.

The problem is that $this->session->set_userdata() seems to not work.

答:
Try changing your cookie config as follows and see if it works:

```<code>$config['cookie_domain']    = '.yourdomain.com';
$config['cookie_path']      = '/crm';
</code>```

If it still doesn't work you can try configuring CI to save the session data in the database instead of disk.

Also make sure your $config['base_url'] is set to http://yourdomain.com/crm/

You should also have an .htaccess in your crm directory and it should contain below lines:

```<code>RewriteEngine on
RewriteBase /crm/
</code>```

Title: CodeIgniter: Cannot set_userdata

Body:
Try changing your cookie config as follows and see if it works:

$config['cookie_domain']    = '.yourdomain.com';
$config['cookie_path']      = '/crm';

If it still doesn't work you can try configuring CI to save the session data in the database instead of disk.

Also make sure your $config['base_url'] is set to http://yourdomain.com/crm/

You should also have an .htaccess in your crm directory and it should contain below lines:

RewriteEngine on
RewriteBase /crm/


问:
c++11 implementation of Well Equidistributed Long-period Linear (WELL) without boost?
Sergiu Dotenco kindly contributed his implementation based on boost, could someone recommend an open-source c++11 style implementation, without boost? Google does provided some results, but this is a bit deep in math, I could not differentiate the quality of the implmentation.</p>

答:
Here's how c++11 has made compile time programming (slightly) easier</p>

```<code>template <typename UIntType> constexpr bool IsPowerOfTwo(UIntType r)
{
    return (r & (r - 1)) == 0;
}

namespace detail
{
    template<class UIntType, UIntType r, bool>
    struct ModuloHelper;

    template<class UIntType, UIntType r>
    struct ModuloHelper<UIntType, r, true>
    {
        template<class T>
        static T calc(T value)
        {
            return value & (r - 1);
        }
    };

    template<class UIntType, UIntType r>
    struct ModuloHelper<UIntType, r, false>
    {
        template<class T>
        static T calc(T value)
        {
            while (value >= r)
            { value -= r; }

            return value;
        }
    };
}

template<class UIntType, UIntType r>
struct Modulo : detail::ModuloHelper<UIntType, r, IsPowerOfTwo(r)>
</code>```

Body:
Here's how c++11 has made compile time programming (slightly) easier

template <typename UIntType> constexpr bool IsPowerOfTwo(UIntType r)
{
    return (r & (r - 1)) == 0;
}

namespace detail
{
    template<class UIntType, UIntType r, bool>
    struct ModuloHelper;

    template<class UIntType, UIntType r>
    struct ModuloHelper<UIntType, r, true>
    {
        template<class T>
        static T calc(T value)
        {
            return value & (r - 1);
        }
    };

    template<class UIntType, UIntType r>
    struct ModuloHelper<UIntType, r, false>
    {
        template<class T>
        static T calc(T value)
        {
            while (value >= r)
            { value -= r; }

            return value;
        }
    };
}

template<class UIntType, UIntType r>
struct Modulo : detail::ModuloHelper<UIntType, r, IsPowerOfTwo(r)>


问:
How to adjust QTextEdit to fit it's contents
I'm developing a Qt Application and I'm trying to find a way to use QTextEdit as a label with long text without the scroll bar. In my ui I have a QScrollArea and inside of it I want to place a couple off QTextEdit widgets and I only want use scrolling inside QScrollArea. Problem is that no matter how I try to resize the QTextEdit it seems it has a maximum height and cuts of text, even if I set the size manually and QTextEdit::size returns the correct value.

I did the same thing with QLabel and it works fine, but in this case I need some methods that are only provided in QTextEdit.

I found this post:
Resizing QT's QTextEdit to Match Text Height: maximumViewportSize()

And the answer given was the following:

I have solved this issue. There were 2 things that I had to do to get
  it to work:

Walk up the widget hierarchy and make sure all the size policies made
  sense to ensure that if any child widget wanted to be big/small, then
  the parent widget would want to be the same thing.

This is the main
  source of the fix. It turns out that since the QTextEdit is inside a
  QFrame that is the main widget in a QScrollArea, the QScrollArea has a
  constraint that it will not resize the internal widget unless the
  "widgetResizable" property is true. The documentation for that is
  here: http://doc.qt.io/qt-4.8/qscrollarea.html#widgetResizable-prop.
  The documentation was not clear to me until I played around with this
  setting and got it to work. From the docs, it seems that this property
  only deals with times where the main scroll area wants to resize a
  widget (i.e. from parent to child). It actually means that if the main
  widget in the scroll area wants to ever resize (i.e. child to parent),
  then this setting has to be set to true. So, the moral of the story is
  that the QTextEdit code was correct in overriding sizeHint, but the
  QScrollArea was ignoring the value returned from the main frame's
  sizeHint.

The problem is that I have no idea how to access the QTextEdit's QScrollArea to enable widgetResizable. Can anyone explain how I can achieve this or suggest a different way of resizing QTextEdit to perfectly fit it's content?

答:
Without a concrete example it's difficult to judge, but... it sounds as if you simply want a QTextEdit whose sizeHint depends on the current document size.

```<code>class text_edit: public QTextEdit {
  using super = QTextEdit;
public:
  explicit text_edit (QWidget *parent = nullptr)
    : super(parent)
    {
      setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
      setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
      setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    }
  virtual QSize sizeHint () const override
    {
      QSize s(document()->size().toSize());

      /*
       * Make sure width and height have `usable' values.
       */
      s.rwidth() = std::max(100, s.width());
      s.rheight() = std::max(100, s.height());
      return(s);
    }
protected:
  virtual void resizeEvent (QResizeEvent *event) override
    {

      /*
       * If the widget has been resized then the size hint will
       * also have changed.  Call updateGeometry to make sure
       * any layouts are notified of the change.
       */
      updateGeometry();
      super::resizeEvent(event);
    }
};
</code>```

Then use as...

```<code>QScrollArea sa;
sa.setWidgetResizable(true);
text_edit te;
te.setPlainText(...);
sa.setWidget(&te);
sa.show();
</code>```

It appears to work as expected in the few tests I've done.

Body:
Without a concrete example it's difficult to judge, but... it sounds as if you simply want a QTextEdit whose sizeHint depends on the current document size.

class text_edit: public QTextEdit {
  using super = QTextEdit;
public:
  explicit text_edit (QWidget *parent = nullptr)
    : super(parent)
    {
      setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
      setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
      setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    }
  virtual QSize sizeHint () const override
    {
      QSize s(document()->size().toSize());

      /*
       * Make sure width and height have `usable' values.
       */
      s.rwidth() = std::max(100, s.width());
      s.rheight() = std::max(100, s.height());
      return(s);
    }
protected:
  virtual void resizeEvent (QResizeEvent *event) override
    {

      /*
       * If the widget has been resized then the size hint will
       * also have changed.  Call updateGeometry to make sure
       * any layouts are notified of the change.
       */
      updateGeometry();
      super::resizeEvent(event);
    }
};

Then use as...

QScrollArea sa;
sa.setWidgetResizable(true);
text_edit te;
te.setPlainText(...);
sa.setWidget(&te);
sa.show();

It appears to work as expected in the few tests I've done.