1 // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
2 package org.rocksdb.util;
3 
4 import org.rocksdb.RocksDBException;
5 import org.rocksdb.WriteBatch;
6 
7 import java.util.Arrays;
8 
9 public class WriteBatchGetter extends WriteBatch.Handler {
10 
11   private int columnFamilyId = -1;
12   private final byte[] key;
13   private byte[] value;
14 
WriteBatchGetter(final byte[] key)15   public WriteBatchGetter(final byte[] key) {
16     this.key = key;
17   }
18 
getValue()19   public byte[] getValue() {
20     return value;
21   }
22 
23   @Override
put(final int columnFamilyId, final byte[] key, final byte[] value)24   public void put(final int columnFamilyId, final byte[] key,
25                   final byte[] value) {
26     if(Arrays.equals(this.key, key)) {
27       this.columnFamilyId = columnFamilyId;
28       this.value = value;
29     }
30   }
31 
32   @Override
put(final byte[] key, final byte[] value)33   public void put(final byte[] key, final byte[] value) {
34     if(Arrays.equals(this.key, key)) {
35       this.value = value;
36     }
37   }
38 
39   @Override
merge(final int columnFamilyId, final byte[] key, final byte[] value)40   public void merge(final int columnFamilyId, final byte[] key,
41                     final byte[] value) {
42     if(Arrays.equals(this.key, key)) {
43       this.columnFamilyId = columnFamilyId;
44       this.value = value;
45     }
46   }
47 
48   @Override
merge(final byte[] key, final byte[] value)49   public void merge(final byte[] key, final byte[] value) {
50     if(Arrays.equals(this.key, key)) {
51       this.value = value;
52     }
53   }
54 
55   @Override
delete(final int columnFamilyId, final byte[] key)56   public void delete(final int columnFamilyId, final byte[] key) {
57     if(Arrays.equals(this.key, key)) {
58       this.columnFamilyId = columnFamilyId;
59       this.value = null;
60     }
61   }
62 
63   @Override
delete(final byte[] key)64   public void delete(final byte[] key) {
65     if(Arrays.equals(this.key, key)) {
66       this.value = null;
67     }
68   }
69 
70   @Override
singleDelete(final int columnFamilyId, final byte[] key)71   public void singleDelete(final int columnFamilyId, final byte[] key) {
72     if(Arrays.equals(this.key, key)) {
73       this.columnFamilyId = columnFamilyId;
74       this.value = null;
75     }
76   }
77 
78   @Override
singleDelete(final byte[] key)79   public void singleDelete(final byte[] key) {
80     if(Arrays.equals(this.key, key)) {
81       this.value = null;
82     }
83   }
84 
85   @Override
deleteRange(final int columnFamilyId, final byte[] beginKey, final byte[] endKey)86   public void deleteRange(final int columnFamilyId, final byte[] beginKey,
87                           final byte[] endKey) {
88     throw new UnsupportedOperationException();
89   }
90 
91   @Override
deleteRange(final byte[] beginKey, final byte[] endKey)92   public void deleteRange(final byte[] beginKey, final byte[] endKey) {
93     throw new UnsupportedOperationException();
94   }
95 
96   @Override
logData(final byte[] blob)97   public void logData(final byte[] blob) {
98     throw new UnsupportedOperationException();
99   }
100 
101   @Override
putBlobIndex(final int columnFamilyId, final byte[] key, final byte[] value)102   public void putBlobIndex(final int columnFamilyId, final byte[] key,
103                            final byte[] value) {
104     if(Arrays.equals(this.key, key)) {
105       this.columnFamilyId = columnFamilyId;
106       this.value = value;
107     }
108   }
109 
110   @Override
markBeginPrepare()111   public void markBeginPrepare() throws RocksDBException {
112     throw new UnsupportedOperationException();
113   }
114 
115   @Override
markEndPrepare(final byte[] xid)116   public void markEndPrepare(final byte[] xid) throws RocksDBException {
117     throw new UnsupportedOperationException();
118   }
119 
120   @Override
markNoop(final boolean emptyBatch)121   public void markNoop(final boolean emptyBatch) throws RocksDBException {
122     throw new UnsupportedOperationException();
123   }
124 
125   @Override
markRollback(final byte[] xid)126   public void markRollback(final byte[] xid) throws RocksDBException {
127     throw new UnsupportedOperationException();
128   }
129 
130   @Override
markCommit(final byte[] xid)131   public void markCommit(final byte[] xid) throws RocksDBException {
132     throw new UnsupportedOperationException();
133   }
134 }
135