Skip to content
This repository has been archived by the owner on Jun 15, 2022. It is now read-only.

Commit

Permalink
Add server starter
Browse files Browse the repository at this point in the history
Open and open funtions renamed in db.go as open and opendAndLoad function names. Open name is used
in server.go to create a connection for server and database. Due to renaming Open and open functions
, test files, example code in /example and README changed.
  • Loading branch information
gozeloglu committed Oct 15, 2021
1 parent 0478c05 commit ecb843e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

func main() {
db, err := kvs.Open("users")
db, err := kvs.Open(":1234", "users")
if err != nil {
log.Fatalf(err.Error())
}
Expand Down
11 changes: 6 additions & 5 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ const (
fileExtension = ".kvs"
)

// Open creates data file for newly creating database. If the database file is
// open creates data file for newly creating database. If the database file is
// already exists, it returns error without creating anything. name indicates
// database name.
func Open(name string) (*Kvs, error) {
func open(name string) (*Kvs, error) {
fullPath := baseDir + name + fileExtension

// Check database's base directory
Expand All @@ -58,12 +58,13 @@ func Open(name string) (*Kvs, error) {
kv: m,
}, nil
} else {
return open(name)
return openAndLoad(name)
}
}

// open opens the named database for file operations.
func open(dbName string) (*Kvs, error) {
// openAndLoad opens the named database file for file operations. Also, loads
// the database file into map to in-memory operations.
func openAndLoad(dbName string) (*Kvs, error) {
mu := sync.Mutex{}
mu.Lock()
defer mu.Unlock()
Expand Down
12 changes: 6 additions & 6 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func TestOpen(t *testing.T) {
tmpDb, err := Open(t.Name())
tmpDb, err := open(t.Name())
if err != nil {
t.Fatalf(err.Error())
}
Expand All @@ -25,7 +25,7 @@ func TestOpen(t *testing.T) {
}

func TestOpenExistsFile(t *testing.T) {
tmpDb, err := Open(t.Name())
tmpDb, err := open(t.Name())
if err != nil {
t.Fatalf(err.Error())
}
Expand All @@ -38,7 +38,7 @@ func TestOpenExistsFile(t *testing.T) {
}
t.Logf("TmpDb created: %s", t.Name())

tmpDb2, err := Open(t.Name())
tmpDb2, err := open(t.Name())
if err != nil {
t.Fatalf("It should not be nil.")
}
Expand All @@ -55,7 +55,7 @@ func TestOpenExistsFile(t *testing.T) {
}

func TestKvs_Close(t *testing.T) {
db, err := Open(t.Name())
db, err := open(t.Name())
if err != nil {
t.Fatalf(err.Error())
}
Expand All @@ -75,7 +75,7 @@ func TestKvs_Close(t *testing.T) {
}

func TestWrite(t *testing.T) {
db, err := Open(t.Name())
db, err := open(t.Name())
if err != nil {
t.Fatalf(err.Error())
}
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestWrite(t *testing.T) {
}

func TestLoad(t *testing.T) {
db, err := Open(t.Name())
db, err := open(t.Name())
if err != nil {
t.Fatalf(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func main() {
db, err := kvs.Open("users")
db, err := kvs.Open(":1234", "users")
if err != nil {
log.Fatalf(err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func TestKvs_GetEmpty(t *testing.T) {
db, err := Open(t.Name())
db, err := open(t.Name())
if err != nil {
t.Fatalf(err.Error())
}
Expand All @@ -32,7 +32,7 @@ func TestKvs_GetEmpty(t *testing.T) {
}

func TestKvs_Get(t *testing.T) {
db, err := Open(t.Name())
db, err := open(t.Name())
if err != nil {
t.Fatalf(err.Error())
}
Expand Down
24 changes: 24 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package kvs

import (
"fmt"
"log"
"net/http"
)

// Open creates an HTTP and database connection. HTTP connection listens HTTP
// requests from client and database connection listens database commands from
// server. addr is the HTTP address and dbName is the database name. If HTTP
// address is empty, localhost and default port is used. In contrast, dbName
// needs to be specified. If it is not specified, it returns error and the
// connection is not established.
func Open(addr string, dbName string) (*Kvs, error) {
if dbName == "" {
return nil, fmt.Errorf("empty database name is not valid")
}
if addr == "" {
addr = ":1234"
}
go log.Fatal(http.ListenAndServe(addr, nil))
return open(dbName)
}
2 changes: 1 addition & 1 deletion set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func TestKvs_Set(t *testing.T) {
db, err := Open(t.Name())
db, err := open(t.Name())
if err != nil {
t.Fatalf(err.Error())
}
Expand Down

0 comments on commit ecb843e

Please sign in to comment.