- Advent of Code 2021 Day 1
- GCP Pub/Sub Local Emulator
- Higher Order Function 小記
- Linux 2、1、& 和 >
- Bitwise Operators
Blog: Blackdiz's Garage
sudo pacman -S docker
sudo docker run hello-world
測試是否安裝成功sudo docker run hello-world
時, 若出現docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
錯誤訊息, 可用 sudo systemctl restart docker
重啟docker後再重新測試docker
指令都需要加上sudo
。解決方法是建立一個docker
group, 給予root
權限, 把user加入該group中, 指令如下:sudo groupadd docker // 新增docker group
sudo usermod -aG docker $USER // 將目前登入的user加入docker group
之後重新登入一次即可
docker ps
列出所有執行中的docker containerdocker ps -a
列出執行過的docker containerdocker run -it
可執行docker container並進入該container的termial中docker rm $(docker ps -a -q -f status=exited)
可刪除status=exited
的container, -q
表示讓docker ps
只回傳container id, -f
則是可指定過濾條件, 如status=exited
, 或者使用docker container prune
可達成相同效果docker run --rm
會在結束執行container後自動刪除該container^
: xor操作符,用在2進位上是把0和1倒轉,所以如果用在if判斷上,則表示若非全true或全false,則回傳0(true),否則回傳1(false),如:(a && !b) || (!a && b)
,可改為a ^ b
。但JS的xor並非logic xor,所以如果^
兩端是數字則會做位元操作,而非boolean判斷
Math.round()
和Number.toFixed(0)
差別在
Math.round()
回傳number,Number.toFixed(0)
回傳stringMath.round()
結果是朝0趨近,Number.toFixed(0)
是相反,如-1.5用Math.round()
會是-1,Number.toFixed(0)
是-2Math.round()
比Number.toFixed(0)
快很多for...in
和for...of
差別在
for...in
是迭代物件的enumerable property,所以是for (const propertyName in object)
,須注意的是prototype的property也會被迭代到,所以使用上需要小心。此外for...in
在迭代時是無序的for...of
是迭代有iterable屬性物件裡Symbol.iterator
指定的value,所以是for (const value in object)
,for...of
是ES6才加入的語法!!
: 可以轉出真正的true或false,如: let test = ""
;,test為falsy但不為boolean的true,因此可以用!!
轉成真正的false,第一個!
會把falsy轉成boolean的true,第二個!
則將其轉回正確的false值
Javascript的Set
在檢查相等性時是用===
,所以用在String
、Number
等premitive type時沒有問題,但用在Object
時是比較兩者的reference是否相等,而非內容值比較
在Promise
中return一個Promise
時,會先resolve return的Promise
等到跑完它的結果,resolve
的話再把結果傳到下一個then
中.
reference You dont know JS CH3:
The key to making a Promise sequence truly async capable at every step is to recall how Promise.resolve(..) operates when what you pass to it is a Promise or thenable instead of a final value. Promise.resolve(..) directly returns a received genuine Promise, or it unwraps the value of a received thenable -- and keeps going recursively while it keeps unwrapping thenables.
The same sort of unwrapping happens if you return a thenable or Promise from the fulfillment (or rejection) handler. Consider:
var p = Promise.resolve( 21 );
p.then( function(v){
console.log( v ); // 21
// create a promise and return it
return new Promise( function(resolve,reject){
// fulfill with value `42`
resolve( v * 2 );
} );
} )
.then( function(v){
console.log( v ); // 42
} );
Even though we wrapped 42 up in a promise that we returned, it still got unwrapped and ended up as the resolution of the chained promise, such that the second then(..) still received 42.
分散式系統一定會面臨CAP問題,下面簡述CAP各自代表的意義:
C - Consistency,一致性,指不同node間的資料在任一時間點都保持一致。完美的一致性是不存在的,因為不管複寫有多快,總是需要花費時間。
A - Availability,可得性,指不同node在收到request時總是能回覆有意義的response,所以如:500 Server Internal Error並不算在內。
P - Partition Tolerance,分區可容忍性,指不同node間可允許通訊中斷,通訊中斷有兩種意思,網路中斷或者node中有node無法正常response都算。
CAP問題是指,在CAP中,你永遠只能滿足其中兩個,也就是CA、CP、AP三者擇一。
設想有兩個node:A、B:
CA:如果A、B的資料須保持一致,且兩者都必須對request正常response,則A、B間的通訊就不可中斷,也就是犧牲P。
CP:如果A、B的資料須保持一致,但又允許兩者中斷通訊,那就表示一但中斷通訊,A、B都不能再繼續對外response,也就是犧牲A(Availability)。
AP:如果A、B可以中斷通訊且繼續對外response,那A、B間的資料就不可能保持一致,所以就是犧牲C。
因為在現實世界中,完全不發生通訊中斷是不可能的,所以其實我們只能選擇CP或AP,CA其實是理論中的選項。
參考資料
https://codahale.com/you-cant-sacrifice-partition-tolerance/
npm script可以加上pre
、post
hook,在指定的script前後執行,加上的方法是在名字前加上pre
或post
,如:
presetup: run pre hook;
setup: run main script;
postsetup: run post hook;
package.json
中可以用$npm_package_$key
來取定義好的變數值,$key
可以多層,例如:
{
"version": "1.2.3",
"config" {
"test" : "123"
}
然後就可以用$npm_package_config_key
來取config的test的值,或者$npm_package_version
取version的值
Express
/api/:id
/api/login
/api/:id
會被優先使用/api/login
會先於/api/:id
,因此順序不影響VSCode設定
jsconfig.json
以設定讓VSCode認得到的NODE_PATH
,內容為:baseUrl
設定好後,project裡的require
路徑會自動從baseUrl
開始,因此就算不使用相對路徑也可讓VSCode自動偵測到正確的檔案All versions of nginx as of 1.4.4 rely on OpenSSL for input parameters to Diffie-Hellman (DH). Unfortunately, this means that Ephemeral Diffie-Hellman (DHE) will use OpenSSL's defaults, which include a 1024-bit key for the key-exchange. Since we're using a 2048-bit certificate, DHE clients will use a weaker key-exchange than non-ephemeral DH clients.
所以可以先用openssl產生新的2048-bit或4096-bit加密key:
openssl dhparam -out /to/path/dhparam.epm 2048
然後在Nginx的config中加入:
ssl_dhparam /to/path/dhparam.pem;
讓Nginx使用新的key
="..."
,如:="0911111111"
,這樣在parse時就會保持原本的格式JAXBElement<String>
的方法,在跑wsdl2java時加上-b /to/path/jaxb-bindings.xml
,jaxb-bindings.xml
的內容可參照jaxb-bindings.xml,這樣產生出來的type就會變成String
了<a href>...</a>
的語法findById
、find
...等method,若找無資料是回傳null
findByIdAndRemove
...等function,若找無資料回傳null
,若有資料且刪除後則回傳被刪的資料save()
時會呼叫,如果要在update前使用則除非用支援{runValidators: true}
的function如:findByIdAndUpdate
、findOneAndUpdate
等functiontrue
到Number
type會變成1,傳入false
會變成0 etc.mongoose.Promise=global.Promise
,這行是把mongoose內的Promise
替換成Nodejs的Promise
mongoose.set('debug', true)
,可以在log中顯示實際跑的querypopulate
是對每個ref document分開query,而非像RDB的joincreate
時會預設給[]
空陣列值cast
function將傳入的資料轉型。 date
中的cast
有這段: if (value instanceof Number || typeof value === 'number'
|| String(value) == Number(value)) {
// support for timestamps
date = new Date(Number(value));
} else if (value.valueOf) {
// support for moment.js
date = new Date(value.valueOf());
}
故直接傳入moment
Object,mongoose會自動轉成JS的Date
Object
Model
的index
event,如果在建立index時有error,就可以做處理,eg.: Test.on('index', (err) => {
console.log('start create index');
if (err) {
console.error(err);
} else {
console.log('success');
}
lean()
,則回傳的不是model而是javascript object,因此可以修改document.toObject()
將model轉換成javascript objectfindAndModify
和update
最大差別在update後回傳的物件,findAndModify
回傳的是該document,預設回傳修改前的值,如果加上{new: true}
則回傳修改後的。update
回傳的是修改結果{ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }
bulkWrite
一次update在10000筆資料下,時間約是2分鐘和40秒的差距(測試機器: MacBook Pro (Retina, 13-inch, Early 2015)、CPU: 2.7 GHz Intel Core i5、RAM: 16G、HD: SSD)update
預設行為是把原document整個取代掉,如果只是要更新特定欄位要加上{$set: {field: value}}
$exists:true, $ne: []
,可得到正確結果且如果該欄位有建index也會使用indexdb.collection.ensureIndex({'array.0': 1})
,如此在使用 array.0
做為查詢條件時可以用到index,但如果是建db.collection.ensureIndex({array: 1})
則不會用indexsort
,最好的index是包含查詢條件和排序條件,如find({name: 'test'}).sort({age: 1})
的話,則建db.collection.ensureIndex({name: 1, age: 1})
則為最佳index,如果分開建立則可能是只用name
index 查詢,query explain時會看到 SORT_KEY_GENERATOR
表示會在memory中做排序,效率較差status
、response
。status
的格式為{"error": true/false", "operation": $OperationType, "statusCode": $statusCode}
,response
的格式為{"timetoken": $millisecond}
message
可以是任何可被serialize的object,但官網建議使用JSON格式,每個message
最大為32KBmessage
透過channel
傳送,所有註冊該channel的subscriber
都會接收到message
。subscriber
可註冊多個channel
。channel
可以動態建立或刪除。channel
是透過同一對Publish key和Subscribe key做區別,所以即使同名的channel
,如果key組合不同則為不同channel
channel
的名字最多為92字channel group
需要開啟stream controller功能才可使用A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.