|
| 1 | +/******************************************************************************* |
| 2 | + * ___ _ ____ ____ |
| 3 | + * / _ \ _ _ ___ ___| |_| _ \| __ ) |
| 4 | + * | | | | | | |/ _ \/ __| __| | | | _ \ |
| 5 | + * | |_| | |_| | __/\__ \ |_| |_| | |_) | |
| 6 | + * \__\_\\__,_|\___||___/\__|____/|____/ |
| 7 | + * |
| 8 | + * Copyright (c) 2014-2019 Appsicle |
| 9 | + * Copyright (c) 2019-2024 QuestDB |
| 10 | + * |
| 11 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 12 | + * you may not use this file except in compliance with the License. |
| 13 | + * You may obtain a copy of the License at |
| 14 | + * |
| 15 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | + * |
| 17 | + * Unless required by applicable law or agreed to in writing, software |
| 18 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | + * See the License for the specific language governing permissions and |
| 21 | + * limitations under the License. |
| 22 | + * |
| 23 | + ******************************************************************************/ |
| 24 | + |
| 25 | +package io.questdb.cairo; |
| 26 | + |
| 27 | +import io.questdb.cairo.vm.MemoryCMARWImpl; |
| 28 | +import io.questdb.std.FilesFacade; |
| 29 | +import io.questdb.std.MemoryTag; |
| 30 | +import io.questdb.std.Numbers; |
| 31 | +import io.questdb.std.Uuid; |
| 32 | +import io.questdb.std.str.CharSink; |
| 33 | +import io.questdb.std.str.Path; |
| 34 | +import io.questdb.std.str.Sinkable; |
| 35 | +import org.jetbrains.annotations.NotNull; |
| 36 | + |
| 37 | +/** |
| 38 | + * DataID handles the mapping of the .data_id file located at the root of the database. |
| 39 | + * Its role is to store a unique id (consisting of a randomly generated 128-bit UUID) |
| 40 | + * to uniquely "tag" a `db` directory so the contained tables can be uniquely identified |
| 41 | + * across backups and enterprise replication. |
| 42 | + * <p> |
| 43 | + * One shouldn't modify the data id in an unblank database as it may cause data loss. |
| 44 | + * </p> |
| 45 | + */ |
| 46 | +public final class DataID implements Sinkable { |
| 47 | + |
| 48 | + /** |
| 49 | + * The file that contains the serialized DataID value has a name that starts with a `.` |
| 50 | + * as this avoids a name clash with a potentially valid table name. |
| 51 | + */ |
| 52 | + public static final CharSequence FILENAME = ".data_id"; |
| 53 | + public static long FILE_SIZE = Long.SIZE * 2; // Storing UUID as binary |
| 54 | + private final CairoConfiguration configuration; |
| 55 | + private final Uuid id; |
| 56 | + |
| 57 | + public DataID(CairoConfiguration configuration, Uuid id) { |
| 58 | + this.id = id; |
| 59 | + this.configuration = configuration; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Read the `.data_id` file (or creates it if it doesn't exist yet with zero value) and returns its current value. |
| 64 | + * |
| 65 | + * @param configuration the configuration that is used to provide the FileFacade and DbRoot. |
| 66 | + * @return a new data id instance. |
| 67 | + */ |
| 68 | + public static DataID open(CairoConfiguration configuration) throws CairoException { |
| 69 | + long lo, hi; |
| 70 | + |
| 71 | + // N.B.: This will create a new empty file of null `FILE_SIZE` bytes if it doesn't exist. |
| 72 | + try (MemoryCMARWImpl mem = openDataIDFile(configuration)) { |
| 73 | + lo = mem.getLong(0); |
| 74 | + hi = mem.getLong(Long.BYTES); |
| 75 | + |
| 76 | + // If we've just created the file, it will still have empty bytes. |
| 77 | + // We need to represent this as a null UUID - our "uninitialized" state. |
| 78 | + if ((lo == 0) && (hi == 0)) { |
| 79 | + lo = hi = Numbers.LONG_NULL; |
| 80 | + } |
| 81 | + } |
| 82 | + return new DataID(configuration, new Uuid(lo, hi)); |
| 83 | + } |
| 84 | + |
| 85 | + public long getHi() { |
| 86 | + return id.getHi(); |
| 87 | + } |
| 88 | + |
| 89 | + public long getLo() { |
| 90 | + return id.getLo(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Returns whether the data id has been initialized or not. |
| 95 | + * |
| 96 | + * @return true if the data id is initialized. |
| 97 | + */ |
| 98 | + public boolean isInitialized() { |
| 99 | + return !Uuid.isNull(id.getLo(), id.getHi()); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Set the data id to a new value and writes it to `.data_id`. |
| 104 | + * This function should be used with care as it may lead to data losses from restore/replication. |
| 105 | + * |
| 106 | + * @param lo The low bits of the UUID value |
| 107 | + * @param hi The high bits of the UUID value |
| 108 | + */ |
| 109 | + public void set(long lo, long hi) { |
| 110 | + if ((lo == id.getLo()) && (hi == id.getHi())) { |
| 111 | + return; |
| 112 | + } |
| 113 | + try (MemoryCMARWImpl mem = openDataIDFile(configuration)) { |
| 114 | + mem.putLong(lo); |
| 115 | + mem.putLong(hi); |
| 116 | + mem.sync(false); |
| 117 | + } |
| 118 | + this.id.of(lo, hi); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void toSink(@NotNull CharSink<?> sink) { |
| 123 | + id.toSink(sink); |
| 124 | + } |
| 125 | + |
| 126 | + private static MemoryCMARWImpl openDataIDFile(CairoConfiguration configuration) { |
| 127 | + try (Path path = new Path()) { |
| 128 | + path.of(configuration.getDbRoot()); |
| 129 | + path.concat(FILENAME); |
| 130 | + |
| 131 | + final FilesFacade ff = configuration.getFilesFacade(); |
| 132 | + return new MemoryCMARWImpl(ff, path.$(), FILE_SIZE, -1, MemoryTag.MMAP_DEFAULT, configuration.getWriterFileOpenOpts()); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments