-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(localstorage): extract storage interface, make registerable
- Loading branch information
Showing
11 changed files
with
131 additions
and
59 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
This file was deleted.
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
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,15 @@ | ||
package memstorage | ||
|
||
import ( | ||
"log/slog" | ||
"os" | ||
"testing" | ||
|
||
"github.com/humanlogio/humanlog/pkg/localstorage" | ||
) | ||
|
||
func TestMemoryStorage(t *testing.T) { | ||
localstorage.RunTest(t, func(t *testing.T) localstorage.Storage { | ||
return NewMemStorage(slog.New(slog.NewTextHandler(os.Stderr, nil))) | ||
}) | ||
} |
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,49 @@ | ||
package localstorage | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"time" | ||
|
||
typesv1 "github.com/humanlogio/api/go/types/v1" | ||
"github.com/humanlogio/humanlog/pkg/sink" | ||
) | ||
|
||
type StorageBuilder func(ctx context.Context, ll *slog.Logger, cfg map[string]interface{}) (Storage, error) | ||
|
||
var registry = make(map[string]StorageBuilder) | ||
|
||
func RegisterStorage(name string, builder StorageBuilder) { | ||
_, ok := registry[name] | ||
if ok { | ||
panic(fmt.Sprintf("already used: %q", name)) | ||
} | ||
registry[name] = builder | ||
} | ||
|
||
func Open(ctx context.Context, name string, ll *slog.Logger, cfg map[string]interface{}) (Storage, error) { | ||
builder, ok := registry[name] | ||
if !ok { | ||
return nil, fmt.Errorf("no storage engine with name %q", name) | ||
} | ||
return builder(ctx, ll, cfg) | ||
} | ||
|
||
type Storage interface { | ||
Queryable | ||
SinkFor(ctx context.Context, machineID, sessionID int64) (_ sink.Sink, heartbeatIn time.Duration, _ error) | ||
Heartbeat(ctx context.Context, machineID, sessionID int64) (time.Duration, error) | ||
} | ||
|
||
type Queryable interface { | ||
Query(context.Context, *typesv1.LogQuery) (<-chan Cursor, error) | ||
} | ||
|
||
type Cursor interface { | ||
IDs() (machineID, sessionID int64) | ||
Next(context.Context) bool | ||
Event(*typesv1.LogEvent) error | ||
Err() error | ||
Close() error | ||
} |
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