Skip to content

Instantly share code, notes, and snippets.

View rystsov's full-sized avatar

Denis Rystsov rystsov

View GitHub Profile
Accepted = namedtuple('Accepted', ['n', 'state'])
class Acceptor:
def __init__(self, node_id):
self.node_id = node_id
self.promise = 0
self.accepted = Accepted(0,None)
emit_accepted(0, node_id=self.node_id, ts=now(), accepted_n=0, state=self.accepted.state)
@synchronized
def prepare(self, ballot_n):
def cas_update(old, new):
def change(x):
if x!=old: raise Conflict(x)
return new
return change
Accepted = namedtuple('Accepted', ['n', 'state'])
class Acceptor:
def __init__(self, node_id, hdd):
self.node_id = node_id
if hdd.is_empty():
hdd.write(promise=0, accepted=Accepted(0,None))
# emit_* functions are used to record a event and capture a bit of context
# The proof is based on reasoning on the structure of the sequence of the event
# that the program can generate
emit_accepted(0, node_id=self.node_id, ts=now(), accepted_n=0, state=self.hdd.accepted.state)
class Variable:
def __init__(self, nodes, q, node_id):
self.proposer = Proposer(nodes, q, node_id)
@endpoint
def write(self, ver, value, due):
def change(previous):
if previous == None or previous.ver==ver:
return Val(ver = ver+1, value = value)
return previous
def query(current):
@rystsov
rystsov / dswitch.py
Last active September 27, 2015 06:11
class Switch:
def __init__(self, nodes, q, node_id):
self.proposer = Proposer(nodes, q, node_id)
@endpoint
def write(self, value, due):
def change(previous):
return value if previous == None else previous
def query(current):
if current == value:
return current
import string
def ltot(l):
def _ltot(l, i):
if len(l)==i: return ()
return (l[i], _ltot(l, i+1))
return _ltot(l, 0)
def symbol(x):
def parser(txt):
@rystsov
rystsov / portqsp.py
Created March 16, 2013 17:38
Задачка Умпутума (Parallel order respectful task queue streaming processing)
from Queue import Queue
from threading import Thread
from time import sleep
# main queues
tasks = Queue()
results = Queue()
# auxiliary queues & workers ####################################################
preresults = Queue()
<html>
<body>
<div id="canvas"></div>
<script type="text/javascript">
var canvas = document.getElementById("canvas");
var input = document.createElement("input");
input.type = "textbox";
input.value = "click here and then on address";
input.style.cssText += ";width:200px;"
input.addEventListener("blur", function(){
class Proposer:
def __init__(self, nodes, q, node_id):
self.q = q
self.nodes = nodes
self.node_id = node_id
# generates a new ballot number which is greater than v
def get_n(self, v):
# ...
@endpoint
def change_query(self, change, query, due):
def search(xs, b, e, x):
if b==e:
return b if xs[b] == x else -1
m = (b+e)/2
# [x_b .. x_m x_{m+1} .. x_e]
# x_b <= x_m and x_{m+1} <= x_e # normal, normal
if xs[b] <= xs[m] and xs[m+1] <= xs[e]:
if xs[b] <= x <= xs[m]:
return search(xs, b, m, x)
if xs[m+1] <= x <= xs[e]: