ARTS WEEK 8

simplest and powerful Productivity Technique & share for docker compose

Posted by Eric Jin on 2019-10-19

Algorithm

Description

Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.

We define an array is non-decreasing if array[i] <= array[i + 1] holds for every i (1 <= i < n).

Example 1:
Input: [4,2,3]
Output: True
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
Example 2:
Input: [4,2,1]
Output: False
Explanation: You can't get a non-decreasing array by modify at most one element.
Note: The n belongs to [1, 10,000].

solution

思路:当我们发现 num[i-1]<num[i]的时候,有两种处理方法:

  1. 把前者改成小于等于当前数
  2. 把当前数改成大于等于前者

如果i-2(或者不存在的时候)小于等于i,我们可以把i-1改为i的值;

如果i-2大于i ,我们就把i改成i-1的值。

public boolean checkPossibility(int[] nums) {
    if (nums == null) return false;
    int len = nums.length;
    if (len == 1) return true;
    int modify = 0;
    for (int i = 1; i < len && modify <=1; i++) {
        if (nums[i] < nums[i-1]) {
            modify++;
            if (i-2 < 0 || nums[i-2] <= nums[i]) nums[i-1] = nums[i];
            else nums[i] = nums[i-1];
        }
    }
    return modify <= 1;
}

Review

review 一篇关于效率的文章,我也非常认同。
The Most Underrated Productivity Technique Is Also the Simplest

这篇文章讲一个被低估的提升效率的技术,同时也是最简单的。
作者开头说,它有一次听说洗冷水澡可以对抗拖延症,但是他洗了之后并没有用。

There was a time when I took cold showers because I heard it would help me combat procrastination. (It did not.)

就如当我们想要学习,想要规划自己的生活的时候,总是会先去找一些时间管理的方法,GTD, 番茄时钟等等的方法来规划自己的时间。
然而最后的结果确实,真正想要最的事情没有做好,却学了一堆时间管理方法。作者也如是说。

only stalling me from the real work of actually getting things done.

所以其实最简单,最直接,最有效的生产力技术就是:

  1. 直接让你的肉体先进入你要做的事的地方(别想,直接干)
  2. 先从小事情开始进入。
  1. Physically get in your work position.
  2. Do one small thing.

这是简单,但是很多人都会说,我要是能让身体坐下来看书,学习,我不就不用找管理方法了嘛。但是,其实你需要的仅仅只是让你的肉体先于你的思想。因为我们要明白,思想和身体是会相互作用的。我们思前想后,玩玩手机,刷刷微博,刷刷抖音,身体的注意力同时也被打散了。需要的就是坐下来,先做一小步。

这个时候你会发现其实身体的影响力是很大的,它会反作用于思想,你会发现你会很快可以好好做你想要做的事情!

然后你就只需要每天这样做就行了。

“We are what we repeatedly do. Excellence, then, is not an act, but a habit.”

然后还有一个Non-Zero Day principle,这个核心思想就是,即使每天只是一点点,也不要让今天是0。

“Didn’t do anything all fucking day and it’s 11:58 PM? Write one sentence. One push up. Read one page of that chapter. One. Because one is non zero.”

如果你晚上11:58了还啥事没做,写一句话,一个提交,读一页书。因为只要有1,就不是0!!! 非常精辟的话语!

We can talk about the newest productivity systems all day, but doing so won’t get the real work done.

我们可以聊一整天最新的生产效率方面的东西,但是这样并不能让我们真正的事情做完!

总而言之,身体先行,先做一点,身体会马上作用思想,然后整个人就调整状态开始做事!

Tips

关于JPA 中@DynamicInsert@DynamicUpdate

首先,这两个annotation都是类级别的。

@DynamicInsert
表示insert对象的时候,生成动态的insert语句,如果这个字段的值是null就不会加入到insert语句当中。

如果你没有添加这个annotation,在使用hibernate 的时候,用sql给某个字段添加了默认值,此时你去操作这个entity的时候,如果该字段为空,那么insert到数据库它还会是空,而不会是sql中设定的默认值。看一看hibernate 生成的sql:

Hibernate: insert into Cat (create_time, update_time, cat_name, id) values (?, ?, ?, ?)

hibernate会对所有字段进行插入,所以默认值的字段自然依旧是空。 添加@DynamicInsert 的结果是:

Hibernate: insert into Cat (cat_name, id) values (?, ?)

所以如果你想对某些值设置默认值,记得使用@DynamicInsert

@DynamicUpdate
表示update对象的时候,生产动态的update语句,如果这个字段没有被修改,就不会update这个字段。
当Entity没有注解@DynamicUpdate 时:

@Entity
public class Account {
 
    @Id
    private int id;
 
    @Column
    private String name;
 
    @Column
    private String type;
 
    @Column
    private boolean active;
 
    // Getters and Setters
}

这是repository

@Repository
public interface AccountRepository extends JpaRepository<Account, Integer> {
}

如果我们更新一个Accout 的名字

Account account = accountRepository.findOne(ACCOUNT_ID);
account.setName("Test Account");
accountRepository.save(account);

hibernate会产生如下sql:

update Account set active=?, name=?, type=? where id=?

如果我们给Entity 添加上@DynamicUpdate

@Entity
@DynamicUpdate
public class Account {
    // Existing data and methods
}

其他都不变,这时候产生的sql 只会包含修改的字段:

update Account set name=? where id=?

所以@DynamicUpdate是怎么运作的呢?
它是如何知道你跟数据库相比修改了那几个字段呢? 这是因为hibernate会跟踪当前entity的状态,当我们更新某个字段的时候,它会跟当前entity的状态进行比较。这就意味着@DynamicUpdate有这个多余的性能消耗。 一般只有当你的entity有很多的字段,而你经常只有少数几个字段需要更新的时候,这个时候去使用@DynamicUpdate注解。

Share

最近看了docker入门指南,然后刷了部分docker 的官方稳定,根据自己遇到的问题,主要是network,volumn 方面的。所以这方面的指令,还有详细介绍看了下,还细看了docker compose方面的东西,自己也把自己的blog,还有平时练手的小东西都通过docker compose部署到了服务器上。 今天先分享一个medium上的文章。后面自己在写一篇怎么把自己的服务docker 化,以及通过gitmaven实现自动部署。以后有时间再接入jenkins

Using Docker Compose to Run Your Applications