我有Rails PostGIS,activerecord-postgis-adapter并且rgeo-geojson正在运行.
目前我可以使用默认的"object.json"URL来获取WKT/WKB格式的JSON字符串.它看起来像这样:
{"description":null,"id":1,"position":"POINT (10.0 47.0)"}
Run Code Online (Sandbox Code Playgroud)
但是现在我想要一个自定义MIME类型,所以我可以调用"object.geojson"来获取GeoJSON格式,如下所示:
{"description":null,"id":1,"position":{"type":"Point","coordinates": [10.0, 47.0]}}
Run Code Online (Sandbox Code Playgroud)
我发现将JSON编码器设置为GeoJSON的唯一方法是使用RGeo::ActiveRecord::GeometryMixin.set_json_generator(:geojson)和全局设置它RGeo::ActiveRecord::GeometryMixin.set_json_generator(:wkt).但我只是想在本地设置它,这可能吗?
我已经加入Mime::Type.register "application/json", :geojson, %w( text/x-json application/jsonrequest )到mime_types.rb和它的作品罚款:我可以在我的控制器使用此代码:
respond_to do |format|
format.json { render json: @object }
format.geojson { render text: "test" }
end
Run Code Online (Sandbox Code Playgroud)
我希望有人可以告诉我如何在不设置全局JSON渲染器的情况下将一些特定对象渲染到GeoJSON :geojson.!?
编辑:
我的对象在Rails控制台中看起来像这样:
#<Anchor id: 1, description: nil, position: #<RGeo::Geos::CAPIPointImpl:0x3fc93970aac0 "POINT (10.0 47.0)">>
它现在是我第四次尝试在Rails应用程序中实现history.js.我有一种方法运行得很好,但代码是如此丑陋.今天我再次查看代码并思考:如何让这更好,更容易,更干?!
remote: true为我的链接js.erb我的HTML看起来像:
<body>
<div id="content">
some content with buttons, etc.
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
该js.erb包含:
History.pushState(
{
func: '$(\'#content\').html(data);',
data: '<%= j(render template: "news/index", formats: [:html]) %>'
},
'test title',
'<%= request.url %>'
);
Run Code Online (Sandbox Code Playgroud)
然后history.js接受函数并为其提供数据.所以它用content新生成的代码替换-div.它还会更新URL.这段代码我必须放在每个(!)js.erb文件中.
remote: true为我的链接js.erb替换content-div的链接data-remote="true"都将获得ajax:success-handlerajax:success新URL上被推送到history.js但是内部仍然存在一个问题.然后我有JavaScript代码:
$(document).on('ajax:success', 'a[data-remote="true"]', function() { ... });
Run Code Online (Sandbox Code Playgroud)
问题是:ajax:success如果我替换div了链接(应触发事件)所在的-tag,则永远不会触发.
也许有人可以解决我的问题......
相关的前一个问题:
在按值(而不是列)分组后,从组中选择一个随机条目?
我当前的查询如下所示:
WITH
points AS (
SELECT unnest(array_of_points) AS p
),
gtps AS (
SELECT DISTINCT ON(points.p)
points.p, m.groundtruth
FROM measurement m, points
WHERE st_distance(m.groundtruth, points.p) < distance
ORDER BY points.p, RANDOM()
)
SELECT DISTINCT ON(gtps.p, gtps.groundtruth, m.anchor_id)
m.id, m.anchor_id, gtps.groundtruth, gtps.p
FROM measurement m, gtps
ORDER BY gtps.p, gtps.groundtruth, m.anchor_id, RANDOM()
Run Code Online (Sandbox Code Playgroud)
语义:
有两个输入值:
array_of_pointsdistance第一段(第1-6行):
第二段(第8-14行):
points表格内的每个点:从表格中获取一个距离< 的随机(!)groundtruth点measurementdistancegtps表中第三段(第16-19行):
groundtruth值gtps:获取所有anchor_id …sql postgresql postgis query-optimization aggregate-functions
我有以下查询:
SELECT DISTINCT ON (ps.p)
m.groundtruth, ps.p, ARRAY_AGG(m.anchor_id), ARRAY_AGG(m.id)
FROM
measurement m
JOIN
(SELECT unnest(point_array) AS p) AS ps
ON ST_DWithin(ps.p, m.groundtruth, distance)
GROUP BY ps.p, m.groundtruth
ORDER BY ps.p, RANDOM()
Run Code Online (Sandbox Code Playgroud)
输出看起来像这样:
groundtruth | p | anchor_array | id_array
------------------------------------------------------
G1 | P1 | {1,3,3,3,4} | {1,2,3,4,5}
G2 | P1 | {1,5,7} | {6,7,8}
G1 | P2 | {1,3,3,3,4} | {1,2,3,4,5}
Run Code Online (Sandbox Code Playgroud)
替代查询:
SELECT DISTINCT ON (ps.p)
m.groundtruth, ps.p, ARRAY_AGG(row(m.anchor_id, m.id))
...
Run Code Online (Sandbox Code Playgroud)
输出:
groundtruth | p | combined_array
-----------------------------------------------------------
G1 | …Run Code Online (Sandbox Code Playgroud) 我无法通过类继承工作获得Spring Data Rest.
我想要一个JSON端点来处理我所有的具体类.
public interface AbstractFooRepo extends KeyValueRepository<AbstractFoo, String> {}
Run Code Online (Sandbox Code Playgroud)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = MyFoo.class, name = "MY_FOO")
})
public abstract class AbstractFoo {
@Id public String id;
public String type;
}
Run Code Online (Sandbox Code Playgroud)
public class MyFoo extends AbstractFoo { }
Run Code Online (Sandbox Code Playgroud)
现在打电话POST /abstractFoos时{"type":"MY_FOO"},它告诉我:java.lang.IllegalArgumentException: PersistentEntity must not be null!.
这似乎发生了,因为Spring不知道MyFoo.
有没有办法告诉Spring Data REST MyFoo而不为它创建存储库和REST端点?
(我使用的是Spring Boot 1.5.1和Spring Data REST 2.6.0)
Application.java: …
inheritance spring spring-data spring-data-rest spring-data-keyvalue
我遇到了(重新)在读/查方面以正确顺序应用来自多个主题的事件的问题.
例:
在写/命令端,我们有2个具有n:m关系的聚合:
这些聚合在2个单独的事件流主题上产生以下事件(因为最佳实践说:每个聚合一个主题.我完全同意):
联系主题:
ContactCreated (contactId: "123", name: "Peter")ContactAddedToGroup (contactId: "123", groupId: "456")小组主题:
GroupCreated (groupId: "456", name: "Customers")在读取/查询方面(例如Elasticsearch)我想执行此查询:
Custo...Custo... (这应该不是问题)为此,有2种读取模型.示例数据:
{contactId: "123", name: "Peter", groups: [{id: "456", name: "Customers"}]}{groupId: "456", name: "Customers"}问题:
事件的顺序只能保证单个事件主题(如在Apache Kafka中).虽然3个活动可以通过多种方式读/查询侧消耗:1,2,3或1,3,2或3,1,2
怎么处理1,2,3?数据库伪语句示例:
INSERT Contact (contactId: "123", name: "Peter")FIND Group WHERE (groupId: "456") (不起作用,因为尚未插入组)UPDATE Contact WHERE (contactId: "123") ADD …concurrency eventual-consistency race-condition cqrs event-sourcing
我想创建一个 Discriminated Union Type,它不需要传递鉴别器值。
这是我当前的代码:
interface Single<T> {
multiple?: false // this is optional, because it should be the default
value: T
onValueChange: (value: T) => void
}
interface Multi<T> {
multiple: true
value: T[]
onValueChange: (value: T[]) => void
}
type Union<T> = Single<T> | Multi<T>
Run Code Online (Sandbox Code Playgroud)
为了测试,我使用这个:
function typeIt<T>(data: Union<T>): Union<T> {
return data;
}
const a = typeIt({ // should be Single<string>
value: "foo",
onValueChange: (value) => undefined // why value is of type any?
})
const b …Run Code Online (Sandbox Code Playgroud) 有没有一种简单的方法可以使用 Jackson 将对象序列化为 base64 编码的 JSON?(对象 -> JSON -> base64)
我尝试使用自定义StdSerializer,但这(当然)会导致无限循环:
class MySerializer extends StdSerializer<Foo> {
public void serialize(Foo value, JsonGenerator gen, SerializerProvider provider) {
StringWriter stringWriter = new StringWriter();
JsonGenerator newGen = gen.getCodec().getFactory().createGenerator(stringWriter);
gen.getCodec().getFactory().getCodec().writeValue(newGen, value);
String json = stringWriter.toString();
String base64 = new String(Base64.getEncoder().encode(json.getBytes()));
gen.writeString(base64);
}
}
Run Code Online (Sandbox Code Playgroud)
解决方法是将所有字段复制到另一个类并使用该类作为中间表示:
class TmpFoo {
public String field1;
public int field2;
// ...
}
class MySerializer extends StdSerializer<Foo> {
public void serialize(Foo value, JsonGenerator gen, SerializerProvider provider) {
TmpFoo tmp = …Run Code Online (Sandbox Code Playgroud) 我想构建某种FactoryFactory:基本上是一个返回工厂函数的通用函数。编写函数本身很简单,但我不知道如何为它做 TypeScript 类型。
该函数应该像这样使用:
const stubFactoryFunction = (...props) => (...values) => ({ /* ... */ });
const factory = stubFactoryFunction("prop1", "prop2");
const instance = factory("foo", 42);
console.log(instance); // { prop1: "foo", prop2: 42 }
Run Code Online (Sandbox Code Playgroud)
起初,我尝试将值类型作为数组提供:
type FactoryFunction<T extends any[]> =
<K extends string[]>(...props: K) =>
(...values: T[number]) =>
{[key in K[number]]: T[number]}
Run Code Online (Sandbox Code Playgroud)
但这将导致{ prop1: string | number, prop2: string | number},因为类型与数组索引不匹配。
接下来,我尝试将整个对象提供为泛型类型:
type FactoryFunction<T extends {[key: string]: any}> =
(...props: (keyof T)[]) =>
(...values: ???) => …Run Code Online (Sandbox Code Playgroud) 我最近从 Elasticsearch 6 升级到 7,偶然发现了 10000 次点击限制。
Changelog, Documentation, and I also found a single blog post from a company that tried this new feature and measured their performance gains.
But I'm still not sure how and why this feature works. Or does it only improve performance under special circumstances?
Especially when sorting is involved, I can't get my head around it. Because (at least in my world) when sorting a collection you have to visit every document, …
generics ×2
postgresql ×2
sql ×2
typescript ×2
arrays ×1
base64 ×1
concurrency ×1
cqrs ×1
geojson ×1
history.js ×1
inheritance ×1
internals ×1
jackson ×1
java ×1
json ×1
optimization ×1
postgis ×1
spring ×1
spring-data ×1
unions ×1
wkt ×1