|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 VK DIGITAL TECHNOLOGIES LIMITED LIABILITY COMPANY |
| 3 | + * All Rights Reserved. |
| 4 | + */ |
| 5 | + |
| 6 | +package io.tarantool.spring.data35; |
| 7 | + |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.Map.Entry; |
| 10 | + |
| 11 | +import com.fasterxml.jackson.annotation.JsonFormat; |
| 12 | +import org.springframework.core.annotation.AnnotatedElementUtils; |
| 13 | +import org.springframework.data.keyvalue.core.AbstractKeyValueAdapter; |
| 14 | +import org.springframework.data.util.CloseableIterator; |
| 15 | +import org.springframework.lang.NonNull; |
| 16 | + |
| 17 | +import io.tarantool.client.crud.TarantoolCrudClient; |
| 18 | +import io.tarantool.spring.data.ProxyTarantoolCrudKeyValueAdapter; |
| 19 | +import io.tarantool.spring.data.mapping.model.CompositeKey; |
| 20 | + |
| 21 | +public class TarantoolCrudKeyValueAdapter extends AbstractKeyValueAdapter { |
| 22 | + |
| 23 | + private final ProxyTarantoolCrudKeyValueAdapter adapter; |
| 24 | + |
| 25 | + public TarantoolCrudKeyValueAdapter(@NonNull TarantoolCrudClient client) { |
| 26 | + super(new TarantoolQueryEngine(client)); |
| 27 | + this.adapter = new ProxyTarantoolCrudKeyValueAdapter(client); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public Object put(Object id, Object item, String keyspace) { |
| 32 | + return adapter.put(convertId(id), item, keyspace); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public boolean contains(Object id, String keyspace) { |
| 37 | + return adapter.contains(convertId(id), keyspace); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public Object get(Object id, String keyspace) { |
| 42 | + return adapter.get(convertId(id), keyspace); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public <T> T get(Object id, String keyspace, Class<T> type) { |
| 47 | + return adapter.get(convertId(id), keyspace, type); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public Object delete(Object id, String keyspace) { |
| 52 | + return adapter.delete(convertId(id), keyspace); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public <T> T delete(Object id, String keyspace, Class<T> type) { |
| 57 | + return adapter.delete(convertId(id), keyspace, type); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public Iterable<?> getAllOf(String keyspace) { |
| 62 | + return adapter.getAllOf(keyspace); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void deleteAllOf(String keyspace) { |
| 67 | + adapter.deleteAllOf(keyspace); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void clear() { |
| 72 | + adapter.clear(); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void destroy() throws Exception { |
| 77 | + adapter.destroy(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public long count(String keyspace) { |
| 82 | + return adapter.count(keyspace); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public CloseableIterator<Entry<Object, Object>> entries(String keyspace) { |
| 87 | + throw new UnsupportedOperationException("Not implemented yet"); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Convert the identifier to the form required by the tarantool-java-sdk driver. |
| 92 | + * |
| 93 | + * @param id identifier object |
| 94 | + * @return identifier in the required form |
| 95 | + */ |
| 96 | + private Object convertId(Object id) { |
| 97 | + if (id instanceof CompositeKey || hasJsonFormatArrayAnnotation(id)) { |
| 98 | + return id; |
| 99 | + } |
| 100 | + return Collections.singletonList(id); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Determine whether the identifier type is annotated with the {@link JsonFormat} annotation. |
| 105 | + * |
| 106 | + * @param id identifier object |
| 107 | + * @return return true if the annotation is present, false otherwise |
| 108 | + */ |
| 109 | + private boolean hasJsonFormatArrayAnnotation(Object id) { |
| 110 | + final JsonFormat jsonFormatAnnotation = |
| 111 | + AnnotatedElementUtils.findMergedAnnotation(id.getClass(), JsonFormat.class); |
| 112 | + |
| 113 | + return jsonFormatAnnotation != null |
| 114 | + && JsonFormat.Shape.ARRAY.equals(jsonFormatAnnotation.shape()); |
| 115 | + } |
| 116 | +} |
0 commit comments