Skip to content

Instantly share code, notes, and snippets.

Soheil Hassas Yeganeh soheilhy

Block or report user

Report or block soheilhy

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View mongodb-node.md

MongoDB + NodeJS Tutorial

Prerequisites

  • You have node.js and npm installed on your machine.
  • If you use CDF:
    • Use bash or zsh as your shell.
    • Make sure npm installs packages locally as you don't have root permissions. To do so, you can edit ~/.npmrc and add the following line:
      • prefix=$NODE_HOME
    • $NODE_HOME is the directory in which you have installed node.js.
@soheilhy
soheilhy / queue.go
Last active Aug 29, 2015
Basic structures in taskq
View queue.go
// Queue represents a named queue.
type Queue string
// Task represents a task in a queue.
type Task struct {
ID uint64 `json:"id"` // Task's globally unique ID assigned by taskq.
Queue Queue `json:"queue"` // Task's queue.
Body []byte `json:"body"` // Task's client data.
}
@soheilhy
soheilhy / queue.go
Last active Aug 29, 2015
Messages in taskq
View queue.go
// Enqueue enqueus a task.
type Enque struct {
Task // The task to be enqueued.
}
// Deque represents a message emitted to dequeue a task from a queue.
type Deque struct {
Queue // The queue to dequeue a task from.
}
@soheilhy
soheilhy / queue.go
Last active Aug 29, 2015
Skeleton of EnQHandler
View queue.go
import (
"github.com/kandoo/beehive"
)
// EnQHandler handles Enque messages.
type EnQHandler struct{}
func (h EnQHandler) Rcv(msg beehive.Msg, ctx beehive.RcvContext) error {...}
func (h EnQHandler) Map(msg beehive.Msg, ctx beehive.MapContext) beehive.MappedCells {...}
@soheilhy
soheilhy / queue.go
Last active Aug 29, 2015
An incomplete, inefficient implementation of EnQHanlder.Rcv
View queue.go
const (
// Dictionary names.
active = "active"
dequed = "dequed"
)
func (h EnQHandler) Rcv(msg beehive.Msg, ctx beehive.RcvContext) error {
// TODO: assign a unique ID.
enq := msg.Data().(Enque)
@soheilhy
soheilhy / queue.go
Created Jul 16, 2015
EnQHandler.Rcv
View queue.go
func (h EnQHandler) Rcv(msg beehive.Msg, ctx beehive.RcvContext) error {
enq := msg.Data().(Enque)
dict := ctx.Dict(active)
key := string(enq.Task.Queue)
var tasks TaskRing
if val, err := dict.Get(key); err == nil {
tasks = val.(TaskRing)
}
@soheilhy
soheilhy / queue.go
Created Jul 16, 2015
EnQHandler.Map
View queue.go
func (h EnQHandler) Map(msg beehive.Msg, ctx beehive.MapContext) beehive.MappedCells {
// Send the Enque message to the bee that owns
// the Queue's entry in the active dictionary.
q := string(msg.Data().(Enque).Queue)
return beehive.MappedCells{{active, q}}
}
@soheilhy
soheilhy / ring.go
Last active Aug 29, 2015
Task ring buffer
View ring.go
// TaskRing is an efficient, gob-compatible ring buffer for Tasks.
// TaskRing always wastes one element.
type TaskRing struct {
...
Stats Stats
}
// Stats represents the statistics of the task ring buffer.
type Stats struct {
Deque uint64 // Deque represents the total nubmer of dequeued tasks.
View args_example.go
package main
import (
"fmt"
"github.com/soheilhy/args"
)
var ListenOn = args.NewInt(args.Default(8080))
var BufferSize = args.NewUint64(args.Default(uint64(1024 * 1024)))
View queue.go
// AckHandler handles Ack messages.
type AckHandler struct{}
func (h AckHandler) Rcv(msg beehive.Msg, ctx beehive.RcvContext) error {
ack := msg.Data().(Ack)
ddict := ctx.Dict(dequed)
key := string(ack.Queue)
if val, err := ddict.Get(key); err == nil {
You can’t perform that action at this time.