-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadabasstructure.go
321 lines (285 loc) · 9.7 KB
/
adabasstructure.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* Copyright © 2018-2025 Software GmbH, Darmstadt, Germany and/or its licensors
*
* SPDX-License-Identifier: Apache-2.0
*
* 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 adabas
import (
"bytes"
"fmt"
"sync"
"sync/atomic"
"time"
"unsafe"
"github.com/SoftwareAG/adabas-go-api/adatypes"
)
const acbxEyecatcher = 'F' /* F - EYECATCHER */
const acbxVersion = '2' /* 2 - VERSION */
// const eAcbxEyecatcher = 0xc6 /* EBCDIC F - EYECATCHER */
// const eAcbxVersion = 0xf2 /* EBCDIC 2 - VERSION */
const acbxLength = 192
// Dbid Adabas database identifier
type Dbid uint32
// Fnr Adabas file number identifier
type Fnr uint32
// Acbx Adabas Control block extended version
type Acbx struct {
Acbxtyp byte /* +00 ADALNK function code */
Acbxrsv1 byte /* +01 Reserved - must be 0x00 */
Acbxver [2]byte /* +02 Version: */
Acbxlen uint16 /* +04 ACBX Length */
Acbxcmd [2]byte /* +06 Command Code */
Acbxrsv2 uint16 /* +08 Reserved - must be 0x00 */
Acbxrsp uint16 /* +0A Response code */
Acbxcid [4]byte /* +0C Command ID */
Acbxdbid Dbid /* +10 Database ID */
Acbxfnr Fnr /* +14 File number */
Acbxisn adatypes.Isn /* +18 ISN */
Acbxisl uint64 /* +20 ISN Lower Limit */
Acbxisq uint64 /* +28 ISN Quantity */
Acbxcop [8]byte /* +30 Command option 1-8 */
Acbxadd1 [8]byte /* +38 Additions 1 */
Acbxadd2 [4]byte /* +40 Additions 2 */
Acbxadd3 [8]byte /* +44 Additions 3 */
Acbxadd4 [8]byte /* +4C Additions 4 */
Acbxadd5 [8]byte /* +54 Additions 5 - (0x00) */
Acbxadd6 [8]byte /* +5C Additions 6 */
Acbxrsv3 [4]byte /* +64 Reserved - must be 0x00 */
Acbxerra uint64 /* +68 Error offset in buffer (64 bit)*/
Acbxerrb [2]byte /* +70 Error char field (FN) */
Acbxerrc uint16 /* +72 Error subcode */
Acbxerrd byte /* +74 Error buffer ID */
Acbxerre byte /* +75 Reserved for future use */
Acbxerrf uint16 /* +76 Error buffer seq num (per ID)*/
Acbxsubr uint16 /* +78 Subcomp response code */
Acbxsubs uint16 /* +7A Subcomp response subcode */
Acbxsubt [4]byte /* +7C Subcomp error text */
Acbxlcmp uint64 /* +80 Compressed record length */
/* (negative of length if not */
/* all of record read) */
Acbxldec uint64 /* +88 Decompressed length of all */
/* returned data */
Acbxcmdt uint64 /* +90 Command time */
Acbxuser [16]byte /* +98 User field */
Acbxsesstime uint64 /* +A8 Time, part of Adabas Session ID*/
Acbxrsv4 [16]byte /* +B0 Reserved - must be 0x00 */
}
func newAcbx(dbid Dbid) *Acbx {
var cb Acbx
cb = Acbx{
Acbxdbid: dbid,
Acbxver: [2]byte{acbxEyecatcher, acbxVersion},
Acbxlen: uint16(unsafe.Sizeof(cb)),
Acbxcmd: empty.code(),
Acbxadd1: [8]byte{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
}
cb.resetAcbx()
return &cb
}
func resetArray(bArray []byte) {
for i := 0; i < len(bArray); i++ {
bArray[i] = ' '
}
}
func (acbx *Acbx) resetCop() {
for i := 0; i < len(acbx.Acbxcop); i++ {
acbx.Acbxcop[i] = ' '
}
}
func (acbx *Acbx) resetAcbx() {
/* memset((char*)((PACBX)pACBX),0,L_ACBX); */
acbx.Acbxver[0] = acbxEyecatcher
acbx.Acbxver[1] = acbxVersion
acbx.Acbxlen = uint16(acbxLength)
adatypes.Central.Log.Debugf("Reset acbx ver=%v", acbx.Acbxver)
adatypes.Central.Log.Debugf("Reset acbx cmd=%v", acbx.Acbxcmd)
adatypes.Central.Log.Debugf("Reset acbx len=%v", acbx.Acbxlen)
acbx.Acbxisn = 0
acbx.Acbxisq = 0
acbx.Acbxrsp = AdaAnact
acbx.resetCop()
for i := 0; i < len(acbx.Acbxadd1); i++ {
acbx.Acbxadd1[i] = ' '
}
resetArray(acbx.Acbxadd2[:])
}
func (acbx *Acbx) String() string {
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("ACBX:\n CmdCode: %c%c", acbx.Acbxcmd[0], acbx.Acbxcmd[1]))
buffer.WriteString(adatypes.FormatBytes(" CmdId: ", acbx.Acbxcid[:], len(acbx.Acbxcid[:]), 1, -1, false))
buffer.WriteString(fmt.Sprintf(" Dbid: %d Filenr: %d", acbx.Acbxdbid, acbx.Acbxfnr))
buffer.WriteString(fmt.Sprintf(" Responsecode: %d Subcode: %d\n", acbx.Acbxrsp, acbx.Acbxerrc))
buffer.WriteString(fmt.Sprintln(" Isn: ", acbx.Acbxisn, " ISN Lower Limit: ", acbx.Acbxisl, "ISN Quantity: ", acbx.Acbxisq))
buffer.WriteString(adatypes.FormatBytes(" CmdOption: ", acbx.Acbxcop[:], len(acbx.Acbxcop[:]), 1, -1, false))
buffer.WriteString(adatypes.FormatBytes(" Add1: ", acbx.Acbxadd1[:], len(acbx.Acbxadd1[:]), 1, -1, false))
buffer.WriteString(adatypes.FormatBytes(" Add2: ", acbx.Acbxadd2[:], len(acbx.Acbxadd2[:]), 1, -1, false))
buffer.WriteString(adatypes.FormatBytes(" Add3: ", acbx.Acbxadd3[:], len(acbx.Acbxadd3[:]), 1, -1, false))
buffer.WriteString(adatypes.FormatBytes(" Add4: ", acbx.Acbxadd4[:], len(acbx.Acbxadd4[:]), 1, -1, false))
buffer.WriteString(adatypes.FormatBytes(" Add5: ", acbx.Acbxadd5[:], len(acbx.Acbxadd5[:]), 1, -1, false))
buffer.WriteString(adatypes.FormatBytes(" Add6: ", acbx.Acbxadd6[:], len(acbx.Acbxadd6[:]), 1, -1, false))
buffer.WriteString(adatypes.FormatBytes(" User Area: ", acbx.Acbxuser[:], len(acbx.Acbxuser[:]), 0, -1, false))
return buffer.String()
}
const adabasIDSize = 32
// AID Adabas id
type AID struct {
level uint16
size uint16
Node [8]byte
User [8]byte
Pid uint32
Timestamp uint64
}
// Status of the referenced connection
type Status struct {
// ref string
open bool
openTransactions uint32
platform *adatypes.Platform
adabas *Adabas
version string
lock sync.Mutex
}
// ID Adabas Id
type ID struct {
connectionMap map[string]*Status
AdaID *AID
user string
pwd string
}
// Clone use base parameter of ID and create new one
// Base parameters are: User, Node
// Dynamic paramters are: Pid, Timestamps
func (adaid *ID) Clone() *ID {
AdaID := AID{level: 3, size: adabasIDSize}
aid := ID{AdaID: &AdaID, connectionMap: make(map[string]*Status),
user: adaid.user, pwd: adaid.pwd}
copy(AdaID.User[:], adaid.AdaID.User[:])
copy(AdaID.Node[:], adaid.AdaID.Node[:])
id := atomic.AddUint32(&idCounter, 1)
AdaID.Timestamp = uint64(time.Now().UnixNano() / 1000)
AdaID.Pid = uint32((AdaID.Timestamp - (AdaID.Timestamp % 100)) + uint64(id))
return &aid
}
// SetUser set the user id name into the ID, prepare byte array correctly
func (adaid *ID) SetUser(User string) {
for i := 0; i < 8; i++ {
adaid.AdaID.User[i] = ' '
}
copy(adaid.AdaID.User[:], User)
}
// SetHost set the host id name into the ID, prepare byte array correctly
func (adaid *ID) SetHost(Host string) {
for i := 0; i < 8; i++ {
adaid.AdaID.Node[i] = ' '
}
copy(adaid.AdaID.Node[:], Host)
}
// SetID set the pid into the ID, prepare byte array correctly
func (adaid *ID) SetID(pid uint32) {
adaid.AdaID.Pid = pid
}
// AddCredential add user id and password credentials
func (adaid *ID) AddCredential(user string, pwd string) {
adaid.user = user
adaid.pwd = pwd
}
// String return string representation of Adabas ID
func (adaid *ID) String() string {
return fmt.Sprintf("%s:%s [%d] %x/%d", string(adaid.AdaID.Node[0:8]), string(adaid.AdaID.User[0:8]),
adaid.AdaID.Pid, adaid.AdaID.Timestamp, adaid.AdaID.Timestamp)
}
func (adaid *ID) status(url string) *Status {
if s, ok := adaid.connectionMap[url]; ok {
return s
}
s := &Status{open: false}
adaid.connectionMap[url] = s
return s
}
func (adaid *ID) platform(url string) *adatypes.Platform {
s := adaid.status(url)
if s == nil {
return nil
}
return s.platform
}
func (adaid *ID) changeOpenState(url string, open bool) {
adatypes.Central.Log.Debugf("Register open=%v to url=%s", open, url)
s := adaid.status(url)
if s == nil {
return
}
s.open = open
if !open {
s.openTransactions = 0
}
}
func (adaid *ID) getAdabas(url *URL) *Adabas {
s := adaid.status(url.String())
if s.adabas == nil {
a, err := NewAdabas(url, adaid)
if err != nil {
return nil
}
return a
}
return s.adabas
}
func (adaid *ID) setAdabas(a *Adabas) {
s := adaid.status(a.URL.String())
if s.adabas == nil {
s.adabas = a
} else {
a.transactions = s.adabas.transactions
}
}
func (adaid *ID) isOpen(url string) bool {
s := adaid.status(url)
if s == nil {
return false
}
open := s.open
adatypes.Central.Log.Debugf("Check is open=%v to url=%s", open, url)
return open
}
func (adaid *ID) transactions(url string) uint32 {
s := adaid.status(url)
if s == nil {
return 0
}
return s.openTransactions
}
func (adaid *ID) incTransactions(url string) {
s := adaid.status(url)
if s == nil {
return
}
s.openTransactions++
}
func (adaid *ID) clearTransactions(url string) {
s := adaid.status(url)
if s == nil {
return
}
s.openTransactions = 0
}
// Close close all open adabas instance for this ID
func (adaid *ID) Close() {
for _, s := range adaid.connectionMap {
s.adabas.Close()
}
}