blob: d73fb8aaddd17cf19d48353a5547f40a19d4336d [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.service;
Madan Jampani12390c12014-11-12 00:35:56 -080017
Madan Jampani12390c12014-11-12 00:35:56 -080018import java.util.List;
19
Madan Jampanif5d263b2014-11-13 10:04:40 -080020import com.google.common.base.MoreObjects;
Madan Jampani7aad2332014-11-12 01:57:07 -080021import com.google.common.collect.ImmutableList;
Madan Jampani12390c12014-11-12 00:35:56 -080022import com.google.common.collect.Lists;
23
24/**
25 * Collection of read requests to be submitted as one batch.
26 */
Madan Jampani23af4fc2014-11-12 00:54:18 -080027public final class BatchReadRequest {
Madan Jampani12390c12014-11-12 00:35:56 -080028
Madan Jampani23af4fc2014-11-12 00:54:18 -080029 private final List<ReadRequest> readRequests;
Madan Jampani12390c12014-11-12 00:35:56 -080030
Madan Jampani23af4fc2014-11-12 00:54:18 -080031 /**
32 * Creates a new BatchReadRequest object from the specified list of read requests.
33 * @param readRequests read requests.
34 * @return BatchReadRequest object.
35 */
36 public static BatchReadRequest create(List<ReadRequest> readRequests) {
37 return new BatchReadRequest(readRequests);
38 }
Madan Jampani12390c12014-11-12 00:35:56 -080039
Madan Jampani23af4fc2014-11-12 00:54:18 -080040 private BatchReadRequest(List<ReadRequest> readRequests) {
Madan Jampani7aad2332014-11-12 01:57:07 -080041 this.readRequests = ImmutableList.copyOf(readRequests);
Madan Jampani23af4fc2014-11-12 00:54:18 -080042 }
Madan Jampani12390c12014-11-12 00:35:56 -080043
Madan Jampani23af4fc2014-11-12 00:54:18 -080044 /**
45 * Returns the number of requests in this batch.
46 * @return size of request batch.
47 */
48 public int batchSize() {
49 return readRequests.size();
50 }
Madan Jampani12390c12014-11-12 00:35:56 -080051
Madan Jampani23af4fc2014-11-12 00:54:18 -080052 /**
53 * Returns the requests in this batch as a list.
54 * @return list of read requests
55 */
56 public List<ReadRequest> getAsList() {
57 return readRequests;
58 }
Madan Jampani12390c12014-11-12 00:35:56 -080059
Madan Jampanif5d263b2014-11-13 10:04:40 -080060 @Override
61 public String toString() {
62 return MoreObjects.toStringHelper(getClass())
63 .add("readRequests", readRequests)
64 .toString();
65 }
66
Madan Jampani23af4fc2014-11-12 00:54:18 -080067 /**
68 * Builder for BatchReadRequest.
69 */
70 public static class Builder {
Madan Jampani12390c12014-11-12 00:35:56 -080071
Madan Jampani23af4fc2014-11-12 00:54:18 -080072 private final List<ReadRequest> readRequests = Lists.newLinkedList();
Madan Jampani12390c12014-11-12 00:35:56 -080073
Madan Jampani23af4fc2014-11-12 00:54:18 -080074 /**
75 * Append a get request.
76 * @param tableName table name
77 * @param key key to fetch.
78 * @return this Builder
79 */
80 public Builder get(String tableName, String key) {
81 readRequests.add(new ReadRequest(tableName, key));
82 return this;
83 }
Madan Jampani12390c12014-11-12 00:35:56 -080084
Madan Jampani23af4fc2014-11-12 00:54:18 -080085 /**
86 * Builds a BatchReadRequest.
87 * @return BatchReadRequest
88 */
89 public BatchReadRequest build() {
90 return new BatchReadRequest(readRequests);
91 }
92 }
93}