Skip to content

Commit 608d770

Browse files
committed
Added Internal ID representation
1 parent 8138392 commit 608d770

File tree

132 files changed

+6705
-2028
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+6705
-2028
lines changed

core/id/cache.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Copyright 2024 Dolthub, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package id
16+
17+
import (
18+
"hash/crc32"
19+
"strconv"
20+
)
21+
22+
// builtinOidLimit is the largest OID that Postgres will assign to built-in items, so we use this to mitigate conflicts
23+
// with existing and future built-in OIDs.
24+
const builtinOidLimit = 65535
25+
26+
var (
27+
// crcTable is the table that is used for our CRC operations.
28+
crcTable = crc32.MakeTable(crc32.Castagnoli)
29+
// globalCache is the cache structure that is used for the server session.
30+
globalCache = &cacheStruct{
31+
toOID: make(map[Internal]uint32),
32+
toInternal: make(map[uint32]Internal),
33+
}
34+
)
35+
36+
// cacheStruct is the cache structure that holds mappings between the Internal ID and external OID (used by Postgres).
37+
// The mappings are temporary, and exist only within a server session. We must discourage users from storing converted
38+
// OIDs, and to use the actual OID type, since the type uses Internal IDs so long as it's not returned to the user.
39+
type cacheStruct struct {
40+
toOID map[Internal]uint32
41+
toInternal map[uint32]Internal
42+
}
43+
44+
// Cache returns the global cache that is used for the server session.
45+
func Cache() *cacheStruct {
46+
return globalCache
47+
}
48+
49+
// ToOID returns the OID associated with the given Internal ID.
50+
func (cache *cacheStruct) ToOID(id Internal) uint32 {
51+
// If the ID is in the cache, then we can just return its associated OID
52+
if oid, ok := cache.toOID[id]; ok {
53+
return oid
54+
}
55+
if id.Section() == Section_OID {
56+
oid, _ := strconv.ParseUint(id.Segment(0), 10, 32)
57+
return uint32(oid)
58+
}
59+
underlyingBytes := id.UnderlyingBytes()
60+
oid := crc32.Checksum(underlyingBytes, crcTable)
61+
// If the generated OID is valid, then we'll add it to the cache and return it
62+
if _, ok := cache.toInternal[oid]; !ok && oid > builtinOidLimit {
63+
cache.toOID[id] = oid
64+
cache.toInternal[oid] = id
65+
return oid
66+
}
67+
// In this case, the OID is not valid, so we'll run a small loop to generate an OID based on the actual ID.
68+
// This retains some level of determinism for OID to ID relationships.
69+
modifiedBytes := make([]byte, len(underlyingBytes)+1)
70+
copy(modifiedBytes[1:], underlyingBytes)
71+
for i := byte(0); i < 255; i++ {
72+
modifiedBytes[0] = i
73+
oid = crc32.Checksum(underlyingBytes, crcTable)
74+
if _, ok := cache.toInternal[oid]; !ok && oid > builtinOidLimit {
75+
cache.toOID[id] = oid
76+
cache.toInternal[oid] = id
77+
return oid
78+
}
79+
}
80+
// If we're here, then we'll just search for an empty OID as a last resort
81+
for i := uint32(4294967295); i > builtinOidLimit; i-- {
82+
if _, ok := cache.toInternal[oid]; !ok {
83+
cache.toOID[id] = oid
84+
cache.toInternal[oid] = id
85+
return oid
86+
}
87+
}
88+
// We must have over 4 billion items in the database, so we'll panic since there's nothing we can do
89+
panic("all OIDs have been taken")
90+
}
91+
92+
// ToInternal returns the Internal ID associated with the given OID.
93+
func (cache *cacheStruct) ToInternal(oid uint32) Internal {
94+
if id, ok := cache.toInternal[oid]; ok {
95+
return id
96+
}
97+
// The OID is not in the cache, so it's invalid
98+
return ""
99+
}
100+
101+
// Exists returns whether the given Internal ID exists within the cache. This should primarily be used for the default
102+
// functions, as it's not guaranteed that user functions will be in the cache, especially after a server restart.
103+
func (cache *cacheStruct) Exists(id Internal) bool {
104+
_, ok := cache.toOID[id]
105+
return ok
106+
}
107+
108+
// setBuiltIn sets the given ID to the OID. This should only be used for the built-in items.
109+
func (cache *cacheStruct) setBuiltIn(id Internal, oid uint32) {
110+
if oid > builtinOidLimit {
111+
panic("oid is not a built-in")
112+
}
113+
cache.toOID[id] = oid
114+
cache.toInternal[oid] = id
115+
}
116+
117+
// update is used to change the OID mapping of an existing Internal ID that has been changed (where the Internal ID
118+
// points to the same logical item).
119+
func (cache *cacheStruct) update(old Internal, new Internal) {
120+
// If the old ID doesn't exist in the cache, then we don't have anything to update
121+
oid, ok := cache.toOID[old]
122+
if !ok {
123+
return
124+
}
125+
// We'll delete the old entry and add the new entry, keeping the OID the same for the server session
126+
delete(cache.toOID, old)
127+
delete(cache.toInternal, oid)
128+
cache.toOID[new] = oid
129+
cache.toInternal[oid] = new
130+
}

0 commit comments

Comments
 (0)