Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
gunplar committed Nov 12, 2024
1 parent e538d28 commit ce1c66d
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class POr() : ListProxy<IPropertyQuery>(IPropertyQuery::class), IPropertyQuery {
}

override fun toString(): String {
//TODO will not work like expected key=val1,val2, rather currently it is key1=val1,key2=val2
return joinToString(",")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ open class PQuery() : AnyObject(), IPropertyQuery {
* The parameter value of the operation; if any.
*/
var value by ANY
// TODO
// override fun toString(): String {
//
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
*/
package com.here.naksha.storage.http;

import static com.here.naksha.storage.http.RequestSender.KeyProperties;

import com.here.naksha.lib.core.models.naksha.Storage;
import com.here.naksha.storage.http.RequestSender.KeyProperties;
import com.here.naksha.storage.http.cache.RequestSenderCache;
import java.util.Map;
import naksha.base.Int64;
import naksha.base.JvmProxyUtil;
import naksha.model.*;
import naksha.model.objects.NakshaFeature;
import org.apache.commons.lang3.NotImplementedException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
Expand All @@ -40,6 +40,13 @@ public class HttpStorage implements IStorage {

public HttpStorage(@NotNull Storage storage) {
HttpStorageProperties properties = HttpStorage.getProperties(storage);
if (properties == null) {
if (!storage.getProperties().hasRaw(HttpStorageProperties.URL)) {
throw new IllegalArgumentException("A HTTP storage must have properties containing a 'url'");
}
properties = new HttpStorageProperties(
storage.getProperties().get(HttpStorageProperties.URL).toString(), null, null, null);
}
requestSender = RequestSenderCache.getInstance()
.getSenderWith(new KeyProperties(
storage.getId(),
Expand All @@ -53,7 +60,7 @@ public HttpStorage(@NotNull Storage storage) {
return new HttpStorageReadSession(context, useMaster, requestSender);
}

private static @NotNull HttpStorageProperties getProperties(@NotNull Storage storage) {
private static @Nullable HttpStorageProperties getProperties(@NotNull Storage storage) {
return JvmProxyUtil.box(storage.getProperties(), HttpStorageProperties.class);
}

Expand All @@ -63,7 +70,13 @@ public void close() {}
@NotNull
@Override
public IReadSession newReadSession(@Nullable SessionOptions options) {
return null;
boolean useMaster = false;
if (options != null) {
requestSender.keyProps.connectionTimeoutSec = options.connectTimeout;
requestSender.keyProps.socketTimeoutSec = options.socketTimeout;
useMaster = options.useMaster;
}
return new HttpStorageReadSession(NakshaContext.currentContext(), useMaster, requestSender);
}

@NotNull
Expand Down Expand Up @@ -132,7 +145,7 @@ public Tuple featureToTuple(@NotNull NakshaFeature feature) {
@NotNull
@Override
public ILock enterLock(@NotNull String id, @NotNull Int64 waitMillis) {
throw new NakshaException(NakshaError.NOT_IMPLEMENTED, "enterLock", null, null);
throw new NotImplementedException("Enter lock not supported");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class HttpStorageProperties extends NakshaProperties {
"Content-Type", "application/json",
"Accept-Encoding", "gzip");

private static final String URL = "url";
static final String URL = "url";
private static final String CONNECTION_TIMEOUT = "connectTimeout";
private static final String SOCKET_TIMEOUT = "socketTimeout";
private static final String HEADERS = "headers";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,6 @@ private static String gzipDecode(byte[] encoded) {
}
}

// static SuccessResponse createHttpResultFromFeatureList(final @NotNull List<Tuple> features) {
// final List<ResultTuple> tuples = new ArrayList<>();
// for (final NakshaFeature feature : features) {
// tuples.add(new ResultTuple());
// codec.setOp(EExecutedOp.READ);
// codec.setFeature(feature);
// codec.setId(feature.getId());
// codecs.add(codec);
// }
//
// final HeapCacheCursor<XyzFeature, XyzFeatureCodec> cursor = new HeapCacheCursor<>(codecFactory, codecs,
// null);
// return new SuccessResponse(cursor);
// }

/**
* @return null if http status is success (200-299)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class RequestSender {
private final HttpClient httpClient;

@NotNull
private final RequestSender.KeyProperties keyProps;
final RequestSender.KeyProperties keyProps;

public RequestSender(@NotNull RequestSender.KeyProperties keyProps) {
this.keyProps = keyProps;
Expand Down Expand Up @@ -114,12 +114,49 @@ public boolean hasKeyProps(KeyProperties thatKeyProps) {
/**
* Set of properties that are just enough to construct the sender
* and distinguish unambiguously between objects
* in terms of their effective configuration
* in terms of their effective configuration.
* Objects of this class are compared based on their contents, not on object reference.
*/
public record KeyProperties(
@NotNull String name,
@NotNull String hostUrl,
@NotNull Map<String, String> defaultHeaders,
long connectionTimeoutSec,
long socketTimeoutSec) {}
public static class KeyProperties {
private final String name;
private final String hostUrl;
private final Map<String, String> defaultHeaders;
long connectionTimeoutSec;
long socketTimeoutSec;

public KeyProperties(
@NotNull String name,
@NotNull String hostUrl,
@NotNull Map<String, String> defaultHeaders,
long connectionTimeoutSec,
long socketTimeoutSec) {
this.name = name;
this.hostUrl = hostUrl;
this.defaultHeaders = defaultHeaders;
this.connectionTimeoutSec = connectionTimeoutSec;
this.socketTimeoutSec = socketTimeoutSec;
}

public String getName() {
return name;
}

public String getHostUrl() {
return hostUrl;
}

public Map<String, String> getDefaultHeaders() {
return defaultHeaders;
}

@Override
public boolean equals(Object o) {
if (!(o instanceof KeyProperties kepProps)) return false;
return (name.equals(kepProps.name)
&& hostUrl.equals(kepProps.hostUrl)
&& defaultHeaders.equals(kepProps.defaultHeaders)
&& connectionTimeoutSec == kepProps.connectionTimeoutSec
&& socketTimeoutSec == kepProps.socketTimeoutSec);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
*/
package com.here.naksha.storage.http.cache;

import static com.here.naksha.storage.http.RequestSender.KeyProperties;

import com.here.naksha.storage.http.RequestSender;
import com.here.naksha.storage.http.RequestSender.KeyProperties;
import java.util.concurrent.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -48,7 +47,7 @@ public static RequestSenderCache getInstance() {
@NotNull
public RequestSender getSenderWith(KeyProperties keyProperties) {
return requestSenders.compute(
keyProperties.name(), (__, cachedSender) -> getUpdated(cachedSender, keyProperties));
keyProperties.getName(), (__, cachedSender) -> getUpdated(cachedSender, keyProperties));
}

private @NotNull RequestSender getUpdated(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ void testOneId() {
TimeUnit.HOURS
);

assertEquals(PROP_ID_1, PROP_ID_1_COPY);
assertNotSame(PROP_ID_1, PROP_ID_1_COPY);

// Tests
Expand Down Expand Up @@ -124,7 +123,6 @@ void testOneIdMapChange() {
TimeUnit.HOURS
);

assertEquals(PROP_ID_1, PROP_ID_1_MAP_COPIED);
assertNotSame(PROP_ID_1, PROP_ID_1_MAP_COPIED);

// Tests
Expand Down

0 comments on commit ce1c66d

Please sign in to comment.