-
Notifications
You must be signed in to change notification settings - Fork 37
/
listener.go
113 lines (94 loc) · 3.19 KB
/
listener.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2022-2023 Tigris Data, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kv
import (
"bytes"
"context"
"github.com/tigrisdata/tigris/internal"
)
const (
InsertEvent = "insert"
ReplaceEvent = "replace"
UpdateEvent = "update"
DeleteEvent = "delete"
)
type EventListenerCtxKey struct{}
// EventListener is listener to buffer all the changes in a transaction. It is attached by server layer in the context,
// and it is only responsible for buffering of the events but doesn't participate in the outcome of the transaction
// i.e. EventListener has no knowledge whether the transaction was committed or rolled back. The lifecycle of this
// listener is managed by QuerySession in server package.
type EventListener interface {
// OnSet buffers insert/replace/update events
OnSet(op string, table []byte, key Key, data *internal.TableData)
// OnClear buffers delete events
OnClear(op string, table []byte, key Key)
// GetEvents is used to access buffered events. These events may be shared by different participants callers are
// strongly discourage to modify the event and if needed copy it to some other buffer. Once transaction completes
// session may discard all the buffered events.
GetEvents() []*Event
}
type Event struct {
Op string
Table []byte
Key Key `json:",omitempty"`
Data *internal.TableData `json:",omitempty"`
Last bool
}
type DefaultListener struct {
Events []*Event
}
func (*DefaultListener) skip(table []byte) bool {
return !bytes.Equal(table[0:4], internal.UserTableKeyPrefix) &&
!bytes.Equal(table[0:4], internal.PartitionKeyPrefix)
}
func (l *DefaultListener) OnSet(op string, table []byte, key Key, data *internal.TableData) {
if l.skip(table) {
return
}
l.Events = append(l.Events, &Event{
Op: op,
Table: table,
Key: key,
Data: data,
})
}
func (l *DefaultListener) OnClear(op string, table []byte, key Key) {
if l.skip(table) {
return
}
l.Events = append(l.Events, &Event{
Op: op,
Table: table,
Key: key,
})
}
func (l *DefaultListener) GetEvents() []*Event {
return l.Events
}
type NoopEventListener struct{}
func (*NoopEventListener) OnSet(string, []byte, Key, *internal.TableData) {}
func (*NoopEventListener) OnClear(string, []byte, Key) {}
func (*NoopEventListener) GetEvents() []*Event { return nil }
func WrapEventListenerCtx(ctx context.Context) context.Context {
return context.WithValue(ctx, EventListenerCtxKey{}, &DefaultListener{})
}
func GetEventListener(ctx context.Context) EventListener {
a := ctx.Value(EventListenerCtxKey{})
if a != nil {
if conv, ok := a.(EventListener); ok {
return conv
}
}
return &NoopEventListener{}
}