1 // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
2 package org
.rocksdb
.util
;
4 import org
.rocksdb
.RocksDBException
;
5 import org
.rocksdb
.WriteBatch
;
7 import java
.util
.ArrayList
;
8 import java
.util
.Arrays
;
10 import java
.util
.Objects
;
13 * A simple WriteBatch Handler which adds a record
14 * of each event that it receives to a list
16 public class CapturingWriteBatchHandler
extends WriteBatch
.Handler
{
18 private final List
<Event
> events
= new ArrayList
<>();
21 * Returns a copy of the current events list
23 * @return a list of the events which have happened upto now
25 public List
<Event
> getEvents() {
26 return new ArrayList
<>(events
);
30 public void put(final int columnFamilyId
, final byte[] key
,
32 events
.add(new Event(Action
.PUT
, columnFamilyId
, key
, value
));
36 public void put(final byte[] key
, final byte[] value
) {
37 events
.add(new Event(Action
.PUT
, key
, value
));
41 public void merge(final int columnFamilyId
, final byte[] key
,
43 events
.add(new Event(Action
.MERGE
, columnFamilyId
, key
, value
));
47 public void merge(final byte[] key
, final byte[] value
) {
48 events
.add(new Event(Action
.MERGE
, key
, value
));
52 public void delete(final int columnFamilyId
, final byte[] key
) {
53 events
.add(new Event(Action
.DELETE
, columnFamilyId
, key
, (byte[])null));
57 public void delete(final byte[] key
) {
58 events
.add(new Event(Action
.DELETE
, key
, (byte[])null));
62 public void singleDelete(final int columnFamilyId
, final byte[] key
) {
63 events
.add(new Event(Action
.SINGLE_DELETE
,
64 columnFamilyId
, key
, (byte[])null));
68 public void singleDelete(final byte[] key
) {
69 events
.add(new Event(Action
.SINGLE_DELETE
, key
, (byte[])null));
73 public void deleteRange(final int columnFamilyId
, final byte[] beginKey
,
74 final byte[] endKey
) {
75 events
.add(new Event(Action
.DELETE_RANGE
, columnFamilyId
, beginKey
,
80 public void deleteRange(final byte[] beginKey
, final byte[] endKey
) {
81 events
.add(new Event(Action
.DELETE_RANGE
, beginKey
, endKey
));
85 public void logData(final byte[] blob
) {
86 events
.add(new Event(Action
.LOG
, (byte[])null, blob
));
90 public void putBlobIndex(final int columnFamilyId
, final byte[] key
,
92 events
.add(new Event(Action
.PUT_BLOB_INDEX
, key
, value
));
96 public void markBeginPrepare() throws RocksDBException
{
97 events
.add(new Event(Action
.MARK_BEGIN_PREPARE
, (byte[])null,
102 public void markEndPrepare(final byte[] xid
) throws RocksDBException
{
103 events
.add(new Event(Action
.MARK_END_PREPARE
, (byte[])null,
108 public void markNoop(final boolean emptyBatch
) throws RocksDBException
{
109 events
.add(new Event(Action
.MARK_NOOP
, (byte[])null, (byte[])null));
113 public void markRollback(final byte[] xid
) throws RocksDBException
{
114 events
.add(new Event(Action
.MARK_ROLLBACK
, (byte[])null, (byte[])null));
118 public void markCommit(final byte[] xid
) throws RocksDBException
{
119 events
.add(new Event(Action
.MARK_COMMIT
, (byte[])null, (byte[])null));
123 public void markCommitWithTimestamp(final byte[] xid
, final byte[] ts
) throws RocksDBException
{
124 events
.add(new Event(Action
.MARK_COMMIT_WITH_TIMESTAMP
, (byte[]) null, (byte[]) null));
127 public static class Event
{
128 public final Action action
;
129 public final int columnFamilyId
;
130 public final byte[] key
;
131 public final byte[] value
;
133 public Event(final Action action
, final byte[] key
, final byte[] value
) {
134 this(action
, 0, key
, value
);
137 public Event(final Action action
, final int columnFamilyId
, final byte[] key
,
138 final byte[] value
) {
139 this.action
= action
;
140 this.columnFamilyId
= columnFamilyId
;
146 public boolean equals(final Object o
) {
150 if (o
== null || getClass() != o
.getClass()) {
153 final Event event
= (Event
) o
;
154 return columnFamilyId
== event
.columnFamilyId
&&
155 action
== event
.action
&&
156 ((key
== null && event
.key
== null)
157 || Arrays
.equals(key
, event
.key
)) &&
158 ((value
== null && event
.value
== null)
159 || Arrays
.equals(value
, event
.value
));
163 public int hashCode() {
164 int result
= Objects
.hash(action
, columnFamilyId
);
165 result
= 31 * result
+ Arrays
.hashCode(key
);
166 result
= 31 * result
+ Arrays
.hashCode(value
);
172 * Enumeration of Write Batch
188 MARK_COMMIT_WITH_TIMESTAMP