|
| 1 | +/* |
| 2 | + * Copyright 2024 Function Stream Org. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package fs |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/functionstream/function-stream/fs/api" |
| 21 | + "github.com/pkg/errors" |
| 22 | +) |
| 23 | + |
| 24 | +var ErrStateStoreNotLoaded = errors.New("state store not loaded") |
| 25 | + |
| 26 | +type FuncCtxImpl struct { |
| 27 | + api.FunctionContext |
| 28 | + store api.StateStore |
| 29 | + putStateFunc func(key string, value []byte) error |
| 30 | + getStateFunc func(key string) ([]byte, error) |
| 31 | +} |
| 32 | + |
| 33 | +func NewFuncCtxImpl(store api.StateStore) *FuncCtxImpl { |
| 34 | + putStateFunc := func(key string, value []byte) error { |
| 35 | + return ErrStateStoreNotLoaded |
| 36 | + } |
| 37 | + getStateFunc := func(key string) ([]byte, error) { |
| 38 | + return nil, ErrStateStoreNotLoaded |
| 39 | + } |
| 40 | + if store != nil { |
| 41 | + putStateFunc = store.PutState |
| 42 | + getStateFunc = store.GetState |
| 43 | + } |
| 44 | + return &FuncCtxImpl{store: store, |
| 45 | + putStateFunc: putStateFunc, |
| 46 | + getStateFunc: getStateFunc} |
| 47 | +} |
| 48 | + |
| 49 | +func (f *FuncCtxImpl) PutState(key string, value []byte) error { |
| 50 | + return f.putStateFunc(key, value) |
| 51 | +} |
| 52 | + |
| 53 | +func (f *FuncCtxImpl) GetState(key string) ([]byte, error) { |
| 54 | + return f.getStateFunc(key) |
| 55 | +} |
0 commit comments