Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
micrypt committed Mar 7, 2012
0 parents commit 758aa92
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/go/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Public Domain (-) 2011 The Ampify Authors.
# See the Ampify UNLICENSE file for details.

include $(GOROOT)/src/Make.inc

TARG=ampstore
GOFILES=\
skiplist.go\

include $(GOROOT)/src/Make.pkg
1 change: 1 addition & 0 deletions src/go/_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Ampstore.go : Draft version of the Ampify data store
1 change: 1 addition & 0 deletions src/go/_serializer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Ampstore.go : Draft version of the Ampify data store
1 change: 1 addition & 0 deletions src/go/_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Ampstore.go : Draft version of the Ampify data store
46 changes: 46 additions & 0 deletions src/go/_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Ampstore.go : Draft version of the Ampify data store
import (
"./skiplist"
"./serializer"
)

type Key []byte
type Value []byte

type Store struct {
keyList *SkipList
table *[Key] Value
}

type Datum struct {
k Key
v Value
}

func (s *Store) Get(k Key) Datum {
datum := s.table.Get(k)
return Datum
}

func (s *Store) Set(k Key, v Value) {
s.keyList.Insert(k)
s.table.Set(k, v)
}

func (s *Store) Scan(start Key, end Key) {
vect = make(Vector)
for k,v := range(s.keyList[start, end]) {
vect.Append(new Datum{ k, v })
}
return Serializer.Write(vect)
}

func (s *Store) Delete(k Key) {
s.keyList.Delete(k)
s.table.Delete(k)
}

func Init(opts Opt) *Store {
store = new Store
return &store
}
99 changes: 99 additions & 0 deletions src/go/skiplist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Ampstore.go : Draft version of the Ampify data store
package ampstore

import (
"bytes"
"fmt"
"math/rand"
"time"
)

type Element struct {
Next []*Element
Value []byte
}

type SkipList struct {
Head *Element
Level int
Length int
}

func New(maxLevel int) *SkipList {
head := &Element{make([]*Element, maxLevel), []byte{}}
return &SkipList{Level: 0, Head: head, Length: 0}
}

func (s *SkipList) Insert(value []byte) {
level := 0
t := time.Now()
rand.Seed((int64)(t.Nanosecond()))
for rand.Float64() < 0.5 {
level++
}
if level+1 > s.Level {
s.Level = level + 1
}

newElement := &Element{make([]*Element, level+1), value}

for i := s.Level - 1; i >= 0; i-- {
var ptr *Element
for ptr = s.Head; ptr.Next[i] != nil; ptr = ptr.Next[i] {
if bytes.Compare(ptr.Next[i].Value, value) > 0 {
break
}
}

if i <= level {
newElement.Next[i] = ptr.Next[i]
ptr.Next[i] = newElement
}
}
s.Length++
}

func (s *SkipList) Delete(value []byte) {
if !s.Contains(value) {
var prePtr *Element
for i := s.Level - 1; i >= 0; i++ {
for ptr := s.Head; ptr.Next[i] != nil; ptr = ptr.Next[i] {
if bytes.Compare(ptr.Value, value) == 0 {
prePtr.Next[i] = ptr.Next[i].Next[i]
break
}
if bytes.Compare(ptr.Next[i].Value, value) > 0 {
break
}
}
}
}
}

func (s *SkipList) Contains(value []byte) bool {
for i := s.Level - 1; i >= 0; i-- {
for ptr := s.Head; ptr.Next[i] != nil; ptr = ptr.Next[i] {
if bytes.Compare(ptr.Next[i].Value, value) == 0 {
return true
}
if bytes.Compare(ptr.Next[i].Value, value) > 0 {
break
}
}
}
return false
}

func (s *SkipList) Len() int {
return s.Length
}

func (s *SkipList) Show() {
for i := s.Level - 1; i >= 0; i-- {
fmt.Printf("Level %d: ", i)
for ptr := s.Head; ptr.Next[i] != nil; ptr = ptr.Next[i] {
fmt.Printf("%v ", ptr.Next[i].Value)
}
fmt.Printf("\n\n")
}
}
15 changes: 15 additions & 0 deletions src/go/skiplist_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Ampstore.go : Draft version of the Ampify data store
package ampstore

import (
"testing"
)

func Test_SkipList(t *testing.T) {
sl := New(32)
sl.Insert([]byte{1, 2, 3})
sl.Insert([]byte{3, 2, 3})
sl.Insert([]byte{5, 2, 3})
sl.Insert([]byte{7, 2, 3})
sl.Show()
}

0 comments on commit 758aa92

Please sign in to comment.