问:
How does the Spark Driver's memory affect Executor resources?
I am learning how to configure Spark applications by reading High Performance Spark, and one sentence mentioned is confusing me:

In my experience, a good heuristic for setting the Spark driver memory is
  simply the lowest possible value that does not lead to memory errors in
  the driver, i.e., which gives the maximum possible resources to the executors.

My understanding is that the driver exists in its own node, and executors exist independently on worker nodes. I would have assumed I can max out driver memory without having to worry about it affecting executors.

Is what the book suggesting true? If it is, why/how does the driver memory affect executor resources?

答:
How does the Spark Driver's memory affect Executor resources?

It does not (except one edge case where the driver and executors all run on the same machine).

My understanding is that the driver exists in its own node, and executors exist independently on worker nodes. I would have assumed I can max out driver memory without having to worry about it affecting executors.

That's my understanding too and can't seem to find a deployment scenario which would prove us wrong.

I think the paragraph without the "i.e." part would make more sense.

In my experience, a good heuristic for setting the Spark driver memory is simply the lowest possible value that does not lead to memory errors in the driver.

That's perfectly OK.


来源: stackexchange

问:
AWS DynamoDB data with and/or without types?
I'm using the aws-sdk for NodeJS to interact with a DynamoDB table. This is my first look at DynamoDB.

When using a call like getItem() or updateItem(), the data structure includes types, like this:

```<code>{
  'a': { S: 'My string' }
}
</code>```

Is there any way to pass and receive these objects without the types..? So...

```<code>{
  'a': 'My string'
}
</code>```

Or, any helper functions already written that will convert objects to and from this format..?

```<code>const data = dbToObj({ 'a': { S: 'My string' } })
// { 'a': 'My string' }
</code>```

So I could convert to it when populating call params, and convert from it when receiving data.

Trying to avoid accessing my data like:

```<code>const myData = data.Item.something.M.somethinElse.M.qwerty.S
</code>```

I know I could write something myself, just wondering if anyone knows of functions/options already available that would do this. I couldn't find any.

答:
Dynogels offer a cleaner way to handle without worrying about types.
We use it in production and works without any issues.

https://github.com/clarkie/dynogels

```<code>Account.create({email: 'foo@example.com', name: 'Foo Bar', age: 21}, function (err, acc) {
  console.log('created account in DynamoDB', acc.get('email'));
});
</code>```

It is a ODM (Object Data Mapper) for dynamodb.

Hope it helps.

ContentLicense: CC BY-SA 3.0

Body:
I'm using the aws-sdk for NodeJS to interact with a DynamoDB table. This is my first look at DynamoDB.

When using a call like getItem() or updateItem(), the data structure includes types, like this:

{
  'a': { S: 'My string' }
}

Is there any way to pass and receive these objects without the types..? So...

{
  'a': 'My string'
}

Or, any helper functions already written that will convert objects to and from this format..?

const data = dbToObj({ 'a': { S: 'My string' } })
// { 'a': 'My string' }

So I could convert to it when populating call params, and convert from it when receiving data.

Trying to avoid accessing my data like:

const myData = data.Item.something.M.somethinElse.M.qwerty.S

I know I could write something myself, just wondering if anyone knows of functions/options already available that would do this. I couldn't find any.

Body:
Dynogels offer a cleaner way to handle without worrying about types.
We use it in production and works without any issues.

https://github.com/clarkie/dynogels

Account.create({email: 'foo@example.com', name: 'Foo Bar', age: 21}, function (err, acc) {
  console.log('created account in DynamoDB', acc.get('email'));
});

It is a ODM (Object Data Mapper) for dynamodb.

Hope it helps.


问:
Buttom alignment of pictures in af row with different hight
I'm using the part with the 8 images of this template as a photogallery.

Template

But if my images has different heights, they will be aligned so the top of the images will fit each other. I need them to be aligned, so the bottom of the images are aligned.

How can I do that?

答:
My link is showing both code and the templates I use:-) The problem with this code is the vertical align of the 8 images, if they have different hights. They will be aligned so the top of the images fit each other. What I need, is that the 8 images is buttom aligned to each other.</p>

```<code><!DOCTYPE html>
<html>
<title>W3.CSS Template</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Karma">
<style>
body,h1,h2,h3,h4,h5,h6 {font-family: "Karma", sans-serif}
.w3-bar-block .w3-bar-item {padding:20px}
</style>
<body>

<!-- Sidebar (hidden by default) -->
<nav class="w3-sidebar w3-bar-block w3-card w3-top w3-xlarge w3-animate-    left" style="display:none;z-index:2;width:40%;min-width:300px" id="mySidebar">
<a href="javascript:void(0)" onclick="w3_close()"
  class="w3-bar-item w3-button">Close Menu</a>
  <a href="#food" onclick="w3_close()" class="w3-bar-item w3-button">Food</a>
  <a href="#about" onclick="w3_close()" class="w3-bar-item w3-button">About</a>
  </nav>

<!-- Top menu -->
<div class="w3-top">
<div class="w3-white w3-xlarge" style="max-width:1200px;margin:auto">
<div class="w3-button w3-padding-16 w3-left" onclick="w3_open()">☰</div>
<div class="w3-right w3-padding-16">Mail</div>
<div class="w3-center w3-padding-16">My Food</div>
</div>
</div>

<!-- !PAGE CONTENT! -->
<div class="w3-main w3-content w3-padding" style="max-width:1200px;margin-top:100px">

 <!-- First Photo Grid-->
 <div class="w3-row-padding w3-padding-16 w3-center" id="food">
 <div class="w3-quarter">
 <img src="/w3images/sandwich.jpg" alt="Sandwich" style="width:100%">
 <h3>The Perfect Sandwich, A Real NYC Classic</h3>
 <p>Just some random text, lorem ipsum text praesent tincidunt ipsum lipsum.</p>
 </div>
 <div class="w3-quarter">
 <img src="/w3images/steak.jpg" alt="Steak" style="width:100%">
 <h3>Let Me Tell You About This Steak</h3>
 <p>Once again, some random text to lorem lorem lorem lorem ipsum text praesent tincidunt ipsum lipsum.</p>
 </div>
 <div class="w3-quarter">
 <img src="/w3images/cherries.jpg" alt="Cherries" style="width:100%">
 <h3>Cherries, interrupted</h3>
 <p>Lorem ipsum text praesent tincidunt ipsum lipsum.</p>
 <p>What else?</p>
 </div>
 <div class="w3-quarter">
 <img src="/w3images/wine.jpg" alt="Pasta and Wine" style="width:100%">
 <h3>Once Again, Robust Wine and Vegetable Pasta</h3>
 <p>Lorem ipsum text praesent tincidunt ipsum lipsum.</p>
 </div>
 </div>

 <!-- Second Photo Grid-->
 <div class="w3-row-padding w3-padding-16 w3-center">
 <div class="w3-quarter">
 <img src="/w3images/popsicle.jpg" alt="Popsicle" style="width:100%">
 <h3>All I Need Is a Popsicle</h3>
 <p>Lorem ipsum text praesent tincidunt ipsum lipsum.</p>
 </div>
 <div class="w3-quarter">
 <img src="/w3images/salmon.jpg" alt="Salmon" style="width:100%">
 <h3>Salmon For Your Skin</h3>
 <p>Once again, some random text to lorem lorem lorem lorem ipsum text praesent tincidunt ipsum lipsum.</p>
 </div>
 <div class="w3-quarter">
 <img src="/w3images/sandwich.jpg" alt="Sandwich" style="width:100%">
 <h3>The Perfect Sandwich, A Real Classic</h3>
 <p>Just some random text, lorem ipsum text praesent tincidunt ipsum lipsum.</p>
 </div>
 <div class="w3-quarter">
 <img src="/w3images/croissant.jpg" alt="Croissant" style="width:100%">
 <h3>Le French</h3>
 <p>Lorem lorem lorem lorem ipsum text praesent tincidunt ipsum lipsum.</p>
 </div>
 </div>

 <!-- Pagination -->
 <div class="w3-center w3-padding-32">
 <div class="w3-bar">
 <a href="#" class="w3-bar-item w3-button w3-hover-black">«</a>
 <a href="#" class="w3-bar-item w3-black w3-button">1</a>
 <a href="#" class="w3-bar-item w3-button w3-hover-black">2</a>
 <a href="#" class="w3-bar-item w3-button w3-hover-black">3</a>
 <a href="#" class="w3-bar-item w3-button w3-hover-black">4</a>
 <a href="#" class="w3-bar-item w3-button w3-hover-black">»</a>
 </div>
 </div>

  <hr id="about">

  <!-- About Section -->
  <div class="w3-container w3-padding-32 w3-center">
  <h3>About Me, The Food Man</h3><br>
  <img src="/w3images/chef.jpg" alt="Me" class="w3-image" style="display:block;margin:auto" width="800" height="533">
  <div class="w3-padding-32">
  <h4><b>I am Who I Am!</b></h4>
  <h6><i>With Passion For Real, Good Food</i></h6>
  <p>Just me, myself and I, exploring the universe of unknownment. I have a heart f love and an interest of lorem ipsum and mauris neque quam blog. I want to share my world with you. Praesent tincidunt sed tellus ut rutrum. Sed vitae justo condimentum, porta lectus vitae, ultricies congue gravida diam non fringilla. Praesent tincidunt sed tellus ut rutrum. Sed vitae justo condimentum, porta lectus vitae, ultricies congue gravida diam non fringilla.</p>
   </div>
   </div>
   <hr>

  <!-- Footer -->
  <footer class="w3-row-padding w3-padding-32">
  <div class="w3-third">
  <h3>FOOTER</h3>
  <p>Praesent tincidunt sed tellus ut rutrum. Sed vitae justo condimentum, porta lectus vitae, ultricies congue gravida diam non fringilla.</p>
  <p>Powered by <a href="https://www.w3schools.com/w3css/default.asp" target="_blank">w3.css</a></p>
  </div>

  <div class="w3-third">
  <h3>BLOG POSTS</h3>
  <ul class="w3-ul w3-hoverable">
  <li class="w3-padding-16">
  <img src="/w3images/workshop.jpg" class="w3-left w3-margin-right" style="width:50px">
  <span class="w3-large">Lorem</span><br>
  <span>Sed mattis nunc</span>
  </li>
  <li class="w3-padding-16">
  <img src="/w3images/gondol.jpg" class="w3-left w3-margin-right" style="width:50px">
  <span class="w3-large">Ipsum</span><br>
  <span>Praes tinci sed</span>
  </li>
  </ul>
  </div>

  <div class="w3-third w3-serif">

  </footer>

<!-- End page content -->
</div>

<script>
// Script to open and close sidebar
function w3_open() {
     document.getElementById("mySidebar").style.display = "block";
}

function w3_close() {
     document.getElementById("mySidebar").style.display = "none";
}
</script>

 </body>
 </html>enter code here
</code>```

Body:
My link is showing both code and the templates I use:-) The problem with this code is the vertical align of the 8 images, if they have different hights. They will be aligned so the top of the images fit each other. What I need, is that the 8 images is buttom aligned to each other.

<!DOCTYPE html>
<html>
<title>W3.CSS Template</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Karma">
<style>
body,h1,h2,h3,h4,h5,h6 {font-family: "Karma", sans-serif}
.w3-bar-block .w3-bar-item {padding:20px}
</style>
<body>

<!-- Sidebar (hidden by default) -->
<nav class="w3-sidebar w3-bar-block w3-card w3-top w3-xlarge w3-animate-    left" style="display:none;z-index:2;width:40%;min-width:300px" id="mySidebar">
<a href="javascript:void(0)" onclick="w3_close()"
  class="w3-bar-item w3-button">Close Menu</a>
  <a href="#food" onclick="w3_close()" class="w3-bar-item w3-button">Food</a>
  <a href="#about" onclick="w3_close()" class="w3-bar-item w3-button">About</a>
  </nav>

<!-- Top menu -->
<div class="w3-top">
<div class="w3-white w3-xlarge" style="max-width:1200px;margin:auto">
<div class="w3-button w3-padding-16 w3-left" onclick="w3_open()">☰</div>
<div class="w3-right w3-padding-16">Mail</div>
<div class="w3-center w3-padding-16">My Food</div>
</div>
</div>

<!-- !PAGE CONTENT! -->
<div class="w3-main w3-content w3-padding" style="max-width:1200px;margin-top:100px">

 <!-- First Photo Grid-->
 <div class="w3-row-padding w3-padding-16 w3-center" id="food">
 <div class="w3-quarter">
 <img src="/w3images/sandwich.jpg" alt="Sandwich" style="width:100%">
 <h3>The Perfect Sandwich, A Real NYC Classic</h3>
 <p>Just some random text, lorem ipsum text praesent tincidunt ipsum lipsum.</p>
 </div>
 <div class="w3-quarter">
 <img src="/w3images/steak.jpg" alt="Steak" style="width:100%">
 <h3>Let Me Tell You About This Steak</h3>
 <p>Once again, some random text to lorem lorem lorem lorem ipsum text praesent tincidunt ipsum lipsum.</p>
 </div>
 <div class="w3-quarter">
 <img src="/w3images/cherries.jpg" alt="Cherries" style="width:100%">
 <h3>Cherries, interrupted</h3>
 <p>Lorem ipsum text praesent tincidunt ipsum lipsum.</p>
 <p>What else?</p>
 </div>
 <div class="w3-quarter">
 <img src="/w3images/wine.jpg" alt="Pasta and Wine" style="width:100%">
 <h3>Once Again, Robust Wine and Vegetable Pasta</h3>
 <p>Lorem ipsum text praesent tincidunt ipsum lipsum.</p>
 </div>
 </div>

 <!-- Second Photo Grid-->
 <div class="w3-row-padding w3-padding-16 w3-center">
 <div class="w3-quarter">
 <img src="/w3images/popsicle.jpg" alt="Popsicle" style="width:100%">
 <h3>All I Need Is a Popsicle</h3>
 <p>Lorem ipsum text praesent tincidunt ipsum lipsum.</p>
 </div>
 <div class="w3-quarter">
 <img src="/w3images/salmon.jpg" alt="Salmon" style="width:100%">
 <h3>Salmon For Your Skin</h3>
 <p>Once again, some random text to lorem lorem lorem lorem ipsum text praesent tincidunt ipsum lipsum.</p>
 </div>
 <div class="w3-quarter">
 <img src="/w3images/sandwich.jpg" alt="Sandwich" style="width:100%">
 <h3>The Perfect Sandwich, A Real Classic</h3>
 <p>Just some random text, lorem ipsum text praesent tincidunt ipsum lipsum.</p>
 </div>
 <div class="w3-quarter">
 <img src="/w3images/croissant.jpg" alt="Croissant" style="width:100%">
 <h3>Le French</h3>
 <p>Lorem lorem lorem lorem ipsum text praesent tincidunt ipsum lipsum.</p>
 </div>
 </div>

 <!-- Pagination -->
 <div class="w3-center w3-padding-32">
 <div class="w3-bar">
 <a href="#" class="w3-bar-item w3-button w3-hover-black">«</a>
 <a href="#" class="w3-bar-item w3-black w3-button">1</a>
 <a href="#" class="w3-bar-item w3-button w3-hover-black">2</a>
 <a href="#" class="w3-bar-item w3-button w3-hover-black">3</a>
 <a href="#" class="w3-bar-item w3-button w3-hover-black">4</a>
 <a href="#" class="w3-bar-item w3-button w3-hover-black">»</a>
 </div>
 </div>

  <hr id="about">

  <!-- About Section -->
  <div class="w3-container w3-padding-32 w3-center">
  <h3>About Me, The Food Man</h3><br>
  <img src="/w3images/chef.jpg" alt="Me" class="w3-image" style="display:block;margin:auto" width="800" height="533">
  <div class="w3-padding-32">
  <h4><b>I am Who I Am!</b></h4>
  <h6><i>With Passion For Real, Good Food</i></h6>
  <p>Just me, myself and I, exploring the universe of unknownment. I have a heart f love and an interest of lorem ipsum and mauris neque quam blog. I want to share my world with you. Praesent tincidunt sed tellus ut rutrum. Sed vitae justo condimentum, porta lectus vitae, ultricies congue gravida diam non fringilla. Praesent tincidunt sed tellus ut rutrum. Sed vitae justo condimentum, porta lectus vitae, ultricies congue gravida diam non fringilla.</p>
   </div>
   </div>
   <hr>

  <!-- Footer -->
  <footer class="w3-row-padding w3-padding-32">
  <div class="w3-third">
  <h3>FOOTER</h3>
  <p>Praesent tincidunt sed tellus ut rutrum. Sed vitae justo condimentum, porta lectus vitae, ultricies congue gravida diam non fringilla.</p>
  <p>Powered by <a href="https://www.w3schools.com/w3css/default.asp" target="_blank">w3.css</a></p>
  </div>

  <div class="w3-third">
  <h3>BLOG POSTS</h3>
  <ul class="w3-ul w3-hoverable">
  <li class="w3-padding-16">
  <img src="/w3images/workshop.jpg" class="w3-left w3-margin-right" style="width:50px">
  <span class="w3-large">Lorem</span><br>
  <span>Sed mattis nunc</span>
  </li>
  <li class="w3-padding-16">
  <img src="/w3images/gondol.jpg" class="w3-left w3-margin-right" style="width:50px">
  <span class="w3-large">Ipsum</span><br>
  <span>Praes tinci sed</span>
  </li>
  </ul>
  </div>

  <div class="w3-third w3-serif">

  </footer>

<!-- End page content -->
</div>

<script>
// Script to open and close sidebar
function w3_open() {
     document.getElementById("mySidebar").style.display = "block";
}

function w3_close() {
     document.getElementById("mySidebar").style.display = "none";
}
</script>

 </body>
 </html>enter code here


问:
Efficient/optimized way of evaluating several conditions?
Which of the following code snippets (A and B below) is more efficient (performance wise) and why?

Furthermore:

Does the compiler rearrange the validate() method calls so that they are called "when needed" i.e. when evaluating each separate condition in the if-statement? In other words, will the compiler treat snippet A just as if it were written as snippet B?

Are there other alternatives (besides snippet A and B) for better perfomance?

(validate(...) checks if a field (provided as an argument)  contains valid values)

Snippet A:

```<code>    boolean okTitle = validate(inputTitle);
    boolean okLocation = validate(inputLocation);
    boolean okDuration = validate(inputDuration);
    boolean okReminder = validate(inputReminder);
    boolean okRepetition = validate(inputRepetition);
    boolean okDate = validate(inputDate);

    if(okTitle && (okLocation || okDuration || okReminder || okRepetition || okDate)){
        setEnabled(true);
    }else{
        setEnabled(false);
    }
</code>```

Snippet B:

```<code>setEnabled(
     validate(inputTitle) &&
     ( validate(inputLocation) || validate(inputDuration) || validate(inputReminder) || validate(inputRepetition) || validate(inputDate) )
   );
</code>```

答:
will the compiler treat snippet A just as if it were written as snippet B

No, they are not equivalent code due to short circuiting.  If you had the validate(...) calls inside the if statement, then it would be equivalent and could compare the bytecode to see if the compiler thinks the same way

Testing both cases using javac Test.java && java Test && javap -c Test

Without if

```<code>public class Test
{
    public static void main(String[] args)
    {
        int a = 5;
        System.out.println(a > 2 && (a < 6 || a < 4));
    }
}
</code>```

Prints true, and bytecode:

```<code>Compiled from "Test.java"
public class Test {
  public Test();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: iconst_5
       1: istore_1
       2: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       5: iload_1
       6: iconst_2
       7: if_icmple     25
      10: iload_1
      11: bipush        6
      13: if_icmplt     21
      16: iload_1
      17: iconst_4
      18: if_icmpge     25
      21: iconst_1
      22: goto          26
      25: iconst_0
      26: invokevirtual #3                  // Method java/io/PrintStream.println:(Z)V
      29: return
}
</code>```

With if

```<code>public class Test
{
    public static void main(String[] args)
    {
        int a = 5;
        if(a > 2 && (a < 6 || a < 4))
            System.out.println(true);
        else
            System.out.println(false);
    }
}
</code>```

Prints true as expected, and the bytecode:

```<code>Compiled from "Test.java"
public class Test {
  public Test();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: iconst_5
       1: istore_1
       2: iload_1
       3: iconst_2
       4: if_icmple     28
       7: iload_1
       8: bipush        6
      10: if_icmplt     18
      13: iload_1
      14: iconst_4
      15: if_icmpge     28
      18: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
      21: iconst_1
      22: invokevirtual #3                  // Method java/io/PrintStream.println:(Z)V
      25: goto          35
      28: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
      31: iconst_0
      32: invokevirtual #3                  // Method java/io/PrintStream.println:(Z)V
      35: return
}
</code>```

So even with identical logic flow, it generates less bytecode to avoid the if statement

(for this compiler at any rate, using Oracle's jdk8)

Body:
Which of the following code snippets (A and B below) is more efficient (performance wise) and why?

Furthermore:

Does the compiler rearrange the validate() method calls so that they are called "when needed" i.e. when evaluating each separate condition in the if-statement? In other words, will the compiler treat snippet A just as if it were written as snippet B?

Are there other alternatives (besides snippet A and B) for better perfomance?

(validate(...) checks if a field (provided as an argument)  contains valid values)

Snippet A:

    boolean okTitle = validate(inputTitle);
    boolean okLocation = validate(inputLocation);
    boolean okDuration = validate(inputDuration);
    boolean okReminder = validate(inputReminder);
    boolean okRepetition = validate(inputRepetition);
    boolean okDate = validate(inputDate);

    if(okTitle && (okLocation || okDuration || okReminder || okRepetition || okDate)){
        setEnabled(true);
    }else{
        setEnabled(false);
    }

Snippet B:

setEnabled(
     validate(inputTitle) &&
     ( validate(inputLocation) || validate(inputDuration) || validate(inputReminder) || validate(inputRepetition) || validate(inputDate) )
   );

Body:
will the compiler treat snippet A just as if it were written as snippet B

No, they are not equivalent code due to short circuiting.  If you had the validate(...) calls inside the if statement, then it would be equivalent and could compare the bytecode to see if the compiler thinks the same way

Testing both cases using javac Test.java && java Test && javap -c Test

Without if

public class Test
{
    public static void main(String[] args)
    {
        int a = 5;
        System.out.println(a > 2 && (a < 6 || a < 4));
    }
}

Prints true, and bytecode:

Compiled from "Test.java"
public class Test {
  public Test();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: iconst_5
       1: istore_1
       2: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       5: iload_1
       6: iconst_2
       7: if_icmple     25
      10: iload_1
      11: bipush        6
      13: if_icmplt     21
      16: iload_1
      17: iconst_4
      18: if_icmpge     25
      21: iconst_1
      22: goto          26
      25: iconst_0
      26: invokevirtual #3                  // Method java/io/PrintStream.println:(Z)V
      29: return
}

With if

public class Test
{
    public static void main(String[] args)
    {
        int a = 5;
        if(a > 2 && (a < 6 || a < 4))
            System.out.println(true);
        else
            System.out.println(false);
    }
}

Prints true as expected, and the bytecode:

Compiled from "Test.java"
public class Test {
  public Test();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: iconst_5
       1: istore_1
       2: iload_1
       3: iconst_2
       4: if_icmple     28
       7: iload_1
       8: bipush        6
      10: if_icmplt     18
      13: iload_1
      14: iconst_4
      15: if_icmpge     28
      18: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
      21: iconst_1
      22: invokevirtual #3                  // Method java/io/PrintStream.println:(Z)V
      25: goto          35
      28: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
      31: iconst_0
      32: invokevirtual #3                  // Method java/io/PrintStream.println:(Z)V
      35: return
}

So even with identical logic flow, it generates less bytecode to avoid the if statement

(for this compiler at any rate, using Oracle's jdk8)


问:
How do I fade a container to the left when I click on an item in the container
I try to make an effect that is executed by clicking on an item within the container, container slide to right/left and fade, after the content is faded it needs do be display hide to make place for new content.

The future header will be placed outside the container thus the header do not fade. Nothing i tried seem to work. Anybody any suggestions for this problem?

I will be implementing the effect on my website www.bartmulder.nl/beta1.0 so when you click on a photo the container whit all the photos will fade and slide before display none

// change .box1 to .container and you will see the effect that i am looking for
$('.box1').on('click', function() {
  $(this).toggleClass('clicked');

  setTimeout(function() {
    document.getElementById("containerWerk").style.display = "none";
  }, 500);
});```
<pre class="snippet-code-css lang-css prettyprint-override"><code>.container {
  -webkit-transition: 0.5s;
  -webkit-transition-timing-function: ease;
  transition-timing-function: ease;
}

.box img {
  margin: 0px auto;
  cursor: pointer;
}

.container.clicked {
  margin-left: -100px;
  opacity: 0;
}</code>```

```

答:
Here's the jQuery / JavaScript way of accomplishing this too...
Simply just call animate on the element and specify what kind of animation, speed, and completion function (optional).

For the example below I am passing a JSON object to specify what actions to accomplish with the animation (which I removed from CSS), then after the animation it will trigger the element to not be displayed any longer.

// change .box1 to .container and you will see the effect that i am looking for
$('.box').on('click', function() {
  $(this).animate({
     opacity: 0,
     marginLeft: -100
  }, 1000, function() {
    $(this).css("display", "none");
  });
});```
<pre class="snippet-code-css lang-css prettyprint-override"><code>.box img {
  margin: 0px auto;
  cursor: pointer;
}</code>```

```

Body:
I try to make an effect that is executed by clicking on an item within the container, container slide to right/left and fade, after the content is faded it needs do be display hide to make place for new content.

The future header will be placed outside the container thus the header do not fade. Nothing i tried seem to work. Anybody any suggestions for this problem?

I will be implementing the effect on my website www.bartmulder.nl/beta1.0 so when you click on a photo the container whit all the photos will fade and slide before display none

// change .box1 to .container and you will see the effect that i am looking for
$('.box1').on('click', function() {
  $(this).toggleClass('clicked');

  setTimeout(function() {
    document.getElementById("containerWerk").style.display = "none";
  }, 500);
});

.container {
  -webkit-transition: 0.5s;
  -webkit-transition-timing-function: ease;
  transition-timing-function: ease;
}

.box img {
  margin: 0px auto;
  cursor: pointer;
}

.container.clicked {
  margin-left: -100px;
  opacity: 0;
}

Body:
Here's the jQuery / JavaScript way of accomplishing this too...
Simply just call animate on the element and specify what kind of animation, speed, and completion function (optional).

For the example below I am passing a JSON object to specify what actions to accomplish with the animation (which I removed from CSS), then after the animation it will trigger the element to not be displayed any longer.

// change .box1 to .container and you will see the effect that i am looking for
$('.box').on('click', function() {
  $(this).animate({
     opacity: 0,
     marginLeft: -100
  }, 1000, function() {
    $(this).css("display", "none");
  });
});

.box img {
  margin: 0px auto;
  cursor: pointer;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="containerWerk" class="container">
  <div class="box box1">
    <img src="https://www.google.com/images/srpr/logo11w.png" width="100" />
  </div>
  <div class="box box2">
    <img src="https://www.google.com/images/srpr/logo11w.png" width="100" />
  </div>


问:
Traversing an object in javascript and convert it in specific format
I have the following object structure:

```<code>{
    a: {
        b: "abc",
        c: [
            {
                d: "pqr",
                e: true
            },
            {
                f: 1,
                e: ["xyz", 1]
            }
        ]
    }
}
</code>```

Now I want to convert these structure in specific format that are shown in expected output.

I'm trying to traverse above structure but the constructed path not correct
Here is what I'm doing

```<code>const getFinalNode = function(data, path){
  if (typeof data === 'object'){
    for (let key in data) {
      // console.log(key);
      if (typeof data[key] === 'object'){
        path += key+'/'
        getFinalNode(data[key], path)
        // console.log(true);
      }else{
        console.log({
          value: data[key],
          key: key,
          path:path
        })
      }
    }
  }
}
getFinalNode(data, "");
</code>```

My expected output should be

```<code>[
    {
        name: "b",
        value: "abc",
        path: "a/b"
    },
    {
        name: "d",
        value: "pqr",
        path: "a/c/0/d"
    },
    {
        name: "e",
        value: true,
        path: "a/c/0/e"
    },
    {
        name: "f",
        value: 1,
        path: "a/c/1/f"
    },
    {
        name: "e",
        value: 'xyz',
        path: "a/c/1/e"
    },
    {
        name: "e",
        value: 1,
        path: "a/c/1/e"
    }
]
</code>```

So how can I traverse above structure and convert it in expected output or is there any solution that can I use in my existing code.

Now I getting following output

答:
You could take a function for collecting the items and a function for a recursive call for iterating the object.

function traverse(object) {

    function iter(o, p) {
        Object.keys(o).forEach(function (k) {
            if (o[k] && typeof o[k] === 'object') {
                return iter(o[k], p.concat(k));
            }
            result.push({ name: k, value: o[k], path: p.join('/') });
        });
    }

    var result = [];
    iter(object, []);
    return result;
}

var object = { a: { b: "abc", c: [{ d: "pqr", e: true }, { f: 1, e: ["xyz", 1] }] } };

console.log(traverse(object));```
<pre class="snippet-code-css lang-css prettyprint-override"><code>.as-console-wrapper { max-height: 100% !important; top: 0; }</code>```

Body:
I have the following object structure:

{
    a: {
        b: "abc",
        c: [
            {
                d: "pqr",
                e: true
            },
            {
                f: 1,
                e: ["xyz", 1]
            }
        ]
    }
}

Now I want to convert these structure in specific format that are shown in expected output.

I'm trying to traverse above structure but the constructed path not correct
Here is what I'm doing

const getFinalNode = function(data, path){
  if (typeof data === 'object'){
    for (let key in data) {
      // console.log(key);
      if (typeof data[key] === 'object'){
        path += key+'/'
        getFinalNode(data[key], path)
        // console.log(true);
      }else{
        console.log({
          value: data[key],
          key: key,
          path:path
        })
      }
    }
  }
}
getFinalNode(data, "");

My expected output should be

[
    {
        name: "b",
        value: "abc",
        path: "a/b"
    },
    {
        name: "d",
        value: "pqr",
        path: "a/c/0/d"
    },
    {
        name: "e",
        value: true,
        path: "a/c/0/e"
    },
    {
        name: "f",
        value: 1,
        path: "a/c/1/f"
    },
    {
        name: "e",
        value: 'xyz',
        path: "a/c/1/e"
    },
    {
        name: "e",
        value: 1,
        path: "a/c/1/e"
    }
]

So how can I traverse above structure and convert it in expected output or is there any solution that can I use in my existing code.

Now I getting following output

Body:
You could take a function for collecting the items and a function for a recursive call for iterating the object.

function traverse(object) {

    function iter(o, p) {
        Object.keys(o).forEach(function (k) {
            if (o[k] && typeof o[k] === 'object') {
                return iter(o[k], p.concat(k));
            }
            result.push({ name: k, value: o[k], path: p.join('/') });
        });
    }

    var result = [];
    iter(object, []);
    return result;
}

var object = { a: { b: "abc", c: [{ d: "pqr", e: true }, { f: 1, e: ["xyz", 1] }] } };

console.log(traverse(object));

.as-console-wrapper { max-height: 100% !important; top: 0; }


问:
How to reload a web page on CodePen full page view by clicking a button
```<code>$("#quoteButton").on("click", function() {
    location.reload();
});
</code>```

I worked on a 'Random Quote Machine' from Free Code Camp web site via NetBeans IDE 8.2 and this code works well, but when I paste it on CodePen for some reason it doesn't work on CodePen full page view. Is there a way that I can make a page reload when I click on a button on CodePen full page view anyway?

In Debug mode view on CodePen it's working. Here's a link to my CodePen: https://codepen.io/NemStep/pen/pdmowz

答:
This is an alternative to location.reload.

The parameter of go is the page index, in this case, 0 is the current page.

For instance, go(-2) would go 2 pages back in the history.

$("#quoteButton").on("click", function() {
    history.go(0);
});
```

Body:
$("#quoteButton").on("click", function() {
    location.reload();
});

I worked on a 'Random Quote Machine' from Free Code Camp web site via NetBeans IDE 8.2 and this code works well, but when I paste it on CodePen for some reason it doesn't work on CodePen full page view. Is there a way that I can make a page reload when I click on a button on CodePen full page view anyway?

In Debug mode view on CodePen it's working. Here's a link to my CodePen: https://codepen.io/NemStep/pen/pdmowz


问:
I want to change the time automatically when i select the drop down option

$(function() {
  var select = $('select').val;
  if (select = 1) {
    $("#selectedTime").val("11:00 AM");
  }
});
else {
  $("#selectedTime").val("8:00 PM");
}
});```
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="" id="" class="select">
  <option value="">Select</option>
  <option value="1">lunch</option>
  <option value="2">Dinner</option>
</select>
<input type="time" id="selectedTime"></code>```

答:
You have quite a few problems here:

There's a random }); in the middle of your code causing a syntax error

val isn't a property, it's a function, so should be .val()

val() also returns a string, so you should compare to '1', not 1

= is used for setting values. == or === is used for comparison

The time values you set should not have AM or PM, and should be in the format 00:00, not 0:00.

Finally, your code only runs once when the page loads. You instead need to hook to the change event of the select. You can then execute your code and update the val() of the time input, something like this:

$(function() {
  $('.select').change(function() {
    var select = $(this).val();
    if (select === '1') {
      $("#selectedTime").val("11:00");
    } else {
      $("#selectedTime").val("08:00");
    }
  });
});```
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="" id="" class="select">
  <option value="">Select</option>
  <option value="1">lunch</option>
  <option value="2">Dinner</option>
</select>
<input type="time" id="selectedTime"></code>```

Body:
$(function() {
  var select = $('select').val;
  if (select = 1) {
    $("#selectedTime").val("11:00 AM");
  }
});
else {
  $("#selectedTime").val("8:00 PM");
}
});

<select name="" id="" class="select">
  <option value="">Select</option>
  <option value="1">lunch</option>
  <option value="2">Dinner</option>
</select>
<input type="time" id="selectedTime">

Body:
You have quite a few problems here:

There's a random }); in the middle of your code causing a syntax error

val isn't a property, it's a function, so should be .val()

val() also returns a string, so you should compare to '1', not 1

= is used for setting values. == or === is used for comparison

The time values you set should not have AM or PM, and should be in the format 00:00, not 0:00.

Finally, your code only runs once when the page loads. You instead need to hook to the change event of the select. You can then execute your code and update the val() of the time input, something like this:

$(function() {
  $('.select').change(function() {
    var select = $(this).val();
    if (select === '1') {
      $("#selectedTime").val("11:00");
    } else {
      $("#selectedTime").val("08:00");
    }
  });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="" id="" class="select">
  <option value="">Select</option>
  <option value="1">lunch</option>
  <option value="2">Dinner</option>
</select>
<input type="time" id="selectedTime">


问:
How to update header parameters from filter with Spring security?
I am developing a filter with Spring security which extends of
OncePerRequestFilter class and It has to update parameters in the REST service .Parameters are entered by the header with the annotation @RequestHeader.

I have tried to update parameters from the filter with the follows class:

```<code>public class HeaderMapRequestWrapper extends HttpServletRequestWrapper {
    /**
     * construct a wrapper for this request
     *
     * @param request
     */
    public HeaderMapRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    private Map<String, String> headerMap = new HashMap<String, String>();

    /**
     * add a header with given name and value
     *
     * @param name
     * @param value
     */
    public void addHeader(String name, String value) {
        headerMap.put(name, value);
    }

    public void removeteHeader(String name){
        headerMap.remove(name);
    }
    @Override
    public String getHeader(String name) {
        String headerValue = super.getHeader(name);
        if (headerMap.containsKey(name)) {
            headerValue = headerMap.get(name);
        }
        return headerValue;
    }

    /**
     * get the Header names
     */
    @Override
    public Enumeration<String> getHeaderNames() {
        List<String> names = Collections.list(super.getHeaderNames());
        for (String name : headerMap.keySet()) {
            names.add(name);
        }
        return Collections.enumeration(names);
    }

    @Override
    public Enumeration<String> getHeaders(String name) {
        List<String> values = Collections.list(super.getHeaders(name));
        if (headerMap.containsKey(name)) {
            values.add(headerMap.get(name));
        }
        return Collections.enumeration(values);
    }

}
</code>```

And with the method .addHeader("parameter", "New value"), but when I read the parameter in the method it has not changed, but if  read it from .getHeader("parameter") method from HttpServletRequest class, which I have inyected in the same method class. The changes are done, but in the method parameters not appear.

The method is the follows:

```<code>@Autowired
HttpServletRequest a;

//Annotations @GetMapping......
public void method (@RequestHeader(value="Parameter") String parameter){
      System.out.print(parameter); //Parameter did not change
      system.out.print(a.getHeader("parameter"));    //Parameter changed.
}
</code>```

Does Someone know how to change the operation parameters from a filter?, or from other way....

答:
I'm not sure what you're doing wrong but when I try it with my setup it seems to work. Please take a look at the following code.</p>

```<code>/**
 * username: test, password: test
 * Added Header: param
 * REST URL: http://localhost:8080/hello
 */

package com.test;

import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.filter.OncePerRequestFilter;

@SpringBootApplication
public class TestRequestWrapperApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestRequestWrapperApplication.class, args);
    }
}

@Component
class RequestWrapperFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        HeaderMapRequestWrapper wrappedRequest = new HeaderMapRequestWrapper((HttpServletRequest)request);
        wrappedRequest.addHeader("param", "Hello World!");

        filterChain.doFilter(wrappedRequest, response);
    }
}

@RestController
class TestRest {
    @GetMapping("hello")
    public String hello(@RequestHeader("param") String param) {
        return "param: " + param;
    }
}

@Configuration
class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("test").authorities("test").password("test");
    }
}

class HeaderMapRequestWrapper extends HttpServletRequestWrapper {
    public HeaderMapRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    private Map<String, String> headerMap = new HashMap<String, String>();

    public void addHeader(String name, String value) {
        headerMap.put(name, value);
    }

    public void removeteHeader(String name) {
        headerMap.remove(name);
    }

    @Override
    public String getHeader(String name) {
        String headerValue = super.getHeader(name);
        if (headerMap.containsKey(name)) {
            headerValue = headerMap.get(name);
        }
        return headerValue;
    }

    @Override
    public Enumeration<String> getHeaderNames() {
        List<String> names = Collections.list(super.getHeaderNames());
        for (String name : headerMap.keySet()) {
            names.add(name);
        }
        return Collections.enumeration(names);
    }

    @Override
    public Enumeration<String> getHeaders(String name) {
        List<String> values = Collections.list(super.getHeaders(name));
        if (headerMap.containsKey(name)) {
            values.add(headerMap.get(name));
        }
        return Collections.enumeration(values);
    }

}
</code>```

Body:
I am developing a filter with Spring security which extends of
OncePerRequestFilter class and It has to update parameters in the REST service .Parameters are entered by the header with the annotation @RequestHeader.

I have tried to update parameters from the filter with the follows class:

public class HeaderMapRequestWrapper extends HttpServletRequestWrapper {
    /**
     * construct a wrapper for this request
     *
     * @param request
     */
    public HeaderMapRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    private Map<String, String> headerMap = new HashMap<String, String>();

    /**
     * add a header with given name and value
     *
     * @param name
     * @param value
     */
    public void addHeader(String name, String value) {
        headerMap.put(name, value);
    }

    public void removeteHeader(String name){
        headerMap.remove(name);
    }
    @Override
    public String getHeader(String name) {
        String headerValue = super.getHeader(name);
        if (headerMap.containsKey(name)) {
            headerValue = headerMap.get(name);
        }
        return headerValue;
    }

    /**
     * get the Header names
     */
    @Override
    public Enumeration<String> getHeaderNames() {
        List<String> names = Collections.list(super.getHeaderNames());
        for (String name : headerMap.keySet()) {
            names.add(name);
        }
        return Collections.enumeration(names);
    }

    @Override
    public Enumeration<String> getHeaders(String name) {
        List<String> values = Collections.list(super.getHeaders(name));
        if (headerMap.containsKey(name)) {
            values.add(headerMap.get(name));
        }
        return Collections.enumeration(values);
    }

}

And with the method .addHeader("parameter", "New value"), but when I read the parameter in the method it has not changed, but if  read it from .getHeader("parameter") method from HttpServletRequest class, which I have inyected in the same method class. The changes are done, but in the method parameters not appear.

The method is the follows:

@Autowired
HttpServletRequest a;

//Annotations @GetMapping......
public void method (@RequestHeader(value="Parameter") String parameter){
      System.out.print(parameter); //Parameter did not change
      system.out.print(a.getHeader("parameter"));    //Parameter changed.
}

Does Someone know how to change the operation parameters from a filter?, or from other way....

Body:
I'm not sure what you're doing wrong but when I try it with my setup it seems to work. Please take a look at the following code.

/**
 * username: test, password: test
 * Added Header: param
 * REST URL: http://localhost:8080/hello
 */

package com.test;

import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.filter.OncePerRequestFilter;

@SpringBootApplication
public class TestRequestWrapperApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestRequestWrapperApplication.class, args);
    }
}

@Component
class RequestWrapperFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        HeaderMapRequestWrapper wrappedRequest = new HeaderMapRequestWrapper((HttpServletRequest)request);
        wrappedRequest.addHeader("param", "Hello World!");

        filterChain.doFilter(wrappedRequest, response);
    }
}

@RestController
class TestRest {
    @GetMapping("hello")
    public String hello(@RequestHeader("param") String param) {
        return "param: " + param;
    }
}

@Configuration
class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("test").authorities("test").password("test");
    }
}

class HeaderMapRequestWrapper extends HttpServletRequestWrapper {
    public HeaderMapRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    private Map<String, String> headerMap = new HashMap<String, String>();

    public void addHeader(String name, String value) {
        headerMap.put(name, value);
    }

    public void removeteHeader(String name) {
        headerMap.remove(name);
    }

    @Override
    public String getHeader(String name) {
        String headerValue = super.getHeader(name);
        if (headerMap.containsKey(name)) {
            headerValue = headerMap.get(name);
        }
        return headerValue;
    }

    @Override
    public Enumeration<String> getHeaderNames() {
        List<String> names = Collections.list(super.getHeaderNames());
        for (String name : headerMap.keySet()) {
            names.add(name);
        }
        return Collections.enumeration(names);
    }

    @Override
    public Enumeration<String> getHeaders(String name) {
        List<String> values = Collections.list(super.getHeaders(name));
        if (headerMap.containsKey(name)) {
            values.add(headerMap.get(name));
        }
        return Collections.enumeration(values);
    }

}


问:
Setting up a personal (Java) workspace: What do I need?
I want to set up a personal workspace on my home machine. I mainly intend to use it for Java development on home projects. Which tools do you recommend me to use? (I prefer free tools, since this is just for home use.)

I hope you could recommend some tools for me, and I'd be grateful if you could give me a brief comparison of some tools in each category. For example, I'm thinking about source control and issue tracking tools, but any other categories are welcome.

I also hope to get some information about the ability to move between suggested tools (e.g. migrate between CVS and SVN), and even between operating systems if applicable. (I'm currently using Windows Vista, but I may want to switch to Linux sometime soon.) Maybe even information about how easy it would be to move the project to an open-source repository (e.g. SF.net).

Thank you.

(P.S. Any comments on how to enhance/clarify my question are more than welcome.)

答:
I use the following tools and have been happy moving between Windows/OS X/Linux without a problem:

IDES

Eclipse 3.4 IDE. Lots of great free plugins. Its a bit slow as an IDE but I prefer it to NetBeans

NetBeans -- another free IDE that has lots of plugins.

Version Control
Basically the things people are using now are SVN, CVS, or maybe GIT.

SVN for version control -- it is more mature and now seems more in use than CVS. Has good integration with Eclipse. However in terms of sourceforge I think it still uses CVS though there are other open source hosting that probably use SVN.

CVS still good support though many people have been moving to SVN

Git I haven't used it myself though I have heard its been gaining a lot of traction and is more powerful than SVN.

Issue Tracking

I use Mantis which is actually a PHP-based one. Its ok not a great.

Bugzilla

Trac

OwnerDisplayName: Hosam Aly

ContentLicense: CC BY-SA 2.5

OwnerDisplayName: Ish


问:
Matplotlib Build Problem: Error C1083: Cannot open include file: 'ft2build.h'
ft2build.h is located here:

C:\Program Files\GnuWin32\include

Initially, I made the same mistake as here:

Fatal Error C1083: Cannot Open Include file: 'tiffio.h': No such file or directory VC++ 2008

but since then, I've corrected that particular error (I've added the above directory to the "include" list, rather than the "executables" list), but I still get an error. The complete output is this:

```<code>BUILDING MATPLOTLIB
            matplotlib: 0.98.5.2
                python: 2.6.2 Stackless 3.1b3 060516 (release26-maint, Apr
                        14 2009, 21:19:36) [MSC v.1500 32 bit (Intel)]
              platform: win32
       Windows version: (5, 1, 2600, 2, 'Service Pack 3')

REQUIRED DEPENDENCIES
                 numpy: 1.3.0
             freetype2: found, but unknown version (no pkg-config)
                        * WARNING: Could not find 'freetype2' headers in any
                        * of '.', '.\freetype2'.

OPTIONAL BACKEND DEPENDENCIES
                libpng: found, but unknown version (no pkg-config)
                        * Could not find 'libpng' headers in any of '.'
               Tkinter: no
                        * No tk/win32 support for this python version yet
              wxPython: 2.8.9.2
                        * WxAgg extension not required for wxPython >= 2.8
                  Gtk+: no
                        * Building for Gtk+ requires pygtk; you must be able
                        * to "import gtk" in your build/install environment
       Mac OS X native: no
                    Qt: no
                   Qt4: no
                 Cairo: no

OPTIONAL DATE/TIMEZONE DEPENDENCIES
              datetime: present, version unknown
              dateutil: matplotlib will provide
                  pytz: matplotlib will provide

OPTIONAL USETEX DEPENDENCIES
                dvipng: no
           ghostscript: no
                 latex: no
               pdftops: no

[Edit setup.cfg to suppress the above messages]
============================================================================
pymods ['pylab']
packages ['matplotlib', 'matplotlib.backends', 'matplotlib.projections', 'mpl_to
olkits', 'matplotlib.numerix', 'matplotlib.numerix.mlab', 'matplotlib.numerix.ma
', 'matplotlib.numerix.npyma', 'matplotlib.numerix.linear_algebra', 'matplotlib.
numerix.random_array', 'matplotlib.numerix.fft', 'matplotlib.delaunay', 'pytz',
'dateutil', 'dateutil/zoneinfo']
running build
running build_py
copying lib\matplotlib\mpl-data\matplotlibrc -> build\lib.win32-2.6\matplotlib\m
pl-data
copying lib\matplotlib\mpl-data\matplotlib.conf -> build\lib.win32-2.6\matplotli
b\mpl-data
running build_ext
building 'matplotlib.ft2font' extension
C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\cl.exe /c /nologo /Ox /MD /W
3 /GS- /DNDEBUG -IC:\Python26\lib\site-packages\numpy\core\include -I. -IC:\Pyth
on26\lib\site-packages\numpy\core\include\freetype2 -I.\freetype2 -IC:\Python26\
include -IC:\Python26\include\Stackless -IC:\Python26\PC /Tpsrc/ft2font.cpp /Fob
uild\temp.win32-2.6\Release\src/ft2font.obj
ft2font.cpp
C:\Program Files\Microsoft Visual Studio 9.0\VC\INCLUDE\xlocale(342) : warning C
4530: C++ exception handler used, but unwind semantics are not enabled. Specify
/EHsc
c:\python26\lib\site-packages\matplotlib-0.98.5.2\src\ft2font.h(13) : fatal erro
r C1083: Cannot open include file: 'ft2build.h': No such file or directory
error: command '"C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\cl.exe"' fa
iled with exit status 2
</code>```

I should mention that this is Python 2.6

答: For those who might have the same issue but on a Mac OS 10.6 (snow leopard) and Python 2.7. , the easiest solution I found was to get a make file which downloads Numpy, scipy and matplotlib and compile them for you. You can customize the make file to get you matplotlib only. Here is the link to the solution.</p>

Body:
ft2build.h is located here:

C:\Program Files\GnuWin32\include

Initially, I made the same mistake as here:

Fatal Error C1083: Cannot Open Include file: 'tiffio.h': No such file or directory VC++ 2008

but since then, I've corrected that particular error (I've added the above directory to the "include" list, rather than the "executables" list), but I still get an error. The complete output is this:

BUILDING MATPLOTLIB
            matplotlib: 0.98.5.2
                python: 2.6.2 Stackless 3.1b3 060516 (release26-maint, Apr
                        14 2009, 21:19:36) [MSC v.1500 32 bit (Intel)]
              platform: win32
       Windows version: (5, 1, 2600, 2, 'Service Pack 3')

REQUIRED DEPENDENCIES
                 numpy: 1.3.0
             freetype2: found, but unknown version (no pkg-config)
                        * WARNING: Could not find 'freetype2' headers in any
                        * of '.', '.\freetype2'.

OPTIONAL BACKEND DEPENDENCIES
                libpng: found, but unknown version (no pkg-config)
                        * Could not find 'libpng' headers in any of '.'
               Tkinter: no
                        * No tk/win32 support for this python version yet
              wxPython: 2.8.9.2
                        * WxAgg extension not required for wxPython >= 2.8
                  Gtk+: no
                        * Building for Gtk+ requires pygtk; you must be able
                        * to "import gtk" in your build/install environment
       Mac OS X native: no
                    Qt: no
                   Qt4: no
                 Cairo: no

OPTIONAL DATE/TIMEZONE DEPENDENCIES
              datetime: present, version unknown
              dateutil: matplotlib will provide
                  pytz: matplotlib will provide

OPTIONAL USETEX DEPENDENCIES
                dvipng: no
           ghostscript: no
                 latex: no
               pdftops: no

[Edit setup.cfg to suppress the above messages]
============================================================================
pymods ['pylab']
packages ['matplotlib', 'matplotlib.backends', 'matplotlib.projections', 'mpl_to
olkits', 'matplotlib.numerix', 'matplotlib.numerix.mlab', 'matplotlib.numerix.ma
', 'matplotlib.numerix.npyma', 'matplotlib.numerix.linear_algebra', 'matplotlib.
numerix.random_array', 'matplotlib.numerix.fft', 'matplotlib.delaunay', 'pytz',
'dateutil', 'dateutil/zoneinfo']
running build
running build_py
copying lib\matplotlib\mpl-data\matplotlibrc -> build\lib.win32-2.6\matplotlib\m
pl-data
copying lib\matplotlib\mpl-data\matplotlib.conf -> build\lib.win32-2.6\matplotli
b\mpl-data
running build_ext
building 'matplotlib.ft2font' extension
C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\cl.exe /c /nologo /Ox /MD /W
3 /GS- /DNDEBUG -IC:\Python26\lib\site-packages\numpy\core\include -I. -IC:\Pyth
on26\lib\site-packages\numpy\core\include\freetype2 -I.\freetype2 -IC:\Python26\
include -IC:\Python26\include\Stackless -IC:\Python26\PC /Tpsrc/ft2font.cpp /Fob
uild\temp.win32-2.6\Release\src/ft2font.obj
ft2font.cpp
C:\Program Files\Microsoft Visual Studio 9.0\VC\INCLUDE\xlocale(342) : warning C
4530: C++ exception handler used, but unwind semantics are not enabled. Specify
/EHsc
c:\python26\lib\site-packages\matplotlib-0.98.5.2\src\ft2font.h(13) : fatal erro
r C1083: Cannot open include file: 'ft2build.h': No such file or directory
error: command '"C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\cl.exe"' fa
iled with exit status 2

I should mention that this is Python 2.6


答:
Excluding local mode, driver can be co-located with executors:

In cluster mode where driver runs on one of the worker nodes.

In client mode if it has been started on one of the worker nodes.

In both cases driver can compete for the same resource pool as executors. In the first case it happens at the resource manger level, in the second case at host system level.

The second case is less interesting, but in the first on resources allocated to driver, will reduce the pool that cluster manager can offer to the executors.

Title: How does the Spark Driver's memory affect Executor resources?

Body:
I am learning how to configure Spark applications by reading High Performance Spark, and one sentence mentioned is confusing me:

In my experience, a good heuristic for setting the Spark driver memory is
  simply the lowest possible value that does not lead to memory errors in
  the driver, i.e., which gives the maximum possible resources to the executors.

My understanding is that the driver exists in its own node, and executors exist independently on worker nodes. I would have assumed I can max out driver memory without having to worry about it affecting executors.

Is what the book suggesting true? If it is, why/how does the driver memory affect executor resources?


问:
Sum outputs from atoi
I am trying to learn C++14 (with a "modern"/non-C style). I am trying to sum the digits in a string, for the digits that are equal to the next digit (or the first digit, if at the end of the string). I solved it in Python, but in C++14, I get unexpected output.

main.cpp:

```<code>#include <iostream>
#include <stdlib.h>

using namespace std;

using sum_t =  uint64_t; // int; // unsigned long long;

// sum_digits sums the digits in the given std::string and returns an integer of type sum_t
sum_t sum_digits(std::string digits) {
    sum_t total = 0;
    for (const auto &digit : digits) {
        total += atoi(new char(digit));
        cout << "sum this far: " << total << endl;
    }
    return total;
}

// return the last char
// returns ' ' if given an empty string
char last(std::string digits) {
    auto dl = digits.length();
    if (dl < 1) {
        return ' ';
    }
    return digits.at(dl - 1);
}

// sum_equal_digits returns a sum of the digits that are equal to the next one
sum_t sum_equal_digits(std::string digits) {
    auto equal_digits = ""s; // a list of equal digits, as std::string
    auto prev_digit = last(digits); // start with the last digit, since the digit list is supposed to be circular
    for (const auto &digit : digits) {
        if (digit == prev_digit) {
            equal_digits += digit;
        }
        prev_digit = digit;
    }
    return sum_digits(equal_digits);
}

void equal(sum_t a, sum_t b) {
    if (a == b) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
        exit(EXIT_FAILURE);
    }
}

int main() {
    equal(sum_equal_digits("1122"s), 3);
    equal(sum_equal_digits("1111"s), 4);
    equal(sum_equal_digits("1234"s), 0);
    equal(sum_equal_digits("91212129"s), 9);
    equal(sum_equal_digits("823936645345581272695677318513459491834641129844393742672553544439126314399846773234845535593355348931499496184839582118817689171948635864427852215325421433717458975771369522138766248225963242168658975326354785415252974294317138511141826226866364555761117178764543435899886711426319675443679829181257496966219435831621565519667989898725836639626681645821714861443141893427672384716732765884844772433374798185955741311116365899659833634237938878181367317218635539667357364295754744829595842962773524584225427969467467611641591834876769829719248136613147351298534885563144114336211961674392912181735773851634298227454157885241769156811787611897349965331474217223461176896643242975397227859696554492996937235423272549348349528559432214521551656971136859972232854126262349381254424597348874447736545722261957871275935756764184378994167427983811716675476257858556464755677478725146588747147857375293675711575747132471727933773512571368467386151966568598964631331428869762151853634362356935751298121849281442128796517663482391226174256395515166361514442624944181255952124524815268864131969151433888721213595267927325759562132732586252438456569556992685896517565257787464673718221817783929691626876446423134331749327322367571432532857235214364221471769481667118117729326429556357572421333798517168997863151927281418238491791975399357393494751913155219862399959646993428921878798119215675548847845477994836744929918954159722827194721564121532315459611433157384994543332773796862165243183378464731546787498174844781781139571984272235872866886275879944921329959736315296733981313643956576956851762149275521949177991988236529475373595217665112434727744235789852852765675189342753695377219374791548554786671473733124951946779531847479755363363288448281622183736545494372344785112312749694167483996738384351293899149136857728545977442763489799693492319549773328626918874718387697878235744154491677922317518952687439655962477734559232755624943644966227973617788182213621899579391324399386146423427262874437992579573858589183571854577861459758534348533553925167947139351819511798829977371215856637215221838924612644785498936263849489519896548811254628976642391428413984281758771868781714266261781359762798"), 1144);
    return 0;
}
</code>```

The last two lines of the output are:

```<code>sum this far: 18446744073627459815
NO
</code>```

But I was expecting:

```<code>sum this far: 1144
YES
</code>```

I compiled the program with:

```<code>g++ -O2 -std=c++14 -Wfatal-errors -pedantic -Wall main.cpp -o main
</code>```

I also tried using sum_t = int; and using sum_t = uint64_t;.

How can the program be corrected?

答: Some additional annotations: The function atoi() is a C-style function, which requires a 0-terminated C-style string (see other comments). Also the header stdlib.h is a C-Header. If you write C++-Code you should use #include <cstdlib> instead. You can also use std::stoi() defined in #include <string>, but it does not performs good, because it has to construct a string object from the character.

Title: Sum outputs from atoi

Body:
I am trying to learn C++14 (with a "modern"/non-C style). I am trying to sum the digits in a string, for the digits that are equal to the next digit (or the first digit, if at the end of the string). I solved it in Python, but in C++14, I get unexpected output.

main.cpp:

#include <iostream>
#include <stdlib.h>

using namespace std;

using sum_t =  uint64_t; // int; // unsigned long long;

// sum_digits sums the digits in the given std::string and returns an integer of type sum_t
sum_t sum_digits(std::string digits) {
    sum_t total = 0;
    for (const auto &digit : digits) {
        total += atoi(new char(digit));
        cout << "sum this far: " << total << endl;
    }
    return total;
}

// return the last char
// returns ' ' if given an empty string
char last(std::string digits) {
    auto dl = digits.length();
    if (dl < 1) {
        return ' ';
    }
    return digits.at(dl - 1);
}

// sum_equal_digits returns a sum of the digits that are equal to the next one
sum_t sum_equal_digits(std::string digits) {
    auto equal_digits = ""s; // a list of equal digits, as std::string
    auto prev_digit = last(digits); // start with the last digit, since the digit list is supposed to be circular
    for (const auto &digit : digits) {
        if (digit == prev_digit) {
            equal_digits += digit;
        }
        prev_digit = digit;
    }
    return sum_digits(equal_digits);
}

void equal(sum_t a, sum_t b) {
    if (a == b) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
        exit(EXIT_FAILURE);
    }
}

int main() {
    equal(sum_equal_digits("1122"s), 3);
    equal(sum_equal_digits("1111"s), 4);
    equal(sum_equal_digits("1234"s), 0);
    equal(sum_equal_digits("91212129"s), 9);
    equal(sum_equal_digits("823936645345581272695677318513459491834641129844393742672553544439126314399846773234845535593355348931499496184839582118817689171948635864427852215325421433717458975771369522138766248225963242168658975326354785415252974294317138511141826226866364555761117178764543435899886711426319675443679829181257496966219435831621565519667989898725836639626681645821714861443141893427672384716732765884844772433374798185955741311116365899659833634237938878181367317218635539667357364295754744829595842962773524584225427969467467611641591834876769829719248136613147351298534885563144114336211961674392912181735773851634298227454157885241769156811787611897349965331474217223461176896643242975397227859696554492996937235423272549348349528559432214521551656971136859972232854126262349381254424597348874447736545722261957871275935756764184378994167427983811716675476257858556464755677478725146588747147857375293675711575747132471727933773512571368467386151966568598964631331428869762151853634362356935751298121849281442128796517663482391226174256395515166361514442624944181255952124524815268864131969151433888721213595267927325759562132732586252438456569556992685896517565257787464673718221817783929691626876446423134331749327322367571432532857235214364221471769481667118117729326429556357572421333798517168997863151927281418238491791975399357393494751913155219862399959646993428921878798119215675548847845477994836744929918954159722827194721564121532315459611433157384994543332773796862165243183378464731546787498174844781781139571984272235872866886275879944921329959736315296733981313643956576956851762149275521949177991988236529475373595217665112434727744235789852852765675189342753695377219374791548554786671473733124951946779531847479755363363288448281622183736545494372344785112312749694167483996738384351293899149136857728545977442763489799693492319549773328626918874718387697878235744154491677922317518952687439655962477734559232755624943644966227973617788182213621899579391324399386146423427262874437992579573858589183571854577861459758534348533553925167947139351819511798829977371215856637215221838924612644785498936263849489519896548811254628976642391428413984281758771868781714266261781359762798"), 1144);
    return 0;
}

The last two lines of the output are:

sum this far: 18446744073627459815
NO

But I was expecting:

sum this far: 1144
YES

I compiled the program with:

g++ -O2 -std=c++14 -Wfatal-errors -pedantic -Wall main.cpp -o main

I also tried using sum_t = int; and using sum_t = uint64_t;.

How can the program be corrected?


问:
Count number of Special Character in Unix Shell
I have a delimited file that is separated by octal \036 or Hexadecimal value 1e.

I need to count the number of delimiters on each line using a bash shell script.

I was trying to use awk, not sure if this is the best way.

Sample Input (| is a representation of \036)

```<code>Example|Running|123|
</code>```

Expected output:

```<code>3
</code>```

答:
You can try</p>

```<code>awk '{print gsub(/\|/,"")}'
</code>```

Body:
I have a delimited file that is separated by octal \036 or Hexadecimal value 1e.

I need to count the number of delimiters on each line using a bash shell script.

I was trying to use awk, not sure if this is the best way.

Sample Input (| is a representation of \036)

Example|Running|123|

Expected output:

3

Body:
You can try

awk '{print gsub(/\|/,"")}'


答:
A couple things here:

First, use the change event handler for the select picker to trigger the change. Second, make sure you use a RFC3339 time format for setting the value. Third, you had some syntax wrong in some of your code (mis-placed braces):

$(function() {
  var select = $('select').val;
  $( "select.select" ).change(function() {
  // Check input( $( this ).val() ) for validity here
    if ($(this).val() == 1) {
      $("#selectedTime").val("11:00:00");
    } else {
      $("#selectedTime").val("08:00:00");
    }
  });
});```
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="" id="" class="select">
  <option value="">Select</option>
  <option value="1">lunch</option>
  <option value="2">Dinner</option>
</select>
<input type="time" id="selectedTime"></code>```

Title: I want to change the time automatically when i select the drop down option

Body:
A couple things here:

First, use the change event handler for the select picker to trigger the change. Second, make sure you use a RFC3339 time format for setting the value. Third, you had some syntax wrong in some of your code (mis-placed braces):

$(function() {
  var select = $('select').val;
  $( "select.select" ).change(function() {
  // Check input( $( this ).val() ) for validity here
    if ($(this).val() == 1) {
      $("#selectedTime").val("11:00:00");
    } else {
      $("#selectedTime").val("08:00:00");
    }
  });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="" id="" class="select">
  <option value="">Select</option>
  <option value="1">lunch</option>
  <option value="2">Dinner</option>
</select>
<input type="time" id="selectedTime">


问:
Is there an addon for firefox that when you look at the source code xml it formats it with appropriate formatting (newlines, indentation..etc)
Any help greatly appreciated thanks.</p>

答:
Just open it in firefox - it has a default stylesheet that will display nicely, assuming no existing stylesheet on the document.

If this isn't enough, you can use firebug - it will display the DOM in the html tab.

Looking around a bit also uncovered flashbug, which required firebug and has an XML pretty printer feature as you are looking for.

Body: Any help greatly appreciated thanks.


问:
Workaround for memory allocation
I read many questions here on memory management. So I cleaned up my GB size data and narrowed it down to 32MB (770K rows) and store it on BigQuery now. But when I try to turn it into matrix, as.data.frame(str_split_fixed(event_list$event_list, ",", max(length(strsplit(event_list$event_list, ","))))), I get this error

Error: cannot allocate vector of size 4472.6 Gb

Is there anyway to fix this problem, what am I doing wrong here? Is that I store it on BigQuery or converting it to matrix increases the size?

答:
@JosephWood nailed it.  If event_list has 700,000 rows, then you're trying to create a data.frame with 700,000 rows and 700,000 columns. strsplit(event_list$event_list, ",") would be a list of length 700,000, so length(strsplit(event_list$event_list, ",")) gives a single number: 700000.  max of one number is just that number. You should use lengths instead.

So your call to str_split_fixed ends up acting like this:

```<code>str_split_fixed(event_list$event_list, ",", n = 700000)
</code>```

That gives a list of 700,000 elements (length of event_list$event_list), each element being a character vector with 700,000 values (n).

On my machine, I roughly estimated the necessary memory:

```<code>format(700000 * object.size(character(700000)), "GB")
# [1] "3650.8 Gb"
</code>```

That's not counting any extra memory required to store those vectors in a data.frame.

The solution:

```<code>split_values <- strsplit(event_list$event_list, ",")
value_counts <- lengths(split_values)
extra_blanks <- lapply(max(value_counts) - value_counts, character)
values_with_blanks <- mapply(split_values, extra_blanks, FUN = c, SIMPLIFY = FALSE)
DF <- as.data.frame(values_with_blanks)
</code>```

Title: Workaround for memory allocation

Body:
@JosephWood nailed it.  If event_list has 700,000 rows, then you're trying to create a data.frame with 700,000 rows and 700,000 columns. strsplit(event_list$event_list, ",") would be a list of length 700,000, so length(strsplit(event_list$event_list, ",")) gives a single number: 700000.  max of one number is just that number. You should use lengths instead.

So your call to str_split_fixed ends up acting like this:

str_split_fixed(event_list$event_list, ",", n = 700000)

That gives a list of 700,000 elements (length of event_list$event_list), each element being a character vector with 700,000 values (n).

On my machine, I roughly estimated the necessary memory:

format(700000 * object.size(character(700000)), "GB")
# [1] "3650.8 Gb"

That's not counting any extra memory required to store those vectors in a data.frame.

The solution:

split_values <- strsplit(event_list$event_list, ",")
value_counts <- lengths(split_values)
extra_blanks <- lapply(max(value_counts) - value_counts, character)
values_with_blanks <- mapply(split_values, extra_blanks, FUN = c, SIMPLIFY = FALSE)
DF <- as.data.frame(values_with_blanks)


问:
Garbage Collector overhead limit exceeded Caused By Array Creation

Hello,

My program spent a lot of Time creating Object in the Heap Memory , so at certain  Time i get this Error:

Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded

I can't Put my Full Application in this discussion So i created A prototype to explain what My program is doing.

The part of my program that deal with Creating Object looks as Follows:

```<code>**The Calling Program :**

public class Example {

public static void main(String[] args) {
        ArrayList list = new ArrayList();
        for (int i = 0; i < 5000; i++) {
            for (int j = 0; j < 5000; j++) {
                if (i==j)
                    continue;
                int weight = new Random().nextInt();
                Edge edge = new Edge(new Vertex(i+""), new Vertex(j+""),weight);
                list.add(edge);
            }
        }
  }
</code>```

}

The Class Vertex :

```<code>public class Vertex {
    private String sequence ;

    public Vertex() {
    }

    public Vertex(String seq) {
        this.sequence = seq;
    }

}
</code>```

The Class Edge :

```<code>public class Edge {

    private Vertex source;
    private Vertex destination;
    private int weight;

    public Edge() {

    }

    public Edge(Vertex source, Vertex destination, int weight) {
        int[][] array = new int[500][500];
        //here i need the array to do some calculation
        anotherClass.someCalculation(array,source,destination);
        this.source = source;
        this.destination = destination;
        this.weight = weight;

    }
}
</code>```

So as you can see:

I have 5000 Vertices , i need To create 5000*5000 Edges , each edge has An array of length 500*500.

For this reason the memory allocated in the Heap Memory end at certain time,the problem as i understood from many disctions I read is that there is no guaranty that the Garbage Collector Will free memory.

So what are the solution for this problem ? normally I don't Need The Edge's Array after constructing the Edge;the array is only needed during the construction of The Edges.

Another question : How can I minimize the Memory Utilisation in my Program ? I tried To turn the int array to char array but it didn't help.

Many Thanks

答:
The problem with your code is due to following reason.
Everytime you create an object of Edge class

```<code>Edge edge = new Edge(new Vertex(i), new Vertex(j),weight);
</code>```

the following constructor is invoked

```<code>public Edge(Vertex source, Vertex destination, int weight)
</code>```

Therefore, for each object creation of Edge class the following 500 X 500 integer array is created

```<code>int[][] array = new int[500][500];
</code>```

which consumes a lot of memory because you are creating around 5000 X 5000 objects of Edge class.

So, if you want to store edge weights , try to make a global integer array for it.

Body:
Hello,

My program spent a lot of Time creating Object in the Heap Memory , so at certain  Time i get this Error:

Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded

I can't Put my Full Application in this discussion So i created A prototype to explain what My program is doing.

The part of my program that deal with Creating Object looks as Follows:

**The Calling Program :**

public class Example {

public static void main(String[] args) {
        ArrayList list = new ArrayList();
        for (int i = 0; i < 5000; i++) {
            for (int j = 0; j < 5000; j++) {
                if (i==j)
                    continue;
                int weight = new Random().nextInt();
                Edge edge = new Edge(new Vertex(i+""), new Vertex(j+""),weight);
                list.add(edge);
            }
        }
  }

}

The Class Vertex :

public class Vertex {
    private String sequence ;

    public Vertex() {
    }

    public Vertex(String seq) {
        this.sequence = seq;
    }

}

The Class Edge :

public class Edge {

    private Vertex source;
    private Vertex destination;
    private int weight;

    public Edge() {

    }

    public Edge(Vertex source, Vertex destination, int weight) {
        int[][] array = new int[500][500];
        //here i need the array to do some calculation
        anotherClass.someCalculation(array,source,destination);
        this.source = source;
        this.destination = destination;
        this.weight = weight;

    }
}

So as you can see:

I have 5000 Vertices , i need To create 5000*5000 Edges , each edge has An array of length 500*500.

For this reason the memory allocated in the Heap Memory end at certain time,the problem as i understood from many disctions I read is that there is no guaranty that the Garbage Collector Will free memory.

So what are the solution for this problem ? normally I don't Need The Edge's Array after constructing the Edge;the array is only needed during the construction of The Edges.

Another question : How can I minimize the Memory Utilisation in my Program ? I tried To turn the int array to char array but it didn't help.

Many Thanks

Body:
The problem with your code is due to following reason.
Everytime you create an object of Edge class

Edge edge = new Edge(new Vertex(i), new Vertex(j),weight);

the following constructor is invoked

public Edge(Vertex source, Vertex destination, int weight)

Therefore, for each object creation of Edge class the following 500 X 500 integer array is created

int[][] array = new int[500][500];

which consumes a lot of memory because you are creating around 5000 X 5000 objects of Edge class.

So, if you want to store edge weights , try to make a global integer array for it.


问:
Investment figures for the Java platform
Are there any market research figures available calculating aggregate investment (worldwide, by region, by country) in the Java platform? This could include software (application servers, IDEs, profiling tools), hardware (Sun servers, bytecode executing processors), personnel (training, books), language development (Java, Clojure, Scala, ..), public/private (universities, governments, corporations), etc.

I'm working on a presentation on JVM languages and would like to illustrate—in some reliable manner—that existing investment in the Java platform is massive and not going anywhere anytime soon (think Cobol). I'm sure this point is clear for IT guys, but I'd like to put a dollar figure on it for the business folks to really drive the point home.

Before the comments start, I recognize that this question is on the outer edge of what most of us consider to be "programming related". Regardless, knowing this information can help many IT department discussions around the future of a platform, and may even assist developers make career decisions.

答:
Market research on this kind of topic is notoriously unreliable - mainly because nobody collects the data at a granular level so there is a lot of guesswork / extrapolation involved. Remember that money spent on the salaries of every coder and project manager in a bank working on Java applications is technically "investment in the Java platofrm" even though it goes nowhere near the books of Oracle or IBM or any other tech company.

I'd suggest trying to estimate the investment / level of activity from a number of different directions, and triangulate these to come to an informed view

Here's some approaches that you could use:

Use something like the TIOBE index as a rough estimate of programming language share and multiply it by your best estimate of Global IT software development spending.

Add up the revenues of Java-aligned tech companies (Oracle, IBM, Google, etc...), weighted by whatever proportion of their activity you guess to be related to Java-based products. Then multiply by 1/(1-X) where X is the percentage of software investment you estimate to be done in-house or with smaller companies not on your list

Look at the growth rates and activity in new JVM languages like Clojure, Scala, Jython, JRuby etc. You can easily make the argument that these are ensuring an innovative future on the Java platform.

LastEditorDisplayName: Roger Pate


问:
Bazel failed to include a external static library .a
-- Question solved --

This question is solved. Thanks for all the help!

The problem and the reason is briefly stated as following for other readers in the future:

[Environment]

Ubuntu 14.04, Bazel, C++

[Question]

I want to include an external library. So I download the source file and make it as as a static lib .a.

I correctly set the BUILD, WORKSPACE and related files. However, when compile, it alerts the error:

```<code>Linking of rule '//main' failed (Exit 1)
main: error: undefined reference to 'void ex_lib::compute(double*, double const*, double const*)'
collect2: error: ld returned 1 exit status
</code>```

I tried with the .lo and alwayslink method here but doesn't work.

Alternate to "`--whole-archive`" in bazel

[Reason]

It is because that the external library source files contains duplicate header files. So I fix the header file problem. Make the .a again. Then everything works.

So nothing wrong with the bazel. It is the library itself that cause the problem. But the error information is confusing and I think it is caused by bazel.

答:
The problem is solved. Special thanks to mhlopko.

It is mainly because there is errors with the lib source file. Thus .a file I use contains errors. Now the bazel works fine with the new lib file.

By the way, now bazel works fine with both .a or .so file that I generated.

Body:
-- Question solved --

This question is solved. Thanks for all the help!

The problem and the reason is briefly stated as following for other readers in the future:

[Environment]

Ubuntu 14.04, Bazel, C++

[Question]

I want to include an external library. So I download the source file and make it as as a static lib .a.

I correctly set the BUILD, WORKSPACE and related files. However, when compile, it alerts the error:

Linking of rule '//main' failed (Exit 1)
main: error: undefined reference to 'void ex_lib::compute(double*, double const*, double const*)'
collect2: error: ld returned 1 exit status

I tried with the .lo and alwayslink method here but doesn't work.

Alternate to "`--whole-archive`" in bazel

[Reason]

It is because that the external library source files contains duplicate header files. So I fix the header file problem. Make the .a again. Then everything works.

So nothing wrong with the bazel. It is the library itself that cause the problem. But the error information is confusing and I think it is caused by bazel.


问:
Return key for JS Map constructor with value
I'm running into difficulty retrieving keys from a JS Map constructor.

Say you have the code below:

```<code>var myMap = new Map();

myMap.set('AK', 'Alaska');
myMap.set('CO', 'Colorado');

console.log(myMap.get('CO'))
</code>```

This console.logs 'Colorado'. I'm trying to figure out how I can do the opposite and return the key ('CO') using the value ('Colorado').

In a perfect world I could just do myMap.get('Colorado') to return 'CO' but this does not work.

答:
Map is best used when you have unique keys mapping to a value.

Assuming that key-value pairs are distinct in your use case, we can write a helper to find key using value.

```<code>function getKey(map, value) {
   var found = Array.from(map.keys())
      .find((key) => map.get(key) === value);
   return found[0]; // returning undefined is just fine.
}
</code>```

Body:
I'm running into difficulty retrieving keys from a JS Map constructor.

Say you have the code below:

var myMap = new Map();

myMap.set('AK', 'Alaska');
myMap.set('CO', 'Colorado');

console.log(myMap.get('CO'))

This console.logs 'Colorado'. I'm trying to figure out how I can do the opposite and return the key ('CO') using the value ('Colorado').

In a perfect world I could just do myMap.get('Colorado') to return 'CO' but this does not work.

Body:
Map is best used when you have unique keys mapping to a value.

Assuming that key-value pairs are distinct in your use case, we can write a helper to find key using value.

function getKey(map, value) {
   var found = Array.from(map.keys())
      .find((key) => map.get(key) === value);
   return found[0]; // returning undefined is just fine.
}


问:
UnicodeDecodeError: 'utf-16-le'
I'm trying to pull a list of tables from an access database. The script goes through and displays about a third of the tables and gets the following error:

Traceback (most recent call last):   File "C:/GageInfo/sourcecode for
  GageTrakNotify/__test script.py", line 31, in
      for fld in cursor2.columns(rows.table_name): UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 132-133: illegal
  encoding

Any tips would be appreciated.

```<code>import pyodbc

cursor1 = conn.cursor()
cursor2 = conn.cursor()

tblCount = 0
for rows in cursor1.tables():
   if rows.table_type == "TABLE":
       tblCount += 1
       print(rows.table_name)
       for fld in cursor2.columns(rows.table_name):
           print(fld.table_name, fld.column_name)

conn.close()
</code>```

答:
Thanks for the tips and feedback. I resolved it by capturing the information in a list and then printing out results, and using a try statement to handle the error and apply the right unicode. </p>

<p>Your suggestions pointed me in the right direction. </p>

Title: UnicodeDecodeError: 'utf-16-le'

Body:
I'm trying to pull a list of tables from an access database. The script goes through and displays about a third of the tables and gets the following error:

Traceback (most recent call last):   File "C:/GageInfo/sourcecode for
  GageTrakNotify/__test script.py", line 31, in
      for fld in cursor2.columns(rows.table_name): UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 132-133: illegal
  encoding

Any tips would be appreciated.

import pyodbc

cursor1 = conn.cursor()
cursor2 = conn.cursor()

tblCount = 0
for rows in cursor1.tables():
   if rows.table_type == "TABLE":
       tblCount += 1
       print(rows.table_name)
       for fld in cursor2.columns(rows.table_name):
           print(fld.table_name, fld.column_name)

conn.close()

Body:
Thanks for the tips and feedback. I resolved it by capturing the information in a list and then printing out results, and using a try statement to handle the error and apply the right unicode.

Your suggestions pointed me in the right direction.


问:
How do i come up with a generic way to store all reference data in my database in SQL?
In my database, a customer can have many accounts and an account can be secured by many securities. A customer can be associated to another customer in the bank. For example property developer may be associated with a Surveyor.

A customer cannot be associated to another customer twice.

I have set up tables for Customers, Accounts Securities and Associations in SQL.

In terms of reference data the business has the following requirement: Accounts, Securities and Associations can be of several types For example, an account can be savings, credit; a security can be a bond, house, and an association can be that Customer 1 is the surveyor of Customer 2 (surveyor being the association).  They do not want many tables to store each of these types. They want a generic way to store all reference (types) data in the database.

I am very new to this and I cannot figure out a generic way to store the reference data. Do i need another table of types which would have Type ID Pk and then the description ( if i do it this way the table would be mixed with unrelated data)?

答:
If you decide you need a look up table for types, then yes you can create a generic table of Types, and add a column that describes what the type is particular to.

TypeID - an IDENTITY Column

TypeDescription - (eg: Savings, Credit,
Bond, House, etc)

TypeOf (or whatever name you prefer) - (eg:    Account, Security,
Association)


问:
Cloning multiple arraylists referencing the same objects
I have an arraylist a containing certain objects. I also have an arraylist b containing arraylists containing some of these objects. Now, I want to clone the arraylist a, while updating the references in arraylist b to the cloned objects. A pseudo code example:

```<code>a = [object1,object2,object3,object4,object5,object6,object7,object8,object9,object10];
b = [[object3,object8,object5],[object2,object9,object1][object6,object7]];

//now, I can clone arraylist a
aClone = [clone1,clone2,clone3,clone4,clone5,clone6,clone7,clone8,clone9,clone10];

//but how do I get a clone of arraylist b containing references to the clones of the objects like so:
bClone = [[clone3,clone8,clone5],[clone2,clone9,clone1][clone6,clone7]];
</code>```

Something I came up with is the following:

```<code>for(int i = 0; i < a.length; i++){
    o = a.get(i);
    aClone.set(i, o);
    for(int j = 0; j < b.length){
        for(int k = 0; k < b.get(j).length){
            if(b.get(j).get(k).value() == o.value())
                bClone.get(j).set(k, o);
        }
    }
}
</code>```

This code checks after creating each clone if the old object is present in b, and replaces it with the clone. I imagine this would be extremely slow though, so does anyone know how this could be done more efficiently?

答:
You can do this by holding wrapper classes in the lists, not direct references to the objects, then you can clone the wrapped object by calling a method like:
myWrapper.cloneObject().

As the lists on b are holding the same references to the wrapper classes, when the objects are cloned, the 2 lists will be synchronized.

Body:
I have an arraylist a containing certain objects. I also have an arraylist b containing arraylists containing some of these objects. Now, I want to clone the arraylist a, while updating the references in arraylist b to the cloned objects. A pseudo code example:

a = [object1,object2,object3,object4,object5,object6,object7,object8,object9,object10];
b = [[object3,object8,object5],[object2,object9,object1][object6,object7]];

//now, I can clone arraylist a
aClone = [clone1,clone2,clone3,clone4,clone5,clone6,clone7,clone8,clone9,clone10];

//but how do I get a clone of arraylist b containing references to the clones of the objects like so:
bClone = [[clone3,clone8,clone5],[clone2,clone9,clone1][clone6,clone7]];

Something I came up with is the following:

for(int i = 0; i < a.length; i++){
    o = a.get(i);
    aClone.set(i, o);
    for(int j = 0; j < b.length){
        for(int k = 0; k < b.get(j).length){
            if(b.get(j).get(k).value() == o.value())
                bClone.get(j).set(k, o);
        }
    }
}

This code checks after creating each clone if the old object is present in b, and replaces it with the clone. I imagine this would be extremely slow though, so does anyone know how this could be done more efficiently?