http://www.pymcu.com/index.html
References pool - Milestones - Notebook
git log
that is our friend, but not all the time.git ls-files
is the answer.# Find all files with the name security in the path. $ git ls-files | grep security lib/dynamo-db/security.js test/security-test.js
grep
options you prefer, like -i
git grep
is your friend.git grep
works similar to a recursive grep grep -r .
but it only# Find all lines matching *crypt* in current version. $ git grep crypt lib/dynamo-db/security.js:var crypto = require('crypto'); lib/dynamo-db/security.js: var hmac = crypto.createHmac('sha256', key); # Also give me the line numbers git grep -n crypt lib/dynamo-db/security.js:2:var crypto = require('crypto'); lib/dynamo-db/security.js:15: var hmac = crypto.createHmac('sha256', key); # List only the file names git grep -l crypt lib/dynamo-db/security.js # Also list how many times (count) it matched. $ git grep -c crypt lib/dynamo-db/security.js:2
grep
to find out what has# Find all files with lines matching *type* in revisions `master` and `8f0fb7f`. git grep -l type master 8f0fb7f master:lib/dynamo-db/index.js master:lib/dynamo-db/security.js master:package.json 8f0fb7f:lib/dynamo-db/index.js 8f0fb7f:package.json
grep
, find
, ack
, and friends. But Git is a versiongit log
to the rescue.git log
shows you the commit logs. It is your eye into the past.# When (in what commit) was my file deleted? $ git log --diff-filter=D -- test/create-table-test.js commit ba6c4d8bc165b8fb8208979c3e5513bd53477d51 Author: Anders Janmyr <anders@janmyr.com> Date: Wed Jan 25 09:46:52 2012 +0100 Removed the stupid failing test.
--summary
option.# When (in what commit) was my file deleted? $ git log --diff-filter=D --summary -- test/create-table-test.js commit ba6c4d8bc165b8fb8208979c3e5513bd53477d51 Author: Anders Janmyr <anders@janmyr.com> Date: Wed Jan 25 09:46:52 2012 +0100 Removed the stupid failing test. delete mode 100644 test/create-table-test.js
test/create-table-test.js
- The filename has to be the relative path--
- The double-dash is used to tell Git that this is not a branch--summary
- Show me what files were deleted or added.--name-status
is similar.--diff-filter
- This is a real beauty, it allows me to limit the logD
, for# Find out when the integration tests where added. $ git log --diff-filter=A --name-status |grep -C 6 integ commit 09420cfea8c7b569cd47f690104750fec358a10a Author: Anders Janmyr <anders@janmyr.com> Date: Tue Jan 24 16:23:52 2012 +0100 Extracted integration test A integration-test/sts-test.js commit 205db3965dec6c2c4c7b2bb75387a591d49e1951 Author: Anders Janmyr <anders@janmyr.com> Date: Sat Jan 21 10:03:59 2012 +0100
--name-status
as a variation on--summary
, it uses the same notation as the --diff-filter
.grep -C 6
to get some context around the found element, ingit blame
$ git blame test/security-test.js 205db396 (Anders Janmyr 2012-01-21 10:03:59 +0100 1) var vows = require('vows'), 205db396 (Anders Janmyr 2012-01-21 10:03:59 +0100 2) assert = require('assert'); 205db396 (Anders Janmyr 2012-01-21 10:03:59 +0100 3) 09420cfe (Anders Janmyr 2012-01-24 16:23:52 +0100 4) var access = 'access'; 09420cfe (Anders Janmyr 2012-01-24 16:23:52 +0100 5) var secret = 'secret'; 90b65208 (Anders Janmyr 2012-01-21 11:58:21 +0100 6) 205db396 (Anders Janmyr 2012-01-21 10:03:59 +0100 7) var security = new Security({ 90b65208 (Anders Janmyr 2012-01-21 11:58:21 +0100 8) access: access, 90b65208 (Anders Janmyr 2012-01-21 11:58:21 +0100 9) secret: secret 205db396 (Anders Janmyr 2012-01-21 10:03:59 +0100 10) }); ...
git blame --reverse
.# Check the what lines have been changed since the last 6 commits. $ git blame --reverse head~6..head security-test.js 558b8e7f (Anders Janmyr 2012-01-25 16:53:52 +0100 1) var vows = require('vows'), 558b8e7f (Anders Janmyr 2012-01-25 16:53:52 +0100 2) assert = require('assert'); 558b8e7f (Anders Janmyr 2012-01-25 16:53:52 +0100 3) 093c13e9 (Anders Janmyr 2012-01-24 17:26:09 +0100 4) var Security = require('dynamo-db').Security; ba6c4d8b (Anders Janmyr 2012-01-25 09:46:52 +0100 5) ^b96c68b (Anders Janmyr 2012-01-21 12:33:50 +0100 6) var access = process.env['S3_KEY']; ^b96c68b (Anders Janmyr 2012-01-21 12:33:50 +0100 7) var secret = process.env['S3_SECRET']; 558b8e7f (Anders Janmyr 2012-01-25 16:53:52 +0100 8) 558b8e7f (Anders Janmyr 2012-01-25 16:53:52 +0100 9) var security = new Security({ 558b8e7f (Anders Janmyr 2012-01-25 16:53:52 +0100 10) access: access, 558b8e7f (Anders Janmyr 2012-01-25 16:53:52 +0100 11) secret: secret 558b8e7f (Anders Janmyr 2012-01-25 16:53:52 +0100 12) });
093c13e9
and ba6c4d8b
^b96c68b
havegit log -S<string>
orgit log -G<regex>
# Find commits that modified the string aws and display the full diff. $ git log -Saws --diff-filter=M --patch commit b96c68b839f204b310b79570bc3d27dc93cff588 Author: Anders Janmyr <anders@janmyr.com> Date: Sat Jan 21 12:33:50 2012 +0100 We have a valid request, tjohoo diff --git a/lib/dynamo-db/security.js b/lib/dynamo-db/security.js index bee6936..8471527 100644 --- a/lib/dynamo-db/security.js +++ b/lib/dynamo-db/security.js @@ -2,6 +2,7 @@ var crypto = require('crypto'); var _ = require('underscore'); var request = require("request"); +var xml2js = require('xml2js'); function Security(options) { this.options = options; @@ -23,7 +24,7 @@ mod.timestamp = function() { mod.defaultParams = function() { return { AWSAccessKeyId: this.options.access, - Version: '2010-05-08', + Version: '2011-06-15', Timestamp: this.timestamp(), SignatureVersion: 2, SignatureMethod: 'HmacSHA256' @@ -57,9 +58,10 @@ mod.url = function(host, path, params) { mod.makeRequest = function(method, host, path, params, callback) { var extParams = _.extend({}, this.defaultParams(), params); - var signedParams = this.signedParams('GET', 'iam.amazonaws.com', '/', extParams); - console.log(extParams,signedParams); - return request({ method: method, url: this.url(host, path, signedParams) }, + var signedParams = this.signedParams(method, host, path, extParams); + var url = this.url(host, path, signedParams); + console.log(url,signedParams); + return request({ method: method, url: url }, ...
var t = Task.Factory.StartNew(someDelegate);is functionally equivalent to:
var t = new Task(someDelegate);Performance-wise, the former is slightly more efficient. As mentioned in response to question #3, Start employs synchronization to ensure that the Task instance on which Start is being called hasn’t already been started, or isn’t concurrently being started. In contrast, the implementation of StartNew knows that no one else could be starting the Task concurrently, as it hasn’t given out that reference to anyone… so StartNew doesn’t need to employ that synchronization.
t.Start();
Task theTask = null;Spot the flaw? There’s a race. During the call to Task.Run, a new Task object is created and is queued to the ThreadPool scheduler. If there’s not that much going on in the ThreadPool, a thread from the pool might pick it up almost instantly and start running it. That thread is now racing to access the variable ‘theTask’ with the main thread that called Task.Run and that needs to store the created Task into that ‘theTask’ variable. I can fix this race by separating the construction and scheduling:
theTask = Task.Run(() => Console.WriteLine(“My ID is {0}.”, theTask.Id));
Task theTask = null;Now I’m now sure that the Task instance will have been stored into the ‘theTask’ variable before the ThreadPool processes the Task, because the ThreadPool won’t even get a reference to the Task until Start is called to queue it, and by that point, the reference has already been set (and for those of you familiar with memory models, the appropriate fences are put in place by Task to ensure this is safe).
theTask = new Task(() =>Console.WriteLine(“My ID is {0}.”, theTask.Id));
theTask.Start(TaskScheduler.Default);
static void Main(string[] args) { // Some initialization of the DB... Task<int> t = GetContentFromDatabase(); // Execute some other code when the task is done t.ContinueWith(r => Console.WriteLine(r.Result)); Console.ReadLine(); } public static async Task<int> GetContentFromDatabase() { int source = 22; // Run starts the execution on another thread var result = (int) await Task.Run( () => { // Simulate DB access Thread.Sleep(1000); return 10; } ); return source + result * 2; }
public static void Main() { MySyncMethod(result => Console.WriteLine(result)); Console.ReadLine(); } public static void GetContentFromDatabase (Action<int> continueWith) { // The first half of the async method (with QueueUserWorkItem) int source = 22; // The second half of the async method Action<int> onResult = result => { continueWith(source + result * 2); }; ThreadPool.QueueUserWorkItem( _ => { // Simulate DB access Thread.Sleep(1000); onResult(10); } ); }
static void Main(string[] args) { IObservable<int> query = GetContentFromDatabase(); // Subscribe to the result and display it query.Subscribe(r => Console.WriteLine(r)); Console.ReadLine(); } public static IObservable<int> GetContentFromDatabase() { int source = 22; // Start the work on another thread (using the ThreadPool) return Observable.Start<int>( () => { Thread.Sleep(1000); return 10; } ) // Project the result when we get it .Select(result => source + result * 2); }
public static void Main() { // Use the observable we've defined before var query = GetContentFromDatabase(); // Once we get the token from the database, transform it first query.Select(r => "Token_" + r) // When we have the token, we can initiate the call to the web service .SelectMany(token => GetFromWebService(token)) // Once we have the result from the web service, print it. .Subscribe(_ => Console.WriteLine(_)); } public static IObservable<string> GetFromWebService(string token) { return Observable.Start( () => new WebClient().DownloadString("http://example.com/" + token) ) .Select(s => Decrypt(s)); }
public interface IFavoritesService { IObservable<Unit> AddFavorite(string name, string value); IObservable<bool> RemoveFavorite(string name); IObservable<string[]> GetAllFavorites(); }