简体   繁体   English

如何使用 protobuf 序列化 java.util.regex.Pattern?

[英]How to serialize a java.util.regex.Pattern using protobuf?

I have an object that I want to serialize using Protocol Buffers and store in redis.我有一个对象,我想使用协议缓冲区序列化并存储在 redis 中。 The object contains a java.util.regex.Pattern that is complied when the object is instantiated.该对象包含一个java.util.regex.Pattern ,该对象在实例化时会被编译。

public class SerializableEntity {
    private Pattern pattern;
    private List<String> options;
}

This pattern is used to validate inputs to a certain api.此模式用于验证特定 api 的输入。 Since compiling the pattern each time is expensive , I'm compiling the pattern once during instantiation and then reusing the same pattern instance each time the api is invoked.由于每次编译模式都很昂贵,我在实例化期间编译了一次模式,然后在每次调用 api 时重用相同的模式实例。 How do I serialize this compile Pattern field in the following schema so I when I de-serialize the object, I can use it without compiling the pattern again?如何在以下模式中序列化此编译Pattern字段,以便在反序列化对象时可以使用它而无需再次编译模式?

 message SerializableEntityProto {
     repeated string option = 1;
     // compiled pattern
 }

Thanks.谢谢。

java.util.regex.Pattern does not have encode and decode proto functions implemented in itself. java.util.regex.Pattern本身没有实现编码和解码 proto 函数。 However, you can implement that yourself pretty easy (as Andy Turner suggests).但是,您可以很容易地自己实现(正如安迪·特纳(Andy Turner)所建议的那样)。 Something like this:像这样的东西:

Proto原型

syntax = "proto2";

package termin4t0r;
option java_package = "com.example.termin4t0r";

// Proto for java.util.regex.Pattern
message RegexPatternProto {
  // See Pattern.pattern()
  optional string pattern = 1;
  // See Pattern.flags()
  optional int64 flags = 2;
}

Java encode and decode functions Java编码和解码功能

class RegexpPatternProtos {
  public static RegexPatternProto encode(java.util.regex.Pattern pattern) {
    return RegexPatternProto.newBuilder()
        .setPattern(pattern.pattern())
        .setFlags(pattern.flags())
        .build();
  }

  public static java.util.regex.Pattern decode(RegexPatternProto patternProto) {
    return new RegexPatternProto(
      patternProto.getPattern(), patternProto.getFlags());
  }
}

I leave the unittests as an exercise :) I even find serializing this way preferable as protocol buffers have forward and backward compatibility, whereas java serialization has problems with that.我将单元测试作为练习 :) 我什至发现以这种方式进行序列化更可取,因为协议缓冲区具有向前和向后兼容性,而 Java 序列化在这方面存在问题。

I think this is a case of square peg and round hole, protobuf and serialization is not meant to be used that way.我认为这是方钉和圆孔的情况,protobuf 和序列化不应该以这种方式使用。

Anyway it seems like you initialize a regex with every API call.无论如何,您似乎在每次 API 调用时都初始化了一个正则表达式。 I don't know how your app decides which Regex to use for a particular API, but you must start out with a Regex string to compile.我不知道您的应用程序如何决定将哪个 Regex 用于特定 API,但您必须从要编译的 Regex 字符串开始。 Instead of trying to serializing the pattern, store it in memory in a HashMap<String,Pattern> ( Regex string as a key and the compiled pattern as value).与其尝试序列化模式,不如将其存储在内存中的HashMap<String,Pattern> (正则表达式字符串作为键,编译后的模式作为值)。 And then get the pattern when you need it.然后在需要时获取模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用java.util.regex.Pattern在java中查找类似的IP - find similar IP in java using java.util.regex.Pattern java.util.regex.Pattern的正则表达式 - Regular expression for java.util.regex.Pattern Java反射:在JRuby中确定java.util.regex.Pattern的类 - Java Reflection: Determining class of java.util.regex.Pattern in JRuby Liferay中java.util.regex.Pattern处的java.lang.StackOverflowError - java.lang.StackOverflowError at at java.util.regex.Pattern in liferay 添加到java.util.regex.Pattern或合并它们 - Adding onto a java.util.regex.Pattern or Merging Them (java.util.regex.Pattern的问题)检查字符串的数字和字母 - (Problems with java.util.regex.Pattern) checking string for digits and letters java.util.regex.Pattern 可以进行部分匹配吗? - Can java.util.regex.Pattern do partial matches? 即使我在Eclipse中使用jre8,也无法使用java.util.regex.Pattern - Can't use java.util.regex.Pattern even though I'm using jre8 in eclipse java.util.regex.PatternSyntaxException:null(在java.util.regex.Pattern中)错误 - java.util.regex.PatternSyntaxException: null (in java.util.regex.Pattern) error java.util.regex.Pattern和java.util.regex.Matcher的设计有什么好处? - What is benefit in design of java.util.regex.Pattern and java.util.regex.Matcher?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM