internal VersionEntry GetVersionEntryByKey(string tableId, object recordKey, long versionKey, VersionEntry ve) { var rs = this.CQLExecute(string.Format(PartitionedCassandraVersionTable.CQL_GET_VERSION_ENTRY, tableId, recordKey.ToString(), versionKey)); var rse = rs.GetEnumerator(); rse.MoveNext(); Row row = rse.Current; if (row == null) { return(null); } if (ve == null) { return(new VersionEntry(versionKey, row.GetValue <long>("begintimestamp"), row.GetValue <long>("endtimestamp"), BytesSerializer.Deserialize(row.GetValue <byte[]>("record")), row.GetValue <long>("txid"), row.GetValue <long>("maxcommitts"))); } else { ve.Set( versionKey, row.GetValue <long>("begintimestamp"), row.GetValue <long>("endtimestamp"), BytesSerializer.Deserialize(row.GetValue <byte[]>("record")), row.GetValue <long>("txid"), row.GetValue <long>("maxcommitts")); return(ve); } }
internal override void Visit(UploadVersionRequest req) { VersionEntry ve = this.GetVersionEntryByKey(req.TableId, req.RecordKey, req.VersionKey, req.LocalVerEntry); if (ve == null) { this.CQLExecute(string.Format(PartitionedCassandraVersionTable.CQL_UPLOAD_VERSION_ENTRY, req.TableId, req.RecordKey.ToString(), req.VersionEntry.VersionKey, req.VersionEntry.BeginTimestamp, req.VersionEntry.EndTimestamp, BytesSerializer.ToHexString(BytesSerializer.Serialize(req.VersionEntry.Record)), req.VersionEntry.TxId, req.VersionEntry.MaxCommitTs)); req.RemoteVerEntry = req.VersionEntry; req.Result = true; } else // write-write conflict { req.RemoteVerEntry = req.VersionEntry; req.Result = false; } req.Finished = true; }
internal override void Visit(ReplaceWholeVersionRequest req) { this.CQLExecute(string.Format(PartitionedCassandraVersionTable.CQL_REPLACE_WHOLE_VERSION, req.TableId, req.VersionEntry.BeginTimestamp, req.VersionEntry.EndTimestamp, BytesSerializer.ToHexString(BytesSerializer.Serialize(req.VersionEntry.Record)), req.VersionEntry.TxId, req.VersionEntry.MaxCommitTs, req.RecordKey.ToString(), req.VersionEntry.VersionKey)); req.Result = 1L; req.Finished = true; }
/// <summary> /// Serialize essential properties in version entry to bytes array /// </summary> /// /// The format of bytes stream is like: /// ------- 8 bytes------ ------- 8 bytes---- --- 8 bytes------ 8 bytes----- --X bytes---- /// [beginTimestamp bytes][endTimestamp bytes][txId bytes][maxCommitTs bytes][record bytes] /// /// <returns>a byte array</returns> public static byte[] Serialize(VersionEntry versionEntry) { List <byte> byteList = new List <byte>(); byteList.AddRange(BitConverter.GetBytes(versionEntry.BeginTimestamp)); byteList.AddRange(BitConverter.GetBytes(versionEntry.EndTimestamp)); byteList.AddRange(BitConverter.GetBytes(versionEntry.TxId)); byteList.AddRange(BitConverter.GetBytes(versionEntry.MaxCommitTs)); byteList.AddRange(BytesSerializer.Serialize(versionEntry.Record)); return(byteList.ToArray()); }
internal override void Visit(UploadVersionRequest req) { bool applied = this.CQLExecuteWithIfApplied(string.Format(CassandraVersionTable.CQL_UPLOAD_VERSION_ENTRY, req.TableId, req.RecordKey.ToString(), req.VersionEntry.VersionKey, req.VersionEntry.BeginTimestamp, req.VersionEntry.EndTimestamp, BytesSerializer.ToHexString(BytesSerializer.Serialize(req.VersionEntry.Record)), req.VersionEntry.TxId, req.VersionEntry.MaxCommitTs)); req.Result = applied ? 1L : 0L; req.Finished = true; }
internal override void Visit(InitiGetVersionListRequest req) { VersionEntry emptyEntry = VersionEntry.InitEmptyVersionEntry(); this.CQLExecute(string.Format(PartitionedCassandraVersionTable.CQL_UPLOAD_VERSION_ENTRY, req.TableId, req.RecordKey.ToString(), emptyEntry.VersionKey, emptyEntry.BeginTimestamp, emptyEntry.EndTimestamp, BytesSerializer.ToHexString(BytesSerializer.Serialize(emptyEntry.Record)), emptyEntry.TxId, emptyEntry.MaxCommitTs)); req.Result = true; req.Finished = true; }
internal bool InsertCommittedVersion(string tableId, object recordKey, object payload, long txId, long commitTs) { if (!this.tableSet.Contains(tableId)) { lock (this.tableLock) { if (!this.tableSet.Contains(tableId)) { try { this.session.Execute($@" CREATE TABLE {CassandraLogStore.KEYSPACE + "." + tableId} ( recordKey blob, beginTs bigint, txId bigint, payload blob, PRIMARY KEY (recordKey, beginTs) );"); } catch (DriverException e) { return(false); } this.tableSet.Add(tableId); } } } try { PreparedStatement ps = this.session.Prepare($@" INSERT INTO {CassandraLogStore.KEYSPACE + "." + tableId} (recordKey, beginTs, txId, payload) VALUES (?, ?, ?, ?)"); Statement statement = ps.Bind(BytesSerializer.Serialize(recordKey), commitTs, txId, BytesSerializer.Serialize(payload)); session.Execute(statement); } catch (DriverException e) { return(false); } return(true); }
internal override void Visit(GetVersionListRequest req) { List <VersionEntry> entries = new List <VersionEntry>(); var rs = this.CQLExecute(string.Format(CassandraVersionTable.CQL_GET_VERSION_TOP_2, req.TableId, req.RecordKey.ToString())); foreach (var row in rs) { entries.Add(new VersionEntry( row.GetValue <long>("versionkey"), row.GetValue <long>("begintimestamp"), row.GetValue <long>("endtimestamp"), BytesSerializer.Deserialize(row.GetValue <byte[]>("record")), row.GetValue <long>("txid"), row.GetValue <long>("maxcommitts") )); } req.Result = entries; req.Finished = true; }
internal override void Visit(GetVersionListRequest req) { var rs = this.CQLExecute(string.Format(PartitionedCassandraVersionTable.CQL_GET_VERSION_TOP_2, req.TableId, req.RecordKey.ToString())); int cnt = 0; foreach (var row in rs) { req.LocalContainer[cnt].Set( row.GetValue <long>("versionkey"), row.GetValue <long>("begintimestamp"), row.GetValue <long>("endtimestamp"), BytesSerializer.Deserialize(row.GetValue <byte[]>("record")), row.GetValue <long>("txid"), row.GetValue <long>("maxcommitts") ); cnt += 1; } req.Result = cnt; req.Finished = true; }
/// <summary> /// Deserialize a version entry by the given recordKey, versionKey and content bytes /// </summary> /// <param name="recordKey"></param> /// <param name="versionKey"></param> /// <param name="bytes"></param> /// <returns></returns> public static VersionEntry Deserialize(long versionKey, byte[] bytes, VersionEntry versionEntry = null) { long beginTimestamp = BitConverter.ToInt64(bytes, VersionEntry.BEGIN_TIMESTAMP_OFFSET); long endTimestamp = BitConverter.ToInt64(bytes, VersionEntry.END_TIMESTAMP_OFFSET); long txId = BitConverter.ToInt64(bytes, VersionEntry.TXID_OFFSET); long maxCommitTs = BitConverter.ToInt64(bytes, VersionEntry.MAX_COMMIT_TS_OFFSET); byte[] recordBytes = new byte[bytes.Length - VersionEntry.RECORD_OFFSET]; Buffer.BlockCopy(bytes, VersionEntry.RECORD_OFFSET, recordBytes, 0, recordBytes.Length); object record = BytesSerializer.Deserialize(recordBytes); if (versionEntry == null) { return(new VersionEntry(versionKey, beginTimestamp, endTimestamp, record, txId, maxCommitTs)); } else { versionEntry.Set(versionKey, beginTimestamp, endTimestamp, record, txId, maxCommitTs); return(versionEntry); } }