go.temporal.io/server@v1.23.0/common/persistence/json_history_token_serializer.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package persistence 26 27 import "encoding/json" 28 29 type ( 30 jsonHistoryTokenSerializer struct{} 31 32 // historyPagingToken is used to serialize/deserialize pagination token for ReadHistoryBranchRequest 33 historyPagingToken struct { 34 LastEventID int64 35 // the pagination token passing to persistence 36 StoreToken []byte 37 // recording which branchRange it is reading 38 CurrentRangeIndex int 39 FinalRangeIndex int 40 41 // LastNodeID is the last known node ID attached to a history node 42 LastNodeID int64 43 // LastTransactionID is the last known transaction ID attached to a history node 44 LastTransactionID int64 45 } 46 ) 47 48 const notStartedIndex = -1 49 50 // newJSONHistoryTokenSerializer creates a new instance of TaskTokenSerializer 51 func newJSONHistoryTokenSerializer() *jsonHistoryTokenSerializer { 52 return &jsonHistoryTokenSerializer{} 53 } 54 55 func (t *historyPagingToken) SetRangeIndexes( 56 current int, 57 final int, 58 ) { 59 60 t.CurrentRangeIndex = current 61 t.FinalRangeIndex = final 62 } 63 64 func (j *jsonHistoryTokenSerializer) Serialize( 65 token *historyPagingToken, 66 ) ([]byte, error) { 67 68 data, err := json.Marshal(token) 69 return data, err 70 } 71 72 func (j *jsonHistoryTokenSerializer) Deserialize( 73 data []byte, 74 defaultLastEventID int64, 75 defaultLastNodeID int64, 76 defaultLastTransactionID int64, 77 ) (*historyPagingToken, error) { 78 79 if len(data) == 0 { 80 token := historyPagingToken{ 81 LastEventID: defaultLastEventID, 82 CurrentRangeIndex: notStartedIndex, 83 LastNodeID: defaultLastNodeID, 84 LastTransactionID: defaultLastTransactionID, 85 } 86 return &token, nil 87 } 88 89 token := historyPagingToken{} 90 err := json.Unmarshal(data, &token) 91 return &token, err 92 }