// Copyright 2017 JanusGraph Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.janusgraph.diskstorage.locking.consistentkey;
import org.janusgraph.diskstorage.ReadBuffer;
import org.janusgraph.diskstorage.StaticBuffer;
import org.janusgraph.diskstorage.WriteBuffer;
import org.janusgraph.diskstorage.util.StaticArrayBuffer;
import org.janusgraph.diskstorage.util.WriteBufferUtil;
import org.janusgraph.diskstorage.util.WriteByteBuffer;
import org.janusgraph.diskstorage.util.time.TimestampProvider;
import java.time.Instant;
/**
* Translate locking coordinates and metadata (data keys, data columns, data
* values, timestamps, and rids) into keys, columns, and values compatible with
* {@link ConsistentKeyLocker} and vice-versa.
*/
public class ConsistentKeyLockerSerializer {
public StaticBuffer toLockKey(StaticBuffer key, StaticBuffer column) {
WriteBuffer b = new WriteByteBuffer(key.length() + column.length() + 4);
b.putInt(key.length());
WriteBufferUtil.put(b,key);
WriteBufferUtil.put(b,column);
return b.getStaticBuffer();
}
public StaticBuffer toLockCol(Instant ts, StaticBuffer rid, TimestampProvider provider) {
WriteBuffer b = new WriteByteBuffer(rid.length() + 8);
b.putLong(provider.getTime(ts));
WriteBufferUtil.put(b, rid);
return b.getStaticBuffer();
}
public TimestampRid fromLockColumn(StaticBuffer lockKey, TimestampProvider provider) {
ReadBuffer r = lockKey.asReadBuffer();
int len = r.length();
long tsNS = r.getLong();
len -= 8;
byte[] curRid = new byte[len];
for (int i = 0; r.hasRemaining(); i++) {
curRid[i] = r.getByte();
}
StaticBuffer rid = new StaticArrayBuffer(curRid);
Instant time = provider.getTime(tsNS);
return new TimestampRid(time, rid);
}
}