mgodave commented on a change in pull request #1363: Schema registry 3/N
URL: https://github.com/apache/incubator-pulsar/pull/1363#discussion_r173833679
 
 

 ##########
 File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/schema/BookkeeperSchemaStorage.java
 ##########
 @@ -0,0 +1,477 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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 org.apache.pulsar.broker.service.schema;
+
+import static com.google.common.collect.Iterables.concat;
+import static com.google.common.collect.Lists.newArrayList;
+import static com.google.protobuf.ByteString.copyFrom;
+import static java.util.Collections.emptyMap;
+import static java.util.Objects.isNull;
+import static java.util.Objects.nonNull;
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static 
org.apache.pulsar.broker.service.schema.BookkeeperSchemaStorage.Functions.newSchemaEntry;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.protobuf.ByteString;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+import javax.validation.constraints.NotNull;
+import org.apache.bookkeeper.client.BKException;
+import org.apache.bookkeeper.client.BookKeeper;
+import org.apache.bookkeeper.client.BookKeeper.DigestType;
+import org.apache.bookkeeper.client.LedgerEntry;
+import org.apache.bookkeeper.client.LedgerHandle;
+import org.apache.bookkeeper.util.ZkUtils;
+import org.apache.pulsar.broker.PulsarService;
+import org.apache.pulsar.common.schema.SchemaVersion;
+import org.apache.pulsar.zookeeper.ZooKeeperCache;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.KeeperException.Code;
+import org.apache.zookeeper.ZooDefs;
+import org.apache.zookeeper.ZooKeeper;
+import org.apache.zookeeper.data.ACL;
+
+public class BookkeeperSchemaStorage implements SchemaStorage {
+    private static final String SchemaPath = "/schemas";
+    private static final List<ACL> Acl = ZooDefs.Ids.OPEN_ACL_UNSAFE;
+
+    private final PulsarService pulsar;
+    private final ZooKeeper zooKeeper;
+    private final ZooKeeperCache localZkCache;
+    private BookKeeper bookKeeper;
+
+    @VisibleForTesting
+    BookkeeperSchemaStorage(PulsarService pulsar) {
+        this.pulsar = pulsar;
+        this.localZkCache = pulsar.getLocalZkCache();
+        this.zooKeeper = localZkCache.getZooKeeper();
+    }
+
+    @VisibleForTesting
+    public void init() throws KeeperException, InterruptedException {
+        try {
+            zooKeeper.create(SchemaPath, new byte[]{}, Acl, 
CreateMode.PERSISTENT);
+        } catch (KeeperException.NodeExistsException error) {
+            // race on startup, ignore.
+        }
+    }
+
+    public void start() throws IOException {
+        this.bookKeeper = pulsar.getBookKeeperClientFactory().create(
+            pulsar.getConfiguration(),
+            pulsar.getZkClient()
+        );
+    }
+
+    @Override
+    public CompletableFuture<SchemaVersion> put(String key, byte[] value, 
byte[] hash) {
+        return putSchemaIfAbsent(key, value, 
hash).thenApply(LongSchemaVersion::new);
+    }
+
+    @Override
+    public CompletableFuture<StoredSchema> get(String key, SchemaVersion 
version) {
+        if (version == SchemaVersion.Latest) {
+            return getSchema(key);
+        } else {
+            LongSchemaVersion longVersion = (LongSchemaVersion) version;
+            return getSchema(key, longVersion.getVersion());
+        }
+    }
+
+    @Override
+    public CompletableFuture<SchemaVersion> delete(String key) {
+        return deleteSchema(key).thenApply(LongSchemaVersion::new);
+    }
+
+    @NotNull
+    private CompletableFuture<StoredSchema> getSchema(String schemaId) {
+        return getSchemaLocator(getSchemaPath(schemaId)).thenCompose(locator 
-> {
+
+            if (!locator.isPresent()) {
+                return completedFuture(null);
+            }
+
+            SchemaStorageFormat.SchemaLocator schemaLocator = 
locator.get().locator;
+            return readSchemaEntry(schemaLocator.getInfo().getPosition())
+                .thenApply(entry ->
+                    new StoredSchema(
+                        entry.getSchemaData().toByteArray(),
+                        new 
LongSchemaVersion(schemaLocator.getInfo().getVersion()),
+                        emptyMap()
+                    )
+                );
+        });
+    }
+
+    @Override
+    public SchemaVersion versionFromBytes(byte[] version) {
+        ByteBuffer bb = ByteBuffer.wrap(version);
+        return new LongSchemaVersion(bb.get());
 
 Review comment:
   Yup. Good catch. it was a type it should have been getLong() but I guess the 
tests pass since the version numbers tend to be low and fit within the first 
byte.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to