Docker – Setup git ssh server using docker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
FROM ubuntu:18.04
 
ENV TZ=Australia/Sydney
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
 
RUN apt-get update
RUN apt-get install -y bash git python3 g++ curl vim openssh-server sudo
 
 
RUN addgroup --gid 9999 git && adduser --uid 9999 --gid 9999 --shell /usr/bin/git-shell git
RUN addgroup --gid 9000 xxx && adduser --uid 9000 --gid 9000 --shell /bin/bash xxx
 
RUN echo git:password | chpasswd
RUN echo xxx:password | chpasswd
 
RUN mkdir -p /home/git
RUN mkdir -p /home/xxx
 
RUN chown -Rh git:git /home/git
 
COPY sshd_config /etc/ssh/sshd_config
 
EXPOSE 22
 
ENTRYPOINT service ssh restart && bash
 
CMD ["/bin/bash"]

1863. Sum of All Subset XOR Totals

key idea is how to generate all subset of array.

take 3 elements of arrays for example,

[a, _, _] -> [1, 0, 0]
[_, b, _] -> [0, 1, 0]
[a, b, _] -> [1, 1, 0]
[_, _, c] -> [0, 0, 1]
[a, _, c] -> [1, 0, 1]
[_, b, c] -> [0, 1, 1]
[a, b, c] -> [1, 1, 1]

integer i is loop through 1 to Math.pow(2, nums.length) – 1, for each integer, we check how many 1 bit

i & 1, if it is 1, we get nums[index] out as elements for subarray.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
class Solution {
    public int subsetXORSum(int[] nums) {
        int result = 0;
    	int x = (int)Math.pow(2, nums.length) - 1;
        for(int i=1;i<=x;i++) {
        	int p = i;
        	int r = 0;
        	for(int j=0;j<nums.length;j++) {
        		int t = p & 1;
        		p >>= 1;
 
        		if (t == 1) {
    				r ^= nums[j];
        		}
        	}
 
        	result += r;
        }
 
        return result;
    }
}

Git – How to sync with two remote repos using git

git remote -v
git remote
git remote add pb https://github.com/paulboone/ticgit

$ git remote -v
origin https://github.com/schacon/ticgit (fetch)
origin https://github.com/schacon/ticgit (push)
pb https://github.com/paulboone/ticgit (fetch)
pb https://github.com/paulboone/ticgit (push)

$ git fetch pb
remote: Counting objects: 43, done.
remote: Compressing objects: 100% (36/36), done.
remote: Total 43 (delta 10), reused 31 (delta 5)
Unpacking objects: 100% (43/43), done.
From https://github.com/paulboone/ticgit
* [new branch] master -> pb/master
* [new branch] ticgit -> pb/ticgit

$ git checkout master

$ git merge upstream/master # important, merge fetch code and master

$ git push

java – Spring Mergeable

There are 4 type of Mergeable Object in Spring

ManagedList
ManagedProperties
ManagedMap
ManagedSet

Take ManagedList as example

if mergeEnabled is set to be false, merge will stop
if parent is null, return itself.
and finally it will add parent firstly and then add children.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	@Override
	@SuppressWarnings("unchecked")
	public List<E> merge(@Nullable Object parent)
	{
		if (!this.mergeEnabled) {
			throw new IllegalStateException("Not allowed to merge when the 'mergeEnabled' property is set to 'false'");
		}
		if (parent == null) {
			return this;
		}
		if (!(parent instanceof List)) {
			throw new IllegalArgumentException("Cannot merge with object of type [" + parent.getClass() + "]");
		}
		List<E> merged = new ManagedList<>();
		merged.addAll((List<E>) parent);
		merged.addAll(this);
		return merged;
	}

java – method bridge

java reflection could get all methods of a class.

isBridge() could check whether method is bridged or not.

when we override parent class general type method in child class, method will become bridged.

here is example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package org.spring.main;
 
import java.lang.reflect.Method;
 
import org.springframework.core.BridgeMethodResolver;
 
public class BridgeMethodResolverEntry {
 
	public class Node<T> {
 
	    public T data;
 
	    public Node(T data) { this.data = data; }
 
	    public void setData(T data) {
	        System.out.println("Node.setData");
	        this.data = data;
	    }
	}
 
	public class MyNode extends Node<Integer> {
 
	    public MyNode(Integer data) { super(data); }
 
	    @Override
	    public void setData(Integer data) {
	        System.out.println("MyNode.setData");
	        super.setData(data);
	    }
	}
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		BridgeMethodResolverEntry x = new BridgeMethodResolverEntry();
		MyNode resolver = x.new MyNode(5);
		Method[] declaredMethods = resolver.getClass().getDeclaredMethods();
 
		for (int i = 0; i < declaredMethods.length; i++) {
		            Method declaredMethod = declaredMethods[i];
		            String methodName = declaredMethod.getName();
		            Class<?> returnType = declaredMethod.getReturnType();
		            Class<?> declaringClass = declaredMethod.getDeclaringClass();
		            boolean bridge = declaredMethod.isBridge();
		            System.out.print((i+1) + " method name is" + methodName + ", return type is " + returnType + "  ");
		            System.out.print(bridge ? " is Bridge Method" : " is not Bridge Method");
		            System.out.println("  it is declared at "+declaringClass.getSimpleName()+"");            
		}                        
 
	}
 
}

curl as postman cli

curl could be used as POSTMan CLI.

for PDF downloading restful API, curl actions better that postman

1
curl -v -X GET -u username:password https://<url>

Java – Static Initialization Block

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.dw.lib.test;
 
public class StaticInitializationBlock {
 
	static  
    {  
        System.out.println("STATIC BLOCK");  
    } 
 
	public StaticInitializationBlock() {
		System.out.println("constructor");
	}
 
	public static void main(String[] args) {
 
		StaticInitializationBlock block = new StaticInitializationBlock();
 
		System.out.println("MAIN METHOD");
 
	}
 
}

output:
STATIC BLOCK
constructor
MAIN METHOD

log4j src – parse log4j.xml building DocumentBuilder

log4j uses DocumentBuilder to parse log4j.xml

here is example code which is same as log4j original source code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
 
package main;
 
import java.io.*;
import java.net.*;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
 
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
 
public class DocumentBuilderEntry {
 
	public static void loop(Node node) {
	    // do something with the current node instead of System.out
	    System.out.println(node.getNodeName());
 
	    NodeList nodeList = node.getChildNodes();
	    for (int i = 0; i < nodeList.getLength(); i++) {
	        Node currentNode = nodeList.item(i);
	        if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
	            //calls this method for all the children which is Element
	        	loop(currentNode);
	        }
	    }
	}
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		URL url = DocumentBuilderEntry.class.getResource("log4j.xml");
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
		try {
			DocumentBuilder parser = factory.newDocumentBuilder();
			URLConnection uConn = url.openConnection();
 
			uConn.setUseCaches(false);
			InputStream stream = uConn.getInputStream();
			try {
			  InputSource src = new InputSource(stream);
			  src.setSystemId(url.toString());
			  Document doc = parser.parse(src);
			  loop(doc.getDocumentElement());
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
			  stream.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
 
 
 
	}
 
}

influxdb – influxdb query for if/else case

influxdb didn’t support if/else in select statement.

mysql could support if/else/case statement, for example.

1
2
3
4
5
6
7
   SELECT
      CASE <field>
         WHEN 1 THEN 100
      ELSE
         0
   FROM TABLE
   WHERE [condition]

However, influxdb didn’t support if/else in select statement.
we have to use a function map for if/else

for example,

input set is (1, 2, 3, 4, 5, 6, 7)
output set is (100, 0, 0, 0, 0, 0, 0)

we design a function f(x) = 100*FLOOR(1/x)

so f(1) = 100, f(2) = 0, … , f(7) = 0;

it could match whole input set and output set

1
SELECT 100*FLOOR(1/FIELD) FROM 'measurement' WHERE <condition>

wordpress – wpdb init and generate all database schema

$wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST );

// very important, it will init all wpdb public/private attributes
// such as $wpdb->termmeta, without call this function , $wpdb->termmeta will be empty string.

wp_set_wpdb_vars();

// get all include user tables and blog tables
echo wp_get_db_schema(‘all’);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
CREATE TABLE wp_users (
  ID BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  user_login VARCHAR(60) NOT NULL DEFAULT '',
  user_pass VARCHAR(255) NOT NULL DEFAULT '',
  user_nicename VARCHAR(50) NOT NULL DEFAULT '',
  user_email VARCHAR(100) NOT NULL DEFAULT '',
  user_url VARCHAR(100) NOT NULL DEFAULT '',
  user_registered datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  user_activation_key VARCHAR(255) NOT NULL DEFAULT '',
  user_status INT(11) NOT NULL DEFAULT '0',
  display_name VARCHAR(250) NOT NULL DEFAULT '',
  PRIMARY KEY  (ID),
  KEY user_login_key (user_login),
  KEY user_nicename (user_nicename),
  KEY user_email (user_email)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;
CREATE TABLE wp_usermeta (
  umeta_id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id BIGINT(20) UNSIGNED NOT NULL DEFAULT '0',
  meta_key VARCHAR(255) DEFAULT NULL,
  meta_value longtext,
  PRIMARY KEY  (umeta_id),
  KEY user_id (user_id),
  KEY meta_key (meta_key(191))
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;
CREATE TABLE wp_termmeta (
  meta_id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  term_id BIGINT(20) UNSIGNED NOT NULL DEFAULT '0',
  meta_key VARCHAR(255) DEFAULT NULL,
  meta_value longtext,
  PRIMARY KEY  (meta_id),
  KEY term_id (term_id),
  KEY meta_key (meta_key(191))
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;
CREATE TABLE wp_terms (
 term_id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
 name VARCHAR(200) NOT NULL DEFAULT '',
 slug VARCHAR(200) NOT NULL DEFAULT '',
 term_group BIGINT(10) NOT NULL DEFAULT 0,
 PRIMARY KEY  (term_id),
 KEY slug (slug(191)),
 KEY name (name(191))
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;
CREATE TABLE wp_term_taxonomy (
 term_taxonomy_id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
 term_id BIGINT(20) UNSIGNED NOT NULL DEFAULT 0,
 taxonomy VARCHAR(32) NOT NULL DEFAULT '',
 description longtext NOT NULL,
 parent BIGINT(20) UNSIGNED NOT NULL DEFAULT 0,
 COUNT BIGINT(20) NOT NULL DEFAULT 0,
 PRIMARY KEY  (term_taxonomy_id),
 UNIQUE KEY term_id_taxonomy (term_id,taxonomy),
 KEY taxonomy (taxonomy)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;
CREATE TABLE wp_term_relationships (
 object_id BIGINT(20) UNSIGNED NOT NULL DEFAULT 0,
 term_taxonomy_id BIGINT(20) UNSIGNED NOT NULL DEFAULT 0,
 term_order INT(11) NOT NULL DEFAULT 0,
 PRIMARY KEY  (object_id,term_taxonomy_id),
 KEY term_taxonomy_id (term_taxonomy_id)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;
CREATE TABLE wp_commentmeta (
  meta_id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  comment_id BIGINT(20) UNSIGNED NOT NULL DEFAULT '0',
  meta_key VARCHAR(255) DEFAULT NULL,
  meta_value longtext,
  PRIMARY KEY  (meta_id),
  KEY comment_id (comment_id),
  KEY meta_key (meta_key(191))
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;
CREATE TABLE wp_comments (
  comment_ID BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  comment_post_ID BIGINT(20) UNSIGNED NOT NULL DEFAULT '0',
  comment_author tinytext NOT NULL,
  comment_author_email VARCHAR(100) NOT NULL DEFAULT '',
  comment_author_url VARCHAR(200) NOT NULL DEFAULT '',
  comment_author_IP VARCHAR(100) NOT NULL DEFAULT '',
  comment_date datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  comment_date_gmt datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  comment_content text NOT NULL,
  comment_karma INT(11) NOT NULL DEFAULT '0',
  comment_approved VARCHAR(20) NOT NULL DEFAULT '1',
  comment_agent VARCHAR(255) NOT NULL DEFAULT '',
  comment_type VARCHAR(20) NOT NULL DEFAULT '',
  comment_parent BIGINT(20) UNSIGNED NOT NULL DEFAULT '0',
  user_id BIGINT(20) UNSIGNED NOT NULL DEFAULT '0',
  PRIMARY KEY  (comment_ID),
  KEY comment_post_ID (comment_post_ID),
  KEY comment_approved_date_gmt (comment_approved,comment_date_gmt),
  KEY comment_date_gmt (comment_date_gmt),
  KEY comment_parent (comment_parent),
  KEY comment_author_email (comment_author_email(10))
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;
CREATE TABLE wp_links (
  link_id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  link_url VARCHAR(255) NOT NULL DEFAULT '',
  link_name VARCHAR(255) NOT NULL DEFAULT '',
  link_image VARCHAR(255) NOT NULL DEFAULT '',
  link_target VARCHAR(25) NOT NULL DEFAULT '',
  link_description VARCHAR(255) NOT NULL DEFAULT '',
  link_visible VARCHAR(20) NOT NULL DEFAULT 'Y',
  link_owner BIGINT(20) UNSIGNED NOT NULL DEFAULT '1',
  link_rating INT(11) NOT NULL DEFAULT '0',
  link_updated datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  link_rel VARCHAR(255) NOT NULL DEFAULT '',
  link_notes mediumtext NOT NULL,
  link_rss VARCHAR(255) NOT NULL DEFAULT '',
  PRIMARY KEY  (link_id),
  KEY link_visible (link_visible)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;
CREATE TABLE wp_options (
  option_id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  option_name VARCHAR(191) NOT NULL DEFAULT '',
  option_value longtext NOT NULL,
  autoload VARCHAR(20) NOT NULL DEFAULT 'yes',
  PRIMARY KEY  (option_id),
  UNIQUE KEY option_name (option_name)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;
CREATE TABLE wp_postmeta (
  meta_id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  post_id BIGINT(20) UNSIGNED NOT NULL DEFAULT '0',
  meta_key VARCHAR(255) DEFAULT NULL,
  meta_value longtext,
  PRIMARY KEY  (meta_id),
  KEY post_id (post_id),
  KEY meta_key (meta_key(191))
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;
CREATE TABLE wp_posts (
  ID BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  post_author BIGINT(20) UNSIGNED NOT NULL DEFAULT '0',
  post_date datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  post_date_gmt datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  post_content longtext NOT NULL,
  post_title text NOT NULL,
  post_excerpt text NOT NULL,
  post_status VARCHAR(20) NOT NULL DEFAULT 'publish',
  comment_status VARCHAR(20) NOT NULL DEFAULT 'open',
  ping_status VARCHAR(20) NOT NULL DEFAULT 'open',
  post_password VARCHAR(255) NOT NULL DEFAULT '',
  post_name VARCHAR(200) NOT NULL DEFAULT '',
  to_ping text NOT NULL,
  pinged text NOT NULL,
  post_modified datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  post_modified_gmt datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  post_content_filtered longtext NOT NULL,
  post_parent BIGINT(20) UNSIGNED NOT NULL DEFAULT '0',
  guid VARCHAR(255) NOT NULL DEFAULT '',
  menu_order INT(11) NOT NULL DEFAULT '0',
  post_type VARCHAR(20) NOT NULL DEFAULT 'post',
  post_mime_type VARCHAR(100) NOT NULL DEFAULT '',
  comment_count BIGINT(20) NOT NULL DEFAULT '0',
  PRIMARY KEY  (ID),
  KEY post_name (post_name(191)),
  KEY type_status_date (post_type,post_status,post_date,ID),
  KEY post_parent (post_parent),
  KEY post_author (post_author)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;