-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
while using new instance db.
- Loading branch information
Showing
20 changed files
with
576 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package database | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/mesg-foundation/core/service" | ||
"github.com/syndtr/goleveldb/leveldb" | ||
) | ||
|
||
// InstanceDB describes the API of Instance database. | ||
type InstanceDB interface { | ||
// Get retrives instance by instance hash. | ||
Get(hash string) (*service.Instance, error) | ||
|
||
// Save saves instance to database. | ||
Save(i *service.Instance) error | ||
|
||
// Close closes underlying database connection. | ||
Close() error | ||
} | ||
|
||
// LevelDBInstanceDB is a database for storing services' instances. | ||
type LevelDBInstanceDB struct { | ||
db *leveldb.DB | ||
} | ||
|
||
// NewInstanceDB returns the database which is located under given path. | ||
func NewInstanceDB(path string) (*LevelDBInstanceDB, error) { | ||
db, err := leveldb.OpenFile(path, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &LevelDBInstanceDB{db: db}, nil | ||
} | ||
|
||
// marshal returns the byte slice from service. | ||
func (d *LevelDBInstanceDB) marshal(i *service.Instance) ([]byte, error) { | ||
return json.Marshal(i) | ||
} | ||
|
||
// unmarshal returns the service from byte slice. | ||
func (d *LevelDBInstanceDB) unmarshal(id string, value []byte) (*service.Instance, error) { | ||
var s service.Instance | ||
if err := json.Unmarshal(value, &s); err != nil { | ||
return nil, &DecodeError{ID: id} | ||
} | ||
return &s, nil | ||
} | ||
|
||
// Get retrives instance by instance hash. | ||
func (d *LevelDBInstanceDB) Get(hash string) (*service.Instance, error) { | ||
tx, err := d.db.OpenTransaction() | ||
if err != nil { | ||
return nil, err | ||
} | ||
b, err := tx.Get([]byte(hash), nil) | ||
if err != nil { | ||
tx.Discard() | ||
if err == leveldb.ErrNotFound { | ||
return nil, &ErrNotFound{ID: hash} | ||
} | ||
return nil, err | ||
} | ||
i, err := d.unmarshal(hash, b) | ||
if err != nil { | ||
tx.Discard() | ||
return nil, err | ||
} | ||
return i, tx.Commit() | ||
} | ||
|
||
// Save saves instance to database. | ||
func (d *LevelDBInstanceDB) Save(i *service.Instance) error { | ||
// check service | ||
if i.Hash == "" { | ||
return errCannotSaveWithoutHash | ||
} | ||
|
||
// open database transaction | ||
tx, err := d.db.OpenTransaction() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// encode service | ||
b, err := d.marshal(i) | ||
if err != nil { | ||
tx.Discard() | ||
return err | ||
} | ||
|
||
// save instance with hash. | ||
if err := tx.Put([]byte(i.Hash), b, nil); err != nil { | ||
tx.Discard() | ||
return err | ||
} | ||
|
||
return tx.Commit() | ||
} | ||
|
||
// Close closes database. | ||
func (d *LevelDBInstanceDB) Close() error { | ||
return d.db.Close() | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
syntax = "proto3"; | ||
|
||
package api; | ||
|
||
service Instance { | ||
rpc Create (CreateInstanceRequest) returns (CreateInstanceResponse) {} | ||
} | ||
|
||
message CreateInstanceRequest { | ||
string id = 1; // Service's sid or hash. | ||
repeated string env = 2; // Env vars to apply to service's instance on runtime. | ||
} | ||
|
||
message CreateInstanceResponse { | ||
string sid = 1; // Service's id. | ||
string hash = 2; // Service's instance hash. | ||
string serviceHash = 3; // Service's bare hash. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.