@@ -102,62 +102,58 @@ public KeyPrefixContainer fromCodecBuffer(@Nonnull CodecBuffer buffer) throws Co
102102 final ByteBuffer byteBuffer = buffer .asReadOnlyByteBuffer ();
103103 final int totalLength = byteBuffer .remaining ();
104104 final int startPosition = byteBuffer .position ();
105-
106105 final byte [] delimiterBytes = KEY_DELIMITER .getBytes (UTF_8 );
107-
108- int firstDelimiterIndex = findDelimiterInBuffer (byteBuffer , delimiterBytes );
109-
110- byteBuffer .position (startPosition );
111-
112- // If no delimiter found, entire buffer is key prefix
113- if (firstDelimiterIndex == -1 ) {
114- String keyPrefix = decodeStringFromBuffer (byteBuffer , totalLength );
115- return KeyPrefixContainer .get (keyPrefix , -1 , -1 );
116- }
117-
118- // Decode key prefix without copying
119- String keyPrefix = decodeStringFromBuffer (byteBuffer , firstDelimiterIndex );
120-
121- // Skip delimiter
122- byteBuffer .position (byteBuffer .position () + delimiterBytes .length );
123-
124- long version = -1 ;
125- long containerId = -1 ;
126-
127- if (byteBuffer .remaining () >= Long .BYTES ) {
128- version = byteBuffer .getLong ();
129-
130- if (byteBuffer .remaining () >= delimiterBytes .length + Long .BYTES ) {
131- // Skip delimiter
132- byteBuffer .position (byteBuffer .position () + delimiterBytes .length );
133- containerId = byteBuffer .getLong ();
134- }
135- }
136-
137- return KeyPrefixContainer .get (keyPrefix , version , containerId );
138- }
139-
140- /**
141- * Find delimiter in ByteBuffer without copying data.
142- * Returns relative position of delimiter, or -1 if not found.
143- */
144- private int findDelimiterInBuffer (ByteBuffer buffer , byte [] delimiter ) {
145- final int startPos = buffer .position ();
146- final int limit = buffer .limit ();
147-
148- for (int i = startPos ; i <= limit - delimiter .length ; i ++) {
149- boolean found = true ;
150- for (int j = 0 ; j < delimiter .length ; j ++) {
151- if (buffer .get (i + j ) != delimiter [j ]) {
152- found = false ;
106+ final int delimiterLength = delimiterBytes .length ;
107+
108+ // Check if we have at least one delimiter + long value
109+ if (totalLength >= delimiterLength + Long .BYTES ) {
110+ // Check for delimiter before the last 8 bytes - could be containerId or version
111+ boolean hasLastDelimiter = true ;
112+ int lastDelimiterStart = startPosition + totalLength - Long .BYTES - delimiterLength ;
113+ byteBuffer .position (lastDelimiterStart );
114+ for (byte delimiterByte : delimiterBytes ) {
115+ if (byteBuffer .get () != delimiterByte ) {
116+ hasLastDelimiter = false ;
153117 break ;
154118 }
155119 }
156- if (found ) {
157- return i - startPos ;
120+
121+ if (hasLastDelimiter ) {
122+ // Extract the last value
123+ long lastValue = byteBuffer .getLong ();
124+ int remainingLength = lastDelimiterStart - startPosition ;
125+
126+ // Check if there's another delimiter+long before this one
127+ if (remainingLength >= delimiterLength + Long .BYTES ) {
128+ boolean hasSecondLastDelimiter = true ;
129+ int secondLastDelimiterStart = startPosition + remainingLength - Long .BYTES - delimiterLength ;
130+ byteBuffer .position (secondLastDelimiterStart );
131+ for (byte delimiterByte : delimiterBytes ) {
132+ if (byteBuffer .get () != delimiterByte ) {
133+ hasSecondLastDelimiter = false ;
134+ break ;
135+ }
136+ }
137+
138+ if (hasSecondLastDelimiter ) {
139+ long version = byteBuffer .getLong ();
140+
141+ byteBuffer .position (startPosition );
142+ String keyPrefix = decodeStringFromBuffer (byteBuffer , secondLastDelimiterStart - startPosition );
143+ return KeyPrefixContainer .get (keyPrefix , version , lastValue );
144+ }
145+ }
146+
147+ // Only one delimiter+value pair - it's a version, not containerId
148+ byteBuffer .position (startPosition );
149+ String keyPrefix = decodeStringFromBuffer (byteBuffer , remainingLength );
150+ return KeyPrefixContainer .get (keyPrefix , lastValue , -1 );
158151 }
159152 }
160- return -1 ;
153+
154+ byteBuffer .position (startPosition );
155+ String keyPrefix = decodeStringFromBuffer (byteBuffer , totalLength );
156+ return KeyPrefixContainer .get (keyPrefix , -1 , -1 );
161157 }
162158
163159 /**
0 commit comments