From 301592ad7d73d08a1eba206624402e06bee43ef7 Mon Sep 17 00:00:00 2001 From: bouncepaw Date: Sun, 3 Jan 2021 01:52:23 +0500 Subject: [PATCH] Start implementing memdb-driven hyphae --- .gitignore | 1 - go.mod | 1 + go.sum | 8 ++++++ hypha/count.go | 33 ++++++++++++++++++++++ hypha/hypha.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 hypha/count.go create mode 100644 hypha/hypha.go diff --git a/.gitignore b/.gitignore index db5b71e..c450f48 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -hypha mycorrhiza diff --git a/go.mod b/go.mod index b5d6fc3..c785b3a 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,6 @@ go 1.14 require ( github.com/adrg/xdg v0.2.2 github.com/gorilla/feeds v1.1.1 + github.com/hashicorp/go-memdb v1.3.0 github.com/valyala/quicktemplate v1.6.3 ) diff --git a/go.sum b/go.sum index 284389c..9b58270 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,14 @@ github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gorilla/feeds v1.1.1 h1:HwKXxqzcRNg9to+BbvJog4+f3s/xzvtZXICcQGutYfY= github.com/gorilla/feeds v1.1.1/go.mod h1:Nk0jZrvPFZX1OBe5NPiddPw7CfwF6Q9eqzaBbaightA= +github.com/hashicorp/go-immutable-radix v1.3.0 h1:8exGP7ego3OmkfksihtSouGMZ+hQrhxx+FVELeXpVPE= +github.com/hashicorp/go-immutable-radix v1.3.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= +github.com/hashicorp/go-memdb v1.3.0 h1:xdXq34gBOMEloa9rlGStLxmfX/dyIK8htOv36dQUwHU= +github.com/hashicorp/go-memdb v1.3.0/go.mod h1:Mluclgwib3R93Hk5fxEfiRhB+6Dar64wWh71LpNSe3g= +github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= +github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/klauspost/compress v1.10.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= github.com/klauspost/compress v1.11.0/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= diff --git a/hypha/count.go b/hypha/count.go new file mode 100644 index 0000000..3c9aef4 --- /dev/null +++ b/hypha/count.go @@ -0,0 +1,33 @@ +package hypha + +import ( + "sync" +) + +type count struct { + value uint + sync.Mutex +} + +// Count is a global variable. Its value is number of all existing hyphae. Hypha mutators are expected to manipulate the value. It is concurrent-safe. +var Count = count{} + +// Increment the value of Count. +func (c *count) Increment() { + c.Lock() + c.value++ + c.Unlock() +} + +// Decrement the value of Count. +func (c *count) Decrement() { + c.Lock() + c.value-- + c.Unlock() +} + +// Get value of Count. +func (c *count) Value() uint { + // it is concurrent-safe to not lock here, right? + return c.value +} diff --git a/hypha/hypha.go b/hypha/hypha.go new file mode 100644 index 0000000..2ca5ffc --- /dev/null +++ b/hypha/hypha.go @@ -0,0 +1,76 @@ +package hypha + +import ( + "errors" + + "github.com/hashicorp/go-memdb" +) + +type Hypha struct { + Name string + Exists bool + TextPath string + BinaryPath string + OutLinks []string + BackLinks []string +} + +func AddHypha(h Hypha) error { + return errors.New("Not implemented") +} + +// Create the DB schema +var schema = &memdb.DBSchema{ + Tables: map[string]*memdb.TableSchema{ + "hyphae": &memdb.TableSchema{ + Name: "hyphae", + Indexes: map[string]*memdb.IndexSchema{ + "name": &memdb.IndexSchema{ + Name: "name", + Unique: true, + Indexer: &memdb.StringFieldIndex{Field: "Name"}, + }, + "exists": &memdb.IndexSchema{ + Name: "exists", + Unique: false, + AllowEmpty: true, + Indexer: &memdb.BoolFieldIndex{Field: "Exists"}, + }, + "text-path": &memdb.IndexSchema{ + Name: "text-path", + Unique: true, + AllowEmpty: true, + Indexer: &memdb.StringFieldIndex{Field: "TextPath"}, + }, + "binary-path": &memdb.IndexSchema{ + Name: "binary-path", + Unique: true, + AllowEmpty: true, + Indexer: &memdb.StringFieldIndex{Field: "BinaryPath"}, + }, + "out-links": &memdb.IndexSchema{ + Name: "out-links", + Unique: false, + AllowEmpty: true, + Indexer: &memdb.StringSliceFieldIndex{Field: "OutLinks"}, + }, + "back-links": &memdb.IndexSchema{ + Name: "back-links", + Unique: false, + AllowEmpty: true, + Indexer: &memdb.StringSliceFieldIndex{Field: "BackLinks"}, + }, + }, + }, + }, +} + +var db *memdb.MemDB + +func init() { + var err error + db, err = memdb.NewMemDB(schema) + if err != nil { + panic(err) + } +}