Skip to content

Commit

Permalink
Model > Collection, Entity > Instance (textileio#261)
Browse files Browse the repository at this point in the history
* Refactor method name

Signed-off-by: Aaron Sutula <[email protected]>

* RegisterCollection refactor

Signed-off-by: Aaron Sutula <[email protected]>

* wip on model > collection

Signed-off-by: Aaron Sutula <[email protected]>

* more refactoring

Signed-off-by: Aaron Sutula <[email protected]>

* refactor android code

Signed-off-by: Aaron Sutula <[email protected]>

* no more entity wording

Signed-off-by: Aaron Sutula <[email protected]>

* Update comment

Signed-off-by: Aaron Sutula <[email protected]>

* delete criterion_test.go

Signed-off-by: Aaron Sutula <[email protected]>

* delete db_test.go

Signed-off-by: Aaron Sutula <[email protected]>

* delete bench_test.go

Signed-off-by: Aaron Sutula <[email protected]>

* delete dispatcher_test.go

Signed-off-by: Aaron Sutula <[email protected]>

* delete manager_test.go

Signed-off-by: Aaron Sutula <[email protected]>

* delete model_test.go

Signed-off-by: Aaron Sutula <[email protected]>

* delete query_json_mode_test.go

Signed-off-by: Aaron Sutula <[email protected]>

* delete query_test.go

Signed-off-by: Aaron Sutula <[email protected]>

* Revert "delete criterion_test.go"

This reverts commit e98d4a6.

Signed-off-by: Aaron Sutula <[email protected]>

* Revert "delete db_test.go"

This reverts commit 3f22b4d.

Signed-off-by: Aaron Sutula <[email protected]>

* add test utils and -v

Signed-off-by: Aaron Sutula <[email protected]>

* Revert "delete bench_test.go"

This reverts commit 662fc01.

Signed-off-by: Aaron Sutula <[email protected]>

* Revert "delete dispatcher_test.go"

This reverts commit a56f1ef.

Signed-off-by: Aaron Sutula <[email protected]>

* Revert "delete manager_test.go"

This reverts commit 2fff263.

Signed-off-by: Aaron Sutula <[email protected]>

* Revert "delete model_test.go"

This reverts commit 3c0e6c9.

Signed-off-by: Aaron Sutula <[email protected]>

* remove funcs that were moved to testutils

Signed-off-by: Aaron Sutula <[email protected]>

* Revert "delete query_json_mode_test.go"

This reverts commit 4603eeb.

Signed-off-by: Aaron Sutula <[email protected]>

* Revert "delete query_test.go"

This reverts commit 1173cec.

Signed-off-by: Aaron Sutula <[email protected]>

* disable Fail/WithoutInstanceID test

Signed-off-by: Aaron Sutula <[email protected]>

* enable all tests, run serially

Signed-off-by: Aaron Sutula <[email protected]>

* fix android test

Signed-off-by: Aaron Sutula <[email protected]>

* back to parallel tests

Signed-off-by: Aaron Sutula <[email protected]>

* remove verbose tests

Signed-off-by: Aaron Sutula <[email protected]>

* make android tests depend on tests

Signed-off-by: Aaron Sutula <[email protected]>

* Update register schema to new collection

Signed-off-by: Aaron Sutula <[email protected]>
  • Loading branch information
asutula committed Mar 11, 2020
1 parent d9154ee commit 6958e22
Show file tree
Hide file tree
Showing 35 changed files with 1,622 additions and 1,618 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:

android-client:
name: Android Client Test
needs: [test]
runs-on: ubuntu-latest
steps:
- name: Set up Go
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Go to https://godoc.org/github.com/textileio/go-threads.
| [`dart-threads-client`](//github.com/textileio/dart-threads-client) | [![Threads version](https://img.shields.io/badge/dynamic/yaml?style=popout-square&color=3527ff&label=go-threads&prefix=v&query=packages.threads_client_grpc.version&url=https%3A%2F%2Fraw.githubusercontent.com%2Ftextileio%2Fdart-threads-client%2Fmaster%2Fpubspec.lock)](https://github.com/textileio/go-threads) [![Build status](https://img.shields.io/github/workflow/status/textileio/dart-threads-client/test/master.svg?style=popout-square)](https://github.com/textileio/dart-threads-client/actions?query=branch%3Amaster) | [![dart](https://img.shields.io/badge/dart-blueviolet.svg?style=popout-square)](https://github.com/textileio/dart-threads-client) [![flutter](https://img.shields.io/badge/flutter-blueviolet.svg?style=popout-square)](https://github.com/textileio/dart-threads-client) | A Dart client for threads daemon. |
| **Examples** |
| [`go-foldersync`](//github.com/textileio/go-foldersync) | [![Threads version](https://img.shields.io/github/v/release/textileio/go-threads?color=3529ff&label=go-threads&style=popout-square)](https://github.com/textileio/go-threads) [![Build status](https://img.shields.io/github/workflow/status/textileio/go-foldersync/Tests/master.svg?style=popout-square)](https://github.com/textileio/js-threads-client/actions?query=branch%3Amaster) | [![go-threads](https://img.shields.io/badge/golang-blueviolet.svg?style=popout-square)](https://github.com/textileio/go-foldersync) | An e2e demo to sync data between two golang clients. |
| [`js-foldersync`](//github.com/textileio/js-foldersync) | [![Threads version](https://img.shields.io/badge/dynamic/json.svg?style=popout-square&color=3527ff&label=go-threads&prefix=v&query=%24.dependencies%5B%27%40textile%2Fthreads-client-grpc%27%5D.version&url=https%3A%2F%2Fraw.githubusercontent.com%2Ftextileio%2Fjs-foldersync%2Fmaster%2Fpackage-lock.json)](https://github.com/textileio/go-threads) [![Build status](https://img.shields.io/github/workflow/status/textileio/js-foldersync/Test/master.svg?style=popout-square)](https://github.com/textileio/js-foldersync/actions?query=branch%3Amaster) | [![web](https://img.shields.io/badge/web-blueviolet.svg?style=popout-square)](https://github.com/textileio/js-foldersync) | A demo of writing and reading models with the js-threads-client. |
| [`js-foldersync`](//github.com/textileio/js-foldersync) | [![Threads version](https://img.shields.io/badge/dynamic/json.svg?style=popout-square&color=3527ff&label=go-threads&prefix=v&query=%24.dependencies%5B%27%40textile%2Fthreads-client-grpc%27%5D.version&url=https%3A%2F%2Fraw.githubusercontent.com%2Ftextileio%2Fjs-foldersync%2Fmaster%2Fpackage-lock.json)](https://github.com/textileio/go-threads) [![Build status](https://img.shields.io/github/workflow/status/textileio/js-foldersync/Test/master.svg?style=popout-square)](https://github.com/textileio/js-foldersync/actions?query=branch%3Amaster) | [![web](https://img.shields.io/badge/web-blueviolet.svg?style=popout-square)](https://github.com/textileio/js-foldersync) | A demo of writing and reading to Collections with the js-threads-client. |

## Contributing

Expand Down
152 changes: 76 additions & 76 deletions api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ import (
type ActionType int

const (
// ActionCreate represents an event for creating a new entity
// ActionCreate represents an event for creating a new instance
ActionCreate ActionType = iota + 1
// ActionSave represents an event for saving changes to an existing entity
// ActionSave represents an event for saving changes to an existing instance
ActionSave
// ActionDelete represents an event for deleting existing entity
// ActionDelete represents an event for deleting existing instance
ActionDelete
)

// Action represents a data event delivered to a listener
type Action struct {
Model string
Type ActionType
EntityID string
Entity []byte
Collection string
Type ActionType
InstanceID string
Instance []byte
}

// ListenActionType describes the type of event action when receiving data updates
Expand All @@ -52,9 +52,9 @@ const (

// ListenOption represents a filter to apply when listening for data updates
type ListenOption struct {
Type ListenActionType
Model string
EntityID string
Type ListenActionType
Collection string
InstanceID string
}

// ListenEvent is used to send data or error values for Listen
Expand Down Expand Up @@ -95,22 +95,22 @@ func (c *Client) NewDB(ctx context.Context) (string, error) {
return resp.GetID(), nil
}

// RegisterSchema registers a new model shecma
func (c *Client) RegisterSchema(ctx context.Context, dbID, name, schema string, indexes ...*db.IndexConfig) error {
idx := make([]*pb.RegisterSchemaRequest_IndexConfig, len(indexes))
// NewCollection creates a new collection
func (c *Client) NewCollection(ctx context.Context, dbID, name, schema string, indexes ...*db.IndexConfig) error {
idx := make([]*pb.NewCollectionRequest_IndexConfig, len(indexes))
for i, index := range indexes {
idx[i] = &pb.RegisterSchemaRequest_IndexConfig{
idx[i] = &pb.NewCollectionRequest_IndexConfig{
Path: index.Path,
Unique: index.Unique,
}
}
req := &pb.RegisterSchemaRequest{
req := &pb.NewCollectionRequest{
DBID: dbID,
Name: name,
Schema: schema,
Indexes: idx,
}
_, err := c.c.RegisterSchema(ctx, req)
_, err := c.c.NewCollection(ctx, req)
return err
}

Expand All @@ -132,26 +132,26 @@ func (c *Client) StartFromAddress(ctx context.Context, dbID string, addr ma.Mult
return err
}

// ModelCreate creates new instances of model objects
func (c *Client) ModelCreate(ctx context.Context, dbID, modelName string, items ...interface{}) error {
// Create creates new instances of objects
func (c *Client) Create(ctx context.Context, dbID, collectionName string, items ...interface{}) error {
values, err := marshalItems(items)
if err != nil {
return err
}

req := &pb.ModelCreateRequest{
DBID: dbID,
ModelName: modelName,
Values: values,
req := &pb.CreateRequest{
DBID: dbID,
CollectionName: collectionName,
Values: values,
}

resp, err := c.c.ModelCreate(ctx, req)
resp, err := c.c.Create(ctx, req)
if err != nil {
return err
}

for i, entity := range resp.GetEntities() {
err := json.Unmarshal([]byte(entity), items[i])
for i, instance := range resp.GetInstances() {
err := json.Unmarshal([]byte(instance), items[i])
if err != nil {
return err
}
Expand All @@ -160,30 +160,30 @@ func (c *Client) ModelCreate(ctx context.Context, dbID, modelName string, items
return nil
}

// ModelSave saves existing instances
func (c *Client) ModelSave(ctx context.Context, dbID, modelName string, items ...interface{}) error {
// Save saves existing instances
func (c *Client) Save(ctx context.Context, dbID, collectionName string, items ...interface{}) error {
values, err := marshalItems(items)
if err != nil {
return err
}

req := &pb.ModelSaveRequest{
DBID: dbID,
ModelName: modelName,
Values: values,
req := &pb.SaveRequest{
DBID: dbID,
CollectionName: collectionName,
Values: values,
}
_, err = c.c.ModelSave(ctx, req)
_, err = c.c.Save(ctx, req)
return err
}

// ModelDelete deletes data
func (c *Client) ModelDelete(ctx context.Context, dbID, modelName string, entityIDs ...string) error {
req := &pb.ModelDeleteRequest{
DBID: dbID,
ModelName: modelName,
EntityIDs: entityIDs,
// Delete deletes data
func (c *Client) Delete(ctx context.Context, dbID, collectionName string, instanceIDs ...string) error {
req := &pb.DeleteRequest{
DBID: dbID,
CollectionName: collectionName,
InstanceIDs: instanceIDs,
}
_, err := c.c.ModelDelete(ctx, req)
_, err := c.c.Delete(ctx, req)
return err
}

Expand All @@ -207,72 +207,72 @@ func (c *Client) GetDBLink(ctx context.Context, dbID string) ([]string, error) {
return res, nil
}

// ModelHas checks if the specified entities exist
func (c *Client) ModelHas(ctx context.Context, dbID, modelName string, entityIDs ...string) (bool, error) {
req := &pb.ModelHasRequest{
DBID: dbID,
ModelName: modelName,
EntityIDs: entityIDs,
// Has checks if the specified instances exist
func (c *Client) Has(ctx context.Context, dbID, collectionName string, instanceIDs ...string) (bool, error) {
req := &pb.HasRequest{
DBID: dbID,
CollectionName: collectionName,
InstanceIDs: instanceIDs,
}
resp, err := c.c.ModelHas(ctx, req)
resp, err := c.c.Has(ctx, req)
if err != nil {
return false, err
}
return resp.GetExists(), nil
}

// ModelFind finds records by query
func (c *Client) ModelFind(ctx context.Context, dbID, modelName string, query *db.JSONQuery, dummySlice interface{}) (interface{}, error) {
// Find finds instances by query
func (c *Client) Find(ctx context.Context, dbID, collectionName string, query *db.JSONQuery, dummySlice interface{}) (interface{}, error) {
queryBytes, err := json.Marshal(query)
if err != nil {
return nil, err
}
req := &pb.ModelFindRequest{
DBID: dbID,
ModelName: modelName,
QueryJSON: queryBytes,
req := &pb.FindRequest{
DBID: dbID,
CollectionName: collectionName,
QueryJSON: queryBytes,
}
resp, err := c.c.ModelFind(ctx, req)
resp, err := c.c.Find(ctx, req)
if err != nil {
return nil, err
}
return processFindReply(resp, dummySlice)
}

// ModelFindByID finds a record by id
func (c *Client) ModelFindByID(ctx context.Context, dbID, modelName, entityID string, entity interface{}) error {
req := &pb.ModelFindByIDRequest{
DBID: dbID,
ModelName: modelName,
EntityID: entityID,
// FindByID finds an instance by id
func (c *Client) FindByID(ctx context.Context, dbID, collectionName, instanceID string, instance interface{}) error {
req := &pb.FindByIDRequest{
DBID: dbID,
CollectionName: collectionName,
InstanceID: instanceID,
}
resp, err := c.c.ModelFindByID(ctx, req)
resp, err := c.c.FindByID(ctx, req)
if err != nil {
return err
}
err = json.Unmarshal([]byte(resp.GetEntity()), entity)
err = json.Unmarshal([]byte(resp.GetInstance()), instance)
return err
}

// ReadTransaction returns a read transaction that can be started and used and ended
func (c *Client) ReadTransaction(ctx context.Context, dbID, modelName string) (*ReadTransaction, error) {
func (c *Client) ReadTransaction(ctx context.Context, dbID, collectionName string) (*ReadTransaction, error) {
client, err := c.c.ReadTransaction(ctx)
if err != nil {
return nil, err
}
return &ReadTransaction{client: client, dbID: dbID, modelName: modelName}, nil
return &ReadTransaction{client: client, dbID: dbID, collectionName: collectionName}, nil
}

// WriteTransaction returns a read transaction that can be started and used and ended
func (c *Client) WriteTransaction(ctx context.Context, dbID, modelName string) (*WriteTransaction, error) {
func (c *Client) WriteTransaction(ctx context.Context, dbID, collectionName string) (*WriteTransaction, error) {
client, err := c.c.WriteTransaction(ctx)
if err != nil {
return nil, err
}
return &WriteTransaction{client: client, dbID: dbID, modelName: modelName}, nil
return &WriteTransaction{client: client, dbID: dbID, collectionName: collectionName}, nil
}

// Listen provides an update whenever the specified db, model type, or model instance is updated
// Listen provides an update whenever the specified db, collection, or instance is updated
func (c *Client) Listen(ctx context.Context, dbID string, listenOptions ...ListenOption) (<-chan ListenEvent, error) {
channel := make(chan ListenEvent)
filters := make([]*pb.ListenRequest_Filter, len(listenOptions))
Expand All @@ -291,9 +291,9 @@ func (c *Client) Listen(ctx context.Context, dbID string, listenOptions ...Liste
return nil, fmt.Errorf("unknown ListenOption.Type %v", listenOption.Type)
}
filters[i] = &pb.ListenRequest_Filter{
ModelName: listenOption.Model,
EntityID: listenOption.EntityID,
Action: action,
CollectionName: listenOption.Collection,
InstanceID: listenOption.InstanceID,
Action: action,
}
}
req := &pb.ListenRequest{
Expand Down Expand Up @@ -330,23 +330,23 @@ func (c *Client) Listen(ctx context.Context, dbID string, listenOptions ...Liste
break loop
}
action := Action{
Model: event.GetModelName(),
Type: actionType,
EntityID: event.GetEntityID(),
Entity: event.GetEntity(),
Collection: event.GetCollectionName(),
Type: actionType,
InstanceID: event.GetInstanceID(),
Instance: event.GetInstance(),
}
channel <- ListenEvent{Action: action}
}
}()
return channel, nil
}

func processFindReply(reply *pb.ModelFindReply, dummySlice interface{}) (interface{}, error) {
func processFindReply(reply *pb.FindReply, dummySlice interface{}) (interface{}, error) {
sliceType := reflect.TypeOf(dummySlice)
elementType := sliceType.Elem().Elem()
length := len(reply.GetEntities())
length := len(reply.GetInstances())
results := reflect.MakeSlice(sliceType, length, length)
for i, result := range reply.GetEntities() {
for i, result := range reply.GetInstances() {
target := reflect.New(elementType).Interface()
err := json.Unmarshal(result, target)
if err != nil {
Expand Down
Loading

0 comments on commit 6958e22

Please sign in to comment.