pretty code

2018年3月2日 星期五

MongoDB PHP example

最近需要測試一下 MongoDB
雖然我很喜歡 Go 語言
但還是直覺認為用 PHP 會比較方便
 
不過網路上的文章大部份都是舊的 PHP Driver 寫法(class MongoClient)
故花點時間把它整理一下

1. 新的用法有分 Low, High Level,High Level 的 code 比較精簡,但兩個都要裝
2. High Level - https://github.com/mongodb/mongo-php-library
3. Low Level - https://github.com/mongodb/mongo-php-driver
4. 兩者都是官方 release 的(官方也有提供其他語言的 Library)
5. 我使用的版本 PHP 7.0.8, MongoDB 3.6

官方說明

High Level - Query

require 'vendor/autoload.php';

$connectStr = 'mongodb://user:password@localhost:27017/databaseName';

try {
    $client = new MongoDB\Client($connectStr);

    $collection = $client->databaseName->collectionName;
    
    $records = $collection->find([]);

    foreach ($records as $item) {
        echo "_id = $item->_id \n";
    }

} catch (MongoDB\Driver\Exception\ConnectionTimeoutException $e) {
    echo $e->getMessage() . "\n";
}

High Level - Insert

require 'vendor/autoload.php';

$connectStr = 'mongodb://user:password@localhost:27017/databaseName';

try {
    $client = new MongoDB\Client($connectStr);

    $collection = $client->databaseName->collectionName;
    $collection->insertOne(['b' => 'CCC']);

} catch (MongoDB\Driver\Exception\ConnectionTimeoutException $e) {
    echo $e->getMessage() . "\n";
}


2018年3月1日 星期四

Rufus dos 冷知識

Rufus 是一個製作 dos 開機碟的工具。

原本以為 Rufus 是把開機相關檔案包進 .h 檔案裡,後來看到網路上的文章,才知道 Rufus 的 MS-DOS 是從 Windows 裡的檔案解壓縮出來的,﹝C:\Windows\system32\diskcopy.dll﹞。

這也解釋了為什麼在 Windows 10 下無法製作 MS-DOS 開機碟,因為 Windows 10 已經沒有這個檔案了。

src/dos.c

/*
 * Rufus: The Reliable USB Formatting Utility
 * DOS boot file extraction, from the FAT12 floppy image in diskcopy.dll
 * (MS WinME DOS) or from the embedded FreeDOS resource files
 * Copyright © 2011-2017 Pete Batard 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

/* Memory leaks detection - define _CRTDBG_MAP_ALLOC as preprocessor macro */

2018年2月27日 星期二

debug binary 檔案

有時候在 parsing log 時,可能因為 log 很大,故開啟時需要很久的時間﹝VIM 在開幾百 MB 檔案時的速度還可以﹞。

如果我們有類似底下的工具,在 Windows 下工作起來就會很方便,可以指定要顯示的行數,幫助我們快速看到內容,使用 C 語言開發不到 100 行即可。


2018年2月26日 星期一

grep binary 檔案

使用 grep parse log 時
如果 log 裡面有非 ASCII 字元時,比如說 0x00
此時 grep 會誤認為檔案為 binary 檔案
故無法 line by line 顯示

此時只要簡單加上 --text 選項即可

MongoDB connection URI

如果 MongoDB 有設定 auth 認證
連線字串大概會類似下面這樣
mongodb://username:password@localhost:27017

由於此字串是用 '@' 來分隔帳密設定及伺服器 IP
假設密碼也有用到 '@' 就會造成切割字串的問題

此時只要遵照 URI encode 的規則
將特殊字元 encode 即可

不過不同程式語言 API 的實做可能不同
PHP 只要 encode 特殊字元即可
其他語言也許是用 options array 的方式來設定帳密
這部份就得參考各 API 的說明文件

2018年2月14日 星期三

Init MongoDB database with javascript

MongoDB Version:3.6

Steps:
1. run mongod
2. run mongo
3. load('xxxx.js')
4. run "init()"

the content of xxxx.js

var d_admin = {
    user: 'd_admin',
    pwd: 'xxxx',
    roles: [
        {
            role: 'readWrite',
            db: 'test'
        }
    ]
}

var d_user = {
    user: 'd_user',
    pwd: 'xxxx',
    roles: [
        {
            role: 'read',
            db: 'test'
        }
    ]
}

var admin = {
    user: 'root',
    pwd: 'xxxx',
    roles: [
        {
            role: 'root',
            db: 'admin'
        }
    ]
}

function init() {
    db = connect('localhost:27017');
    db = db.getSiblingDB('admin');
    db.createUser(admin);

    db = db.getSiblingDB('test');
    db.createCollection('collection1');
    db.createCollection('collection2');
    db.createUser(d_admin);
    db.createUser(d_user);
    db.getLastError();
}

2018年1月3日 星期三

UEFI NVRAM 小研究

NVRAM is non-volatile RAM that is used in EFI to store variables that need to persist between boots. 

BIOS 選項理論上應該是儲存在 NVRAM 裡,UEFI Shell 有一個 command "dmpstore",我們可以利用它來看目前有那些變數。

另外,也許可以利用下面服務及 function 來動態存取變數。

    //
    // Variable Services
    //
    Status = RowPointer ( SocketFD,
                          pPort,
                          "GetVariable",
                          (VOID *)gRT->GetVariable,
                          NULL );
    if ( EFI_ERROR ( Status )) {
      break;
    }
    Status = RowPointer ( SocketFD,
                          pPort,
                          "GetNextVariableName",
                          (VOID *)gRT->GetNextVariableName,
                          NULL );
    if ( EFI_ERROR ( Status )) {
      break;
    }
    Status = RowPointer ( SocketFD,
                          pPort,
                          "SetVariable",
                          (VOID *)gRT->SetVariable,
                          NULL );
    if ( EFI_ERROR ( Status )) {
      break;
    }

這篇文章是 kurtqiao 介紹如何調整 BootOrder 的文章,可以參考。

2019/06/19 更新
可以參考我的最新實驗。