forked from snowplow/snowplow-golang-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
159 lines (133 loc) · 3.9 KB
/
storage.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// Copyright (c) 2016-2019 Snowplow Analytics Ltd. All rights reserved.
//
// This program is licensed to you under the Apache License Version 2.0,
// and you may not use this file except in compliance with the Apache License Version 2.0.
// You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the Apache License Version 2.0 is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
//
package tracker
import (
"github.com/hashicorp/go-memdb"
"sync/atomic"
)
const (
DB_DRIVER = "sqlite3"
DB_TABLE_NAME = "events"
DB_COLUMN_ID = "id"
DB_COLUMN_EVENT = "event"
)
type Storage interface {
AddEventRow(payload Payload) bool
DeleteAllEventRows() int64
DeleteEventRows(ids []int) int64
GetAllEventRows() []EventRow
GetEventRowsWithinRange(eventRange int) []EventRow
}
type RawEventRow struct {
id int
event []byte
}
type RawEventRowUint struct {
id uint
event []byte
}
type EventRow struct {
id int
event Payload
}
// --- Memory Storage Implementation
type StorageMemory struct {
Db *memdb.MemDB
Index *uint32
}
func InitStorageMemory() *StorageMemory {
schema := &memdb.DBSchema{
Tables: map[string]*memdb.TableSchema{
DB_TABLE_NAME: {
Name: DB_TABLE_NAME,
Indexes: map[string]*memdb.IndexSchema{
DB_COLUMN_ID: {
Name: DB_COLUMN_ID,
Unique: true,
Indexer: &memdb.UintFieldIndex{Field: DB_COLUMN_ID},
},
DB_COLUMN_EVENT: {
Name: DB_COLUMN_EVENT,
Indexer: &memdb.StringFieldIndex{Field: DB_COLUMN_EVENT},
},
},
},
},
}
db, err := memdb.NewMemDB(schema)
checkErr(err)
return &StorageMemory{Db: db, Index: new(uint32)}
}
// AddEventRow adds a new event to the database
//
// NOTE: As entries are not auto-incremeneting the id is incremented manually which
// limits inserts to 4,294,967,295 in single session
func (s StorageMemory) AddEventRow(payload Payload) bool {
txn := s.Db.Txn(true)
byteBuffer := SerializeMap(payload.Get())
rer := &RawEventRowUint{event: byteBuffer, id: uint(atomic.AddUint32(s.Index, 1))}
err := txn.Insert(DB_TABLE_NAME, rer)
checkErr(err)
txn.Commit()
return true
}
// DeleteAllEventRows removes all rows within the memory store
func (s StorageMemory) DeleteAllEventRows() int64 {
txn := s.Db.Txn(true)
result, err := txn.DeleteAll(DB_TABLE_NAME, DB_COLUMN_ID)
checkErr(err)
txn.Commit()
return int64(result)
}
// DeleteEventRows removes all rows with matching identifiers
func (s StorageMemory) DeleteEventRows(ids []int) int64 {
txn := s.Db.Txn(true)
deleteCount := 0
for _, id := range ids {
result, err := txn.DeleteAll(DB_TABLE_NAME, DB_COLUMN_ID, uint(id))
checkErr(err)
deleteCount += result
}
txn.Commit()
return int64(deleteCount)
}
// GetAllEventRows returns all rows within the memory store
func (s StorageMemory) GetAllEventRows() []EventRow {
eventItems := []EventRow{}
txn := s.Db.Txn(false)
defer txn.Abort()
result, err := txn.Get(DB_TABLE_NAME, DB_COLUMN_ID)
checkErr(err)
for row := result.Next(); row != nil; row = result.Next() {
item := row.(*RawEventRowUint)
eventMap, _ := DeserializeMap(item.event)
eventItems = append(eventItems, EventRow{int(item.id), Payload{eventMap}})
}
return eventItems
}
// GetEventRowsWithinRange returns all available events or a maximal slice
func (s StorageMemory) GetEventRowsWithinRange(eventRange int) []EventRow {
eventItems := s.GetAllEventRows()
if len(eventItems) <= eventRange {
return eventItems
} else {
return eventItems[:eventRange]
}
}
// --- Helpers
// checkErr throws a panic for all non-nil errors passed to it.
func checkErr(err error) {
if err != nil {
panic(err.Error())
}
}