1 /*
2  * Hunt - A redis client library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11  
12 module hunt.redis.StreamEntryID;
13 
14 import hunt.Exceptions;
15 import hunt.util.Common;
16 import hunt.util.Comparator;
17 
18 import std.concurrency : initOnce;
19 import std.conv;
20 import std.string;
21 
22 class StreamEntryID : Comparable!(StreamEntryID) { // , Serializable
23 
24     // dfmt off
25     /**
26     * Should be used only with XADD  
27     * 
28     * <code>
29     * XADD mystream * field1 value1 
30     * </code>
31     */
32     static StreamEntryID NEW_ENTRY() {
33         __gshared StreamEntryID inst;
34         return initOnce!inst(new class StreamEntryID {
35             override string toString(){
36             return "*";
37             }
38         });
39     } 
40   
41   
42   /**
43   * Should be used only with XGROUP CREATE  
44   * 
45   * <code>
46   * XGROUP CREATE mystream consumer-group-name $
47   * </code>
48   */
49   static StreamEntryID LAST_ENTRY() {
50     __gshared StreamEntryID inst;
51     return initOnce!inst(new class StreamEntryID {
52         override string toString(){
53         return "$";
54         }
55     });
56   }
57   
58   /**
59    * Should be used only with XREADGROUP
60    * <code>
61    * XREADGROUP $GroupName $ConsumerName BLOCK 2000 COUNT 10 STREAMS mystream >
62    * </code>
63    */
64   static StreamEntryID UNRECEIVED_ENTRY() {
65     __gshared StreamEntryID inst;
66     return initOnce!inst(new class StreamEntryID {
67         override string toString(){
68         return ">";
69         }
70     });
71   }
72 
73 // dfmt on
74 
75     private long time;
76     private long sequence;
77 
78     this() {
79         this(0, 0L);
80     }
81 
82     this(string id) {
83         string[] split = id.split("-");
84         this.time = split[0].to!long;
85         this.sequence = split[1].to!long;
86     }
87 
88     this(long time, long sequence) {
89         this.time = time;
90         this.sequence = sequence;
91     }
92 
93     override string toString() {
94         return time.to!string() ~ "-" ~ sequence.to!string();
95     }
96 
97     override bool opEquals(Object obj) {
98         if (this is obj)
99             return true;
100         if (obj is null)
101             return false;
102         StreamEntryID other = cast(StreamEntryID) obj;
103         if (other is null)
104             return false;
105         return this.time == other.time && this.sequence == other.sequence;
106     }
107 
108     override size_t toHash() @trusted nothrow {
109         try {
110             return this.toString().hashOf();
111         } catch (Exception ex) {
112             // do nothing
113             return 0;
114         }
115     }
116 
117     override int opCmp(StreamEntryID other) {
118         int timeComapre = compare(this.time, other.time);
119         return timeComapre != 0 ? timeComapre : compare(this.sequence, other.sequence);
120     }
121     
122     alias opCmp = Object.opCmp;
123 
124     long getTime() {
125         return time;
126     }
127 
128     long getSequence() {
129         return sequence;
130     }
131 
132     //   private void writeObject(java.io.ObjectOutputStream out) {
133     //     out.writeLong(this.time);
134     //     out.writeLong(this.sequence);
135     //   }
136 
137     //   private void readObject(java.io.ObjectInputStream in) {
138     //     this.time = in.readLong();
139     //     this.sequence = in.readLong();
140     //   }
141 }