aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/Source/Local/FSTWriteGroup.mm
blob: 9aee31a2b3a525c74c29ad3ca2afbd2039fadb71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * Copyright 2017 Google
 *
 * 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.
 */

#import "Firestore/Source/Local/FSTWriteGroup.h"

#include <leveldb/write_batch.h>

#import "Firestore/Source/Local/FSTLevelDBKey.h"
#import "Firestore/Source/Util/FSTAssert.h"

using firebase::firestore::local::LevelDbTransaction;
using Firestore::StringView;
using leveldb::DB;
using leveldb::Slice;
using leveldb::Status;
using leveldb::WriteBatch;
using leveldb::WriteOptions;

NS_ASSUME_NONNULL_BEGIN

@interface FSTWriteGroup ()
- (instancetype)initWithAction:(NSString *)action NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithAction:(NSString *)action transaction:(LevelDbTransaction *)transaction;
@end

@implementation FSTWriteGroup {
  int _changes;
}

+ (instancetype)groupWithAction:(NSString *)action {
  return [[FSTWriteGroup alloc] initWithAction:action];
}

+ (instancetype)groupWithAction:(NSString *)action
                    transaction:(firebase::firestore::local::LevelDbTransaction *)transaction {
  return [[FSTWriteGroup alloc] initWithAction:action transaction:transaction];
}

- (instancetype)initWithAction:(NSString *)action {
  if (self = [super init]) {
    _action = action;
    _transaction = nullptr;
  }
  return self;
}

- (instancetype)initWithAction:(NSString *)action transaction:(LevelDbTransaction *)transaction {
  if (self = [self initWithAction:action]) {
    _transaction = transaction;
  }
  return self;
}

- (void)removeMessageForKey:(StringView)key {
  FSTAssert(_transaction != nullptr, @"Using group without a transaction");
  Slice keySlice = key;
  _transaction->Delete(keySlice.ToString());
  _changes += 1;
}

- (void)setMessage:(GPBMessage *)message forKey:(StringView)key {
  FSTAssert(_transaction != nullptr, @"Using group without a transaction");
  Slice keySlice = key;
  _transaction->Put(keySlice.ToString(), message);
  _changes += 1;
}

- (void)setData:(StringView)data forKey:(StringView)key {
  FSTAssert(_transaction != nullptr, @"Using group without a transaction");
  Slice keySlice = key;
  Slice valueSlice = data;
  std::string value = valueSlice.ToString();
  _transaction->Put(keySlice.ToString(), value);
  _changes += 1;
}

- (BOOL)isEmpty {
  return _changes == 0;
}

@end

NS_ASSUME_NONNULL_END