Skip to content

Commit

Permalink
utils: re-struct
Browse files Browse the repository at this point in the history
- rename `store` from structs to `content`
- rename values*.go to `store*.go`
- comments
- some minor changes
  • Loading branch information
tbdsux committed Jun 1, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent c217fe1 commit a6c517f
Showing 12 changed files with 72 additions and 71 deletions.
41 changes: 21 additions & 20 deletions collections-query.go
Original file line number Diff line number Diff line change
@@ -5,36 +5,37 @@ import (
"strings"
)

// Remove removes the first element that is equal to v.
// Remove removes the first element that is equal to v. It wraps around RemoveMany().
func (c *MiniCollections) Remove(v interface{}) error {
return c.RemoveMany(v, 1)
}

// RemoveAll removes all elements that are equal to v.
// RemoveAll removes all elements that are equal to v. It wraps around RemoveMany().
func (c *MiniCollections) RemoveAll(v interface{}) error {
return c.RemoveMany(v, -1)
}

// RemoveMany removes the number of elements corresponding to l. Use RemoveAll() for removing all elements.
// RemoveMany removes the number of elements corresponding to l. Use RemoveAll() for removing
// all elements and Remove() for a single element.
// `l` cannot be less than -1 or equal to 0.
func (c *MiniCollections) RemoveMany(v interface{}, l int) error {
if l < -1 || l == 0 {
return errors.New("l cannot be less than -1 or equal to 0")
}

d := c.getOrCreateMutex(len(c.store) + l)
d := c.getOrCreateMutex(len(c.content) + l)
d.Lock()
defer d.Unlock()

values := []interface{}{}
removed := []interface{}{}

for index, x := range c.store {
for index, x := range c.content {
// -1 means remove all similar values
if l > 0 {
// check if total is similar
if len(removed) == l {
values = append(values, c.store[index:]...)
values = append(values, c.content[index:]...)
break
}
}
@@ -49,41 +50,41 @@ func (c *MiniCollections) RemoveMany(v interface{}, l int) error {
}

// write new values
c.store = values
c.content = values
c.writeToDB()

return nil
}

// Push adds an item to the store slice.
func (c *MiniCollections) Push(v ...interface{}) {
d := c.getOrCreateMutex(len(c.store) + 1)
d := c.getOrCreateMutex(len(c.content) + 1)
d.Lock()
defer d.Unlock()

c.store = append(c.store, v...)
c.content = append(c.content, v...)
c.writeToDB()
}

// List returns all of the contents of the collection
func (c *MiniCollections) List() []interface{} {
return c.store
return c.content
}

// First returns the first element of the collections.
func (c *MiniCollections) First() interface{} {
return c.store[0]
return c.content[0]
}

// Last returns the last element of the collections.
func (c *MiniCollections) Last() interface{} {
return c.store[len(c.store)-1]
return c.content[len(c.content)-1]
}

// Find attemps to find the value from the store and returns it's index.
// If it doesn't exist, it will return -1.
func (c *MiniCollections) Find(v interface{}) int {
for index, value := range c.store {
for index, value := range c.content {
if value == v {
return index
}
@@ -96,7 +97,7 @@ func (c *MiniCollections) Find(v interface{}) int {
func (c *MiniCollections) FindAll(v interface{}) []interface{} {
values := []interface{}{}

for _, value := range c.store {
for _, value := range c.content {
if value == v {
values = append(values, value)
}
@@ -107,7 +108,7 @@ func (c *MiniCollections) FindAll(v interface{}) []interface{} {

// MatchString returns the first element that contains v.
func (c *MiniCollections) MatchString(v string) (string, error) {
for _, value := range c.store {
for _, value := range c.content {
s, ok := value.(string)
if ok {
if strings.Contains(s, v) {
@@ -123,7 +124,7 @@ func (c *MiniCollections) MatchString(v string) (string, error) {
func (c *MiniCollections) MatchStringAll(v string) ([]string, error) {
values := []string{}

for _, value := range c.store {
for _, value := range c.content {
s, ok := value.(string)
if ok {
if strings.Contains(s, v) {
@@ -139,7 +140,7 @@ func (c *MiniCollections) MatchStringAll(v string) ([]string, error) {
func (c *MiniCollections) FilterString() []string {
values := []string{}

for _, i := range c.store {
for _, i := range c.content {
if v, ok := i.(string); ok {
values = append(values, v)
}
@@ -152,7 +153,7 @@ func (c *MiniCollections) FilterString() []string {
func (c *MiniCollections) FilterInt() []int {
values := []int{}

for _, i := range c.store {
for _, i := range c.content {
if v, ok := i.(int); ok {
values = append(values, v)
}
@@ -165,7 +166,7 @@ func (c *MiniCollections) FilterInt() []int {
func (c *MiniCollections) FilterBool() []bool {
values := []bool{}

for _, i := range c.store {
for _, i := range c.content {
if v, ok := i.(bool); ok {
values = append(values, v)
}
@@ -178,7 +179,7 @@ func (c *MiniCollections) FilterBool() []bool {
func (c *MiniCollections) FilterFloat() []float64 {
values := []float64{}

for _, i := range c.store {
for _, i := range c.content {
if v, ok := i.(float64); ok {
values = append(values, v)
}
12 changes: 6 additions & 6 deletions collections.go
Original file line number Diff line number Diff line change
@@ -7,9 +7,9 @@ import (
)

// base function for creating a new collection
func newMiniCollection(filename string) *MiniCollections {
func minicollection(filename string) *MiniCollections {
db := &MiniCollections{
store: []interface{}{},
content: []interface{}{},
mutexes: make(map[int]*sync.Mutex),
BaseMiniDB: BaseMiniDB{
db: filename,
@@ -20,7 +20,7 @@ func newMiniCollection(filename string) *MiniCollections {
if content, f := ensureInitialDB(db.db); f {
db.writeToDB()
} else {
json.Unmarshal(content, &db.store)
json.Unmarshal(content, &db.content)
}

return db
@@ -34,13 +34,13 @@ func (db *MiniDB) Collections(key string) *MiniCollections {

// if the key exists, get the file's name,
// otherwise, create a new one
filename, ok := db.store.Collections[key]
filename, ok := db.content.Collections[key]
if !ok {
filename = generateFileName("cols")
}

db.store.Collections[key] = filename
db.content.Collections[key] = filename
db.writeToDB()

return newMiniCollection(path.Join(db.path, filename))
return minicollection(path.Join(db.path, filename))
}
18 changes: 9 additions & 9 deletions keys-query.go
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ import (
// FindKey gets the key in the keys map and returns its corresponding filename.
// It returns nil if it exists.
func (db *MiniDB) FindKey(key string) (string, error) {
filename, ok := db.store.Keys[key]
filename, ok := db.content.Keys[key]

if !ok {
return "", errors.New("the key does not exist")
@@ -21,7 +21,7 @@ func (db *MiniDB) FindKey(key string) (string, error) {
// FindCollection gets the key in the keys map and returns its corresponding filename.
// It returns nil if it exists.
func (db *MiniDB) FindCollection(key string) (string, error) {
filename, ok := db.store.Collections[key]
filename, ok := db.content.Collections[key]

if !ok {
return "", errors.New("the key does not exist")
@@ -33,7 +33,7 @@ func (db *MiniDB) FindCollection(key string) (string, error) {
// FindStore gets the key in the keys map and returns its corresponding filename.
// It returns nil if it exists.
func (db *MiniDB) FindStore(key string) (string, error) {
filename, ok := db.store.Store[key]
filename, ok := db.content.Store[key]

if !ok {
return "", errors.New("the key does not exist")
@@ -50,13 +50,13 @@ func (db *MiniDB) RemoveCollection(key string) error {
defer d.Unlock()

// get the filename if it exists
filename, ok := db.store.Collections[key]
filename, ok := db.content.Collections[key]
if !ok {
return errors.New("collections key does not exist")
}

// remove the key and the filename
delete(db.store.Collections, key)
delete(db.content.Collections, key)

return os.RemoveAll(path.Join(db.path, filename))
}
@@ -69,13 +69,13 @@ func (db *MiniDB) RemoveStore(key string) error {
defer d.Unlock()

// get the filename if it exists
filename, ok := db.store.Store[key]
filename, ok := db.content.Store[key]
if !ok {
return errors.New("collections key does not exist")
}

// remove the key and the filename
delete(db.store.Store, key)
delete(db.content.Store, key)

return os.RemoveAll(path.Join(db.path, filename))
}
@@ -88,13 +88,13 @@ func (db *MiniDB) RemoveKey(key string) error {
defer d.Unlock()

// get the filename if it exists
filename, ok := db.store.Keys[key]
filename, ok := db.content.Keys[key]
if !ok {
return errors.New("collections key does not exist")
}

// remove the key and the filename
delete(db.store.Keys, key)
delete(db.content.Keys, key)

return os.RemoveAll(path.Join(db.path, filename))
}
12 changes: 6 additions & 6 deletions keys.go
Original file line number Diff line number Diff line change
@@ -7,11 +7,11 @@ import (
)

// this is helper for creating a new key db
func newMiniDB(dir, filename string) *MiniDB {
func minidb(dir, filename string) *MiniDB {
db := &MiniDB{
path: dir,
filename: filename,
store: MiniDBStore{
content: MiniDBContent{
Keys: map[string]string{},
Collections: map[string]string{},
Store: map[string]string{},
@@ -27,7 +27,7 @@ func newMiniDB(dir, filename string) *MiniDB {
if content, f := ensureInitialDB(db.db); f {
db.writeToDB()
} else {
json.Unmarshal(content, &db.store)
json.Unmarshal(content, &db.content)
}

return db
@@ -42,13 +42,13 @@ func (db *MiniDB) Key(key string) *MiniDB {

// if the key exists, get the file's name,
// otherwise, create a new one
filename, ok := db.store.Keys[key]
filename, ok := db.content.Keys[key]
if !ok {
filename = generateFileName("key")
}

db.store.Keys[key] = filename
db.content.Keys[key] = filename
db.writeToDB()

return newMiniDB(db.path, filename)
return minidb(db.path, filename)
}
20 changes: 10 additions & 10 deletions minidb.go
Original file line number Diff line number Diff line change
@@ -17,28 +17,28 @@ type (
MiniDB struct {
path string
filename string
store MiniDBStore
content MiniDBContent
mutexes map[string]*sync.Mutex
BaseMiniDB
}

// MiniDBStore is the types of MiniDB.store
MiniDBStore struct {
// MiniDBContent is the types of MiniDB.store
MiniDBContent struct {
Keys map[string]string `json:"keys"`
Collections map[string]string `json:"collections"`
Store map[string]string `json:"store"`
}

// MiniCollections is a collections store.
MiniCollections struct {
store []interface{}
content []interface{}
mutexes map[int]*sync.Mutex
BaseMiniDB
}

// MiniStore is a key-value store.
MiniStore struct {
store map[string]interface{}
content map[string]interface{}
mutexes map[string]*sync.Mutex
BaseMiniDB
}
@@ -48,24 +48,24 @@ type (
// The dir will be created if it doesn't exist and a file named `__default.json` will also be generated.
// It is better to use this in managing multiple json files.
func New(dir string) *MiniDB {
return newMiniDB(dir, "__default.json")
return minidb(dir, "__default.json")
}

// NewMiniStore creates and returns a new key-store collection json db.
func NewMiniStore(f string) *MiniStore {
return newMiniStore(f)
return ministore(f)
}

// NewMiniCollections creates and returns a new collections json db.
func NewMiniCollections(f string) *MiniCollections {
return newMiniCollection(f)
return minicollection(f)
}

// ListCollections returns the list of collections created.
func (db *MiniDB) ListCollections() []string {
cols := []string{}

for i := range db.store.Collections {
for i := range db.content.Collections {
cols = append(cols, i)
}

@@ -76,7 +76,7 @@ func (db *MiniDB) ListCollections() []string {
func (db *MiniDB) ListStores() []string {
stores := []string{}

for i := range db.store.Store {
for i := range db.content.Store {
stores = append(stores, i)
}

2 changes: 1 addition & 1 deletion minidb_test.go
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ func TestNewMiniCollections(t *testing.T) {

func TestNewMiniStore(t *testing.T) {
file := "store.json"
newMiniStore(file)
NewMiniStore(file)

cleanFileAfter(file, t)
}
Loading

0 comments on commit a6c517f

Please sign in to comment.