Skip to content

Instantly share code, notes, and snippets.

@leommoore
leommoore / javascript_observer_pattern.md
Last active December 24, 2015 05:59
JavaScript - Observer Pattern

#JavaScript - Observer Pattern An array of subscribers are just callback functions

  • addSubscriber() and removeSubscriber() methods that add to and remove from the subscribers array
  • A publish() method that takes data and calls all subscribers, passing the data to them
  • A make() method that takes any object and turns it into a publisher by adding all of the above methods to it
var observer = {
@leommoore
leommoore / mongodb_basic_commands.md
Last active January 16, 2021 16:36
MongoDB - Basic Commands

#MongoDB - Basic Commands

##Saving Data

db  //Tells you the current database

show collections //Shows the collections available in the current db

db.foo.save({_id:1, x:10}) //Save the document into the foo collection  

db.bar.save({_id:1, x:10}) //Save the document into the bar collection

@leommoore
leommoore / mongodb_mongo_shell.md
Last active October 11, 2017 20:47
MongoDB - Mongo Shell

#MongoDB - Mongo Shell

###Single Task You can run commands in MongoDB from the command prompt by feeding the command into the mongo shell using:

mongo server1/admin --eval "db.runCommand({logRotate:1})"

mongo localhost:30000/admin --eval "db.runCommand({logRotate:1})"

This example will rotate the log file without remaining in the mongo shell. Note: admin refers to the admin database. Executing the command returns:

@leommoore
leommoore / mongodb_setting_up_a_replica_set.md
Last active December 8, 2021 09:58
MongoDB - Setting up a Replica Set

#MongoDB - Setting up a Replica Set

cd \mongodbdir\

mkdir db1
mkdir db2
mkdir db3

###Primary mongod --dbpath ./db1 --port 30000 --replSet "demo"

@leommoore
leommoore / mongodb_mapreduce.md
Last active May 2, 2021 20:29
MongoDB MapReduce

#MongoDB MapReduce

(See http://mapreduce.org/ for more information on MapReduce)

MapReduce is a programming framework popularized by Google and used to simplify data processing across massive data sets. As people rapidly increase their online activity and digital footprint, organizations are finding it vital to quickly analyze the huge amounts of data their customers and audiences generate to better understand and serve them. MapReduce is the tool that is helping those organizations.

Most enterprises deal with multiple types of data (text, rich text, rdbms, graph, etc…) and need to process all this data quickly and efficiently to derive meaningful insights that bring business value to the organization. With MapReduce, computational processing can occur on data stored either in a filesystem (unstructured) or within a database (structured).

There are two fundamental pieces of a MapReduce query:

@leommoore
leommoore / node_running_in_production.md
Last active November 21, 2021 00:50
Node - Running in Production

#Node - Running in Production This gist is based on the excellent post by @hacksparrow which is found at http://www.hacksparrow.com/running-express-js-in-production-mode.html. The main principle is that you want the application to detect that it is running on a production server and to use the production configuration. The way to do this is to set the NODE_ENV=production. To do this you need to do the following:

$ export NODE_ENV=production

But we have a little problem here. The NODE_ENV environment variable will be lost if the server restarts, so it is safer to put it in the .bash_profile file. That way the variable will set again every time the system reboots. You will find the file in your home directory. It's a hidden file, so you can't see it unless you do a ls -la. We will append the export command to the .bash_profile file.

@leommoore
leommoore / javascript_memoization.md
Last active August 1, 2022 08:26
JavaScript - Memoization

JavaScript - Memoization

Memoization is a programming technique which attempts to increase a function’s performance by caching its previously computed results. Because JavaScript objects behave like associative arrays, they are ideal candidates to act as caches. Each time a memoized function is called, its parameters are used to index the cache. If the data is present, then it can be returned, without executing the entire function. However, if the data is not cached, then the function is executed, and the result is added to the cache.

  • Memoization can potentially increase performance by caching the results of previous function calls.
  • Memoized functions store a cache which is indexed by their input arguments. If the arguments exist in the cache, then the - cached value is returned. Otherwise, the function is executed and the newly computed value is added to the cache.
  • Object arguments should be stringified before using as an index.
  • Memoization can be automatically applied to referentially transpa
@leommoore
leommoore / mongodb_basics.md
Last active May 17, 2018 11:33
MongoDB - Basics

#MongoDB - Basics

##Installation The Debian package management tool (i.e. dpkg and apt) ensure package consistency and authenticity by requiring that distributors sign packages with GPG keys. Issue the following command to import the 10gen public GPG Key:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10

Create a /etc/apt/sources.list.d/10gen.list file using the following command.

echo 'deb http://downloads-distro.mongodb.org/repo/debian-sysvinit dist 10gen' | sudo tee /etc/apt/sources.list.d/10gen.list

@leommoore
leommoore / linux_ssh_basics.md
Last active March 31, 2021 17:03
Linux - SSH (Secure SHell) Basics

#Linux - SSH (Secure SHell) Basics ssh is a secure mechanism to interact with a computer remotely.

##Logging on ssh allows a user to log in remotely to a server with a ssh server running (ie openSSHServer). For example:

ssh pi@192.168.1.2

or, using a domain name like:

@leommoore
leommoore / linux_log_file_monitoring.md
Last active December 17, 2015 11:39
Linux - Log File Monitoring

#Linux - Log File Monitoring

Logwatch is a really useful tool which normally runs as a cron job which mails a summary of the log files to root. It shows a summary of software installed, repeated authentication failures from programs such as sshd and su.

For example running:

logwatch --detail med --range Today --format text --output stdout