This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package simplelist | |
import "testing" | |
func Test_List(t *testing.T) { | |
l := New() | |
l.Add([]byte("item1")) | |
l.Add([]byte("item2")) | |
if l.Size() != 2 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sortedlist/simplelist" | |
"sync" | |
) | |
func main() { | |
l := simplelist.New() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type list struct { | |
mu sync.Mutex | |
head *node | |
size int | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (l *list) Add(item []byte) bool { | |
var pred, curr *node | |
key, _ := hashstructure.Hash(item, nil) | |
l.mu.Lock() | |
defer l.mu.Unlock() | |
pred = l.head | |
curr = pred.next | |
for curr.key < key { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sortedlist/coarselist" | |
"sync" | |
) | |
func main() { | |
l := coarselist.New() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type node struct { | |
mu sync.Mutex | |
item []byte | |
key uint64 | |
next *node | |
} | |
func (n *node) lock() { | |
n.mu.Lock() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (l *list) Add(item []byte) bool { | |
key, _ := hashstructure.Hash(item, nil) | |
l.head.lock() | |
pred := l.head | |
curr := pred.next | |
curr.lock() | |
// Displace locks during each iteration. | |
// We keep the lock for curr when unlocking pred. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (l *list) Remove(item []byte) bool { | |
key, _ := hashstructure.Hash(item, nil) | |
l.head.lock() | |
pred := l.head | |
curr := pred.next | |
curr.lock() | |
for curr.key < key { | |
pred.unlock() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from peewee import * | |
from playhouse.sqlite_ext import TableFunction | |
import json | |
# https://github.com/coleifer/peewee/ | |
# Basic connection and sql execution | |
db = SqliteDatabase('foo.db') | |
db.connect() | |
db.execute_sql( | |
"CREATE TABLE IF NOT EXISTS pageAds (pageid STRING, adid_list JSON);") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Implementation of a SkipList using the original paper's algorithm: | |
// https://15721.courses.cs.cmu.edu/spring2018/papers/08-oltpindexes1/pugh-skiplists-cacm1990.pdf | |
// | |
// Example usage: | |
// sl := db.NewSkipList() | |
// sl.Insert([]byte("foo"), []byte("bar")) | |
// sl.Insert([]byte("foo"), []byte("world")) | |
// fmt.Println(string(sl.Search([]byte("foo")))) | |
// | |
package db |