I have a RepeatedPtrField<M::Table> and a protobuf message M as:
message M {
message Table {
optional string guid = 1;
optional int64 schema_version = 2;
optional int64 data_version = 3;
repeated Column column = 4;
}
repeated Table table = 1;
}
How to I create a instance of M having the contents of RepeatedPtrField. I can write a for loop to copy data explicitly, but I am currently looking for something more concise, preferably using std::move() like optimization.
If you're using a new version of Protobuf, like Protobuf 3.6.0, RepeatedPtrField defines move constructor, and you can call std::move to achieve your goal.
If you're using an old version, you have to call Swap to do the work, as you mentioned in the comment.
Related
I have a class (as a protobuf) OrderChange, that represents when an order (imagine Amazon.com) changes:
message OrderChange {
Order old_order = 1;
Order new_order = 2;
}
message Order {
OrderType order_type = 1;
OrderCategory order_category = 2;
OrderStatus order_status = 3;
// many more fields
}
enum OrderType {
ORDER_TYPE_RETAIL = 0;
ORDER_TYPE_BUSINESS = 1;
}
enum OrderCategory {
ORDER_CATEGORY_ELECTRONICS = 0;
ORDER_CATEGORY_FOOD = 1;
ORDER_CATEGORY_FURNITURE = 2;
ORDER_CATEGORY_FITNESS = 3;
ORDER_CATEGORY_HOUSEHOLD = 4;
}
enum OrderStatus {
ORDER_STATUS_PAID = 0;
ORDER_STATUS_SHIPPED = 1;
ORDER_STATUS_DELIVERED = 2;
}
For each OrderChange object, I want to trigger some code for each conditional. For example if Order is RETAIL, FURNITURE, and PAID, I want to send a specific email.
OrderType and OrderCategory probably won't change between old_order and new_order, so my code can look at only new_order for these fields. Other fields such as OrderType will change, and my code can compare old_order and new_order to know what changed.
My problem is that Order has many fields, each with many values, so the number of conditional combinations is huge. Using only if/else or switch/case would be unmaintainable code.
So my question is, what pattern can I use to make these conditionals more maintainable?
Maybe I can break the fields into handlers - a handler for each OrderType, then each of these handlers contain a list of handlers for each OrderCategory, and continuing to nest a single field for each level until there are no more fields. My issue would be that beyond maybe the OrderType being the highest level field, there is no clear hierarchical relationship for the other fields. As in, it's not clear that OrderCategory handlers should contain OrderType handlers.
How should I design this?
Is the intent that there is only one action that should be triggered? Say you have actions that should run on RETAIL + PAID and RETAIL + FURNITURE + PAID, these clearly can compete with each other and there would need to be a priority type system to deal with ensuring only one handled it. Something like chain of responsibility could be used, where you register the handlers in priority order. If on the other hand, you want all eligible handlers to process it, then you can simply iterate all registered change handlers and pass each the change notification and allow each to process it, which is effectively just a simple observer.
I have an object like:
class Person {
Phone phone;
}
class Phone {
String number;
String prefix;
Phone(String n, String p) {
number = n;
prefix = p;
}
}
Now consider this code:
Person p = new Person();
p.phone = new Phone("444444", "01");
javers.commit(p);
p.phone = new Phone("555555", "01");
javers.commit(p);
In this case sees that the reference of Phone has changed. While that's good info, I don't really care about that. I just want to know when the value of the number field has changed, that's really what I am tracking.
How would I achieve that? I tried defining the Phone class as a ValueObject, but it doesn't seem to do the job, I still get it as reference change rather than value change in the resulting Commit snapshot. Should I register it as a Value instead?
Map Phone as ValueObject or let JaVers to apply default mapping, which is also ValueObject object. ValueObject are always compared property by property and never by reference. What do you mean by it doesn't seem to do the job?
I'm new to Java so if this has already been answered somewhere else then I either don't know enough to search for the correct things or I just couldn't understand the answers.
So the question being:
I have a bunch of objects in a list:
try(Stream<String> logs = Files.lines(Paths.get(args))) {
return logs.map(LogLine::parseLine).collect(Collectors.toList());
}
And this is how the properties are added:
LogLine line = new LogLine();
line.setUri(matcher.group("uri"));
line.setrequestDuration(matcher.group("requestDuration"));
....
How do I sort logs so that I end up with list where objects with same "uri" are displayed only once with average requestDuration.
Example:
object1.uri = 'uri1', object1.requestDuration = 20;
object2.uri = 'uri2', object2.requestDuration = 30;
object3.uri = 'uri1', object3.requestDuration = 50;
Result:
object1.uri = 'uri1', 35;
object2.uri = 'uri2', 30;
Thanks in advance!
Take a look at Collectors.groupingBy and Collectors.averagingDouble. In your case, you could use them as follows:
Map<String, Double> result = logLines.stream()
.collect(Collectors.groupingBy(
LogLine::getUri,
TreeMap::new,
Collectors.averagingDouble(LogLine::getRequestDuration)));
The Collectors.groupingBy method does what you want. It is overloaded, so that you can specify the function that returns the key to group elements by, the factory that creates the returned map (I'm using TreeMap here, because you want the entries ordered by key, in this case the URI), and a downstream collector, which collects the elements that match the key returned by the first parameter.
If you want an Integer instead of a Double value for the averages, consider using Collectors.averagingInt.
This assumes LogLine has getUri() and getRequestDuration() methods.
How to get the tag number of a particular field from a protobuf object after compiling into a C++ class?
Consider the below example protobuf message and I had compiled this message to get the corresponding C++ header for Person class
File: person.proto
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
}
In my C++ code
Person *foo = new Person();
foo->set_id(123);
foo->set_name("bar");
foo->set_email("baz#qux.com");
Now I want to get the tag number of each field in the person message like the below one
int tag_id = foo->some_method_to_get_tag_number_of_id(); \\tag number of id is 1
int tag_name = foo->some_method_to_get_tag_number_of_name(); \\tag number is 2
int tag_email = foo->some_method_to_get_tag_number_of_email(); \\tag number is 3
Is it possible to get the tag number, if so how?
Take a look at Descriptor. For example (untested, just to get the idea):
Person::descriptor()->FindFieldByName("id")->number()
I am getting following exception on windows while running the below
ERROR: operator does not exist: numeric = character varying Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts." while executing with query parameter
I am passing the Numeric String for parameter using function as a named parameter to the query
getUIDCount(String id) {
...
select count(UID) as icrd FROM UID_tbl WHERE id = ?
...
}
where id is numeric(5,0)" in table
Everything works well on Ubuntu but getting Error while running the same code on windows. I have to do the explicit casting just for windows. I am using PostgreSQL 9.4.3. I am using "org.hibernate.dialect.PostgreSQLDialec" and grails 2.3.11 with runtime 'org.postgresql:postgresql:9.3-1100-jdbc41'
updated with how it is getting called
def Integer getUIDSetSize(String _id)
{
Integer i = 0;
Sql sql = new Sql(dataSource);
String sqlt = """select count(UID) as icrd FROM UID_tbl WHERE _id = ?""";
log.trace(sqlt);
sql.eachRow(sqlt, [_id], { row -> i = row.icrd; });
return i;
}
This how it get called def _id1 = params._id1; count1 = HelperService.getUIDSetSize(_id1)
The workaround for casting from varchar to numeric is
CREATE CAST(VARCHAR AS NUMERIC) WITH INOUT AS IMPLICIT;
This is not the best solution and would suggest selective casting from the code.