protected AbstractPrimitiveService(PrimitiveType primitiveType, Class<C> clientInterface) { this.primitiveType = primitiveType; this.clientInterface = clientInterface; this.serializer = Serializer.using(primitiveType.namespace()); }
byte[] encode(Object value) { return serializer.encode(value); }
<T> T decode(byte[] bytes) { return serializer.decode(bytes); }
/** * Encodes the given object using the configured {@link #userSerializer}. * * @param object the object to encode * @param <T> the object type * @return the encoded bytes */ protected <T> byte[] encode(T object) { return object != null ? userSerializer.encode(object) : null; }
/** * Decodes an internal object. * * @param bytes the bytes to decode * @param <T> the object type * @return the internal object */ private <T> T decodeInternal(byte[] bytes) { return INTERNAL_SERIALIZER.decode(bytes); }
/** * Takes a snapshot of the given service. * * @param writer the snapshot writer * @param service the service to snapshot */ private void snapshotService(SnapshotWriter writer, RaftServiceContext service) { writer.writeLong(service.serviceId().id()); writer.writeString(service.serviceType().name()); writer.writeString(service.serviceName()); byte[] config = Serializer.using(service.serviceType().namespace()).encode(service.serviceConfig()); writer.writeInt(config.length).writeBytes(config); try { service.takeSnapshot(writer); } catch (Exception e) { logger.error("Failed to take snapshot of service {}", service.serviceId(), e); } }
/** * Initializes a new service. */ @SuppressWarnings("unchecked") private RaftServiceContext initializeService(PrimitiveId primitiveId, PrimitiveType primitiveType, String serviceName, byte[] config) { RaftServiceContext oldService = raft.getServices().getService(serviceName); ServiceConfig serviceConfig = config == null ? new ServiceConfig() : Serializer.using(primitiveType.namespace()).decode(config); RaftServiceContext service = new RaftServiceContext( primitiveId, serviceName, primitiveType, serviceConfig, primitiveType.newService(serviceConfig), raft, threadContextFactory); raft.getServices().registerService(service); // If a service with this name was already registered, remove all of its sessions. if (oldService != null) { raft.getSessions().removeSessions(oldService.serviceId()); } return service; }
/** * Encodes the given object using the configured {@link #serializer}. * * @param object the object to encode * @param <T> the object type * @return the encoded bytes */ protected <T> byte[] encode(T object) { return object != null ? serializer.encode(object) : null; }
/** * Decodes the given object using the configured {@link #serializer}. * * @param bytes the bytes to decode * @param <T> the object type * @return the decoded object */ protected <T> T decode(byte[] bytes) { return bytes != null ? serializer.decode(bytes) : null; }
/** * Opens a new {@link MetaStore}, recovering metadata from disk if it exists. * <p> * The meta store will be loaded using based on the configured {@link StorageLevel}. If the storage level is persistent * then the meta store will be loaded from disk, otherwise a new meta store will be created. * * @return The metastore. */ public MetaStore openMetaStore() { return new MetaStore(this, Serializer.using(namespace)); }
protected <T, U> void registerHandler(String type, Function<T, CompletableFuture<U>> handler) { messagingService.registerHandler(type, (e, p) -> { CompletableFuture<byte[]> future = new CompletableFuture<>(); handler.apply(serializer.decode(p)).whenComplete((result, error) -> { if (error == null) { future.complete(serializer.encode(result)); } else { future.completeExceptionally(error); } }); return future; }); }
/** * Encodes the given object using the configured {@link #serializer}. * * @param object the object to encode * @param <T> the object type * @return the encoded bytes */ private <T> byte[] encode(T object) { return object != null ? serializer.encode(object) : null; }
/** * Decodes the given object using the configured {@link #serializer}. * * @param bytes the bytes to decode * @param <T> the object type * @return the decoded object */ protected <T> T decode(byte[] bytes) { return bytes != null ? serializer.decode(bytes) : null; }
private static <S> Collection<ProxySession<S>> createSessions( PrimitiveType primitiveType, Class<S> serviceType, Collection<SessionClient> partitions) { Serializer serializer = Serializer.using(primitiveType.namespace()); return partitions.stream() .map(partition -> new DefaultProxySession<>(partition, serviceType, serializer)) .collect(Collectors.toList()); }
protected <T, U> void registerHandler(String type, Function<T, CompletableFuture<U>> handler) { messagingService.registerHandler(type, (e, p) -> { CompletableFuture<byte[]> future = new CompletableFuture<>(); handler.apply(serializer.decode(p)).whenComplete((result, error) -> { if (error == null) { future.complete(serializer.encode(result)); } else { future.completeExceptionally(error); } }); return future; }); }