Skip to content

Instantly share code, notes, and snippets.

View dgabriele's full-sized avatar

Daniel Gabriele dgabriele

  • Brooklyn, New York
View GitHub Profile
@dgabriele
dgabriele / query.ts
Created December 9, 2021 17:42
query builder for web3.js getParsedProgramAccounts
async getUserLikes(
userPubkey: PublicKey,
): Promise<Array<Account<UserVotingRecordAccountData>>> {
return await this.query(UserVotingRecordAccountData)
.match({
kind: 2,
user_pubkey: userPubkey,
choice: 1
})
.size(32 * 2 + 2)
@dgabriele
dgabriele / optional_arguments.rs
Last active December 8, 2021 01:12
Optional Arguments in Rust
fn say_hello(name: Option<&String>) {
let greeting = match name {
Some(name_str) => format!("Hello, {}!", name_str),
None => "You don't have a name!"
};
println!(greeting);
}
// called like:
let name = String::from("Jeff");

Ravel Service Layer

The service layer is conceptually similar to a web framework in that there are apps, endpoints, middleware, requests and responses. As a compared to a web framework, however, Ravel has a more abstract notion of what these things are. Ultimately, the service layer presents a public interface to the outside world.

Service Architecture

As mentioned before, the architecture of the service layer is a lot like a web framework, but there are some important additions--namely, stages 2, 3, and 7 below.

Stage 1: Pre-request Middleware

In the first stage, middleware has a chance to initiate services and access the arguments with which an API endpoint was called. Note that arguments may differ, depending on the type of Ravel application being run. In a web app, these arguments might consist of an HTTP request; for a gRPC service, they'd consist of protocol buffer messages.

Ravel Business Logic Layer

What an app does is, by definition, its business logic. This layer is sandwiched between the service layer, which communicates with the outside world, and the data access layer (DAL), which communicates with the database.

One of our primary goals when creating Ravel was to give ourselves a way to write scalable and extensible code that requires only as much effort as it takes to write pseudocode on a napkin. In Ravel, new apps and features can be fully expressed without a database or the need for manual fixture generation. Frontend development is therefore immediately unblocked, as incidental complexity on the backend is minimized.

Understanding the business logic layer is essential for harnessing Ravel's most powerful features.

Example: Users & Accounts

Imagine an app where there are users and accounts to which users belong. The class definitions might looks something like this:

class Intent(BizObject):
name = String()
class BizList(BizList):
yearly_financials = BatchRelationship(
conditions=lambda intents: (
Intent._id,
YearlyFinancial.intent_id,
YearlyFinancial.intent_id.including(intents._id)
),
import random
import time
import threading
from itertools import product
from Queue import Queue
import numpy
class Thing(object):
def __init__(self, key):
@dgabriele
dgabriele / kazoo-fs-example.py
Last active December 19, 2015 05:39
This is an example of working with Kazoo.fs.FileSystem. The effect of this code is to atomically fetch and increment a counter associated with a hypothetical "user" znode, called "frank".
''' file: kazoo-fs-example.py
- See: https://github.com/basefook/kazoo/blob/master/kazoo/fs.py
WHAT IS THIS?
----------------------
This is an example of working with FileSystem. The effect of this
code is to atomically fetch and increment a counter associated with
a hypothetical "user" znode, called "frank".
@dgabriele
dgabriele / bp.cpp
Created August 11, 2012 18:59
lock-free Bloom filter with quiescent consistency
#include <iostream>
#include <atomic>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <stdint.h>
#include <limits.h>
class bloom_filter
@dgabriele
dgabriele / gist:2125936
Created March 19, 2012 19:54
Simple Client/Server Model in Erlang
-module(server).
-export([factorial/2, factorial/1, client/2, server/1, start/0]).
%% BACKLOG is effectively the number of clients
-define(BACKLOG, 1000).
factorial(1) ->
1;
factorial(X) ->
X * factorial(X-1).
@dgabriele
dgabriele / pf.erl
Created June 21, 2011 16:41
Primitive Recursion in Erlang
%% Daniel Gabriele
%% Jun 20, 2011
-module(pf).
-export([
reverse/1, succ/1, zero/1, proj/2, pred/1, add/2, minus/2, eq/2, ne/2,
min/2, max/2, gt/2, ge/2, lt/2, le/2, len/1, gcd/2
]).