| Total Lines | 57 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | package memory |
||
| 2 | |||
| 3 | import ( |
||
| 4 | "context" |
||
| 5 | "errors" |
||
| 6 | "sync" |
||
| 7 | |||
| 8 | "github.com/Permify/permify/internal/storage" |
||
| 9 | "github.com/Permify/permify/internal/storage/memory/constants" |
||
| 10 | db "github.com/Permify/permify/pkg/database/memory" |
||
| 11 | base "github.com/Permify/permify/pkg/pb/base/v1" |
||
| 12 | ) |
||
| 13 | |||
| 14 | var ( |
||
| 15 | headVersion map[string]string |
||
| 16 | mu sync.Mutex |
||
| 17 | ) |
||
| 18 | |||
| 19 | func init() { |
||
| 20 | headVersion = make(map[string]string) |
||
| 21 | } |
||
| 22 | |||
| 23 | // SchemaWriter - Structure for Schema Writer |
||
| 24 | type SchemaWriter struct { |
||
| 25 | database *db.Memory |
||
| 26 | } |
||
| 27 | |||
| 28 | // NewSchemaWriter creates a new SchemaWriter |
||
| 29 | func NewSchemaWriter(database *db.Memory) *SchemaWriter { |
||
| 30 | return &SchemaWriter{ |
||
| 31 | database: database, |
||
| 32 | } |
||
| 33 | } |
||
| 34 | |||
| 35 | // WriteSchema - Write Schema to repository |
||
| 36 | func (w *SchemaWriter) WriteSchema(_ context.Context, definitions []storage.SchemaDefinition) error { |
||
| 37 | var err error |
||
| 38 | txn := w.database.DB.Txn(true) |
||
| 39 | defer txn.Abort() |
||
| 40 | |||
| 41 | var tenantID string |
||
| 42 | var version string |
||
| 43 | |||
| 44 | for _, definition := range definitions { |
||
| 45 | if err = txn.Insert(constants.SchemaDefinitionsTable, definition); err != nil { |
||
| 46 | return errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String()) |
||
| 47 | } |
||
| 48 | tenantID = definition.TenantID |
||
| 49 | version = definition.Version |
||
| 50 | } |
||
| 51 | txn.Commit() |
||
| 52 | |||
| 53 | mu.Lock() |
||
| 54 | headVersion[tenantID] = version |
||
| 55 | mu.Unlock() |
||
| 56 | |||
| 57 | return nil |
||
| 58 | } |
||
| 59 |