Skip to content

Commit

Permalink
Moved application files into Yasmine directory
Browse files Browse the repository at this point in the history
  • Loading branch information
devmangel committed Sep 2, 2024
0 parents commit dbbbf32
Show file tree
Hide file tree
Showing 30 changed files with 1,384 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cmd/Yasmine
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
"fmt"
)

func main() {
fmt.Println("Yasmine Database Server Starting...")
// Aquí inicializarás el servidor más adelante
}

3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/devmangel/yasmine

go 1.23.0
67 changes: 67 additions & 0 deletions internal/core/cluster/cluster_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package cluster

import (
"sync"
)

// ClusterManager gestiona la coordinación entre nodos en el clúster.
type ClusterManager struct {
nodes []*Node
lock sync.RWMutex
}

// NewClusterManager crea una nueva instancia de ClusterManager.
func NewClusterManager() *ClusterManager {
return &ClusterManager{
nodes: make([]*Node, 0),
}
}

// AddNode añade un nuevo nodo al clúster.
func (cm *ClusterManager) AddNode(node *Node) {
cm.lock.Lock()
defer cm.lock.Unlock()
cm.nodes = append(cm.nodes, node)
}

// RemoveNode elimina un nodo del clúster.
func (cm *ClusterManager) RemoveNode(nodeID string) {
cm.lock.Lock()
defer cm.lock.Unlock()

for i, node := range cm.nodes {
if node.ID == nodeID {
cm.nodes = append(cm.nodes[:i], cm.nodes[i+1:]...)
break
}
}
}

// GetNode devuelve un nodo basado en su ID.
func (cm *ClusterManager) GetNode(nodeID string) *Node {
cm.lock.RLock()
defer cm.lock.RUnlock()

for _, node := range cm.nodes {
if node.ID == nodeID {
return node
}
}

return nil
}

// BalanceLoad selecciona un nodo para distribuir la carga.
func (cm *ClusterManager) BalanceLoad() *Node {
cm.lock.RLock()
defer cm.lock.RUnlock()

// Lógica simple de balanceo de carga (por ejemplo, round-robin o el nodo con menos carga)
if len(cm.nodes) == 0 {
return nil
}

// Por simplicidad, seleccionamos el primer nodo.
// Puedes implementar un algoritmo más sofisticado.
return cm.nodes[0]
}
34 changes: 34 additions & 0 deletions internal/core/cluster/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cluster

import "time"

// Node representa un nodo en el clúster.
type Node struct {
ID string
Address string
LastCheck time.Time
IsAlive bool
}

// NewNode crea una nueva instancia de Node.
func NewNode(id, address string) *Node {
return &Node{
ID: id,
Address: address,
LastCheck: time.Now(),
IsAlive: true,
}
}

// CheckHealth actualiza el estado de salud del nodo.
func (n *Node) CheckHealth() {
// Lógica para verificar si el nodo está activo
n.LastCheck = time.Now()
// Aquí se debería implementar la lógica de verificación, por ahora asumimos que está vivo.
n.IsAlive = true
}

// MarkDead marca un nodo como inactivo.
func (n *Node) MarkDead() {
n.IsAlive = false
}
22 changes: 22 additions & 0 deletions internal/core/commands/base_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package commands

// Command es una interfaz que todos los comandos deben implementar.
type Command interface {
Execute(args ...interface{}) (interface{}, error)
Name() string
}

// BaseCommand es una estructura que puede ser embebida en comandos específicos.
type BaseCommand struct {
commandName string
}

// Name devuelve el nombre del comando.
func (bc *BaseCommand) Name() string {
return bc.commandName
}

// Execute es un método que debe ser implementado por los comandos específicos.
func (bc *BaseCommand) Execute(args ...interface{}) (interface{}, error) {
return nil, nil
}
101 changes: 101 additions & 0 deletions internal/core/commands/list_commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package commands

import "github.com/devmangel/Yasmine/internal/core/data_structures"

// LPushCommand añade un valor al inicio de una lista.
type LPushCommand struct {
BaseCommand
list *data_structures.List
}

// NewLPushCommand crea una nueva instancia de LPushCommand.
func NewLPushCommand(list *data_structures.List) *LPushCommand {
return &LPushCommand{
BaseCommand: BaseCommand{commandName: "LPUSH"},
list: list,
}
}

// Execute ejecuta el comando LPUSH.
func (cmd *LPushCommand) Execute(args ...interface{}) (interface{}, error) {
if len(args) != 1 {
return nil, ErrInvalidArguments
}

cmd.list.Prepend(args[0])
return "OK", nil
}

// RPushCommand añade un valor al final de una lista.
type RPushCommand struct {
BaseCommand
list *data_structures.List
}

// NewRPushCommand crea una nueva instancia de RPushCommand.
func NewRPushCommand(list *data_structures.List) *RPushCommand {
return &RPushCommand{
BaseCommand: BaseCommand{commandName: "RPUSH"},
list: list,
}
}

// Execute ejecuta el comando RPUSH.
func (cmd *RPushCommand) Execute(args ...interface{}) (interface{}, error) {
if len(args) != 1 {
return nil, ErrInvalidArguments
}

cmd.list.Append(args[0])
return "OK", nil
}

// LPopCommand elimina y devuelve el primer valor de una lista.
type LPopCommand struct {
BaseCommand
list *data_structures.List
}

// NewLPopCommand crea una nueva instancia de LPopCommand.
func NewLPopCommand(list *data_structures.List) *LPopCommand {
return &LPopCommand{
BaseCommand: BaseCommand{commandName: "LPOP"},
list: list,
}
}

// Execute ejecuta el comando LPOP.
func (cmd *LPopCommand) Execute(args ...interface{}) (interface{}, error) {
if cmd.list.Len() == 0 {
return nil, nil
}

firstNode := cmd.list.head
cmd.list.Remove(firstNode)
return firstNode.Value, nil
}

// RPopCommand elimina y devuelve el último valor de una lista.
type RPopCommand struct {
BaseCommand
list *data_structures.List
}

// NewRPopCommand crea una nueva instancia de RPopCommand.
func NewRPopCommand(list *data_structures.List) *RPopCommand {
return &RPopCommand{
BaseCommand: BaseCommand{commandName: "RPOP"},
list: list,
}
}

// Execute ejecuta el comando RPOP.
func (cmd *RPopCommand) Execute(args ...interface{}) (interface{}, error) {
if cmd.list.Len() == 0 {
return nil, nil
}

lastNode := cmd.list.tail
cmd.list.Remove(lastNode)
return lastNode.Value, nil
}
104 changes: 104 additions & 0 deletions internal/core/commands/set_commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package commands

import "github.com/devmangel/Yasmine/internal/core/data_structures"

// SAddCommand añade uno o más elementos a un conjunto.
type SAddCommand struct {
BaseCommand
set *data_structures.Set
}

// NewSAddCommand crea una nueva instancia de SAddCommand.
func NewSAddCommand(set *data_structures.Set) *SAddCommand {
return &SAddCommand{
BaseCommand: BaseCommand{commandName: "SADD"},
set: set,
}
}

// Execute ejecuta el comando SADD.
func (cmd *SAddCommand) Execute(args ...interface{}) (interface{}, error) {
if len(args) == 0 {
return nil, ErrInvalidArguments
}

for _, item := range args {
cmd.set.Add(item)
}

return "OK", nil
}

// SRemCommand elimina uno o más elementos de un conjunto.
type SRemCommand struct {
BaseCommand
set *data_structures.Set
}

// NewSRemCommand crea una nueva instancia de SRemCommand.
func NewSRemCommand(set *data_structures.Set) *SRemCommand {
return &SRemCommand{
BaseCommand: BaseCommand{commandName: "SREM"},
set: set,
}
}

// Execute ejecuta el comando SREM.
func (cmd *SRemCommand) Execute(args ...interface{}) (interface{}, error) {
if len(args) == 0 {
return nil, ErrInvalidArguments
}

for _, item := range args {
cmd.set.Remove(item)
}

return "OK", nil
}

// SIsMemberCommand verifica si un elemento es miembro de un conjunto.
type SIsMemberCommand struct {
BaseCommand
set *data_structures.Set
}

// NewSIsMemberCommand crea una nueva instancia de SIsMemberCommand.
func NewSIsMemberCommand(set *data_structures.Set) *SIsMemberCommand {
return &SIsMemberCommand{
BaseCommand: BaseCommand{commandName: "SISMEMBER"},
set: set,
}
}

// Execute ejecuta el comando SISMEMBER.
func (cmd *SIsMemberCommand) Execute(args ...interface{}) (interface{}, error) {
if len(args) != 1 {
return nil, ErrInvalidArguments
}

item := args[0]
return cmd.set.Contains(item), nil
}

// SMembersCommand devuelve todos los miembros de un conjunto.
type SMembersCommand struct {
BaseCommand
set *data_structures.Set
}

// NewSMembersCommand crea una nueva instancia de SMembersCommand.
func NewSMembersCommand(set *data_structures.Set) *SMembersCommand {
return &SMembersCommand{
BaseCommand: BaseCommand{commandName: "SMEMBERS"},
set: set,
}
}

// Execute ejecuta el comando SMEMBERS.
func (cmd *SMembersCommand) Execute(args ...interface{}) (interface{}, error) {
if len(args) != 0 {
return nil, ErrInvalidArguments
}

return cmd.set.Items(), nil
}
Loading

0 comments on commit dbbbf32

Please sign in to comment.